first connection
This commit is contained in:
parent
5dbc3eb983
commit
df55e6e499
|
@ -1,7 +1,13 @@
|
||||||
//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
|
//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
|
||||||
//Board Name: Node Mcu 1.0
|
//Board Name: Node Mcu 1.0
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
//protocol definition:
|
||||||
|
//1st byte: length of content +1
|
||||||
|
//2nd byte: packet type
|
||||||
|
//rest: content
|
||||||
|
|
||||||
#include <SoftwareSerial.h>
|
#include <SoftwareSerial.h>
|
||||||
|
|
||||||
#define LED 2
|
#define LED 2
|
||||||
|
@ -36,10 +42,6 @@ void reportStatus() {
|
||||||
ser.print(statusbits & STATUS_WLAN ? "W" : "w");
|
ser.print(statusbits & STATUS_WLAN ? "W" : "w");
|
||||||
ser.print(statusbits & STATUS_SERVICE ? "S" : "s");
|
ser.print(statusbits & STATUS_SERVICE ? "S" : "s");
|
||||||
ser.print("\0");//EOT
|
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) {
|
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) {
|
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() {
|
void loop() {
|
||||||
|
@ -82,29 +107,30 @@ void loop() {
|
||||||
Serial.println("Data available");
|
Serial.println("Data available");
|
||||||
|
|
||||||
if(client.available()) {//never enters this state, why?
|
if(client.available()) {//never enters this state, why?
|
||||||
Serial.println("recive stuff");
|
Serial.print("\nrecive stuff ");
|
||||||
lastping = millis();//reset lastping
|
lastping = millis();//reset lastping
|
||||||
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
|
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
|
||||||
Serial.print("awaiting: ");
|
//Serial.print("awaiting: ");
|
||||||
Serial.println(ch, DEC);
|
Serial.println(ch, DEC);
|
||||||
char* buff = new char[ch];
|
char* buff = new char[ch];
|
||||||
for(unsigned char i = 0; i < ch; i++) {
|
for(unsigned char i = 0; i < ch; i++) {
|
||||||
if(client.available()) {
|
if(client.available()) {
|
||||||
buff[i] = static_cast<char>(client.read());
|
buff[i] = static_cast<char>(client.read());
|
||||||
Serial.print(buff[i], HEX);
|
//Serial.print(buff[i], HEX);
|
||||||
} else {
|
} else {
|
||||||
buff[i] = 0;
|
buff[i] = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//done reciving processing
|
//done reciving processing
|
||||||
Serial.print("\nrecived: ");
|
//Serial.print("\nrecived: ");
|
||||||
Serial.println(buff);
|
//Serial.println(buff);
|
||||||
handleData(buff, ch);
|
handleData(buff, ch);
|
||||||
|
client.flush();
|
||||||
}
|
}
|
||||||
if( (millis() - lastping)/1000 > PINGREQUIRED) {
|
if( (millis() - lastping)/1000 > PINGREQUIRED) {
|
||||||
//request ping
|
//request ping
|
||||||
client.print("\x01p");
|
client.println("\x01p");
|
||||||
Serial.println("ping sent");
|
Serial.println("ping sent");
|
||||||
} else if((millis() - lastping)/1000 > PINGTIMEOUT) {
|
} else if((millis() - lastping)/1000 > PINGTIMEOUT) {
|
||||||
//timeout
|
//timeout
|
||||||
|
@ -138,4 +164,5 @@ void loop() {
|
||||||
D("IP address: ");
|
D("IP address: ");
|
||||||
D(WiFi.localIP());
|
D(WiFi.localIP());
|
||||||
}
|
}
|
||||||
|
delay(100);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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') {};
|
||||||
|
};
|
||||||
|
*/
|
Loading…
Reference in New Issue