From df55e6e499409d18eca636223a4df11c137c163f Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 25 Feb 2019 22:15:19 +0100 Subject: [PATCH] first connection --- nudelmaschine/esp/esp.ino | 49 +++++++++++++++++++++++++++++--------- nudelmaschine/main/proto.h | 24 +++++++++++++++++++ 2 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 nudelmaschine/main/proto.h diff --git a/nudelmaschine/esp/esp.ino b/nudelmaschine/esp/esp.ino index 3889a07..ad64425 100644 --- a/nudelmaschine/esp/esp.ino +++ b/nudelmaschine/esp/esp.ino @@ -1,7 +1,13 @@ //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 #define LED 2 @@ -36,10 +42,6 @@ void reportStatus() { 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) { @@ -54,7 +56,30 @@ void setStatus(unsigned char mask, bool newstat) { } void handleData(const char* data, size_t length) { - + Serial.println("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 + break; + case 's': //start + ser.print('g');//go + break; + 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 loop() { @@ -82,29 +107,30 @@ void loop() { Serial.println("Data available"); if(client.available()) {//never enters this state, why? - Serial.println("recive stuff"); + Serial.print("\nrecive stuff "); lastping = millis();//reset lastping unsigned char ch = static_cast(client.read()); // read the size - Serial.print("awaiting: "); + //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); + //Serial.print(buff[i], HEX); } else { buff[i] = 0; break; } } //done reciving processing - Serial.print("\nrecived: "); - Serial.println(buff); + //Serial.print("\nrecived: "); + //Serial.println(buff); handleData(buff, ch); + client.flush(); } if( (millis() - lastping)/1000 > PINGREQUIRED) { //request ping - client.print("\x01p"); + client.println("\x01p"); Serial.println("ping sent"); } else if((millis() - lastping)/1000 > PINGTIMEOUT) { //timeout @@ -138,4 +164,5 @@ void loop() { D("IP address: "); D(WiFi.localIP()); } + delay(100); } diff --git a/nudelmaschine/main/proto.h b/nudelmaschine/main/proto.h new file mode 100644 index 0000000..41c1364 --- /dev/null +++ b/nudelmaschine/main/proto.h @@ -0,0 +1,24 @@ +//A file to support the main program on using the protocol +#pragma once + +enum State { + Idle = 0, + Abortable, + Unabortable, + Done, + Failed, + Unknown; +} + +/* +struct Data { + byte length = 0; + byte type = 'h'; + String data; + + Data(byte l, byte t, const char* d) : type(t), data(&d) { + for(; d[length] != 0; length++) { } + } + Data(byte t) : type(t), data('\0') {}; +}; +*/