aded nobacklight, more security checks

This commit is contained in:
mrbesen 2019-03-21 13:45:44 +01:00
parent df55e6e499
commit 03b66fc9ac
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 234 additions and 131 deletions

View File

@ -38,15 +38,15 @@ void setup() {
}
void reportStatus() {
ser.print("s");
ser.print(statusbits & STATUS_WLAN ? "W" : "w");
ser.print(statusbits & STATUS_SERVICE ? "S" : "s");
ser.print("\0");//EOT
ser.print("s");
ser.print(statusbits & STATUS_WLAN ? "W" : "w");
ser.print(statusbits & STATUS_SERVICE ? "S" : "s");
ser.print("\0");//EOT
}
void setStatus(unsigned char mask, bool newstat) {
bool old = statusbits & mask;
if(old != newstat) {
if (old != newstat) {
//only when its realy a new state
//flip
statusbits ^= mask;
@ -55,33 +55,87 @@ void setStatus(unsigned char mask, bool newstat) {
}
}
//manage the connection to the arduino
void handleSerial() {
if (ser.available()) {
char in = ser.read();
if (in == 's') {
reportStatus();
} else if(in == 'r') {//report
} else if(in == 'a') {
}
}
}
void handleData(const char* data, size_t length) {
Serial.println("handle");
char type = data[0];
switch(type) {
switch (type) {
case 'c': //close connection
client.stop();
break;
break;
case 'p': //pingrequest
client.println("\x01P");
Serial.println("awnsered");
break;
break;
case 'h': //hello
D("Hello recived")
ser.print('r');//get status
break;
case 's': //start
ser.print('g');//go
break;
break;
case 'g': //go (start)
case 'r'://request report
case 'e'://error report
case 'a'://abort
ser.print(type);
break;
break;
default: Serial.print("unknown Data"); Serial.println(data); break;
}
}
void connectWLAN() {
setStatus(STATUS_WLAN, false);
setStatus(STATUS_SERVICE, false);
//try to connect to wifi
LEDOFF
Serial.println("Connect to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet, gateway);
while (WiFi.status() != WL_CONNECTED) delay(500);
D("IP address: ");
D(WiFi.localIP());
}
void connectService() {
//try to reconnect to server
setStatus(STATUS_SERVICE, false);
Serial.println("connect to server");
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
} else {
client.setNoDelay(true);
Serial.println("connected");
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");
Serial.println("ping sent");
} else if ((millis() - lastping) / 1000 > PINGTIMEOUT) {
//timeout
Serial.println("timedout");
client.stop();
}
}
void loop() {
//logic
//TODO:
@ -94,75 +148,49 @@ void loop() {
//when WIFI connection is reestablished -> notify mashine and continue
//when mashine sends error -> fwd to server
handleSerial();
if (WiFi.status() == WL_CONNECTED) {
if (WiFi.status() == WL_CONNECTED) {
setStatus(STATUS_WLAN, true);
LEDON
if (client.connected()) {
setStatus(STATUS_SERVICE, true);
//do reciving stuff
while(!client.available())
delay(1000);
//do reciving stuff
while (!client.available() && client.connected()) {
delay(100);
handleSerial();
}
Serial.println("Data available");
if(client.available()) {//never enters this state, why?
Serial.print("\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);
char* buff = new char[ch];
for(unsigned char i = 0; i < ch; i++) {
if(client.available()) {
if (client.available()) {
Serial.print("\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);
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);
} else {
} else {
buff[i] = 0;
break;
}
}
//done reciving processing
//Serial.print("\nrecived: ");
//Serial.println(buff);
handleData(buff, ch);
client.flush();
}
if( (millis() - lastping)/1000 > PINGREQUIRED) {
//request ping
client.println("\x01p");
Serial.println("ping sent");
} else if((millis() - lastping)/1000 > PINGTIMEOUT) {
//timeout
Serial.println("timedout");
client.stop();
}
}
//done reciving processing
//Serial.print("\nrecived: ");
//Serial.println(buff);
handleData(buff, ch);
client.flush();
}
checkTimeout();
} else {//client.connected()
//try to reconnect to server
setStatus(STATUS_SERVICE, false);
Serial.println("connect to server");
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
} else {
client.setNoDelay(true);
Serial.println("connected");
client.println("\x01h");
client.setTimeout(60*5000);//5min
}
connectService();
}
} else {
setStatus(STATUS_WLAN, false);
//try to connect to wifi
LEDOFF
Serial.println("Connect to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip,gateway,subnet,gateway);
while(WiFi.status() != WL_CONNECTED) delay(500);
D("IP address: ");
D(WiFi.localIP());
}
delay(100);
} else {
connectWLAN();
}
delay(100);
}

View File

@ -4,17 +4,20 @@
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0x3F,20,4); //https://github.com/marcoschwartz/LiquidCrystal_I2C
LiquidCrystal_I2C lcd(0x3F, 20, 4); //https://github.com/marcoschwartz/LiquidCrystal_I2C
OneWire oneWire(4); //OneWire Library (with Tim Stud)
DallasTemperature tsensor(&oneWire); //DallasTempratureSensor Library
SoftwareSerial wlan(6, 5); //RX, TX
long lastLCDupdate = millis();
boolean lcdchanged = true;
int temp = 0;
byte cancelcause = 0; //why is the heater off? -> 0= None, 1 = canceled, 2= temp, 3=to long on
byte cancelcause = 0; //why is the heater off? -> 0= None, 1 = canceled (btn), 2= temp, 3=to long on, 4=tempmovement, 5=abort(inet)
long heating = -1;//millis() value when heater started
char* status = "--------------------";
int starttemp = 0;
char status[21] = "--------------------";//dont forget \0 at the end
const byte padd = 6;//used in updateLCD(), used to determine how far left the right part of the screen should be
void setup() {
pinMode(2, OUTPUT);
@ -22,18 +25,18 @@ void setup() {
digitalWrite(2, LOW);
lcd.init(); // initialize the lcd
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(6,0);
lcd.setCursor(6, 0);
lcd.print("Autokocher");
lcd.setCursor(3,1);
lcd.setCursor(3, 1);
lcd.print("Yannis Gerlach");
delay(1000);
tsensor.begin();
//Serial.begin(115200);//DEBUG
wlan.begin(9600);
updateLCD();
@ -41,101 +44,173 @@ void setup() {
int getTemp() {
tsensor.requestTemperatures();
return (tsensor.getTempCByIndex(0)*100);
return (tsensor.getTempCByIndex(0) * 100);
}
void updateLCD() {
if (lcdchanged || heating > 0) {
lcd.clear();
lcd.clear();
lcd.setCursor(0,0);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Herd:");
lcd.setCursor(15,0);
lcd.setCursor(padd, 0);
lcd.print(heating > 0 ? "An" : "Aus");
lcd.setCursor(0,1);
lcd.setCursor(0, 1);
lcd.print("Temp:");
lcd.setCursor(15,1);
lcd.setCursor(padd, 1);
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0,2);
if(cancelcause != 0) {
lcd.print("Abbruch Grund:");
lcd.setCursor(15,2);
lcd.setCursor(0, 2);
if (cancelcause != 0) {
lcd.print("Err :");
lcd.setCursor(padd, 2);
switch (cancelcause) {
case 1: lcd.print("Abbr."); break;
case 2: lcd.print("Temp"); break;
case 1: lcd.print("Abbr-Btn."); break;
case 2: lcd.print("Tempzuhoch"); break;
case 3: lcd.print("Zeit"); break;
case 4: lcd.print("TempSensor"); break;
case 5: lcd.print("Abbr-INet."); break;
default: lcd.print("undef.");
}
} else if(heating > 0) {
lcd.print("An seit:");
lcd.setCursor(15,2);
lcd.print((millis()-heating)/1000);
} else if (heating > 0) {
lcd.print("An :");
lcd.setCursor(padd, 2);
lcd.print((millis() - heating) / 1000);
lcd.print("s");
}
lcd.setCursor(0,3);
lcd.setCursor(0, 3);
lcd.print(status);
lastLCDupdate = millis();
lcdchanged = false;
} else if (millis() - lastLCDupdate > 10000) {
lcd.noBacklight();
}
}
void sendStatus() {
wlan.print('r');//msg type
wlan.print(heating);//heater status
wlan.print('.');//delimiter
wlan.print(cancelcause);//error?
wlan.print('.');//delimiter
wlan.print(temp);//temp
wlan.print('\0');//EOT
}
//handle the conenction to the esp
void handleWLAN() {
if(wlan.available()) {
if (status[0] == '-' || status[0] == ' ') { //TODO: this line does not work as intended???
/*Serial.print("requeststatus: ");
Serial.print(status[0]);
Serial.print(status[1]);
Serial.print(status[2]);
Serial.print(" ");
Serial.println(status);*/
wlan.write('s');
wlan.flush();
}
if (wlan.available()) {
char first = wlan.read();
if(first == 's') {
if (first == 's') {
//status update
bool ava = true;
for(unsigned char i = 0; i < 20; i++) {
status[i] = ' ';
if(wlan.available() && ava) {
status[i] = wlan.read();
if(status[i] == '\0') {
for (unsigned char i = 0; i < 20; i++) {
char old = status[i];
char new_ = ' ';
if (wlan.available() && ava) {
new_ = wlan.read();
if (new_ == '\0') {
ava = false;
status[i] = ' ';
new_ = ' ';
}
}
if (old != new_) {
lcdchanged = true;
status[i] = new_;
}
}
//status[2] = (rand()%10)+'a';//DEBUG
} else if (first == 'r') { //report
sendStatus();
} else if (first == 'a') { //abort!
setheater(false);
wlan.print('a');
} else if (first == 'g') { //go
if (heating < 0) { //heater off?
setheater(true);
}
}
}
}
bool presed = false;
void loop() {
delay(25);
//security checks
void checkHeater() {
if (temp > 105) { //overtemp
cancelcause = 2;
setheater(false);
}
handleWLAN();
temp = getTemp()/100;
if((millis() - lastLCDupdate) > 500) {//reprint LCD every 2 seconds
updateLCD();
}
if(digitalRead(3) == LOW) {
if(!presed) {
presed = true;
//toggle
if(heating > 0) {
cancelcause = 1;
}
setheater(heating < 0);
updateLCD();
}
} else {
presed = false;
}
if(heating > 0 && temp > 97) {//stop
setheater(false);
cancelcause = 2;
}
if(heating > 0 && millis() - heating > 600000) {//longer on tha 10min
if (millis() - heating > 600000) { //longer than 10min on
cancelcause = 3;
setheater(false);
}
}
if (millis() - heating > 60000 && starttemp >= temp - 1) { //on for 60s and no temp did not change
cancelcause = 4;
setheater(false);
}
}
bool presed = false;
void loop() {
delay(25);
handleWLAN();
{
int old = temp;
temp = getTemp() / 100;
/* if(old != temp)
lcdchanged = true;*/
}
if ((millis() - lastLCDupdate) > 500) { //reprint LCD every 1/2 seconds
updateLCD();
}
//button pressed
if (digitalRead(3) == LOW) {
if (!presed) {
presed = true;
//toggle
if (heating > 0) {
cancelcause = 1;
}
setheater(heating < 0);//toggle heater
updateLCD();
}
} else {
presed = false;
}
if (heating > 0)
checkHeater();
}
void setheater(bool on) {
heating = on ? millis() : -1;
if(on)
if (on) {
cancelcause = 0;
starttemp = temp;
}
digitalWrite(2, on);
digitalWrite(13, on);
lcdchanged = true;
}