Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- some random description...
- ~thanks Samaki <3s
- */
- //DEPENDENCIES (listed all, since who the hell has the time to check insides of every library...)
- //COMMUNICATION
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include <WiFiUdp.h>
- #include <PubSubClient.h>
- //TIME MANAGEMENT
- #include <NTPtimeESP.h>
- #include <Metro.h>
- #include <RestartManager.h>
- //ACCESORIES
- #include <CoreRL.h>
- #include <CoreRF.h>
- //OTHER
- #include <ESPHelper.h>
- #include <ArduinoJson.h>
- #include <FS.h>
- #include <CoreMonitor.h>
- //CONST SETTINGS
- #define MQTT_TRANSMITTER_BUFFER_SIZE 300
- #define MQTT_PROCESSING_BUFFER_SIZE 300
- #define BAUD_RATE 115200
- #define INTERNAL_LED 2
- //RL RELATED
- const bool RL_INSTALLED = true;
- //const bool RL_MOD_LATCH = false; <-- this setting is not available
- const bool RL_MOD_FLICK = false;
- const uint16_t RL_MOD_FLICK_DELAY = 500;
- //RF RELATED
- const bool RF_INSTALLED = false;
- //ENCRYPTION RELATED
- const bool USE_SSL_ENCRYPTION = false;
- const char* fingerprint = "";
- //WIFI/CONNECTION RELATED
- //DELETED PRIVATE INFORMATION
- netInfo *knownNetworks[3] = {
- &client,
- &home,
- &debug
- };
- ESPHelper myESP(knownNetworks, 3);
- char* host = "omniNode";
- const char* update_path = "/update";
- const char* update_username = "admin";
- const char* update_password = "debug4meESP";
- const char *HAP_IN = "/HAP_IN";
- const char *HAP_OUT = "/HAP_OUT";
- const char *ESP_LINK_IN = "/ESP_LINK_IN";
- const char *ESP_LINK_OUT = "/ESP_LINK_OUT";
- const char *ESP_MQTT_DEBUG_SERIAL = "/ESP_MQTT_DEBUG_SERIAL";
- //RESTART SCHEDULE RELATED
- const uint8_t scheduleRestartAtHour = 2;
- const uint8_t scheduleRestartAtDay = 14;
- const bool useGpioForRestart = false;
- const uint8_t restartPin = NULL;
- //GENERIC SETTINGS
- const bool useSerialDebug = true;
- const bool useMQTTDebug = true;
- const uint32_t monitoringInterval = 30000;
- const bool useLED = true;
- const uint32_t PING_INTERVAL = 120000;
- const uint32_t OTA_ON_DEFAULT_TIME = 600000;
- //CODE STARTS HERE ------------
- //DECLARING LIBRRY RELATED STUFF
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- NTPtime NTPch("ch.pool.ntp.org");
- strDateTime dateTime;
- CoreMonitor MNT(monitoringInterval);
- RestartManager RST;
- CoreRL RL(13, true);
- CoreRF RF(5);
- Metro pingMetro(PING_INTERVAL);
- //DECLARING GLOBAL VARIABLES
- //GENERIC FLAGS&VARS
- String macAdress;
- bool updateServerEnabled = false;
- //CONNECTION RELATED FLAGS&VARS
- bool enabledSerialDebug = false;
- bool enabledMQTTDebug = false;
- bool connectedForFirstTimeSinceBoot = false;
- bool connected = false;
- uint32_t timeOfLostConnection;
- //MQTT BROKER NAMING SCHEME
- enum MQTT_naming_scheme { BOOTED, CONNECTED, PING, LOG_REPORT, ERR_RESTART_FAILED };
- void setup(){
- //BEGIN SERIAL && DEBUG RELATED STUFF
- if(useSerialDebug){
- Serial.begin(BAUD_RATE);
- enabledSerialDebug = true;
- }
- enabledMQTTDebug = false;
- DEBUG_MESSAGE(">>>SYSTEM ALIVE<<<<");
- DEBUG_MESSAGE("setup(): serial begin successful");
- if(useLED){
- pinMode(INTERNAL_LED, OUTPUT);
- digitalWrite(INTERNAL_LED, true);
- }
- //BEGIN ESP HELPER & CONNECTION setup
- macAdress = WiFi.macAddress();
- if(USE_SSL_ENCRYPTION)
- myESP.useSecureClient(fingerprint);
- myESP.addSubscription(HAP_IN);
- myESP.addSubscription(ESP_LINK_IN);
- myESP.setMQTTCallback(incomingMQTT);
- myESP.setWifiCallback(wifiConnected);
- myESP.begin();
- DEBUG_MESSAGE("setup(): ESP HELPER initialization finished");
- //BEGIN Initialization of aceessories
- if(RL_INSTALLED)
- RL.begin();
- if(RF_INSTALLED)
- RF.begin();
- MNT.pauseMonitoring();
- DEBUG_MESSAGE("setup(): accessories initialization finished, setup() finished");
- }
- void wifiConnected(){
- DEBUG_MESSAGE("wifiConnected(): wifi connected, running second stage setup!");
- //SENDING "BOOTED" MSG TO MQTT BROKER
- //SEND "BOOTED MSG TO BROKER"
- DEBUG_MESSAGE("wifiConnected(): MQTT BOOTED MSG transmitted!");
- runLoop(20);
- //GET NPT TIME AND SET UP RESTART SCHEDULE
- while(true){
- dateTime = NTPch.getNTPtime(1.0, 1);
- if(dateTime.valid){
- RST.setSchedule(((24-dateTime.hour+scheduleRestartAtHour)+scheduleRestartAtDay*24*60*60*1000));
- RST.setBeforeRestartCallback(restartIncoming);
- RST.setScheduledRestartTimeFailCallback(restartScheduleFailed);
- if(useGpioForRestart)
- RST.setGpioRestart(restartPin);
- DEBUG_MESSAGE("wifiConnected(): NTP request get(): success! time calculation finished, RST manager running!");
- break;
- }
- else{
- DEBUG_MESSAGE("wifiConnected(): NTP request get(): failed! running loop to maintain the system core");
- runLoop(20);
- }
- }
- runLoop(20);
- //BEGIN HOSTING THE UPDATE server
- MDNS.begin(host);
- httpUpdater.setup(&httpServer, update_path, update_username, update_password);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- DEBUG_MESSAGE("wifiConnected(): update setup finished!");
- //STARTING SYSTEM monitoring
- MNT.resumeMonitoring();
- if(useLED)
- digitalWrite(INTERNAL_LED, false);
- DEBUG_MESSAGE("wifiConnected(): generic setup finished, wifiConneced(): finished!");
- }
- void loop(){
- maintainConnection();
- if(updateServerEnabled)
- httpServer.handleClient();
- maintainConnection();
- MNT.run();
- RST.run();
- RL.run();
- maintainConnection();
- if(pingMetro.check()){
- //PING
- DEBUG_MESSAGE("pingMetro: MSG transmitted!");
- }
- }
- void runLoop(uint8_t cycles){
- for(int i = 0; i <= cycles; i++){
- loop();
- }
- }
- void maintainConnection(){
- if(myESP.loop() == FULL_CONNECTION){
- if(!connectedForFirstTimeSinceBoot){
- //SENDING "CONNECTED FROM BOOT" MSG TO MQTT BROKER
- connectedForFirstTimeSinceBoot = true;
- connected = true;
- return;
- }
- if(!connected){
- //SENDING "CONNECTED" MSG TO MQTT BROKER
- connected = true;
- }
- }
- else{
- if(connected){
- connected = false;
- timeOfLostConnection = millis();
- }
- }
- }
- void restartIncoming(){
- DEBUG_MESSAGE("restartIncoming(): triggered, preparing for restart!");
- //SENDING "RESTARTING" MSG TO MQTT BROKER
- DEBUG_MESSAGE("restartIncoming(): restarting now!");
- }
- void restartScheduleFailed(){
- //SENDING "RESTART_FAILED" MSG TO MQTT BROKER
- DEBUG_MESSAGE("restartScheduleFailed(): Restart failed, unhalted exception, system stability voided, upcoming: millis() overflow, nothing is certain!!!");
- }
- void DEBUG_MESSAGE(char *text){
- if(useSerialDebug)
- Serial.println(text);
- if(useMQTTDebug)
- myESP.publish(ESP_MQTT_DEBUG_SERIAL, text, false);
- }
- // FROM THIS LINE ON IT IS NOT WORKING LOL --------
- void incomingMQTT(char* topic, byte* payload, unsigned int length){
- }
- void sendMQTT(int enumInput){
- }
- void sendMQTTjson(uint8_t sendMQTTmsgID, uint8_t MQTTmsgParameter){
- Serial.println("Decice is packing the payload to JSON file: sendMQTTmsgID, MQTTmsgParameter");
- StaticJsonBuffer<MQTT_TRANSMITTER_BUFFER_SIZE> jsonTransmitterBuffer;
- JsonObject& json = jsonTransmitterBuffer.createObject();
- json["macAdress"] = macAdress;
- if(sendMQTTmsgID == 1) { json["RST_CONFIRM"] = true; }
- if(sendMQTTmsgID == 2) { json["PING_RESPONCE"] = true; }
- if(sendMQTTmsgID == 3) { json["OTA_CONFIRM"] = true; }
- if(sendMQTTmsgID == 4) { json["BOOTED"] = true; }
- if(sendMQTTmsgID == 5) { json["ERR"] = true; }
- if(sendMQTTmsgID == 6) { json["RECONNECTED"] = true; }
- if(sendMQTTmsgID == 7) { json["HAP"] = MQTTmsgParameter; }
- char charBuffer[100];
- json.printTo(charBuffer);q
- myESP.publish(ESP_LINK_OUT, charBuffer, false);
- jsonTransmitterBuffer.clear();
- }
- void processIncomingMQTTjson(char* topic, byte* payload, unsigned int length){
- StaticJsonBuffer<MQTT_PROCESSING_BUFFER_SIZE> jsonPayloadBuffer;
- JsonObject& jsonPayload = jsonPayloadBuffer.parseObject(payload);
- String tmpMacAdressJsonString = jsonPayload["macAdress"];
- bool globalMsg = jsonPayload["globalMsg"];
- if(!globalMsg && !(tmpMacAdressJsonString == macAdress)){
- Serial.println("this packed is not going to be executed - it isnt adressed to this device...");
- Serial.println(macAdress);
- Serial.println(tmpMacAdressJsonString);
- return;
- }
- jsonPayloadBuffer.clear(); //unalloc the json struct
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement