bugfixes, moved to http instead of mqtt

This commit is contained in:
l0sted 2018-10-15 00:00:51 +03:00
parent 56d7018812
commit fe936941f1
2 changed files with 68 additions and 200 deletions

View File

@ -1,7 +1,7 @@
{ {
"board": "esp8266:esp8266:generic", "output": "Output",
"configuration": "CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=2M,ResetMethod=ck,Debug=Disabled,DebugLevel=None____",
"port": "/dev/ttyUSB0", "port": "/dev/ttyUSB0",
"sketch": "frontend.ino", "board": "esp8266:esp8266:generic",
"output": "Output" "configuration": "CpuFrequency=80,VTable=flash,ResetMethod=ck,CrystalFreq=26,FlashFreq=40,FlashMode=qio,FlashSize=4M2M,led=2,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,FlashErase=none,UploadSpeed=921600",
"sketch": "frontend.ino"
} }

View File

@ -1,70 +1,20 @@
//TODO
//AT LEAST MAKE IT WORK (show ui and get data + ntp) - compiling - ... - WORKS!!11
//debug -> release
//remove useless serial debug - DONE
//show wifi info on oled - DONE
//reset on not connected - DONE
//check connectivity during work and do actions - DONE
//beep hourly - DONE
//github - oh yeahhh
//load config
//webconfig (?) - in progress
//mqtt update interval
//second screen
//reconfig wifi if not found (start webserver and AP)
//GRAPH
/*
get json from server
~12 values
hourly values + hourly display
if pressure lowers then weather gonna be bad
if pressure uppers then weather gonna be good
*/
//CONFIG
/*
beep time
on time
off time
mqtt update interval
*/
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
// #include <ESP8266WiFiGeneric.h>
#include <WiFiClient.h> #include <WiFiClient.h>
// #include <ESP8266WebServer.h> #include <ESP8266mDNS.h>
// #include <ESP8266mDNS.h>
#include <NTPClient.h> #include <NTPClient.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include "SSD1306Brzo.h" #include "SSD1306Brzo.h"
// #include <ArduinoJson.h>
// #include <FS.h>
#include "wifi.h" #include "wifi.h"
#include "Adafruit_MQTT.h" #include <ESP8266HTTPClient.h>
#include "Adafruit_MQTT_Client.h"
double inTemp,outTemp,pressure,humid; String inTemp,outTemp,pressure,humid;
unsigned short int lastNtp, lastBeep, onTime=6, offTime=23, beepDelay=60; const unsigned short int onTime=6, offTime=23, beepDelay=60, timeOffset=3;
bool mqttAvail; short int lastBeep;
const String httpAddr="http://192.168.100.101:1880";
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP); NTPClient timeClient(ntpUDP);
SSD1306Brzo display(0x3C, 4, 5); //oled display w/ address 0x3C with SDA on GPIO4 and SCL on GPIO5 //address == offset SSD1306Brzo display(0x3C, 5, 4); //oled display w/ address 0x3C with SDA on GPIO4 and SCL on GPIO5 //address == offset
WiFiClient client;
// ESP8266WebServer server(80);
Adafruit_MQTT_Client mqtt(&client, "192.168.100.100", 1883);
Adafruit_MQTT_Subscribe pressureFeed = Adafruit_MQTT_Subscribe(&mqtt, "pressure");
Adafruit_MQTT_Subscribe inFeed = Adafruit_MQTT_Subscribe(&mqtt, "bmpTemp");
Adafruit_MQTT_Subscribe outFeed = Adafruit_MQTT_Subscribe(&mqtt, "externalTemp");
Adafruit_MQTT_Subscribe humidFeed = Adafruit_MQTT_Subscribe(&mqtt, "humid");
void wifiConnect() { void wifiConnect() {
int beginMillis = millis(); int beginMillis = millis();
@ -79,7 +29,6 @@ void wifiConnect() {
delay(10000); delay(10000);
ESP.reset(); ESP.reset();
} else { } else {
// MDNS.begin("esp8266-frontend");
Serial.print("Connected to " + String(ssid) + "; IP address: "); Serial.print("Connected to " + String(ssid) + "; IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
displayStatus(0); displayStatus(0);
@ -93,29 +42,17 @@ void setup(){
display.setContrast(255); display.setContrast(255);
//==WIFI CONNECT== //==WIFI CONNECT==
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
wifiConnect(); // wifiConnect();
MDNS.begin("esp8266-frontend");
//==NTP INIT== //==NTP INIT==
timeClient.begin(); timeClient.begin();
timeClient.setTimeOffset(10800); timeClient.setTimeOffset(timeOffset*3600);
//MQTT
pressureFeed.setCallback(pressureCall);
inFeed.setCallback(inCall);
outFeed.setCallback(outCall);
humidFeed.setCallback(humidCall);
mqtt.subscribe(&pressureFeed);
mqtt.subscribe(&outFeed);
mqtt.subscribe(&inFeed);
mqtt.subscribe(&humidFeed);
netTasks(); netTasks();
} }
void loop(){ void loop(){
WiFi.disconnect();
mainScreen();//it could be cool and smooth if we could update screen independently, in some kind of separate thread or smthn similar mainScreen();//it could be cool and smooth if we could update screen independently, in some kind of separate thread or smthn similar
WiFi.forceSleepBegin(); if (((timeClient.getHours()*60 + timeClient.getMinutes()) % beepDelay == 0)&&(timeClient.getSeconds() < 1)&&(!nightMode())){
//network required tasks
if (((timeClient.getHours()*60 + timeClient.getMinutes() + timeClient.getSeconds()) % beepDelay == 0)&&(!nightMode())){ //beep and start tasks
tone(15,1000); tone(15,1000);
delay(100); delay(100);
noTone(15); noTone(15);
@ -123,76 +60,47 @@ void loop(){
delay(100); delay(100);
noTone(15); noTone(15);
lastBeep = timeClient.getHours()*60; lastBeep = timeClient.getHours()*60;
// secondDisplay();
netTasks(); netTasks();
} }
} }
//====================IN PROGRESS=================== //====================IN PROGRESS===================
void netTasks() { //show other display before void netTasks() {
HTTPClient http;
WiFi.forceSleepWake(); WiFi.forceSleepWake();
if (WiFi.status() != WL_CONNECTED) { //check wifi status wifiConnect();
wifiConnect(); if (WiFi.status() != WL_CONNECTED) {
} wifiConnect();
// server.handleClient(); }
MQTT_connect();//check connection and get packets for 0.5s http.begin(httpAddr+"/inTemp");
mqtt.processPackets(8000); if (http.GET() < 0) //IDK why the fuck i cant getString without this check
updateNtp();//update time return;
inTemp="i:"+http.getString();
http.end();
http.begin(httpAddr+"/outTemp");
if (http.GET() < 0)
return;
outTemp="o:"+http.getString();
http.end();
http.begin(httpAddr+"/humid");
if (http.GET() < 0)
return;
humid="h:"+http.getString()+"%";
http.end();
http.begin(httpAddr+"/pressure");
if (http.GET() < 0)
return;
pressure="p:"+http.getString();
http.end();
updateNtp();
WiFi.disconnect();
} }
// void secondDisplay() {
// display.clear();
// display.setFont(ArialMT_Plain_16);
// display.setTextAlignment(TEXT_ALIGN_CENTER);
// display.drawString(0, 30, timeClient.getFormattedTime());
// display.drawRect(0,18,128,53); //frame
// /*
// 128/12=10px
// 128x32 resolution
// 32-19=13px for graph
// */
// int x = 0;
// for (int i=0;i<12;++i){
// display.drawLine(x+i*10, y[x], x+i*10+10, y[x]);
// }
// }
void coolBeep() {
tone(15,1000);
delay(200);
noTone(15);
tone(15,1200);
delay(200);
noTone(15);
tone(15,1000);
delay(200);
noTone(15);
tone(15,1200);
delay(250);
noTone(15);
tone(15,1400);
delay(350);
noTone(15);
tone(15,1500);
delay(350);
noTone(15);
}
//====================paused===================
//===================WELL DONE=======================
void mainScreen() { void mainScreen() {
display.clear(); display.clear();
if (!nightMode()) {
if (!nightMode()) { //turn off screen at night
int x = (timeClient.getHours()*60+timeClient.getMinutes())/11; int x = (timeClient.getHours()*60+timeClient.getMinutes())/11;
//Time //Time
display.setFont(ArialMT_Plain_24); display.setFont(ArialMT_Plain_24);
@ -200,34 +108,28 @@ void mainScreen() {
display.drawString(64, 22, timeClient.getFormattedTime()); display.drawString(64, 22, timeClient.getFormattedTime());
//progress bar //progress bar
display.drawLine(0,22,x,22); display.drawLine(0,22,x,22);
if (mqttAvail){ //do not show info if mqtt not available // external temp //top left
//external temp //top left display.setFont(ArialMT_Plain_10);
display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setTextAlignment(TEXT_ALIGN_LEFT); display.drawString(0, 0, outTemp);
display.drawString(0, 0, "o:"+String(outTemp)); //pressure //top right
//pressure //top right display.setFont(ArialMT_Plain_10);
display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.setTextAlignment(TEXT_ALIGN_RIGHT); display.drawString(128, 0, pressure);
display.drawString(128, 0, "p:"+String(pressure)); //inside temp //bottom left
//inside temp //bottom left display.setFont(ArialMT_Plain_10);
display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setTextAlignment(TEXT_ALIGN_LEFT); display.drawString(0, 53, inTemp);
display.drawString(0, 53, "i:"+String(inTemp)); //humid //bottom right
//humid //bottom right display.setFont(ArialMT_Plain_10);
display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_RIGHT);
display.setTextAlignment(TEXT_ALIGN_RIGHT); display.drawString(128, 53, humid);
display.drawString(128, 53, "h:"+String(humid)+"%");
}
else {
display.setFont(ArialMT_Plain_10);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.drawString(0, 53, "mqtt unavailable");
}
} }
display.display(); display.display();
} }
void displayStatus(int state){ void displayStatus(int state){
display.clear(); display.clear();
display.setTextAlignment(TEXT_ALIGN_CENTER);
switch (state) { switch (state) {
case 0: case 0:
display.drawString(64, 22, "Connected!"); display.drawString(64, 22, "Connected!");
@ -244,43 +146,10 @@ void displayStatus(int state){
} }
display.display(); display.display();
} }
void MQTT_connect() {
if (mqtt.connected()) {
return;
}
int8_t ret;
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
displayStatus(3);
mqtt.disconnect();
delay(5000);
retries--;
if (retries == 0) {
mqttAvail = false;
}
}
if (mqtt.connected())
mqttAvail = true;
}
void pressureCall(double x) {
pressure = x;
}
void inCall(double x) {
inTemp = x;
}
void outCall(double x) {
outTemp = x;
}
void humidCall(double x){
humid = x;
}
void updateNtp() { void updateNtp() {
if (lastNtp != timeClient.getHours()) { // if (lastNtp != timeClient.getHours()) {
if (timeClient.update()) while (!timeClient.update())
Serial.println(timeClient.getHours());
lastNtp = timeClient.getHours();
}
} }
bool nightMode() { bool nightMode() {
if ((timeClient.getHours() > offTime)||(timeClient.getHours() < onTime)) { //turn off screen between loaded time if ((timeClient.getHours() > offTime)||(timeClient.getHours() < onTime)) { //turn off screen between loaded time
@ -289,4 +158,3 @@ bool nightMode() {
else else
return false; return false;
} }