//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json //Board Name: Node Mcu 1.0 #include "config.h" #include #define LED 2 #define DEBUG 1 #if DEBUG > 0 #define D(A) Serial.println(A); #else #define D(A) ; #endif #define LEDON digitalWrite(LED, LOW); #define LEDOFF digitalWrite(LED, HIGH); #define STATUS_WLAN 1 #define STATUS_SERVICE 2 #define PINGREQUIRED 180 //alle 3 Minuten Ping requesten #define PINGTIMEOUT 200 //nach weiteren 20 sekunden sollte ein ping erfolgt sein SoftwareSerial ser(14, 12);//RX: GPIO 14 = D5; TX: GPIO 12 = D6 WiFiClient client; unsigned char statusbits = 0; //bit 0 = wlan, bit 1 = service; unsigned long lastping = 0; void setup() { pinMode(LED, OUTPUT); ser.begin(9600); Serial.begin(115200); wifi_station_set_hostname("NudelMaschine"); } void reportStatus() { ser.print("s"); ser.print(statusbits & STATUS_WLAN ? "W" : "w"); ser.print(statusbits & STATUS_SERVICE ? "S" : "s"); ser.print("\0");//EOT /*ser.print("WLAN: "); ser.print(statusbits & STATUS_WLAN ? "y" : "n"); ser.print(" Service: "); ser.print(statusbits & STATUS_SERVICE ? "y" : "n");*/ } void setStatus(unsigned char mask, bool newstat) { bool old = statusbits & mask; if(old != newstat) { //only when its realy a new state //flip statusbits ^= mask; //update reportStatus(); } } void handleData(const char* data, size_t length) { } void loop() { //logic //TODO: //wait for data: when nothing recived for 3min -> send ping shoud respond within 2 seconds if not -> notify mashine and go in idle mode //when recived command for mashine -> fwd to mashine (start, stop, emergency stop, reset errors, ....) //when garbage recived -> notify mashine to display error //when connection failed -> notify mashine to display error //when ping recived -> respond fast! //when WIFI connection lost -> try to reconnect & notify mashine //when WIFI connection is reestablished -> notify mashine and continue //when mashine sends error -> fwd to server if (WiFi.status() == WL_CONNECTED) { setStatus(STATUS_WLAN, true); LEDON if (client.connected()) { setStatus(STATUS_SERVICE, true); //do reciving stuff while(!client.available()) delay(1000); Serial.println("Data available"); if(client.available()) {//never enters this state, why? Serial.println("recive stuff"); lastping = millis();//reset lastping unsigned char ch = static_cast(client.read()); // read the size Serial.print("awaiting: "); Serial.println(ch, DEC); char* buff = new char[ch]; for(unsigned char i = 0; i < ch; i++) { if(client.available()) { buff[i] = static_cast(client.read()); Serial.print(buff[i], HEX); } else { buff[i] = 0; break; } } //done reciving processing Serial.print("\nrecived: "); Serial.println(buff); handleData(buff, ch); } if( (millis() - lastping)/1000 > PINGREQUIRED) { //request ping client.print("\x01p"); Serial.println("ping sent"); } else if((millis() - lastping)/1000 > PINGTIMEOUT) { //timeout Serial.println("timedout"); client.stop(); } } else {//client.connected() //try to reconnect to server setStatus(STATUS_SERVICE, false); Serial.println("connect to server"); if (!client.connect(host, port)) { Serial.println("connection failed"); delay(5000); } else { client.setNoDelay(true); Serial.println("connected"); client.println("\x01h"); client.setTimeout(60*5000);//5min } } } else { setStatus(STATUS_WLAN, false); //try to connect to wifi LEDOFF Serial.println("Connect to WiFi"); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); WiFi.config(ip,gateway,subnet,gateway); while(WiFi.status() != WL_CONNECTED) delay(500); D("IP address: "); D(WiFi.localIP()); } }