Arduino/nudelmaschine/main/main.ino

232 lines
5.5 KiB
Arduino
Raw Normal View History

2018-10-28 20:10:19 +01:00
#include <LiquidCrystal_I2C.h>
2019-01-04 12:04:49 +01:00
#include <SoftwareSerial.h>
//temp sensor
#include <OneWire.h>
#include <DallasTemperature.h>
2018-10-28 20:10:19 +01:00
2019-03-21 13:45:44 +01:00
LiquidCrystal_I2C lcd(0x3F, 20, 4); //https://github.com/marcoschwartz/LiquidCrystal_I2C
2019-01-04 12:04:49 +01:00
OneWire oneWire(4); //OneWire Library (with Tim Stud)
DallasTemperature tsensor(&oneWire); //DallasTempratureSensor Library
SoftwareSerial wlan(6, 5); //RX, TX
long lastLCDupdate = millis();
2019-03-21 13:45:44 +01:00
boolean lcdchanged = true;
2019-01-04 12:04:49 +01:00
int temp = 0;
2019-03-21 13:45:44 +01:00
byte cancelcause = 0; //why is the heater off? -> 0= None, 1 = canceled (btn), 2= temp, 3=to long on, 4=tempmovement, 5=abort(inet)
2019-06-17 15:27:09 +02:00
byte state = 0; //current satate -> 0 = idle, 1 = heating, 2 = cooking
2019-01-04 12:04:49 +01:00
long heating = -1;//millis() value when heater started
2019-03-21 13:45:44 +01:00
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
2019-06-17 15:27:09 +02:00
const byte cookingtemp = 98; //desired start temp
2018-10-28 20:10:19 +01:00
void setup() {
pinMode(2, OUTPUT);
pinMode(3, INPUT_PULLUP);
digitalWrite(2, LOW);
2019-03-21 13:45:44 +01:00
lcd.init(); // initialize the lcd
2018-10-28 20:10:19 +01:00
// Print a message to the LCD.
lcd.backlight();
2019-03-21 13:45:44 +01:00
lcd.setCursor(6, 0);
2019-01-04 12:04:49 +01:00
lcd.print("Autokocher");
2019-03-21 13:45:44 +01:00
lcd.setCursor(3, 1);
2019-01-04 12:04:49 +01:00
lcd.print("Yannis Gerlach");
2018-10-28 20:10:19 +01:00
delay(1000);
2019-01-04 12:04:49 +01:00
tsensor.begin();
2019-03-21 21:19:35 +01:00
Serial.begin(115200);//DEBUG
2019-01-04 12:04:49 +01:00
wlan.begin(9600);
2018-10-28 20:10:19 +01:00
updateLCD();
}
2019-01-04 12:04:49 +01:00
int getTemp() {
tsensor.requestTemperatures();
2019-03-21 13:45:44 +01:00
return (tsensor.getTempCByIndex(0) * 100);
2019-01-04 12:04:49 +01:00
}
2018-10-28 20:10:19 +01:00
void updateLCD() {
2019-03-21 13:45:44 +01:00
if (lcdchanged || heating > 0) {
2018-10-28 20:10:19 +01:00
lcd.clear();
lcd.clear();
2019-03-21 13:45:44 +01:00
lcd.backlight();
lcd.setCursor(0, 0);
2019-01-04 12:04:49 +01:00
lcd.print("Herd:");
2019-03-21 13:45:44 +01:00
lcd.setCursor(padd, 0);
2019-01-04 12:04:49 +01:00
lcd.print(heating > 0 ? "An" : "Aus");
2019-03-21 13:45:44 +01:00
lcd.setCursor(0, 1);
2019-01-04 12:04:49 +01:00
lcd.print("Temp:");
2019-03-21 13:45:44 +01:00
lcd.setCursor(padd, 1);
2019-01-04 12:04:49 +01:00
lcd.print(temp);
lcd.print("C");
2019-03-21 13:45:44 +01:00
lcd.setCursor(0, 2);
if (cancelcause != 0) {
lcd.print("Err :");
lcd.setCursor(padd, 2);
2019-01-04 12:04:49 +01:00
switch (cancelcause) {
2019-03-21 13:45:44 +01:00
case 1: lcd.print("Abbr-Btn."); break;
case 2: lcd.print("Tempzuhoch"); break;
2019-01-04 12:04:49 +01:00
case 3: lcd.print("Zeit"); break;
2019-03-21 13:45:44 +01:00
case 4: lcd.print("TempSensor"); break;
case 5: lcd.print("Abbr-INet."); break;
2019-01-04 12:04:49 +01:00
default: lcd.print("undef.");
}
2019-03-21 13:45:44 +01:00
} else if (heating > 0) {
lcd.print("An :");
lcd.setCursor(padd, 2);
lcd.print((millis() - heating) / 1000);
2019-01-04 12:04:49 +01:00
lcd.print("s");
}
2019-03-21 13:45:44 +01:00
lcd.setCursor(0, 3);
2019-01-04 12:04:49 +01:00
lcd.print(status);
lastLCDupdate = millis();
2019-03-21 13:45:44 +01:00
lcdchanged = false;
} else if (millis() - lastLCDupdate > 10000) {
lcd.noBacklight();
}
2019-01-04 12:04:49 +01:00
}
2019-03-21 13:45:44 +01:00
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
2019-06-17 15:27:09 +02:00
wlan.print('.');//delimiter
wlan.print(state);
2019-03-21 13:45:44 +01:00
wlan.print('\0');//EOT
}
//handle the conenction to the esp
2019-01-04 12:04:49 +01:00
void handleWLAN() {
2019-03-21 13:45:44 +01:00
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()) {
2019-01-04 12:04:49 +01:00
char first = wlan.read();
2019-03-21 13:45:44 +01:00
if (first == 's') {
2019-01-04 12:04:49 +01:00
//status update
2019-01-14 16:00:31 +01:00
bool ava = true;
2019-06-16 22:24:15 +02:00
for (unsigned char i = 0; i < 20; i++) { //does somehow not exit this loop???
2019-03-21 13:45:44 +01:00
char old = status[i];
char new_ = ' ';
if (wlan.available() && ava) {
new_ = wlan.read();
2019-03-21 21:19:35 +01:00
Serial.print(new_);
Serial.print(new_, DEC);
Serial.println(new_, HEX);
2019-03-21 13:45:44 +01:00
if (new_ == '\0') {
2019-01-14 16:00:31 +01:00
ava = false;
2019-03-21 13:45:44 +01:00
new_ = ' ';
2019-01-14 16:00:31 +01:00
}
}
2019-03-21 13:45:44 +01:00
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);
2019-01-04 12:04:49 +01:00
}
}
}
2018-10-28 20:10:19 +01:00
}
2019-03-21 13:45:44 +01:00
//security checks
void checkHeater() {
if (temp > 105) { //overtemp
cancelcause = 2;
setheater(false);
}
if (millis() - heating > 600000) { //longer than 10min on
cancelcause = 3;
setheater(false);
}
2019-06-17 15:27:09 +02:00
if (millis() - heating > 60000 && starttemp >= temp - 1) { //on for 60s and temp did not change
2019-03-21 13:45:44 +01:00
cancelcause = 4;
setheater(false);
}
}
2018-10-28 20:10:19 +01:00
bool presed = false;
void loop() {
2019-06-17 15:27:09 +02:00
delay(5);
2019-03-21 13:45:44 +01:00
handleWLAN();
2019-01-04 12:04:49 +01:00
2019-03-21 13:45:44 +01:00
{
int old = temp;
temp = getTemp() / 100;
/* if(old != temp)
2019-06-17 15:27:09 +02:00
lcdchanged = true;*/ //cause random lcd turn on
2019-03-21 13:45:44 +01:00
}
if ((millis() - lastLCDupdate) > 500) { //reprint LCD every 1/2 seconds
2019-01-04 12:04:49 +01:00
updateLCD();
2019-03-21 13:45:44 +01:00
}
//button pressed
if (digitalRead(3) == LOW) {
if (!presed) {
presed = true;
//toggle
if (heating > 0) {
cancelcause = 1;
2018-10-28 20:10:19 +01:00
}
2019-03-21 13:45:44 +01:00
setheater(heating < 0);//toggle heater
updateLCD();
}
} else {
2018-10-28 20:10:19 +01:00
presed = false;
2019-03-21 13:45:44 +01:00
}
if (heating > 0)
checkHeater();
2019-06-17 15:27:09 +02:00
if(temp >= cookingtemp && state == 1) {
//coocking temp reached!
//go !
state = 2;
sendStatus();
//TODO: trigger servo
}
2018-10-28 20:10:19 +01:00
}
2019-01-04 12:04:49 +01:00
void setheater(bool on) {
heating = on ? millis() : -1;
2019-03-21 13:45:44 +01:00
if (on) {
2019-01-04 12:04:49 +01:00
cancelcause = 0;
2019-06-17 15:27:09 +02:00
starttemp = temp;
2019-03-21 13:45:44 +01:00
}
2019-01-04 12:04:49 +01:00
digitalWrite(2, on);
digitalWrite(13, on);
2019-03-21 13:45:44 +01:00
lcdchanged = true;
2019-06-17 15:27:09 +02:00
state = on; // on = true -> state = 1 (heating); on = false -> state = 0 (idle)
sendStatus();
2019-01-04 12:04:49 +01:00
}