aded nobacklight, more security checks
This commit is contained in:
parent
df55e6e499
commit
03b66fc9ac
|
@ -38,15 +38,15 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void reportStatus() {
|
void reportStatus() {
|
||||||
ser.print("s");
|
ser.print("s");
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
void setStatus(unsigned char mask, bool newstat) {
|
void setStatus(unsigned char mask, bool newstat) {
|
||||||
bool old = statusbits & mask;
|
bool old = statusbits & mask;
|
||||||
if(old != newstat) {
|
if (old != newstat) {
|
||||||
//only when its realy a new state
|
//only when its realy a new state
|
||||||
//flip
|
//flip
|
||||||
statusbits ^= mask;
|
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) {
|
void handleData(const char* data, size_t length) {
|
||||||
Serial.println("handle");
|
Serial.println("handle");
|
||||||
char type = data[0];
|
char type = data[0];
|
||||||
switch(type) {
|
switch (type) {
|
||||||
case 'c': //close connection
|
case 'c': //close connection
|
||||||
client.stop();
|
client.stop();
|
||||||
break;
|
break;
|
||||||
case 'p': //pingrequest
|
case 'p': //pingrequest
|
||||||
client.println("\x01P");
|
client.println("\x01P");
|
||||||
Serial.println("awnsered");
|
Serial.println("awnsered");
|
||||||
break;
|
break;
|
||||||
case 'h': //hello
|
case 'h': //hello
|
||||||
D("Hello recived")
|
D("Hello recived")
|
||||||
ser.print('r');//get status
|
ser.print('r');//get status
|
||||||
break;
|
break;
|
||||||
case 's': //start
|
case 'g': //go (start)
|
||||||
ser.print('g');//go
|
|
||||||
break;
|
|
||||||
case 'r'://request report
|
case 'r'://request report
|
||||||
case 'e'://error report
|
case 'e'://error report
|
||||||
case 'a'://abort
|
case 'a'://abort
|
||||||
ser.print(type);
|
ser.print(type);
|
||||||
break;
|
break;
|
||||||
default: Serial.print("unknown Data"); Serial.println(data); 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() {
|
void loop() {
|
||||||
//logic
|
//logic
|
||||||
//TODO:
|
//TODO:
|
||||||
|
@ -94,75 +148,49 @@ void loop() {
|
||||||
//when WIFI connection is reestablished -> notify mashine and continue
|
//when WIFI connection is reestablished -> notify mashine and continue
|
||||||
//when mashine sends error -> fwd to server
|
//when mashine sends error -> fwd to server
|
||||||
|
|
||||||
|
handleSerial();
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
setStatus(STATUS_WLAN, true);
|
setStatus(STATUS_WLAN, true);
|
||||||
LEDON
|
LEDON
|
||||||
if (client.connected()) {
|
if (client.connected()) {
|
||||||
setStatus(STATUS_SERVICE, true);
|
setStatus(STATUS_SERVICE, true);
|
||||||
//do reciving stuff
|
//do reciving stuff
|
||||||
while(!client.available())
|
while (!client.available() && client.connected()) {
|
||||||
delay(1000);
|
delay(100);
|
||||||
|
handleSerial();
|
||||||
|
}
|
||||||
|
|
||||||
Serial.println("Data available");
|
Serial.println("Data available");
|
||||||
|
|
||||||
if(client.available()) {//never enters this state, why?
|
if (client.available()) {
|
||||||
Serial.print("\nrecive 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();
|
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();
|
|
||||||
}
|
}
|
||||||
|
checkTimeout();
|
||||||
} else {//client.connected()
|
} else {//client.connected()
|
||||||
//try to reconnect to server
|
connectService();
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
setStatus(STATUS_WLAN, false);
|
connectWLAN();
|
||||||
//try to connect to wifi
|
}
|
||||||
LEDOFF
|
delay(100);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,20 @@
|
||||||
#include <OneWire.h>
|
#include <OneWire.h>
|
||||||
#include <DallasTemperature.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)
|
OneWire oneWire(4); //OneWire Library (with Tim Stud)
|
||||||
DallasTemperature tsensor(&oneWire); //DallasTempratureSensor Library
|
DallasTemperature tsensor(&oneWire); //DallasTempratureSensor Library
|
||||||
SoftwareSerial wlan(6, 5); //RX, TX
|
SoftwareSerial wlan(6, 5); //RX, TX
|
||||||
|
|
||||||
long lastLCDupdate = millis();
|
long lastLCDupdate = millis();
|
||||||
|
boolean lcdchanged = true;
|
||||||
int temp = 0;
|
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
|
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() {
|
void setup() {
|
||||||
pinMode(2, OUTPUT);
|
pinMode(2, OUTPUT);
|
||||||
|
@ -22,18 +25,18 @@ void setup() {
|
||||||
|
|
||||||
digitalWrite(2, LOW);
|
digitalWrite(2, LOW);
|
||||||
|
|
||||||
lcd.init(); // initialize the lcd
|
lcd.init(); // initialize the lcd
|
||||||
// Print a message to the LCD.
|
// Print a message to the LCD.
|
||||||
lcd.backlight();
|
lcd.backlight();
|
||||||
lcd.setCursor(6,0);
|
lcd.setCursor(6, 0);
|
||||||
lcd.print("Autokocher");
|
lcd.print("Autokocher");
|
||||||
lcd.setCursor(3,1);
|
lcd.setCursor(3, 1);
|
||||||
lcd.print("Yannis Gerlach");
|
lcd.print("Yannis Gerlach");
|
||||||
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
tsensor.begin();
|
tsensor.begin();
|
||||||
|
|
||||||
|
//Serial.begin(115200);//DEBUG
|
||||||
wlan.begin(9600);
|
wlan.begin(9600);
|
||||||
|
|
||||||
updateLCD();
|
updateLCD();
|
||||||
|
@ -41,101 +44,173 @@ void setup() {
|
||||||
|
|
||||||
int getTemp() {
|
int getTemp() {
|
||||||
tsensor.requestTemperatures();
|
tsensor.requestTemperatures();
|
||||||
return (tsensor.getTempCByIndex(0)*100);
|
return (tsensor.getTempCByIndex(0) * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateLCD() {
|
void updateLCD() {
|
||||||
|
if (lcdchanged || heating > 0) {
|
||||||
lcd.clear();
|
lcd.clear();
|
||||||
lcd.clear();
|
lcd.clear();
|
||||||
lcd.setCursor(0,0);
|
lcd.backlight();
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
lcd.print("Herd:");
|
lcd.print("Herd:");
|
||||||
lcd.setCursor(15,0);
|
lcd.setCursor(padd, 0);
|
||||||
lcd.print(heating > 0 ? "An" : "Aus");
|
lcd.print(heating > 0 ? "An" : "Aus");
|
||||||
lcd.setCursor(0,1);
|
lcd.setCursor(0, 1);
|
||||||
lcd.print("Temp:");
|
lcd.print("Temp:");
|
||||||
lcd.setCursor(15,1);
|
lcd.setCursor(padd, 1);
|
||||||
lcd.print(temp);
|
lcd.print(temp);
|
||||||
lcd.print("C");
|
lcd.print("C");
|
||||||
lcd.setCursor(0,2);
|
lcd.setCursor(0, 2);
|
||||||
if(cancelcause != 0) {
|
if (cancelcause != 0) {
|
||||||
lcd.print("Abbruch Grund:");
|
lcd.print("Err :");
|
||||||
lcd.setCursor(15,2);
|
lcd.setCursor(padd, 2);
|
||||||
switch (cancelcause) {
|
switch (cancelcause) {
|
||||||
case 1: lcd.print("Abbr."); break;
|
case 1: lcd.print("Abbr-Btn."); break;
|
||||||
case 2: lcd.print("Temp"); break;
|
case 2: lcd.print("Tempzuhoch"); break;
|
||||||
case 3: lcd.print("Zeit"); break;
|
case 3: lcd.print("Zeit"); break;
|
||||||
|
case 4: lcd.print("TempSensor"); break;
|
||||||
|
case 5: lcd.print("Abbr-INet."); break;
|
||||||
default: lcd.print("undef.");
|
default: lcd.print("undef.");
|
||||||
}
|
}
|
||||||
} else if(heating > 0) {
|
} else if (heating > 0) {
|
||||||
lcd.print("An seit:");
|
lcd.print("An :");
|
||||||
lcd.setCursor(15,2);
|
lcd.setCursor(padd, 2);
|
||||||
lcd.print((millis()-heating)/1000);
|
lcd.print((millis() - heating) / 1000);
|
||||||
lcd.print("s");
|
lcd.print("s");
|
||||||
}
|
}
|
||||||
|
|
||||||
lcd.setCursor(0,3);
|
lcd.setCursor(0, 3);
|
||||||
lcd.print(status);
|
lcd.print(status);
|
||||||
lastLCDupdate = millis();
|
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() {
|
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();
|
char first = wlan.read();
|
||||||
if(first == 's') {
|
if (first == 's') {
|
||||||
//status update
|
//status update
|
||||||
bool ava = true;
|
bool ava = true;
|
||||||
for(unsigned char i = 0; i < 20; i++) {
|
for (unsigned char i = 0; i < 20; i++) {
|
||||||
status[i] = ' ';
|
char old = status[i];
|
||||||
if(wlan.available() && ava) {
|
char new_ = ' ';
|
||||||
status[i] = wlan.read();
|
if (wlan.available() && ava) {
|
||||||
if(status[i] == '\0') {
|
new_ = wlan.read();
|
||||||
|
if (new_ == '\0') {
|
||||||
ava = false;
|
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;
|
//security checks
|
||||||
void loop() {
|
void checkHeater() {
|
||||||
delay(25);
|
if (temp > 105) { //overtemp
|
||||||
|
cancelcause = 2;
|
||||||
|
setheater(false);
|
||||||
|
}
|
||||||
|
|
||||||
handleWLAN();
|
if (millis() - heating > 600000) { //longer than 10min on
|
||||||
|
|
||||||
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
|
|
||||||
cancelcause = 3;
|
cancelcause = 3;
|
||||||
setheater(false);
|
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) {
|
void setheater(bool on) {
|
||||||
heating = on ? millis() : -1;
|
heating = on ? millis() : -1;
|
||||||
if(on)
|
if (on) {
|
||||||
cancelcause = 0;
|
cancelcause = 0;
|
||||||
|
starttemp = temp;
|
||||||
|
}
|
||||||
digitalWrite(2, on);
|
digitalWrite(2, on);
|
||||||
digitalWrite(13, on);
|
digitalWrite(13, on);
|
||||||
|
lcdchanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue