********* by Ningh with reference to Rui Santos http://randomnerdtutorials.com Arduino IDE example: Examples > Arduino OTA > BasicOTA.ino *********/ //持久数据 #include #include //OTA相关 #include #include #include #include //时钟模块相关 #include #include "RTClib.h" //esp8266中断 #include "Ticker.h" //引入调度器头文件 Servo myservo; //建立时钟对象 RTC_DS3231 rtc; //建立中断调度的对象 Ticker myTicker; Ticker POF;//timer to stop pump Ticker minu;//timer to enable ce check boolean tag = false; boolean ce = true; // Replace with your network credentials const char* ssid = "your-ssid"; const char* password = "your-password"; const char* assid = "feeder"; const char* asecret = "12345678"; WiFiServer server(80);//创建tcp server const int PUMP_Pin = D5; int connCount = 0;//连接失败计数 int addr = 1; String html_eep=""; DateTime now; void setup() { WiFi.mode(WIFI_AP_STA); WiFi.softAP(assid, asecret); WiFi.begin(ssid, password); // myservo.attach(D3); //myservo.write(90); while (WiFi.waitForConnectResult() != WL_CONNECTED) { delay(500); if(connCount > 30) {//如果连续30次连不上,则跳出循环,执行后面的程序 break; } connCount += 1; //ESP.restart(); } // Hostname defaults to esp8266-[ChipID] //ArduinoOTA.setHostname("WemosEXP"); ArduinoOTA.begin(); server.begin();//启动tcp连接 Serial.begin(115200); if (! rtc.begin()) {//开始RTC //Serial.println("Couldn't find RTC"); while (1); } // //rtc.adjust(DateTime(2019, 4, 11, 21, 40, 0)); EEPROM.begin(512); myTicker.attach(1, tickerHandle); //Serial.print(WiFi.localIP()); } void tickerHandle() //到时间时需要执行的任务 { now = rtc.now(); tag = true; } String prepareHtmlPage(){ String htmlPage = String("HTTP/1.1 200 OK\r\n") + "Content-Type: text/html\r\n" + "Connection: close\r\n" + // the connection will be closed after completion of the response // "Refresh: 5\r\n" + // refresh the page automatically every 5 sec "\r\n" + " \n"+ "\n"+ "Pump Control\n"+ "\n"+ "\n"+ "\n"+ "

Digital PIN3 status: "+String(digitalRead(D3))+"

\n"+ "

"+ "

"+ "

"+html_eep+"

"+ "
"+ "第1组开始:
"+ "时长 (秒):

"+ "第2组开始:
"+ "时长 (秒):

"+ ""+ "" + "\r\n"; return htmlPage; } String tp1 = ""; String tp2 = ""; int reading = HIGH; int previous; void loop() { ArduinoOTA.handle(); WiFiClient client = server.available(); // wait for a client (web browser) to connect if (client){ Serial.println("Client!!"); while (client.connected()){ // 不断读取请求内容 if (client.available()){ String request = client.readStringUntil('\r'); if ( request.indexOf("LEDON") > 0&&ce ) { sweep(); ce = false; minu.attach(5, ceEna); } else if ( request.indexOf("LEDOFF") > 0 ) { Serial.println("OFF"); } int total; //如果有返回值,则tp1不为空,eeprom占3位 if ( request.indexOf("time") > 0 ) { tp1 = setMem(request, "time1", 0, "&time2", 0); tp2 = setMem(request, "time2", 0); logEep(tp1); total = 3; EEPROM.write(0, total); EEPROM.commit(); delay(5); } //如果tp2不为空,eeprom占6位 if ((tp2.indexOf("&period") - tp2.indexOf("&time")) > 7 && (tp2.length() - tp2.indexOf("&period") > 9 )) { logEep(tp1); logEep(tp2); total = 6; EEPROM.write(0, total); EEPROM.commit(); delay(5); } addr = 1; String s = "内部时钟" + String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()) + "
"; //String s = ""; if (EEPROM.read(0) == 3) { html_eep = s + String(EEPROM.read(1)) + ":" + String(EEPROM.read(2)) + "----" + String(EEPROM.read(3)); } if (EEPROM.read(0) == 6) { html_eep = s + String(EEPROM.read(1)) + ":" + String(EEPROM.read(2)) + "----" + String(EEPROM.read(3)) + "
" + String(EEPROM.read(4)) + ":" + String(EEPROM.read(5)) + "----" + String(EEPROM.read(6)) + "
"; } // wait for end of client's request, that is marked with an empty line if (request.length() == 1 && request[0] == '\n'){ //返回响应内容 client.println(prepareHtmlPage()); break; } } } delay(5); // give the web browser time to receive the data // close the connection: client.flush();//注意这里必须用client.flush(),如果是client.close就会报错的 } if (tag) { // Serial.println(now.second()); if (EEPROM.read(0) == 3) { checkRun(EEPROM.read(1), EEPROM.read(2), EEPROM.read(3)); } if (EEPROM.read(0) == 6) { checkRun(EEPROM.read(1), EEPROM.read(2), EEPROM.read(3)); checkRun(EEPROM.read(4), EEPROM.read(5), EEPROM.read(6)); } tag = false; } } void checkRun(int th, int tm, int ts) { if (th == now.hour() && tm == now.minute() && ce) { sweep(); ce = false; minu.attach(60, ceEna); } } void ceEna() { ce = true; minu.detach(); Serial.println(ce); } void logEep(String text) { int hours = setMem(text, "time", 6, "%3A", 0).toInt(); int minutes = setMem(text, "%3A", 3, "&period", 0).toInt(); int seconds = setMem(text, "&period", 9).toInt(); write(hours); write(minutes); write(seconds); } String setMem(String ori, String start, int offs, String end, int offe) { int s = ori.indexOf(start) + offs; int e = ori.indexOf(end) + offe; return ori.substring(s, e); } String setMem(String ori, String start, int offs) { int s = ori.indexOf(start) + offs; return ori.substring(s); } void write(int value) { EEPROM.write(addr, value); addr = addr + 1; } int flag=0; void sweep(){ myservo.attach(D3); if(flag==0){ for (int pos = 90; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(5); // waits 15ms for the servo to reach the position } for (int pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(5); // waits 15ms for the servo to reach the position } flag=1; } else{ for (int pos = 90; pos >= 0; pos -= 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(5); // waits 15ms for the servo to reach the position } for (int pos = 0; pos <= 90; pos += 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(5); // waits 15ms for the servo to reach the position } flag=0; } delay(500); myservo.detach(); Serial.println("sweep!"); }