some powersaves
This commit is contained in:
parent
eeda3bea86
commit
38bc729b95
2
.vscode/arduino.json
vendored
2
.vscode/arduino.json
vendored
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"board": "esp8266:esp8266:generic",
|
"board": "esp8266:esp8266:generic",
|
||||||
"configuration": "CpuFrequency=160,FlashFreq=80,FlashMode=dio,UploadSpeed=115200,FlashSize=1M512,ResetMethod=ck,Debug=Disabled,DebugLevel=None____",
|
"configuration": "CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=1M512,ResetMethod=ck,Debug=Disabled,DebugLevel=None____",
|
||||||
"port": "/dev/ttyUSB0",
|
"port": "/dev/ttyUSB0",
|
||||||
"sketch": "frontend.ino",
|
"sketch": "frontend.ino",
|
||||||
"output": "Output"
|
"output": "Output"
|
||||||
|
219
frontend.ino
219
frontend.ino
@ -1,18 +1,18 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include <ESP8266WebServer.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 <ArduinoJson.h>
|
||||||
#include <FS.h>
|
// #include <FS.h>
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
#include "Adafruit_MQTT.h"
|
#include "Adafruit_MQTT.h"
|
||||||
#include "Adafruit_MQTT_Client.h"
|
#include "Adafruit_MQTT_Client.h"
|
||||||
|
|
||||||
double inTemp,outTemp,pressure,humid;
|
double inTemp,outTemp,pressure,humid;
|
||||||
unsigned short int onTime = 5, offTime = 23, beepDelay = 60, lastNtp, lastBeep;
|
unsigned short int onTime = 5, offTime = 23, beepDelay = 60, lastBeep, lastWifi;
|
||||||
bool mqttAvail;
|
bool mqttAvail;
|
||||||
|
|
||||||
WiFiUDP ntpUDP;
|
WiFiUDP ntpUDP;
|
||||||
@ -20,7 +20,7 @@ NTPClient timeClient(ntpUDP);
|
|||||||
SSD1306Brzo display(0x3C, 5, 4); //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;
|
WiFiClient client;
|
||||||
ESP8266WebServer server(80);
|
// ESP8266WebServer server(80);
|
||||||
Adafruit_MQTT_Client mqtt(&client, "192.168.100.100", 1883);
|
Adafruit_MQTT_Client mqtt(&client, "192.168.100.100", 1883);
|
||||||
|
|
||||||
Adafruit_MQTT_Subscribe pressureFeed = Adafruit_MQTT_Subscribe(&mqtt, "pressure");
|
Adafruit_MQTT_Subscribe pressureFeed = Adafruit_MQTT_Subscribe(&mqtt, "pressure");
|
||||||
@ -57,11 +57,11 @@ void setup(){
|
|||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
wifiConnect();
|
wifiConnect();
|
||||||
//==READ CONFIG==
|
//==READ CONFIG==
|
||||||
if (!SPIFFS.begin())
|
// if (!SPIFFS.begin())
|
||||||
SPIFFS.format();
|
// SPIFFS.format();
|
||||||
|
|
||||||
if (!readConfig())
|
// if (!readConfig())
|
||||||
defConfig();
|
// defConfig();
|
||||||
|
|
||||||
//==NTP INIT==
|
//==NTP INIT==
|
||||||
timeClient.begin();
|
timeClient.begin();
|
||||||
@ -75,18 +75,23 @@ void setup(){
|
|||||||
mqtt.subscribe(&outFeed);
|
mqtt.subscribe(&outFeed);
|
||||||
mqtt.subscribe(&inFeed);
|
mqtt.subscribe(&inFeed);
|
||||||
mqtt.subscribe(&humidFeed);
|
mqtt.subscribe(&humidFeed);
|
||||||
server.on("/edit", editConfig);
|
// server.on("/edit", editConfig);
|
||||||
server.begin();
|
// server.begin();
|
||||||
|
updateNtp();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
if (WiFi.status() != WL_CONNECTED) { //check wifi status
|
if (WiFi.status() != WL_CONNECTED) { //check wifi status
|
||||||
wifiConnect();
|
wifiConnect();
|
||||||
}
|
}
|
||||||
server.handleClient();
|
// server.handleClient();
|
||||||
MQTT_connect();//check connection and get packets for 0.5s
|
if (timeClient.getHours()*60+timeClient.getMinutes() - lastWifi >= 60){ // due to powersaving
|
||||||
mqtt.processPackets(300);
|
lastWifi = timeClient.getHours()*60+timeClient.getMinutes();
|
||||||
|
MQTT_connect();//check connection and get packets for 0.5s
|
||||||
|
mqtt.processPackets(750);
|
||||||
|
updateNtp();//update time
|
||||||
|
}
|
||||||
|
else delay(900);
|
||||||
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
|
||||||
|
|
||||||
if ((timeClient.getHours()*60 + timeClient.getMinutes() - lastBeep >= beepDelay)&&(!nightMode())){ //beep every $lastBeep
|
if ((timeClient.getHours()*60 + timeClient.getMinutes() - lastBeep >= beepDelay)&&(!nightMode())){ //beep every $lastBeep
|
||||||
@ -96,28 +101,9 @@ void loop(){
|
|||||||
lastBeep = timeClient.getHours()*60;
|
lastBeep = timeClient.getHours()*60;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateNtp();//update time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//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
|
//second screen
|
||||||
|
|
||||||
//reconfig wifi if not found (start webserver and AP)
|
|
||||||
|
|
||||||
//GRAPH
|
//GRAPH
|
||||||
/*
|
/*
|
||||||
get json from server
|
get json from server
|
||||||
@ -127,106 +113,98 @@ if pressure lowers then weather gonna be bad
|
|||||||
if pressure uppers then weather gonna be good
|
if pressure uppers then weather gonna be good
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//CONFIG
|
|
||||||
/*
|
|
||||||
beep time
|
|
||||||
on time
|
|
||||||
off time
|
|
||||||
mqtt update interval
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
//====================IN PROGRESS===================
|
//====================IN PROGRESS===================
|
||||||
void editConfig(){
|
// void editConfig(){
|
||||||
if (server.args() > 0 ) {
|
// if (server.args() > 0 ) {
|
||||||
for ( uint8_t i = 0; i < server.args(); i++ ){
|
// for ( uint8_t i = 0; i < server.args(); i++ ){
|
||||||
String Argument_Name = server.argName(i);
|
// String Argument_Name = server.argName(i);
|
||||||
String client_response = server.arg(i);
|
// String client_response = server.arg(i);
|
||||||
|
|
||||||
if (Argument_Name == "beepDelay"){
|
// if (Argument_Name == "beepDelay"){
|
||||||
beepDelay = client_response.toInt();
|
// beepDelay = client_response.toInt();
|
||||||
}
|
// }
|
||||||
if (Argument_Name == "onTime"){
|
// if (Argument_Name == "onTime"){
|
||||||
onTime = client_response.toInt();
|
// onTime = client_response.toInt();
|
||||||
}
|
// }
|
||||||
if (Argument_Name == "offTime"){
|
// if (Argument_Name == "offTime"){
|
||||||
offTime = client_response.toInt();
|
// offTime = client_response.toInt();
|
||||||
}
|
// }
|
||||||
updateConfig();
|
// updateConfig();
|
||||||
server.send(200, "text/plain", "updatedConfig");
|
// server.send(200, "text/plain", "updatedConfig");
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
server.send(200, "text/plain", "to update config, goto " + String(WiFi.localIP()) + "/edit?parameter=value");
|
// server.send(200, "text/plain", "to update config, goto " + String(WiFi.localIP()) + "/edit?parameter=value");
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool updateConfig() {
|
// bool updateConfig() {
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
// StaticJsonBuffer<200> jsonBuffer;
|
||||||
JsonObject& json = jsonBuffer.createObject();
|
// JsonObject& json = jsonBuffer.createObject();
|
||||||
json["onTime"] = onTime;
|
// json["onTime"] = onTime;
|
||||||
json["offTime"] = offTime;
|
// json["offTime"] = offTime;
|
||||||
json["beepDelay"] = beepDelay;
|
// json["beepDelay"] = beepDelay;
|
||||||
File configFile = SPIFFS.open("/config.json", "w");
|
// File configFile = SPIFFS.open("/config.json", "w");
|
||||||
if (!configFile) {
|
// if (!configFile) {
|
||||||
Serial.println("Failed to open config file for writing");
|
// Serial.println("Failed to open config file for writing");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
json.printTo(configFile);
|
// json.printTo(configFile);
|
||||||
return true;
|
// return true;
|
||||||
|
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool readConfig() {
|
// bool readConfig() {
|
||||||
File configFile = SPIFFS.open("/config.json", "r");
|
// File configFile = SPIFFS.open("/config.json", "r");
|
||||||
if (!configFile) {
|
// if (!configFile) {
|
||||||
Serial.println("Failed to open config file");
|
// Serial.println("Failed to open config file");
|
||||||
|
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
size_t size = configFile.size();
|
// size_t size = configFile.size();
|
||||||
if (size > 1024) {
|
// if (size > 1024) {
|
||||||
Serial.println("Config file size is too large");
|
// Serial.println("Config file size is too large");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Allocate a buffer to store contents of the file.
|
// // Allocate a buffer to store contents of the file.
|
||||||
std::unique_ptr<char[]> buf(new char[size]);
|
// std::unique_ptr<char[]> buf(new char[size]);
|
||||||
|
|
||||||
// We don't use String here because ArduinoJson library requires the input buffer to be mutable. If you don't use ArduinoJson, you may as well use configFile.readString instead.
|
// // We don't use String here because ArduinoJson library requires the input buffer to be mutable. If you don't use ArduinoJson, you may as well use configFile.readString instead.
|
||||||
configFile.readBytes(buf.get(), size);
|
// configFile.readBytes(buf.get(), size);
|
||||||
|
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
// StaticJsonBuffer<200> jsonBuffer;
|
||||||
JsonObject& json = jsonBuffer.parseObject(buf.get());
|
// JsonObject& json = jsonBuffer.parseObject(buf.get());
|
||||||
|
|
||||||
if (!json.success()) {
|
// if (!json.success()) {
|
||||||
Serial.println("Failed to parse config file");
|
// Serial.println("Failed to parse config file");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
offTime = int(json["offTime"]);
|
// offTime = int(json["offTime"]);
|
||||||
onTime = int(json["onTime"]);
|
// onTime = int(json["onTime"]);
|
||||||
beepDelay = int(json["beepDelay"]);
|
// beepDelay = int(json["beepDelay"]);
|
||||||
return true;
|
// return true;
|
||||||
|
|
||||||
}
|
// }
|
||||||
bool defConfig() {
|
// bool defConfig() {
|
||||||
StaticJsonBuffer<200> jsonBuffer;
|
// StaticJsonBuffer<200> jsonBuffer;
|
||||||
JsonObject& json = jsonBuffer.createObject();
|
// JsonObject& json = jsonBuffer.createObject();
|
||||||
json["onTime"] = "6";
|
// json["onTime"] = "6";
|
||||||
json["offTime"] = "23";
|
// json["offTime"] = "23";
|
||||||
json["beepDelay"] = "60";
|
// json["beepDelay"] = "60";
|
||||||
File configFile = SPIFFS.open("/config.json", "w");
|
// File configFile = SPIFFS.open("/config.json", "w");
|
||||||
if (!configFile) {
|
// if (!configFile) {
|
||||||
Serial.println("Failed to open config file for writing");
|
// Serial.println("Failed to open config file for writing");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
json.printTo(configFile);
|
// json.printTo(configFile);
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
//====================paused===================
|
//====================paused===================
|
||||||
|
|
||||||
@ -342,10 +320,13 @@ void humidCall(double x){
|
|||||||
humid = x;
|
humid = x;
|
||||||
}
|
}
|
||||||
void updateNtp() {
|
void updateNtp() {
|
||||||
if (lastNtp != timeClient.getHours()) {
|
// if (lastNtp != timeClient.getHours()) {
|
||||||
if (timeClient.update())
|
// if (timeClient.update())
|
||||||
lastNtp = timeClient.getHours();
|
while (!timeClient.update()) {
|
||||||
|
delay(100);
|
||||||
}
|
}
|
||||||
|
// 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
|
||||||
|
Loading…
Reference in New Issue
Block a user