//***************************************************************** // 玄関の外灯自動on/off プログラム //***************************************************************** // // 1.Wifiからlilght on/off のコマンドを受信して、IO13をon/offさせる // 2.IO13の先にNPNを経由してAC-SWを接続し、玄関外灯をon/offさせる // 3.Wifiから受信するコマンドは // (1) http://192.168.0.102/lighton // -> IO13 on // (2) http://192.168.0.102/lightoff // -> IO13 off // (3) http://192.168.0.102/ // -> program versin,reset time,operating time を返す // 4.IPは固定IPを使用する。 // -> 192.168.0.102 // // // ESP32 DevKitC ----------------------------- // // +----------------+ // | ANT | // -------+----------------+---------- // | 01. 3V3(PIC/VDD) 01. GND | // | 02. EN(PIC/GP0) 02. IO23 | // | 03. VP 03. IO22 | // | 04. VN 04. TX | // | 05. IO34 05. RX | // | 06. IO33 06. IO21 | // | 07. IO32(PIC/MCLR) 07. GND | // | 08. IO33 08. IO19 | // | 09. IO25 09. IO18 | // | 10. IO26 10. IO5 | // | 11. IO27 11. IO17 | // | 12. IO14 12. IO16 | // | 13. IO12(ticktack) 13. IO4 | // | 14. GND 14. IO0 | // | 15. IO13(light on/off)15. IO2 | // | 16. SD2 16. IO15 | // | 17. SD3 17. SD1 | // | 18. CMD 18. SD0 | // | 19. 5V 19. CLK | // ------------+-----+---------------- // | USB | // // 2022.03.09 V05 OTA 機能を追加(windows macafee のファイアーウオールを解除要) // 2022.03.10 V06 / operating time 追加 // 2022.03.11 V07 / operating time day hour min sec // 2022.03.11 V08 60 -> 62 分Wifi access が無かったらreset // 2022.03.11 V09 operating time 計算方法変更 // 2022.03.11 V10 Wifi 自宅とiPhone自動選択 // 2022.03.12 V11 if(w_counter4>=(1*100)){ // 2022.03.13 V12 getLocalTime(&timeInfo); // 2022.03.14 V13 getLocalTime(&timeInfo) は固定IPに変更する前で実行 // 2022.03.15 V14 プログラム中にコメントを追加 // 2022.03.23 V15 立ち上がり時の点滅を1回にする // 2022.03.25 V16 // 2022.03.26 V17 立ち上がり時の点滅をやめる // 2022.03.26 V18 WiFi=玄関のBuffaloに固定する // 2022.03.26 V19 WiFi=2FのBuffaloに固定する char w_version[30]="2022.03.26 V19"; // Program の版数 #include //#include #include #include #include #include #include #include WebServer server(80); //WiFiMulti wifiMulti; IPAddress ip; int64_t w_millis_last; int64_t w_millis_now; int64_t w_lap_time; int w_counter1=0; // 1sec counter int w_counter2=0; // 10sec counter int w_counter3=0; // Wifi counter int w_IO12_f=0; int w_IO25_f=0; int w_IO34; char w_http[100]; char w_ipaddress_str[100]; int w_gaitousw_on_off=0; int w_day=0; int w_hour=0; int w_min=0; int w_sec=0; int w_counter4=0; // 1sec counter struct tm timeInfo; char w_start_time_str[30]; //******************************************************************************* //* set up * //******************************************************************************* void setup() { int i=0; strcpy(w_http," OK\n Program version : "); strcat(w_http,w_version); Serial.begin(115200); Serial.println("Booting"); Serial.println(" "); Serial.print("gaitousw program started version is "); Serial.println(w_version); pinMode(32,OUTPUT); // PIC12F675 MCLR digitalWrite(32,HIGH); pinMode(13,OUTPUT); // light on/off digitalWrite(13,LOW); pinMode(12,OUTPUT); // TikTac digitalWrite(12,LOW); WiFi.mode(WIFI_STA); WiFi.disconnect(); // wifiMulti.addAP("Buffalo-G-68B8","xjvbv75jmg3i5"); // wifiMulti.addAP("106F3F397EC1", "2ftj1rega9rjm"); // wifiMulti.addAP("iPhone", "19571128"); Serial.println(" "); Serial.print("WiFi connecting ... "); WiFi.begin("106F3F397EC1", "2ftj1rega9rjm"); // while(wifiMulti.run() != WL_CONNECTED){ while(WiFi.status() != WL_CONNECTED){ delay(250); Serial.print("."); if(w_IO12_f==1){ w_IO12_f=0; digitalWrite(12,LOW); }else{ w_IO12_f=1; digitalWrite(12,HIGH); } } Serial.println(" Connected!"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); ip=WiFi.localIP(); configTime(9 * 3600L, 0, "ntp.nict.jp", "time.google.com", "ntp.jst.mfeed.ad.jp"); getLocalTime(&timeInfo); sprintf(w_start_time_str, " %04d/%02d/%02d %02d:%02d:%02d", timeInfo.tm_year + 1900, timeInfo.tm_mon + 1, timeInfo.tm_mday, timeInfo.tm_hour, timeInfo.tm_min, timeInfo.tm_sec); Serial.print("Start time : "); Serial.println(w_start_time_str); if((ip[0]==192) && (ip[1]==168)){ WiFi.config(IPAddress(192,168,0,102),WiFi.gatewayIP(),WiFi.subnetMask()); //自宅、固定IP } Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); ArduinoOTA .onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else // U_SPIFFS type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() Serial.println("Start updating " + type); }) .onEnd([]() { Serial.println("\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); server.on("/",wifi_cmd); server.on("/lighton",wifi_lighton); server.on("/lightoff",wifi_lightoff); server.on("/reset",wifi_reset); server.onNotFound(wifi_notfound); server.begin(); w_millis_last=millis(); } //******************************************************************************* //* main loop * //******************************************************************************* void loop() { ArduinoOTA.handle(); server.handleClient(); // 正確に10msec になるまで待つ。main loop は10msec毎に回る w_millis_now=millis(); w_lap_time=w_millis_now-w_millis_last; if(w_lap_time<0){ }else{ while(w_lap_time<10){ w_millis_now=millis(); w_lap_time=w_millis_now-w_millis_last; } } w_millis_last=w_millis_now; //******************************************* //*** 1.0 sec **** //******************************************* // operating time w_counter4++; if(w_counter4>=(1*100)){ w_counter4=0; w_sec++; if (w_sec>59){ w_sec=0; w_min++; } if (w_min>59){ w_min=0; w_hour++; } if (w_hour>23){ w_hour=0; w_day++; } } //******************************************* //*** 1.0 sec **** //******************************************* // 生きている証に、IO12を1.0秒毎に点滅させる Tiktak w_counter1++; if(w_counter1>(1.0*100)) { w_counter1=0; if(w_IO12_f==1){ w_IO12_f=0; digitalWrite(12,LOW); }else{ w_IO12_f=1; digitalWrite(12,HIGH); } } //******************************************* //*** 10 sec **** //******************************************* // 10秒毎にIO32にLOWパルスを出して、PIC12F675(MCLR)をリセットする w_counter2++; if(w_counter2>(10*100)) { w_counter2=0; digitalWrite(32,LOW); delay(10); digitalWrite(32,HIGH); } //******************************************* //*** 62 minutes **** //******************************************* // 62分間wifiアクセスがなかったら、Wifiがハングアップしたと判断して、自己リセットする w_counter3++; if(w_counter3>(62*60*100)) { w_counter3=0; delay(100); ESP.restart(); } } //************************************************************** // wifi_cmd //************************************************************** void wifi_cmd(){ char w_data[1000]; char w_mill_str[100]; strcpy(w_data,w_http); Serial.println(" wifi access / "); if(w_gaitousw_on_off==1){ strcat(w_data,"\n gaitousw : on \n"); }else{ strcat(w_data,"\n gaitousw : off \n"); } strcat(w_data," reset date time : "); strcat(w_data,w_start_time_str); sprintf(w_mill_str,"\n operating time : %3dD ",w_day); strcat(w_data,w_mill_str); sprintf(w_mill_str,"%2dH ",w_hour); strcat(w_data,w_mill_str); sprintf(w_mill_str,"%2dM ",w_min); strcat(w_data,w_mill_str); sprintf(w_mill_str,"%2dS \n",w_sec); strcat(w_data,w_mill_str); server.send(200,"text/plain",w_data); w_counter3=0; } //************************************************************** // wifi_lighton //************************************************************** void wifi_lighton(){ digitalWrite(13,HIGH); Serial.println(" wifi access /lighton "); server.send(200,"text/plain","OK light on"); w_counter3=0; w_gaitousw_on_off=1; } //************************************************************** // wifi_lightoff //************************************************************** void wifi_lightoff(){ digitalWrite(13,LOW); Serial.println(" wifi access /lightoff "); server.send(200,"text/plain","OK light off"); w_counter3=0; w_gaitousw_on_off=0; } //************************************************************** // wifi_reset //************************************************************** void wifi_reset(){ Serial.println(" wifi access /reset "); server.send(200,"text/plain","OK reset"); delay(1000); ESP.restart(); } //************************************************************** // wifi_notfound //************************************************************** void wifi_notfound(){ server.send(200,"text/plain","NOT FOUND"); }