Arduino/nudelmaschine/esp/esp.ino

218 lines
5.3 KiB
Arduino
Raw Normal View History

2019-01-04 12:04:49 +01:00
//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
//Board Name: Node Mcu 1.0
2019-02-25 22:15:19 +01:00
2019-01-04 12:04:49 +01:00
#include "config.h"
2019-02-25 22:15:19 +01:00
//protocol definition:
//1st byte: length of content +1
//2nd byte: packet type
//rest: content
2019-01-04 12:04:49 +01:00
#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);
2019-06-16 22:24:15 +02:00
#if DEBUG > 0
2019-01-04 12:04:49 +01:00
Serial.begin(115200);
2019-06-16 22:24:15 +02:00
#endif
2019-01-04 12:04:49 +01:00
wifi_station_set_hostname("NudelMaschine");
}
void reportStatus() {
2019-03-21 21:19:35 +01:00
ser.print('s');
ser.print(statusbits & STATUS_WLAN ? 'W' : 'w');
ser.print(statusbits & STATUS_SERVICE ? 'S' : 's');
ser.print('\0');//EOT
2019-01-04 12:04:49 +01:00
}
void setStatus(unsigned char mask, bool newstat) {
bool old = statusbits & mask;
2019-03-21 13:45:44 +01:00
if (old != newstat) {
2019-01-04 12:04:49 +01:00
//only when its realy a new state
//flip
statusbits ^= mask;
//update
reportStatus();
}
}
2019-03-21 13:45:44 +01:00
//manage the connection to the arduino
void handleSerial() {
if (ser.available()) {
char in = ser.read();
if (in == 's') {
reportStatus();
} else if(in == 'r') {//report
2019-06-17 15:27:09 +02:00
D("read status:")
2019-06-16 22:24:15 +02:00
char* buffer = new char[64];
size_t i = 0;
for(; i < 64 && ser.available(); i++) {
buffer[i] = ser.read();
}
2019-06-17 15:27:09 +02:00
D("status recived, sending")
D(buffer)
D("i:")
D(i)
client.write((i+1)-1);
2019-06-16 22:24:15 +02:00
client.print('r');
2019-06-17 15:27:09 +02:00
client.write(buffer, i-1);
client.println();
client.flush();
delete[] buffer;
D("sending complete")
2019-03-21 13:45:44 +01:00
} else if(in == 'a') {
2019-03-21 21:19:35 +01:00
//TODO: forword to Service
2019-03-21 13:45:44 +01:00
}
}
}
2019-01-14 16:00:31 +01:00
void handleData(const char* data, size_t length) {
2019-06-16 22:24:15 +02:00
D("handle");
2019-02-25 22:15:19 +01:00
char type = data[0];
2019-03-21 13:45:44 +01:00
switch (type) {
2019-02-25 22:15:19 +01:00
case 'c': //close connection
client.stop();
2019-03-21 13:45:44 +01:00
break;
2019-02-25 22:15:19 +01:00
case 'p': //pingrequest
client.println("\x01P");
Serial.println("awnsered");
2019-03-21 13:45:44 +01:00
break;
2019-02-25 22:15:19 +01:00
case 'h': //hello
D("Hello recived")
ser.print('r');//get status
2019-06-17 15:27:09 +02:00
D("status requested")
2019-03-21 13:45:44 +01:00
break;
case 'g': //go (start)
2019-02-25 22:15:19 +01:00
case 'r'://request report
case 'e'://error report
case 'a'://abort
ser.print(type);
2019-03-21 13:45:44 +01:00
break;
2019-02-25 22:15:19 +01:00
default: Serial.print("unknown Data"); Serial.println(data); break;
}
2019-01-14 16:00:31 +01:00
}
2019-03-21 13:45:44 +01:00
void connectWLAN() {
setStatus(STATUS_WLAN, false);
setStatus(STATUS_SERVICE, false);
//try to connect to wifi
LEDOFF
2019-06-16 22:24:15 +02:00
D("Connect to WiFi");
2019-03-21 13:45:44 +01:00
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
2019-06-16 22:24:15 +02:00
#ifndef DHCP
2019-03-21 13:45:44 +01:00
WiFi.config(ip, gateway, subnet, gateway);
2019-06-16 22:24:15 +02:00
#endif
2019-03-21 13:45:44 +01:00
while (WiFi.status() != WL_CONNECTED) delay(500);
D("IP address: ");
D(WiFi.localIP());
}
void connectService() {
//try to reconnect to server
setStatus(STATUS_SERVICE, false);
2019-06-16 22:24:15 +02:00
D("connect to server");
2019-03-21 13:45:44 +01:00
if (!client.connect(host, port)) {
2019-06-16 22:24:15 +02:00
D("connection failed")
2019-03-21 13:45:44 +01:00
delay(5000);
} else {
client.setNoDelay(true);
2019-06-16 22:24:15 +02:00
D("connected")
2019-03-21 13:45:44 +01:00
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");
2019-06-16 22:24:15 +02:00
D("ping sent")
2019-03-21 13:45:44 +01:00
} else if ((millis() - lastping) / 1000 > PINGTIMEOUT) {
//timeout
2019-06-16 22:24:15 +02:00
D("timedout")
2019-03-21 13:45:44 +01:00
client.stop();
}
}
2019-01-04 12:04:49 +01:00
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
2019-03-21 13:45:44 +01:00
handleSerial();
2019-01-04 12:04:49 +01:00
2019-03-21 13:45:44 +01:00
if (WiFi.status() == WL_CONNECTED) {
2019-01-04 12:04:49 +01:00
setStatus(STATUS_WLAN, true);
LEDON
if (client.connected()) {
setStatus(STATUS_SERVICE, true);
2019-03-21 13:45:44 +01:00
//do reciving stuff
while (!client.available() && client.connected()) {
delay(100);
handleSerial();
}
2019-06-16 22:24:15 +02:00
D("Data available")
2019-03-21 13:45:44 +01:00
if (client.available()) {
2019-06-16 22:24:15 +02:00
D("\nrecive stuff ");
2019-03-21 13:45:44 +01:00
lastping = millis();//reset lastping
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
//Serial.print("awaiting: ");
2019-06-16 22:24:15 +02:00
D(ch);
2019-03-21 13:45:44 +01:00
char* buff = new char[ch];
for (unsigned char i = 0; i < ch; i++) {
if (client.available()) {
2019-01-04 12:04:49 +01:00
buff[i] = static_cast<char>(client.read());
2019-02-25 22:15:19 +01:00
//Serial.print(buff[i], HEX);
2019-03-21 13:45:44 +01:00
} else {
2019-01-04 12:04:49 +01:00
buff[i] = 0;
break;
2019-03-21 13:45:44 +01:00
}
}
//done reciving processing
//Serial.print("\nrecived: ");
//Serial.println(buff);
handleData(buff, ch);
client.flush();
2019-01-04 12:04:49 +01:00
}
2019-03-21 13:45:44 +01:00
checkTimeout();
2019-01-14 16:00:31 +01:00
} else {//client.connected()
2019-03-21 13:45:44 +01:00
connectService();
2019-01-04 12:04:49 +01:00
}
2019-03-21 13:45:44 +01:00
} else {
connectWLAN();
}
delay(100);
2019-01-04 12:04:49 +01:00
}