This commit is contained in:
l0sted 2018-09-17 23:54:23 +03:00
parent 38bc729b95
commit 56d7018812
3 changed files with 101 additions and 144 deletions

View File

@ -1,6 +1,6 @@
{
"board": "esp8266:esp8266:generic",
"configuration": "CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=1M512,ResetMethod=ck,Debug=Disabled,DebugLevel=None____",
"configuration": "CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=2M,ResetMethod=ck,Debug=Disabled,DebugLevel=None____",
"port": "/dev/ttyUSB0",
"sketch": "frontend.ino",
"output": "Output"

View File

@ -1,3 +1,6 @@
{
"C_Cpp.intelliSenseEngineFallback": "Enabled"
"C_Cpp.intelliSenseEngineFallback": "Enabled",
"files.associations": {
"*.inc": "cpp"
}
}

View File

@ -1,23 +1,61 @@
//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 <ESP8266WiFiGeneric.h>
#include <WiFiClient.h>
// #include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// #include <ESP8266mDNS.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "SSD1306Brzo.h"
#include <ArduinoJson.h>
// #include <ArduinoJson.h>
// #include <FS.h>
#include "wifi.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
double inTemp,outTemp,pressure,humid;
unsigned short int onTime = 5, offTime = 23, beepDelay = 60, lastBeep, lastWifi;
unsigned short int lastNtp, lastBeep, onTime=6, offTime=23, beepDelay=60;
bool mqttAvail;
WiFiUDP ntpUDP;
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, 4, 5); //oled display w/ address 0x3C with SDA on GPIO4 and SCL on GPIO5 //address == offset
WiFiClient client;
// ESP8266WebServer server(80);
@ -41,7 +79,7 @@ void wifiConnect() {
delay(10000);
ESP.reset();
} else {
MDNS.begin("esp8266-frontend");
// MDNS.begin("esp8266-frontend");
Serial.print("Connected to " + String(ssid) + "; IP address: ");
Serial.println(WiFi.localIP());
displayStatus(0);
@ -56,13 +94,6 @@ void setup(){
//==WIFI CONNECT==
WiFi.mode(WIFI_STA);
wifiConnect();
//==READ CONFIG==
// if (!SPIFFS.begin())
// SPIFFS.format();
// if (!readConfig())
// defConfig();
//==NTP INIT==
timeClient.begin();
timeClient.setTimeOffset(10800);
@ -75,139 +106,41 @@ void setup(){
mqtt.subscribe(&outFeed);
mqtt.subscribe(&inFeed);
mqtt.subscribe(&humidFeed);
// server.on("/edit", editConfig);
// server.begin();
updateNtp();
netTasks();
}
void loop(){
if (WiFi.status() != WL_CONNECTED) { //check wifi status
wifiConnect();
}
// server.handleClient();
if (timeClient.getHours()*60+timeClient.getMinutes() - lastWifi >= 60){ // due to powersaving
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
if ((timeClient.getHours()*60 + timeClient.getMinutes() - lastBeep >= beepDelay)&&(!nightMode())){ //beep every $lastBeep
WiFi.disconnect();
mainScreen();//it could be cool and smooth if we could update screen independently, in some kind of separate thread or smthn similar
WiFi.forceSleepBegin();
//network required tasks
if (((timeClient.getHours()*60 + timeClient.getMinutes() + timeClient.getSeconds()) % beepDelay == 0)&&(!nightMode())){ //beep and start tasks
tone(15,1000);
delay(100);
noTone(15);
tone(15,1000);
delay(100);
noTone(15);
lastBeep = timeClient.getHours()*60;
// secondDisplay();
netTasks();
}
}
//second screen
//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
*/
//====================IN PROGRESS===================
// void editConfig(){
// if (server.args() > 0 ) {
// for ( uint8_t i = 0; i < server.args(); i++ ){
// String Argument_Name = server.argName(i);
// String client_response = server.arg(i);
// if (Argument_Name == "beepDelay"){
// beepDelay = client_response.toInt();
// }
// if (Argument_Name == "onTime"){
// onTime = client_response.toInt();
// }
// if (Argument_Name == "offTime"){
// offTime = client_response.toInt();
// }
// updateConfig();
// server.send(200, "text/plain", "updatedConfig");
// }
// } else {
// server.send(200, "text/plain", "to update config, goto " + String(WiFi.localIP()) + "/edit?parameter=value");
// }
// }
// bool updateConfig() {
// StaticJsonBuffer<200> jsonBuffer;
// JsonObject& json = jsonBuffer.createObject();
// json["onTime"] = onTime;
// json["offTime"] = offTime;
// json["beepDelay"] = beepDelay;
// File configFile = SPIFFS.open("/config.json", "w");
// if (!configFile) {
// Serial.println("Failed to open config file for writing");
// return false;
// }
// json.printTo(configFile);
// return true;
// }
// bool readConfig() {
// File configFile = SPIFFS.open("/config.json", "r");
// if (!configFile) {
// Serial.println("Failed to open config file");
// return false;
// }
// size_t size = configFile.size();
// if (size > 1024) {
// Serial.println("Config file size is too large");
// return false;
// }
// // Allocate a buffer to store contents of the file.
// 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.
// configFile.readBytes(buf.get(), size);
// StaticJsonBuffer<200> jsonBuffer;
// JsonObject& json = jsonBuffer.parseObject(buf.get());
// if (!json.success()) {
// Serial.println("Failed to parse config file");
// return false;
// }
// offTime = int(json["offTime"]);
// onTime = int(json["onTime"]);
// beepDelay = int(json["beepDelay"]);
// return true;
// }
// bool defConfig() {
// StaticJsonBuffer<200> jsonBuffer;
// JsonObject& json = jsonBuffer.createObject();
// json["onTime"] = "6";
// json["offTime"] = "23";
// json["beepDelay"] = "60";
// File configFile = SPIFFS.open("/config.json", "w");
// if (!configFile) {
// Serial.println("Failed to open config file for writing");
// return false;
// }
// json.printTo(configFile);
// return true;
// }
//====================paused===================
void netTasks() { //show other display before
WiFi.forceSleepWake();
if (WiFi.status() != WL_CONNECTED) { //check wifi status
wifiConnect();
}
// server.handleClient();
MQTT_connect();//check connection and get packets for 0.5s
mqtt.processPackets(8000);
updateNtp();//update time
}
// void secondDisplay() {
// display.clear();
@ -228,7 +161,34 @@ if pressure uppers then weather gonna be good
// }
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() {
display.clear();
@ -266,12 +226,8 @@ void mainScreen() {
}
display.display();
}
void displayStatus(int state){
display.clear();
display.setFont(ArialMT_Plain_16);
display.setTextAlignment(TEXT_ALIGN_CENTER);
switch (state) {
case 0:
display.drawString(64, 22, "Connected!");
@ -320,13 +276,11 @@ void humidCall(double x){
humid = x;
}
void updateNtp() {
// if (lastNtp != timeClient.getHours()) {
// if (timeClient.update())
while (!timeClient.update()) {
delay(100);
if (lastNtp != timeClient.getHours()) {
if (timeClient.update())
lastNtp = timeClient.getHours();
}
// lastNtp = timeClient.getHours();
// }
}
bool nightMode() {
if ((timeClient.getHours() > offTime)||(timeClient.getHours() < onTime)) { //turn off screen between loaded time