Sample Code
#include <WiFi.h> #include <WebServer.h>
// WiFi credentials const char* ssid = "Muensel"; const char* password = "Team_Muensel123";
// Button pins const int BUTTON1_PIN = D0; const int BUTTON2_PIN = D2;
WebServer server(80); bool buttonPressed = false; int lastPressedButton = 0;
// Webpage HTML with permission request const char INDEX_HTML[] PROGMEM = R"rawliteral( <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Simple Speech Assistant</title> <style> body { font-family: Arial; margin: 20px auto; max-width: 600px; padding: 20px; } .button { width: 100%; padding: 20px; margin: 10px 0; font-size: 20px; background-color: #4CAF50; color: white; border: none; border-radius: 8px; } .status { padding: 10px; margin: 10px 0; background-color: #f0f0f0; text-align: center; } .permission-button { background-color: #ff9800; } </style> </head> <body> <h1>Speech Assistant</h1> <div id="status" class="status">Please enable speech synthesis</div> <button class="button permission-button" onclick="requestPermission()">Enable Speech</button> <button class="button" onclick="speak(1)">I am thirsty</button> <button class="button" onclick="speak(2)">I feel sick</button>
<script> let synth = window.speechSynthesis; let speaking = false; let speechEnabled = false; const messages = { 1: "I am thirsty", 2: "I feel sick" };
// Check speech synthesis support on load window.onload = function() { if (typeof speechSynthesis === 'undefined') { document.getElementById('status').textContent = 'Speech synthesis not supported in this browser'; return; } };
// Request permission and initialize speech function requestPermission() { if (typeof speechSynthesis === 'undefined') { document.getElementById('status').textContent = 'Speech synthesis not supported'; return; } // Try to speak a silent message to trigger permission let utterance = new SpeechSynthesisUtterance(''); utterance.volume = 0; utterance.onend = function() { speechEnabled = true; document.getElementById('status').textContent = 'Speech enabled - ready to use'; document.getElementById('status').style.color = 'green'; }; utterance.onerror = function() { document.getElementById('status').textContent = 'Error enabling speech. Please try again.'; document.getElementById('status').style.color = 'red'; }; synth.speak(utterance); }
function speak(buttonNumber) { if (speaking) return; if (!speechEnabled) { document.getElementById('status').textContent = 'Please enable speech first'; document.getElementById('status').style.color = 'red'; return; } let utterance = new SpeechSynthesisUtterance(messages[buttonNumber]); utterance.onstart = () => { speaking = true; document.getElementById('status').textContent = 'Speaking...'; document.getElementById('status').style.color = 'black'; }; utterance.onend = () => { speaking = false; document.getElementById('status').textContent = 'Ready'; document.getElementById('status').style.color = 'black'; }; synth.speak(utterance); }
// Poll for button presses setInterval(async () => { try { const response = await fetch('/button'); const data = await response.json(); if (data.pressed) { speak(data.button); } } catch (error) { console.error('Error polling button:', error); } }, 500); </script> </body> </html> )rawliteral";
void setup() { Serial.begin(115200); // Initialize buttons with pull-up resistors pinMode(BUTTON1_PIN, INPUT_PULLUP); pinMode(BUTTON2_PIN, INPUT_PULLUP); // Connect to WiFi Serial.print("Connecting to WiFi"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi Connected!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // Set up web server routes server.on("/", HTTP_GET, []() { server.send(200, "text/html", INDEX_HTML); }); server.on("/button", HTTP_GET, []() { String json; if (buttonPressed) { json = "{\"pressed\":true,\"button\":" + String(lastPressedButton) + "}"; buttonPressed = false; } else { json = "{\"pressed\":false,\"button\":0}"; } server.send(200, "application/json", json); }); server.begin(); Serial.println("Web server started"); }
void loop() { server.handleClient(); // Check buttons if (digitalRead(BUTTON1_PIN) == LOW) { buttonPressed = true; lastPressedButton = 1; Serial.println("Button 1 pressed"); delay(50); // Simple debounce } if (digitalRead(BUTTON2_PIN) == LOW) { buttonPressed = true; lastPressedButton = 2; Serial.println("Button 2 pressed"); delay(50); // Simple debounce } }
// WiFi credentials const char* ssid = "Muensel"; const char* password = "Team_Muensel123";
// Button pins const int BUTTON1_PIN = D0; const int BUTTON2_PIN = D2;
WebServer server(80); bool buttonPressed = false; int lastPressedButton = 0;
// Webpage HTML with permission request const char INDEX_HTML[] PROGMEM = R"rawliteral( <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Simple Speech Assistant</title> <style> body { font-family: Arial; margin: 20px auto; max-width: 600px; padding: 20px; } .button { width: 100%; padding: 20px; margin: 10px 0; font-size: 20px; background-color: #4CAF50; color: white; border: none; border-radius: 8px; } .status { padding: 10px; margin: 10px 0; background-color: #f0f0f0; text-align: center; } .permission-button { background-color: #ff9800; } </style> </head> <body> <h1>Speech Assistant</h1> <div id="status" class="status">Please enable speech synthesis</div> <button class="button permission-button" onclick="requestPermission()">Enable Speech</button> <button class="button" onclick="speak(1)">I am thirsty</button> <button class="button" onclick="speak(2)">I feel sick</button>
<script> let synth = window.speechSynthesis; let speaking = false; let speechEnabled = false; const messages = { 1: "I am thirsty", 2: "I feel sick" };
// Check speech synthesis support on load window.onload = function() { if (typeof speechSynthesis === 'undefined') { document.getElementById('status').textContent = 'Speech synthesis not supported in this browser'; return; } };
// Request permission and initialize speech function requestPermission() { if (typeof speechSynthesis === 'undefined') { document.getElementById('status').textContent = 'Speech synthesis not supported'; return; } // Try to speak a silent message to trigger permission let utterance = new SpeechSynthesisUtterance(''); utterance.volume = 0; utterance.onend = function() { speechEnabled = true; document.getElementById('status').textContent = 'Speech enabled - ready to use'; document.getElementById('status').style.color = 'green'; }; utterance.onerror = function() { document.getElementById('status').textContent = 'Error enabling speech. Please try again.'; document.getElementById('status').style.color = 'red'; }; synth.speak(utterance); }
function speak(buttonNumber) { if (speaking) return; if (!speechEnabled) { document.getElementById('status').textContent = 'Please enable speech first'; document.getElementById('status').style.color = 'red'; return; } let utterance = new SpeechSynthesisUtterance(messages[buttonNumber]); utterance.onstart = () => { speaking = true; document.getElementById('status').textContent = 'Speaking...'; document.getElementById('status').style.color = 'black'; }; utterance.onend = () => { speaking = false; document.getElementById('status').textContent = 'Ready'; document.getElementById('status').style.color = 'black'; }; synth.speak(utterance); }
// Poll for button presses setInterval(async () => { try { const response = await fetch('/button'); const data = await response.json(); if (data.pressed) { speak(data.button); } } catch (error) { console.error('Error polling button:', error); } }, 500); </script> </body> </html> )rawliteral";
void setup() { Serial.begin(115200); // Initialize buttons with pull-up resistors pinMode(BUTTON1_PIN, INPUT_PULLUP); pinMode(BUTTON2_PIN, INPUT_PULLUP); // Connect to WiFi Serial.print("Connecting to WiFi"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi Connected!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // Set up web server routes server.on("/", HTTP_GET, []() { server.send(200, "text/html", INDEX_HTML); }); server.on("/button", HTTP_GET, []() { String json; if (buttonPressed) { json = "{\"pressed\":true,\"button\":" + String(lastPressedButton) + "}"; buttonPressed = false; } else { json = "{\"pressed\":false,\"button\":0}"; } server.send(200, "application/json", json); }); server.begin(); Serial.println("Web server started"); }
void loop() { server.handleClient(); // Check buttons if (digitalRead(BUTTON1_PIN) == LOW) { buttonPressed = true; lastPressedButton = 1; Serial.println("Button 1 pressed"); delay(50); // Simple debounce } if (digitalRead(BUTTON2_PIN) == LOW) { buttonPressed = true; lastPressedButton = 2; Serial.println("Button 2 pressed"); delay(50); // Simple debounce } }