nun mit WLAN basics

This commit is contained in:
mrbesen 2019-01-04 12:04:49 +01:00
parent e64628c7c5
commit 965d3248e9
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 229 additions and 12 deletions

133
nudelmaschine/esp/esp.ino Normal file
View File

@ -0,0 +1,133 @@
//Board URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
//Board Name: Node Mcu 1.0
#include "config.h"
#include <SoftwareSerial.h>
#define LED 2
#define DEBUG 1
#if DEBUG > 0
#define D(A) Serial.println(A);
#else
#define D(A) ;
#endif
#define LEDON digitalWrite(LED, LOW);
#define LEDOFF digitalWrite(LED, HIGH);
#define STATUS_WLAN 1
#define STATUS_SERVICE 2
#define PINGREQUIRED 180 //alle 3 Minuten Ping requesten
#define PINGTIMEOUT 200 //nach weiteren 20 sekunden sollte ein ping erfolgt sein
SoftwareSerial ser(14, 12);//RX: GPIO 14 = D5; TX: GPIO 12 = D6
WiFiClient client;
unsigned char statusbits = 0; //bit 0 = wlan, bit 1 = service;
unsigned long lastping = 0;
void setup() {
pinMode(LED, OUTPUT);
ser.begin(9600);
Serial.begin(115200);
wifi_station_set_hostname("NudelMaschine");
// while (WiFi.status() != WL_CONNECTED) {
// delay(500);
// Serial.print(".");
// }
// D("IP address: ");
// D(WiFi.localIP());
}
void reportStatus() {
ser.print("s");
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) {
bool old = statusbits & mask;
if(old != newstat) {
//only when its realy a new state
//flip
statusbits ^= mask;
//update
reportStatus();
}
}
void loop() {
//logic
//TODO:
//wait for data: when nothing recived for 3min -> send ping shoud respond within 2 seconds if not -> notify mashine and go in idle mode
//when recived command for mashine -> fwd to mashine (start, stop, emergency stop, reset errors, ....)
//when garbage recived -> notify mashine to display error
//when connection failed -> notify mashine to display error
//when ping recived -> respond fast!
//when WIFI connection lost -> try to reconnect & notify mashine
//when WIFI connection is reestablished -> notify mashine and continue
//when mashine sends error -> fwd to server
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);
if(client.available()) {//never enters this state, why?
Serial.println("recive stuff");
lastping = millis();
unsigned char ch = static_cast<unsigned char>(client.read()); // read the size
char* buff = new char[ch];
for(unsigned char i = 0; i < ch; i++) {
if(client.available()) {
buff[i] = static_cast<char>(client.read());
} else {
buff[i] = 0;
break;
}
}
//done reciving processing
Serial.print("recived: ");
Serial.println(buff);
//handle ?
}
if( (millis() - lastping) > PINGREQUIRED) {
//request ping
client.print("ping");
} else if((millis() - lastping) > PINGTIMEOUT) {
//timeout
client.stop();
}
} else {
//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 {
Serial.println("connected");
client.println("hello");
}
}
} 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());
}
}

View File

@ -1,6 +1,20 @@
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
//temp sensor
#include <OneWire.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0x3F,20,4);
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();
int temp = 0;
byte cancelcause = 0; //why is the heater off? -> 0= None, 1 = canceled, 2= temp, 3=to long on
long heating = -1;//millis() value when heater started
char* status = "--------------------";
void setup() {
pinMode(2, OUTPUT);
@ -12,39 +26,109 @@ void setup() {
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(6,0);
lcd.print("Auto Cooker");
lcd.setCursor(1,1);
lcd.print("By Yannis Gerlach");
lcd.print("Autokocher");
lcd.setCursor(3,1);
lcd.print("Yannis Gerlach");
delay(1000);
tsensor.begin();
wlan.begin(9600);
updateLCD();
}
bool heating = false;
int getTemp() {
tsensor.requestTemperatures();
return (tsensor.getTempCByIndex(0)*100);
}
void updateLCD() {
lcd.clear();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Heater:");
lcd.print("Herd:");
lcd.setCursor(15,0);
lcd.print(heating ? "ON" : "OFF");
lcd.print(heating > 0 ? "An" : "Aus");
lcd.setCursor(0,1);
lcd.print("Temp:");
lcd.setCursor(15,1);
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0,2);
if(cancelcause != 0) {
lcd.print("Abbruch Grund:");
lcd.setCursor(15,2);
switch (cancelcause) {
case 1: lcd.print("Abbr."); break;
case 2: lcd.print("Temp"); break;
case 3: lcd.print("Zeit"); break;
default: lcd.print("undef.");
}
} else if(heating > 0) {
lcd.print("An seit:");
lcd.setCursor(15,2);
lcd.print((millis()-heating)/1000);
lcd.print("s");
}
lcd.setCursor(0,3);
lcd.print(status);
lastLCDupdate = millis();
}
void handleWLAN() {
if(wlan.available()) {
char first = wlan.read();
if(first == 's') {
//status update
for(unsigned char i = 0; i < 20; i++) {
status[i] = wlan.available() ? wlan.read() : ' ';
}
}
}
}
bool presed = false;
void loop() {
delay(25);
handleWLAN();
temp = getTemp()/100;
if((millis() - lastLCDupdate) > 500) {//reprint LCD every 2 seconds
updateLCD();
}
if(digitalRead(3) == LOW) {
if(!presed) {
presed = true;
//toggle
heating = !heating;
digitalWrite(2, heating);
digitalWrite(13, heating);
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;
setheater(false);
}
}
void setheater(bool on) {
heating = on ? millis() : -1;
if(on)
cancelcause = 0;
digitalWrite(2, on);
digitalWrite(13, on);
}