bool WIFI::confMode(byte a) { if (!wifiPresent) return false; String data; print("AT+CWMODE="); println(String(a)); unsigned long start; start = millis(); while (millis()-start<2000) { if(_cell.available()>0) { char a =_cell.read(); data=data+a; } if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1) { return true; } if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1) { while(_cell.available()>0) { char a =_cell.read(); data=data+a; } DBGLN(data); return false; } } }
bool WIFI::begin(void) { boolean result = false; _cell.begin(115200); //The default baud rate of ESP8266 is 115200 DebugSerial.begin(debugBaudRate); _cell.flush(); _cell.setTimeout(3000); println("AT+RST"); result = _cell.find("ready"); if(result) { DBGLN("Module is ready"); wifiPresent = true; } else { DBGLN("Module have no response"); wifiPresent = false; } return wifiPresent; }
/************************************************************************* //reboot the wifi module ***************************************************************************/ void WIFI::Reset(void) { if (!wifiPresent) return; println("AT+RST"); unsigned long start; start = millis(); while (millis()-start<5000) { if(_cell.find("ready")==true) { DBGLN("reboot wifi is OK"); break; } } }
int waterDo(unsigned int time) { if (waterLastTime != 0 && (unsigned long)(fixedMillis() - waterLastTime) < WATER_MIN_INTERVAL) { return ERROR_INTERVALTOOSHORT; } if (distanceLastValue > WATER_TANK_THRESHOLD) { return ERROR_NOWATER; } DBG("Watering: "); relay(true); delay(time); relay(false); DBGLN("done."); // TODO(twd2): check if moisture changed waterLastTime = fixedMillis(); return ERROR_OK; }
/************************************************************************* //receive message from wifi buf: buffer for receiving data chlID: <id>(0-4) return: size of the buffer ***************************************************************************/ int WIFI::ReceiveMessage(char *buf) { if (!wifiPresent) return 0; //+IPD,<len>:<data> //+IPD,<id>,<len>:<data> String data = ""; if (_cell.available()>0) { unsigned long start; start = millis(); char c0 = _cell.read(); if (c0 == '+') { while (millis()-start<5000) { if (_cell.available()>0) { char c = _cell.read(); data += c; } if (data.indexOf("\nOK")!=-1) { break; } } int sLen = strlen(data.c_str()); int i,j; for (i = 0; i <= sLen; i++) { if (data[i] == ':') { break; } } boolean found = false; for (j = 4; j <= i; j++) { if (data[j] == ',') { found = true; break; } } int iSize; DBGLN(data); if(found ==true) { String _id = data.substring(4, j); chlID = _id.toInt(); String _size = data.substring(j+1, i); iSize = _size.toInt(); //DBG(_size); String str = data.substring(i+1, i+1+iSize); strcpy(buf, str.c_str()); //DBG(str); } else { String _size = data.substring(4, i); iSize = _size.toInt(); //DBGLN(iSize); String str = data.substring(i+1, i+1+iSize); strcpy(buf, str.c_str()); //DBG(str); } return iSize; } } return 0; }
void WIFI::println(const String &s) { DBGLN(s); _cell.println(s); }