Arduino/nudelmaschine/esp/esp.ino

126 lines
3.5 KiB
C++

//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
//Board Name: Node Mcu 1.0
#include "config.h"
#include <SoftwareSerial.h>
#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("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 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);
if(client.available()) {//never enters this state, why?
Serial.println("recive stuff");
lastping = millis();
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
char* buff = new char[ch];
for(unsigned char i = 0; i < ch; i++) {
if(client.available()) {
buff[i] = static_cast<char>(client.read());
} else {
buff[i] = 0;
break;
}
}
//done reciving processing
Serial.print("recived: ");
Serial.println(buff);
//handle ?
}
if( (millis() - lastping) > PINGREQUIRED) {
//request ping
client.print("ping");
} else if((millis() - lastping) > PINGTIMEOUT) {
//timeout
client.stop();
}
} else {
//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 {
Serial.println("connected");
client.println("hello");
}
}
} 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());
}
}