218 lines
5.3 KiB
C++
218 lines
5.3 KiB
C++
//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
|
|
//Board Name: Node Mcu 1.0
|
|
|
|
#include "config.h"
|
|
|
|
//protocol definition:
|
|
//1st byte: length of content +1
|
|
//2nd byte: packet type
|
|
//rest: content
|
|
|
|
#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);
|
|
#if DEBUG > 0
|
|
Serial.begin(115200);
|
|
#endif
|
|
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
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
//manage the connection to the arduino
|
|
void handleSerial() {
|
|
if (ser.available()) {
|
|
char in = ser.read();
|
|
if (in == 's') {
|
|
reportStatus();
|
|
} else if(in == 'r') {//report
|
|
D("read status:")
|
|
char* buffer = new char[64];
|
|
size_t i = 0;
|
|
for(; i < 64 && ser.available(); i++) {
|
|
buffer[i] = ser.read();
|
|
}
|
|
D("status recived, sending")
|
|
D(buffer)
|
|
D("i:")
|
|
D(i)
|
|
client.write((i+1)-1);
|
|
client.print('r');
|
|
client.write(buffer, i-1);
|
|
client.println();
|
|
client.flush();
|
|
delete[] buffer;
|
|
D("sending complete")
|
|
} else if(in == 'a') {
|
|
//TODO: forword to Service
|
|
}
|
|
}
|
|
}
|
|
|
|
void handleData(const char* data, size_t length) {
|
|
D("handle");
|
|
char type = data[0];
|
|
switch (type) {
|
|
case 'c': //close connection
|
|
client.stop();
|
|
break;
|
|
case 'p': //pingrequest
|
|
client.println("\x01P");
|
|
Serial.println("awnsered");
|
|
break;
|
|
case 'h': //hello
|
|
D("Hello recived")
|
|
ser.print('r');//get status
|
|
D("status requested")
|
|
break;
|
|
case 'g': //go (start)
|
|
case 'r'://request report
|
|
case 'e'://error report
|
|
case 'a'://abort
|
|
ser.print(type);
|
|
break;
|
|
default: Serial.print("unknown Data"); Serial.println(data); break;
|
|
}
|
|
}
|
|
|
|
void connectWLAN() {
|
|
setStatus(STATUS_WLAN, false);
|
|
setStatus(STATUS_SERVICE, false);
|
|
//try to connect to wifi
|
|
LEDOFF
|
|
D("Connect to WiFi");
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
#ifndef DHCP
|
|
WiFi.config(ip, gateway, subnet, gateway);
|
|
#endif
|
|
while (WiFi.status() != WL_CONNECTED) delay(500);
|
|
D("IP address: ");
|
|
D(WiFi.localIP());
|
|
}
|
|
|
|
void connectService() {
|
|
//try to reconnect to server
|
|
setStatus(STATUS_SERVICE, false);
|
|
D("connect to server");
|
|
if (!client.connect(host, port)) {
|
|
D("connection failed")
|
|
delay(5000);
|
|
} else {
|
|
client.setNoDelay(true);
|
|
D("connected")
|
|
client.println("\x01h");
|
|
client.setTimeout(60 * 5000); //5min
|
|
}
|
|
}
|
|
|
|
void checkTimeout() {
|
|
if ( (millis() - lastping) / 1000 > PINGREQUIRED) { //TODO: check if ping allready sent
|
|
//request ping
|
|
client.println("\x01p");
|
|
D("ping sent")
|
|
} else if ((millis() - lastping) / 1000 > PINGTIMEOUT) {
|
|
//timeout
|
|
D("timedout")
|
|
client.stop();
|
|
}
|
|
}
|
|
|
|
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
|
|
|
|
handleSerial();
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
setStatus(STATUS_WLAN, true);
|
|
LEDON
|
|
if (client.connected()) {
|
|
setStatus(STATUS_SERVICE, true);
|
|
//do reciving stuff
|
|
while (!client.available() && client.connected()) {
|
|
delay(100);
|
|
handleSerial();
|
|
}
|
|
|
|
D("Data available")
|
|
|
|
if (client.available()) {
|
|
D("\nrecive stuff ");
|
|
lastping = millis();//reset lastping
|
|
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
|
|
//Serial.print("awaiting: ");
|
|
D(ch);
|
|
char* buff = new char[ch];
|
|
for (unsigned char i = 0; i < ch; i++) {
|
|
if (client.available()) {
|
|
buff[i] = static_cast<char>(client.read());
|
|
//Serial.print(buff[i], HEX);
|
|
} else {
|
|
buff[i] = 0;
|
|
break;
|
|
}
|
|
}
|
|
//done reciving processing
|
|
//Serial.print("\nrecived: ");
|
|
//Serial.println(buff);
|
|
handleData(buff, ch);
|
|
client.flush();
|
|
}
|
|
checkTimeout();
|
|
} else {//client.connected()
|
|
connectService();
|
|
}
|
|
} else {
|
|
connectWLAN();
|
|
}
|
|
delay(100);
|
|
}
|