The Wi-Fi Smiley Dashboard is an expressive 32×32 LED face powered by an ESP32 that connects to your network and responds to API calls with animated moods and audio feedback. Eight distinct expressions—from happy to sleepy—display on the LED panel while a speaker adds voice and sound effects, creating an engaging visual and audio companion for your projects.
This guide provides everything needed to assemble and configure your smiley: a wiring diagram showing connections between the ESP32, LED panel, and speaker; a complete parts list; pre-built firmware with Wi-Fi setup, mood control, and HTTPS API support; and step-by-step assembly instructions. Once powered up, the device runs standalone with no external dependencies, ready to receive commands and express itself.
Wiring diagram

Assemble it in 3 steps
1. Plug in the 32×32 LED panel
Connect the 32×32 HUB75 LED panel to the board's 16-pin ribbon connector (J1) with the panel's ribbon cable. Plug it into the panel's INPUT connector (the arrows on the panel's back point away from it). Connect the panel's own power lead to a 5 V supply.
- The firmware drives this panel as 32×64 and draws only rows 0–31, rotated 180°. A different panel needs the size constants in src/main.cpp changed.
- Never plug the panel's power lead into 12 V: the LEDs and driver chips will burn out.
2. Plug in the speaker
Plug a small speaker (8 Ω, up to 5 W) into the board's 2-pin speaker connector. The volume starts at 80.
3. Power it up
Connect the board's USB-C port to your computer with a data cable for the first flash. Afterwards a good 5 V supply is enough; no computer needed.
- Hold BOOT only after the face appears; holding it while resetting puts the board into download mode instead of starting Smiley.
Deploy the firmware
// Standalone Smiley: the ESP32 owns setup, audio and HTTPS API calls.
// USB is used to flash and for optional diagnostics, never for the conversation.
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <ESPmDNS.h>
#include <Preferences.h>
#include <ESP32-HUB75-MatrixPanel-I2S-DMA.h>
#include <esp_http_client.h>
#include <esp_crt_bundle.h>
#include <esp_heap_caps.h>
#include <cJSON.h>
#include <mbedtls/base64.h>
#include "audio_format.h"
#include <atomic>
#include <math.h>
#include <time.h>
#include "schematik_audio.h"
#include "config.h"
#include "web_ui.h"
#define BUTTON 0
#define PANEL_W 32
#define PANEL_H 32
#define HW_PANEL_H 64
#define PANEL_CHAIN 1
#define PANEL_ROTATE_180 1
#define BRIGHTNESS 128
MatrixPanel_I2S_DMA *matrix = nullptr;
enum Mood { NEUTRAL, HAPPY, LISTENING, THINKING, WORRIED, SURPRISED, SAD, SLEEPY, MOOD_COUNT };
static const char *MOOD_NAMES[] = {"neutral", "happy", "listening", "thinking", "worried", "surprised", "sad", "sleepy"};
volatile float g_talkLevel = 0, g_gazeTarget = 0;
volatile int g_mood = NEUTRAL;
volatile uint32_t g_moodUntil = 0, g_gazeAt = 0, g_timerEnd = 0, g_timerTotal = 0;
#include "face.h"
static WebServer web(80);
static DNSServer dns;
static Preferences prefs;
static SemaphoreHandle_t stateLock;
static QueueHandle_t jobs;
static bool audioReady = false, memoryReady = false, busy = false;
static bool setupMode = false, joining = false, networkWasUp = false;
static uint32_t joinStarted = 0, apCloseAt = 0;
static String ssid, wifiPassword, apiKey, adminPassword, csrf, setupName;
static String state = "idle", lastUser, lastReply, lastError;
static int volume = DEFAULT_VOLUME;
static std::atomic<bool> stopRecording{false};
static std::atomic<bool> recording{false};
struct Job { char text[401]; bool voice; bool test; };
struct Turn { String user, reply; };
static Turn history[3]; // worker owns history; clearing is allowed only while idle
static size_t historyCount = 0;
static uint32_t lastTurn = 0;
struct Guard {
Guard() { xSemaphoreTake(stateLock, portMAX_DELAY); }
~Guard() { xSemaphoreGive(stateLock); }
};
struct Json {
cJSON *p;
Json() : p(cJSON_CreateObject()) {}
explicit Json(const char *s) : p(cJSON_Parse(s)) {}
~Json() { cJSON_Delete(p); }
Json(const Json&) = delete;
};
static String jsonText(cJSON *p) {
char *s = cJSON_PrintUnformatted(p);
String result = s ? s : "{}";
cJSON_free(s);
return result;
}
static String field(cJSON *p, const char *key) {
cJSON *v = cJSON_GetObjectItemCaseSensitive(p, key);
return cJSON_IsString(v) ? String(v->valuestring) : String();
}
static void setState(const char *s) { Guard guard; state = s; }
static void fail(const String &s) { Guard guard; lastError = s; g_mood = WORRIED; }
static void sendJson(int code, const String &s) {
web.sendHeader("Cache-Control", "no-store");
web.send(code, "application/json", s);
}
static void error(int code, const char *s) {
Json j; cJSON_AddStringToObject(j.p, "error", s); sendJson(code, jsonText(j.p));
}
static bool onSetupNetwork() {
return setupMode && web.client().localIP() == WiFi.softAPIP();
}
static bool authorized() {
if (onSetupNetwork()) return true;
if (adminPassword.length() && web.authenticate("smiley", adminPassword.c_str())) return true;
web.requestAuthentication(DIGEST_AUTH, "Smiley", "Use username smiley and your Smiley password.");
return false;
}
static bool mutationAllowed() {
if (!authorized()) return false;
if (web.header("X-Smiley-Token") != csrf) {
error(403, "Reload this page and try again."); return false;
}
if (web.header("Content-Type") != "application/json" || web.arg("plain").length() > 4096) {
error(400, "Invalid request."); return false;
}
return true;
}
static bool idle() {
Guard guard;
if (!busy) return true;
error(409, "Wait for Smiley to finish."); return false;
}
// esp_http_client decodes both fixed-length and chunked responses. Never disable TLS validation.
struct Response {
uint8_t *data;
size_t used = 0, capacity;
bool overflow = false;
explicit Response(size_t maxBytes) : capacity(maxBytes) {
data = (uint8_t*)heap_caps_malloc(capacity + 1, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (data) data[0] = 0;
}
~Response() { free(data); }
};
static esp_err_t onHttp(esp_http_client_event_t *event) {
if (event->event_id != HTTP_EVENT_ON_DATA) return ESP_OK;
auto *r = (Response*)event->user_data;
if (!r->data || (size_t)event->data_len > r->capacity - r->used) {
r->overflow = true; return ESP_FAIL;
}
memcpy(r->data + r->used, event->data, event->data_len);
r->used += event->data_len;
r->data[r->used] = 0;
return ESP_OK;
}
static bool request(const char *path, const char *contentType, const uint8_t *body, size_t length, Response &out) {
if (!out.data) { fail("Not enough PSRAM for the reply."); return false; }
if (WiFi.status() != WL_CONNECTED) { fail("Wi-Fi is disconnected."); return false; }
// A valid clock is required to verify certificate validity dates.
uint32_t started = millis();
while (time(nullptr) < 1700000000 && millis() - started < 15000) delay(100);
if (time(nullptr) < 1700000000) { fail("Clock sync failed. Check internet access and try again."); return false; }
String url = String("https://api.openai.com/v1/") + path;
esp_http_client_config_t config = {};
config.url = url.c_str();
config.method = HTTP_METHOD_POST;
config.crt_bundle_attach = esp_crt_bundle_attach;
config.timeout_ms = 45000;
config.disable_auto_redirect = true;
config.event_handler = onHttp;
config.user_data = &out;
config.buffer_size = 2048;
config.buffer_size_tx = 2048;
esp_http_client_handle_t client = esp_http_client_init(&config);
if (!client) { fail("Could not start the HTTPS connection."); return false; }
String auth = "Bearer " + apiKey;
esp_http_client_set_header(client, "Authorization", auth.c_str());
esp_http_client_set_header(client, "Content-Type", contentType);
esp_http_client_set_post_field(client, (const char*)body, length);
esp_err_t result = esp_http_client_perform(client);
int code = esp_http_client_get_status_code(client);
bool complete = esp_http_client_is_complete_data_received(client);
esp_http_client_cleanup(client);
if (out.overflow) { fail("The reply was too long. Try a shorter question."); return false; }
if (result != ESP_OK || !complete) { fail("The API connection failed or timed out. Try again."); return false; }
if (code != 200) {
if (code == 401) fail("API key rejected. Update it in setup.");
else if (code == 429) fail("API limit reached. Check credits and try again later.");
else if (code == 403 || code == 404) fail("API model unavailable. Check account access and model settings in config.h.");
else fail(String("API request failed (HTTP ") + code + "). Try again.");
return false;
}
return true;
}
static bool requestJson(const char *path, cJSON *body, Response &out) {
String payload = jsonText(body);
return request(path, "application/json", (const uint8_t*)payload.c_str(), payload.length(), out);
}
static String recordAndTranscribe() {
constexpr size_t maxSamples = RECORD_RATE * RECORD_SECONDS;
int16_t *pcm = (int16_t*)heap_caps_malloc(maxSamples * 2, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!pcm) { fail("Not enough PSRAM to record."); return ""; }
setState("listening"); g_mood = LISTENING;
stopRecording = false;
schematikAudioTone(880, 90, 0.08f);
// Drain old DMA samples and the chime's acoustic tail before recording.
int16_t stereo[960*2];
for (int i=0;i<15;i++) schematikAudioRead(stereo,960);
recording = true;
size_t count=0, silence=0, voiced=0;
bool heard=false;
int peak=0;
uint32_t began=millis();
while (count < maxSamples && millis()-began < RECORD_SECONDS*1000u && !stopRecording) {
size_t frames=schematikAudioRead(stereo,960);
if (!frames) { fail("Could not read the microphone."); break; }
double sum=0;
size_t monoFrames=frames/3;
for (size_t i=0;i<monoFrames && count<maxSamples;i++) {
int32_t value=0;
for (int k=0;k<3;k++) value+=stereo[(i*3+k)*2]+stereo[(i*3+k)*2+1];
int16_t sample=constrain(value/6, -32768,32767);
pcm[count++]=sample;
peak=max(peak,abs((int)sample)); sum+=(double)sample*sample;
}
float rms=sqrt(sum/max<size_t>(1,monoFrames));
if (rms > 65) { voiced+=monoFrames; silence=0; }
else silence+=monoFrames;
if (voiced >= RECORD_RATE/10) heard=true;
if (heard && silence >= RECORD_RATE && count >= RECORD_RATE) break;
if (!heard && count >= RECORD_RATE*6) break;
delay(1);
}
recording = false;
if (!heard || count < RECORD_RATE/2) {
free(pcm); fail("No speech heard. Tap BOOT and speak near Smiley."); return "";
}
// Normalize the quiet onboard microphones for transcription.
float gain=peak ? min(64.0f, 28000.0f/peak) : 1;
for (size_t i=0;i<count;i++) pcm[i]=(int16_t)constrain(pcm[i]*gain,-32768.0f,32767.0f);
setState("transcribing"); g_mood=THINKING;
const String boundary="----SmileyAudio7a9e";
String prefix="--"+boundary+"\r\nContent-Disposition: form-data; name=\"model\"\r\n\r\n"+TRANSCRIBE_MODEL+
"\r\n--"+boundary+"\r\nContent-Disposition: form-data; name=\"response_format\"\r\n\r\njson\r\n--"+boundary+
"\r\nContent-Disposition: form-data; name=\"file\"; filename=\"speech.wav\"\r\nContent-Type: audio/wav\r\n\r\n";
String suffix="\r\n--"+boundary+"--\r\n";
size_t size=prefix.length()+44+count*2+suffix.length();
uint8_t *body=(uint8_t*)heap_caps_malloc(size,MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT);
if (!body) { free(pcm); fail("Not enough PSRAM to upload audio."); return ""; }
memcpy(body,prefix.c_str(),prefix.length());
writeWavHeader(body+prefix.length(),count*2,RECORD_RATE);
memcpy(body+prefix.length()+44,pcm,count*2); free(pcm);
memcpy(body+prefix.length()+44+count*2,suffix.c_str(),suffix.length());
Response response(16384);
String type="multipart/form-data; boundary="+boundary;
bool ok=request("audio/transcriptions",type.c_str(),body,size,response);
free(body);
if (!ok) return "";
Json j((char*)response.data);
String text=field(j.p,"text"); text.trim();
if (!text.length()) fail("No words recognized. Try again.");
if (text.length()>400) text=text.substring(0,400);
return text;
}
static bool playWav(const uint8_t *data,size_t length);
static void answer(const String &text) {
setState("thinking"); g_mood=THINKING;
if (millis()-lastTurn>600000) historyCount=0;
Json j;
cJSON_AddStringToObject(j.p,"model",AUDIO_MODEL);
cJSON_AddNumberToObject(j.p,"max_completion_tokens",1024);
cJSON *modalities=cJSON_AddArrayToObject(j.p,"modalities");
cJSON_AddItemToArray(modalities,cJSON_CreateString("text"));
cJSON_AddItemToArray(modalities,cJSON_CreateString("audio"));
cJSON *audioOptions=cJSON_AddObjectToObject(j.p,"audio");
cJSON_AddStringToObject(audioOptions,"voice",SPEECH_VOICE);
cJSON_AddStringToObject(audioOptions,"format","wav");
cJSON_AddBoolToObject(j.p,"store",false);
cJSON *messages=cJSON_AddArrayToObject(j.p,"messages");
auto message=[&](const char *role,const char *content) {
cJSON *m=cJSON_CreateObject(); cJSON_AddStringToObject(m,"role",role);
cJSON_AddStringToObject(m,"content",content); cJSON_AddItemToArray(messages,m);
};
message("system",SYSTEM_PROMPT);
for(size_t i=0;i<historyCount;i++) { message("user",history[i].user.c_str()); message("assistant",history[i].reply.c_str()); }
message("user",text.c_str());
// Bound the base64 JSON response; audio is parsed in PSRAM, not internal SRAM.
Response response(4*1024*1024);
if(!requestJson("chat/completions",j.p,response)) return;
Json result((char*)response.data);
// Free the HTTP copy before decoding the JSON audio copy.
free(response.data); response.data=nullptr;
cJSON *choice=cJSON_GetArrayItem(cJSON_GetObjectItemCaseSensitive(result.p,"choices"),0);
cJSON *messageObject=cJSON_GetObjectItemCaseSensitive(choice,"message");
cJSON *audio=cJSON_GetObjectItemCaseSensitive(messageObject,"audio");
String reply=field(audio,"transcript"); reply.trim();
if(!reply.length()) reply=field(messageObject,"content");
if(!reply.length()) { fail("The API returned no reply. Try again."); return; }
if(reply.length()>1600) { fail("The API reply was too long. Try a shorter question."); return; }
{ Guard guard; lastReply=reply; }
cJSON *encoded=cJSON_GetObjectItemCaseSensitive(audio,"data");
if(!cJSON_IsString(encoded)) { fail("The API returned no spoken audio."); return; }
size_t encodedSize=strlen(encoded->valuestring),bytes=0;
if(encodedSize>4*1024*1024) { fail("Spoken reply was too long."); return; }
size_t capacity=encodedSize/4*3+4;
uint8_t *decoded=(uint8_t*)heap_caps_malloc(capacity,MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT);
if(!decoded) { fail("Not enough PSRAM to decode speech."); return; }
int errorCode=mbedtls_base64_decode(decoded,capacity,&bytes,(const uint8_t*)encoded->valuestring,encodedSize);
if(errorCode!=0) { free(decoded); fail("The speech data was invalid."); return; }
// Release the large encoded string before playback.
cJSON_DeleteItemFromObjectCaseSensitive(audio,"data");
bool played=playWav(decoded,bytes); free(decoded);
if(!played) return;
if(historyCount==3) { history[0]=history[1]; history[1]=history[2]; historyCount=2; }
history[historyCount++]={text,reply}; lastTurn=millis();
}
static bool playWav(const uint8_t *data,size_t length) {
WavView wav;
if(!parseWav(data,length,wav)) { fail("The API returned unsupported or incomplete WAV audio."); return false; }
setState("speaking"); g_mood=HAPPY;
size_t samples=wav.bytes/(wav.channels*2);
int up=48000/wav.rate;
int peak=1;
for(size_t i=0;i<samples;i++) peak=max(peak,abs(wavSample(wav,i)));
float gain=28000.0f/peak*0.25f;
int16_t stereo[256*2];
size_t frames=0; float sum=0; size_t sumN=0;
for(size_t i=0;i<samples;i++) {
int a=wavSample(wav,i),b=(i+1<samples)?wavSample(wav,i+1):a;
sum+=(float)a*a; sumN++;
for(int k=0;k<up;k++) {
int16_t value=(int16_t)((a+(b-a)*k/up)*gain);
stereo[frames*2]=stereo[frames*2+1]=value;
if(++frames==256) {
g_talkLevel=min(1.0f,sqrtf(sum/max<size_t>(1,sumN))/(peak*0.35f));
if(schematikAudioWrite(stereo,frames)!=frames) { g_talkLevel=0; fail("Speaker playback failed."); return false; }
frames=0; sum=0; sumN=0;
}
}
}
if(frames && schematikAudioWrite(stereo,frames)!=frames) { g_talkLevel=0; fail("Speaker playback failed."); return false; }
g_talkLevel=0;
return true;
}
static void assistantTask(void *) {
Job job;
for(;;) {
if(xQueueReceive(jobs,&job,portMAX_DELAY)!=pdTRUE) continue;
if(job.test) {
schematikAudioTone(880,120,0.08f);
schematikAudioTone(1318,120,0.08f);
} else {
String text=job.voice?recordAndTranscribe():String(job.text);
if(text.length()) {
{ Guard guard; lastUser=text; lastReply=""; }
answer(text);
}
}
{ Guard guard; busy=false; state="idle"; if(!lastError.length()) g_mood=NEUTRAL; }
}
}
static bool startJob(const String &text,bool voice,bool test=false) {
Guard guard;
if(busy) return false;
if(!audioReady || !memoryReady || !jobs) { lastError="Audio or PSRAM initialization failed. Check diagnostics."; return false; }
if(!test && (WiFi.status()!=WL_CONNECTED || !apiKey.length())) { lastError="Complete Wi-Fi and API-key setup first."; return false; }
Job job={}; text.toCharArray(job.text,sizeof(job.text)); job.voice=voice; job.test=test;
busy=true; lastError=""; state="starting";
if(xQueueSend(jobs,&job,0)!=pdTRUE) { busy=false; state="idle"; return false; }
return true;
}
static void beginSetup() {
if(setupMode) { apCloseAt=0; return; }
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(setupName.c_str(),SETUP_PASSWORD);
dns.start(53,"*",WiFi.softAPIP());
setupMode=true; apCloseAt=0;
g_mood=LISTENING;
Serial.printf("Setup: join %s, password %s; open http://192.168.4.1\n",setupName.c_str(),SETUP_PASSWORD);
}
static void joinWifi() {
if(!ssid.length()) { beginSetup(); return; }
WiFi.begin(ssid.c_str(),wifiPassword.c_str());
joining=true; joinStarted=millis(); networkWasUp=false;
}
static void status() {
if(!authorized()) return;
Guard guard; Json j;
cJSON_AddStringToObject(j.p,"csrf",csrf.c_str());
cJSON_AddStringToObject(j.p,"state",state.c_str());
cJSON_AddStringToObject(j.p,"error",lastError.c_str());
cJSON_AddStringToObject(j.p,"user",lastUser.c_str());
cJSON_AddStringToObject(j.p,"reply",lastReply.c_str());
cJSON_AddStringToObject(j.p,"ip",WiFi.localIP().toString().c_str());
cJSON_AddStringToObject(j.p,"ssid",ssid.c_str());
cJSON_AddBoolToObject(j.p,"connected",WiFi.status()==WL_CONNECTED);
cJSON_AddBoolToObject(j.p,"configured",apiKey.length()>0);
cJSON_AddBoolToObject(j.p,"setup",onSetupNetwork());
cJSON_AddBoolToObject(j.p,"busy",busy);
cJSON_AddBoolToObject(j.p,"recording",recording);
cJSON_AddBoolToObject(j.p,"audio",audioReady);
cJSON_AddBoolToObject(j.p,"memory",memoryReady);
cJSON_AddNumberToObject(j.p,"volume",volume);
sendJson(200,jsonText(j.p));
}
static void saveConfig() {
if(!mutationAllowed() || !idle()) return;
Json body(web.arg("plain").c_str());
String s=field(body.p,"ssid"),p=field(body.p,"password"),k=field(body.p,"key"),a=field(body.p,"admin");
s.trim(); k.trim();
if(!p.length() && s==ssid) p=wifiPassword;
if(!s.length() || s.length()>32 || p.length()>63 || (p.length() && p.length()<8) ||
k.length()>512 || k.indexOf('\r')>=0 || k.indexOf('\n')>=0 || a.length()>64 ||
(!k.length()&&!apiKey.length()) || (!adminPassword.length()&&a.length()<8) || (a.length()&&a.length()<8)) {
error(400,"Enter a Wi-Fi name, valid Wi-Fi password, API key and a Smiley password of at least 8 characters."); return;
}
ssid=s; wifiPassword=p; if(k.length()) apiKey=k; if(a.length()) adminPassword=a;
prefs.begin("smiley",false);
bool ok=prefs.putString("ssid",ssid)>0 && prefs.putString("wifi",wifiPassword)>0 &&
prefs.putString("key",apiKey)>0 && prefs.putString("admin",adminPassword)>0;
prefs.end();
if(!ok) { error(500,"Could not save settings. Try again."); return; }
{ Guard guard; lastError=""; historyCount=0; lastUser=""; lastReply=""; }
sendJson(200,"{\"ok\":true}"); joinWifi();
}
static void startWeb() {
const char *headers[]={"X-Smiley-Token","Content-Type"}; web.collectHeaders(headers,2);
web.on("/",HTTP_GET,[](){
if(!authorized()) return;
web.sendHeader("Cache-Control","no-store");
web.sendHeader("X-Content-Type-Options","nosniff");
web.send_P(200,"text/html; charset=utf-8",SMILEY_PAGE);
});
web.on("/api/status",HTTP_GET,status);
web.on("/api/setup",HTTP_POST,saveConfig);
web.on("/api/ask",HTTP_POST,[](){
if(!mutationAllowed()) return;
Json j(web.arg("plain").c_str()); String text=field(j.p,"text"); text.trim();
if(!text.length()||text.length()>400) { error(400,"Enter a message of up to 400 characters."); return; }
if(!startJob(text,false)) { error(409,"Smiley is busy or needs setup. Check status."); return; }
sendJson(202,"{\"ok\":true}");
});
web.on("/api/record",HTTP_POST,[](){
if(!mutationAllowed()) return;
if(recording) { stopRecording=true; sendJson(200,"{\"ok\":true}"); return; }
if(!startJob("",true)) { error(409,"Smiley is busy or needs setup. Check status."); return; }
sendJson(202,"{\"ok\":true}");
});
web.on("/api/test",HTTP_POST,[](){
if(!mutationAllowed()) return;
if(!startJob("",false,true)) { error(409,"Smiley is busy or audio is unavailable."); return; }
sendJson(202,"{\"ok\":true}");
});
web.on("/api/volume",HTTP_POST,[](){
if(!mutationAllowed()||!idle()) return;
Json j(web.arg("plain").c_str()); cJSON *v=cJSON_GetObjectItemCaseSensitive(j.p,"volume");
if(!cJSON_IsNumber(v)||v->valuedouble<0||v->valuedouble>100) { error(400,"Choose a volume from 0 to 100."); return; }
volume=v->valueint; schematikAudioSetVolume(volume);
prefs.begin("smiley",false); prefs.putInt("volume",volume); prefs.end();
sendJson(200,"{\"ok\":true}");
});
web.on("/api/clear",HTTP_POST,[](){
if(!mutationAllowed()||!idle()) return;
{ Guard guard; for(auto &t:history) t={}; historyCount=0; lastUser=""; lastReply=""; lastError=""; }
sendJson(200,"{\"ok\":true}");
});
web.on("/api/forget",HTTP_POST,[](){
if(!mutationAllowed()||!idle()) return;
prefs.begin("smiley",false); prefs.remove("key"); prefs.end(); apiKey="";
{ Guard guard; for(auto &t:history) t={}; historyCount=0; lastUser=""; lastReply=""; }
sendJson(200,"{\"ok\":true}");
});
web.onNotFound([](){
if(onSetupNetwork()&&web.method()==HTTP_GET) { web.sendHeader("Location","http://192.168.4.1/"); web.send(302,"text/plain",""); }
else error(404,"Not found.");
});
web.begin();
}
void setup() {
Serial.begin(115200); // no wait for a USB host
pinMode(BUTTON,INPUT_PULLUP);
stateLock=xSemaphoreCreateMutex();
if(!stateLock) { delay(1000); ESP.restart(); }
memoryReady=psramFound() && ESP.getPsramSize()>=8*1024*1024;
// cJSON duplicates base64 audio. Keep those allocations out of the small internal heap.
cJSON_Hooks hooks={[](size_t bytes)->void* {
void *p=heap_caps_malloc(bytes,MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT);
return p ? p : (bytes<=4096 ? malloc(bytes) : nullptr);
},free};
cJSON_InitHooks(&hooks);
prefs.begin("smiley",true);
ssid=prefs.getString("ssid",""); wifiPassword=prefs.getString("wifi","");
apiKey=prefs.getString("key",""); adminPassword=prefs.getString("admin","");
volume=constrain(prefs.getInt("volume",DEFAULT_VOLUME),0,100); prefs.end();
schematikAudioSetVolume(volume);
audioReady=schematikAudioBegin();
matrixInit();
if(!audioReady||!memoryReady) { fail("Audio or PSRAM initialization failed. Rebuild for the correct board."); }
uint64_t mac=ESP.getEfuseMac(); char name[32]; snprintf(name,sizeof(name),"Smiley-Setup-%04X",(unsigned)(mac&0xffff)); setupName=name;
char token[33]; for(int i=0;i<4;i++) snprintf(token+i*8,9,"%08lx",(unsigned long)esp_random()); csrf=token;
WiFi.mode(WIFI_STA); WiFi.setHostname("smiley"); WiFi.setSleep(false); WiFi.setAutoReconnect(true);
configTime(0,0,"pool.ntp.org","time.nist.gov");
if(!ssid.length()||!apiKey.length()||!adminPassword.length()) beginSetup();
joinWifi(); startWeb();
jobs=xQueueCreate(1,sizeof(Job));
if(!jobs || xTaskCreatePinnedToCore(assistantTask,"smiley-ai",16384,nullptr,1,nullptr,1)!=pdPASS) {
jobs=nullptr; fail("Could not start Smiley's audio task.");
}
Serial.printf("Smiley standalone: audio=%s PSRAM=%u volume=%d\n",audioReady?"OK":"FAILED",ESP.getPsramSize(),volume);
}
void loop() {
web.handleClient();
if(setupMode) dns.processNextRequest();
uint32_t now=millis();
bool up=WiFi.status()==WL_CONNECTED;
if(up&&!networkWasUp) {
joining=false; MDNS.end(); MDNS.begin("smiley"); MDNS.addService("http","tcp",80);
configTime(0,0,"pool.ntp.org","time.nist.gov");
Serial.printf("Smiley: http://%s (or http://smiley.local)\n",WiFi.localIP().toString().c_str());
if(setupMode&&apiKey.length()&&adminPassword.length()) apCloseAt=now+60000;
}
if(!up&&networkWasUp) { joining=true; joinStarted=now; apCloseAt=0; }
networkWasUp=up;
if(joining&&!up&&now-joinStarted>30000) { joining=false; beginSetup(); }
if(setupMode&&apCloseAt&&(int32_t)(now-apCloseAt)>=0&&up) {
dns.stop(); WiFi.softAPdisconnect(true); WiFi.mode(WIFI_STA); setupMode=false; apCloseAt=0;
}
static bool pressed=false,held=false;
static uint32_t downAt=0;
bool down=digitalRead(BUTTON)==LOW;
if(down&&!pressed) { downAt=now; held=false; }
if(down&&!held&&now-downAt>=5000) { held=true; beginSetup(); }
if(!down&&pressed&&!held&&now-downAt>=40) {
if(recording) stopRecording=true;
else startJob("",true);
}
pressed=down;
static uint32_t lastFrame=0;
if(now-lastFrame>=33) { lastFrame=now; updateFace(now); drawFace(now); }
delay(1);
}Download project files
Remix this project
Make it yours in one click
Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.




