small changes

This commit is contained in:
mrbesen 2019-06-16 22:24:15 +02:00
parent 03b66fc9ac
commit ea5b999b41
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
3 changed files with 24 additions and 12 deletions

View File

@ -33,7 +33,9 @@ unsigned long lastping = 0;
void setup() {
pinMode(LED, OUTPUT);
ser.begin(9600);
#if DEBUG > 0
Serial.begin(115200);
#endif
wifi_station_set_hostname("NudelMaschine");
}
@ -62,7 +64,14 @@ void handleSerial() {
if (in == 's') {
reportStatus();
} else if(in == 'r') {//report
char* buffer = new char[64];
size_t i = 0;
for(; i < 64 && ser.available(); i++) {
buffer[i] = ser.read();
}
client.write(i+1);
client.print('r');
client.write(buffer, i);
} else if(in == 'a') {
}
@ -70,7 +79,7 @@ void handleSerial() {
}
void handleData(const char* data, size_t length) {
Serial.println("handle");
D("handle");
char type = data[0];
switch (type) {
case 'c': //close connection
@ -99,11 +108,13 @@ void connectWLAN() {
setStatus(STATUS_SERVICE, false);
//try to connect to wifi
LEDOFF
Serial.println("Connect to WiFi");
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());
@ -112,13 +123,13 @@ void connectWLAN() {
void connectService() {
//try to reconnect to server
setStatus(STATUS_SERVICE, false);
Serial.println("connect to server");
D("connect to server");
if (!client.connect(host, port)) {
Serial.println("connection failed");
D("connection failed")
delay(5000);
} else {
client.setNoDelay(true);
Serial.println("connected");
D("connected")
client.println("\x01h");
client.setTimeout(60 * 5000); //5min
}
@ -128,10 +139,10 @@ void checkTimeout() {
if ( (millis() - lastping) / 1000 > PINGREQUIRED) { //TODO: check if ping allready sent
//request ping
client.println("\x01p");
Serial.println("ping sent");
D("ping sent")
} else if ((millis() - lastping) / 1000 > PINGTIMEOUT) {
//timeout
Serial.println("timedout");
D("timedout")
client.stop();
}
}
@ -161,14 +172,14 @@ void loop() {
handleSerial();
}
Serial.println("Data available");
D("Data available")
if (client.available()) {
Serial.print("\nrecive stuff ");
D("\nrecive stuff ");
lastping = millis();//reset lastping
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
//Serial.print("awaiting: ");
Serial.println(ch, DEC);
D(ch);
char* buff = new char[ch];
for (unsigned char i = 0; i < ch; i++) {
if (client.available()) {

View File

@ -117,7 +117,7 @@ void handleWLAN() {
if (first == 's') {
//status update
bool ava = true;
for (unsigned char i = 0; i < 20; i++) {
for (unsigned char i = 0; i < 20; i++) { //does somehow not exit this loop???
char old = status[i];
char new_ = ' ';
if (wlan.available() && ava) {

View File

@ -22,3 +22,4 @@ struct Data {
Data(byte t) : type(t), data('\0') {};
};
*/