以下が、IOT蚊取り機のESP-WROOM-02のスケッチです。他のIOT機器から流用したので、不要なコードが残っています。自宅の他のIOT機器のコントロールをするWebサーバーの一員(WebブラウザからON/OFF、他のIOTへのLink)として存在するようなスケッチとしています。
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <WiFiUdp.h> #include <WString.h> // Stringライブラリ #include "Ticker.h" extern "C" { #include <user_interface.h> } /* Set these to your desired credentials. */ //const char *ssid = "ESP-Cerevo"; //const char *pass = ""; ESP8266WebServer server(80); Ticker ticker; String StairsColor = "#0000ff"; String WallColor = "#0000ff"; String NorthColor = "#0000ff"; String SouthColor = "#0000ff"; String UpColor = "#0000ff"; String DownColor = "#ff0000"; String form; WiFiUDP udp; IPAddress rIp = IPAddress(192, 168, 2, 5); //hp PC unsigned int rPort = 10001; ////////////////////////////////////////////////////////////////// void setup() { ////////////////////////////////////////////////////////////////// char* ssid = "**********"; char* pass = "**********"; //wifi_set_sleep_type(LIGHT_SLEEP_T); unsigned long T0 = millis(); boolean cflag = true; //ticker.attach(1, AnalogReadSend); setform(); Serial.begin(115200); delay(100); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); if (5000 <= millis() - T0) { cflag = false; break; } } WiFi.config(IPAddress(192, 168, 2, 106), WiFi.gatewayIP(), WiFi.subnetMask()); //固定IPアドレス delay(100); Serial.println(""); Serial.println("WiFi connected"); Serial.print("Connecting to "); Serial.println(ssid); IPAddress myIP = WiFi.localIP(); Serial.print("AP IP address: "); Serial.println(myIP); Serial.print("gateway:"); Serial.println(WiFi.gatewayIP()); Serial.println("Starting UDP"); server.on("/", handleRoot); // And as regular external functions: server.on("/sw_onoff", sw_onoff); //server.on("/off", handle_off); // /offが無いと何故か動かない server.begin(); udp.begin(10001); pinMode(16, OUTPUT); //SW ON/OFF digitalWrite(16, HIGH); server.send(200, "text/html", form); } ////////////////////////////////////////////////////////////////// void loop() { //////////////////////////////////////////////////////////////// server.handleClient(); delay(100); AnalogReadSend(); } ////////////////////////////////////////////////////////////////// void handleRoot() { //////////////////////////////////////////////////////////////// server.send(200, "text/html", form); } ////////////////////////////////////////////////////////////////// void sw_onoff() { // SW ON/OFF //////////////////////////////////////////////////////////////// static int flag = 0; //ticker.detach(); if (flag == 0) { digitalWrite(16, HIGH); flag = 1; WallColor = "#0000ff"; } else { digitalWrite(16, LOW); flag = 0; WallColor = "#ff0000"; } setform(); server.send(200, "text/html", form); //ticker.attach(1, AnalogReadSend); } ////////////////////////////////////////////////////////////////// void AnalogReadSend() { //////////////////////////////////////////////////////////////// long sum=0; int k,Val; for(k=0;k<10;k++){ Val=analogRead(A0); //A0は予約定数 sum=sum+Val; } Val=sum/10; udp.beginPacket(rIp, rPort); udp.write((byte *)&Val, 2); udp.endPacket(); } void setform() { form = \ "<html><head><meta name=viewport content=width=400> <meta http-equiv=Refresh content=2;URL=http://192.168.2.106/><meta charset=UTF-8></head>" "<div align=center>" "<Font Size=7><strong>IOT蚊取り機 </strong></Font><br><br>" "</div>" "<form action=sw_onoff><input type=submit value=SW(ON/OFF) style=font-size:20pt;position:absolute;left:120px;top:150px;color:#ffffff;background-color:" + WallColor + " target=tif></form>" "<iframe src=javascript:false style=display:none name=tif id=tif></iframe>" "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>" "<Font Size=6><strong>" "<a href=http://192.168.2.101>工房</a> " "<a href=http://192.168.2.102>物置1F</a> " "<a href=http://192.168.2.103>工房北</a> " "<a href=http://192.168.2.104>庭LED</a> " "</strong></Font>" "</html>"; }