first connection

This commit is contained in:
mrbesen 2019-02-25 22:15:19 +01:00
parent 5dbc3eb983
commit df55e6e499
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 62 additions and 11 deletions

View File

@ -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 <SoftwareSerial.h>
#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<unsigned char>(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<char>(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);
}

View File

@ -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') {};
};
*/