char index_html[]=
"<html>\n\
<head>\n\
<title>Arduino Seeed XIAO ESP32C3 Controller</title>\n\
<meta name=\"viewport\" content=\"width=device-width\" />\n\
<style type=\"text/css\">\n\
<!--\n\
.style1 {\n\
font-family: Arial, Helvetica, sans-serif;\n\
font-size: 18px;\n\
}\n\
-->\n\
</style></head>\n\
<body>\n\
<form action=\"http://esp32.local/setvol\" method=\"get\" class=\"style1\">\n\
<p>Arduino Seeed XIAO ESP32C3<br>\n\
Simple Controller Prototype</p>\n\
<p>EFFECT:<input type=\"text\" name=\"v1\" value=\"128\"/></p>\n\
<p>FEEDBACK:<input type=\"text\" name=\"v2\" value=\"92\"/></p>\n\
<p>TIME:<input type=\"text\" name=\"v3\" value=\"188\"/></p>\n\
<p>----:<input type=\"text\" name=\"v4\" value=\"0\"/></p>\n\
<br/>\n\
<p><input type=\"submit\" name=\"sn\" value=\"Send\"/></p>\n\
</form>\n\
</body>\n\
</html>\n";
/*
Arduino Web Server simplified version
Device:
IoT board: Seeed Studio XIAO ESP32C3
LCD: AQM1602Y
Usage:
Access the URL with a web browser,
http://esp32.local/ <- using mDNS
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <HTTPUpdateServer.h>
#include <time.h>
#define JST 3600*9
#ifndef STASSID
#define STASSID "your ssid"
#define STAPSK "your password"
#endif
#include <Wire.h>
#include <ST7032.h>
#include <EEPROM.h>
#define SIGNATURE "SGNT101"
#define LED1_PIN D7
struct EEPROM_DATA{
char value1[128];
char value2[128];
char check[10]; // ""
};
EEPROM_DATA eeprom;
ST7032 lcd;
const char* host = "esp32-webupdate";
const char* ssid = STASSID;
const char* password = STAPSK;
extern char index_html[];
WebServer httpServer(80);
HTTPUpdateServer httpUpdater;
// Utility: string copy with length limit
void CopyCharByLength(char *org, char *dest, int max_length)
{
int i=0;
while(1)
{
if(org[i]=='\0') break;
if(i>=max_length) break;
dest[i] = org[i];
i++;
}
dest[i] = '\0';
}
// Utility: get real time string HH:MM.SS
void getRTC( char* timestr )
{
time_t t;
struct tm *tm;
t = time(NULL);
tm = localtime(&t);
sprintf(timestr, "%02d:%02d.%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
}
// Utility: print RTC on LCD
void LcdPrintRTC(void)
{
// Real time
char rtcstr[16];
getRTC(rtcstr);
lcd.setCursor(0, 1);
lcd.print(rtcstr);
}
// Read from EEPROM
int load_eeprom() {
EEPROM.get<EEPROM_DATA>(0, eeprom);
if(strcmp(eeprom.check, SIGNATURE)==0)
{
Serial.print("Load EEPROM(Value1):");
Serial.print(eeprom.value1);
return 1;
}
else
{
// Erase buffer memory if signature is wrong
Serial.print("EEPROM:error, wrong signature\n");
memset(&eeprom, 0, sizeof(eeprom));
return 0;
}
}
// Save to EEPROM
void save_eeprom() {
strcpy(eeprom.check, SIGNATURE);
EEPROM.put<EEPROM_DATA>(0, eeprom);
Serial.print("Save EEPROM(Value1):");
Serial.println(eeprom.value1);
EEPROM.commit();
}
// callback when root url accessed
#define HELLO_MESSAGE "Hello from esp32"
void handleRoot() {
//LED
digitalWrite(LED1_PIN, HIGH);
// LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(WiFi.localIP());
//lcd.setCursor(0, 1);
//lcd.print("index.html sent");
LcdPrintRTC();
//httpServer.send(200, "text/plain", HELLO_MESSAGE);
httpServer.send(200, "text/html", index_html);
//LED
digitalWrite(LED1_PIN, LOW);
}
// callback when '/setvol' uri accessed
void handleSetVol() {
// LCD
//lcd.setCursor(0, 0);
//lcd.print(httpServer.uri());
//LED
digitalWrite(LED1_PIN, HIGH);
String message = "Receive params..\n\n";
message += "URI: ";
message += httpServer.uri();
message += "\nMethod: ";
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += httpServer.args();
message += "\n";
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
}
httpServer.send(404, "text/plain", message);
// LCD
String lcdMessage = "";
lcd.clear();
lcd.setCursor(0, 0);
for (uint8_t i = 0; i < httpServer.args(); i++) {
//lcdMessage += httpServer.argName(i);
//lcdMessage += "=";
char tmp[128];
httpServer.argName(i).toCharArray(tmp, 127);
if(strcmp(tmp, "sn")!=0){
lcdMessage += httpServer.arg(i);
lcdMessage += ",";
}
}
lcd.print(lcdMessage);
//httpServer.send(404, "text/plain", lcdMessage);
// Write to EEPROM
lcdMessage.toCharArray(eeprom.value1, 127);
eeprom.value1[127]='\0';
Serial.println(eeprom.value1);
//CopyCharByLength(message, eeprom.value1, 127);
save_eeprom();
//LED
digitalWrite(LED1_PIN, LOW);
}
// System setup
void setup(void) {
// digital pin
pinMode(LED1_PIN, OUTPUT);
// Serial monitor: start
Serial.begin(115200);
Serial.println();
Serial.println("Booting Sketch...");
// WiFi: start
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.begin(ssid, password);
Serial.println("WiFi failed, retrying.");
}
// WiFi: DNS start , register "esp32.local" to Multicast Domain Name System(mDNS)
MDNS.begin(host);
if (MDNS.begin("esp32")) {
Serial.println("mDNS responder started");
}
// Time: get JST time
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
// Web Server: setup
httpUpdater.setup(&httpServer);
// Web Server: handler when root accessed
httpServer.on("/", handleRoot);
httpServer.onNotFound(handleRoot);
// Web Server: handler when /setvol accessed
httpServer.on("/setvol", handleSetVol);
// Web Server: launch web server
httpServer.begin();
MDNS.addService("http", "tcp", 80);
Serial.print("HTTPUpdateServer ready! \n");
Serial.print("http://");
Serial.println(WiFi.localIP());
// LCD: setup
lcd.begin(16, 2);
lcd.setContrast(45);
lcd.setCursor(0, 0);
lcd.print(WiFi.localIP());
// Time: print RTC on LCD
LcdPrintRTC();
// EEPROM: start
memset(&eeprom, 0, sizeof(eeprom));
EEPROM.begin(1024);
load_eeprom();
}
// Event loop
void loop(void) {
static int counter=0;
httpServer.handleClient();
// Blink arrow on LCD to check system alive
char str_arrow[2] = {0x7e, 0};
lcd.setCursor(15, 1);
counter++;
counter %= 500;
switch(counter)
{
case 0: lcd.print(" "); break;
case 250: lcd.print(str_arrow); break;
default: break;
}
}