Advertisement
Guest User

THE CODE

a guest
Mar 3rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.65 KB | None | 0 0
  1. /*
  2.  
  3.  
  4. some random description...
  5.  
  6.   ~thanks Samaki <3s
  7.  
  8.  
  9. */
  10.  
  11. //DEPENDENCIES    (listed all, since who the hell has the time to check insides of every library...)
  12.  
  13. //COMMUNICATION
  14. #include <ESP8266WiFi.h>
  15. #include <WiFiClient.h>
  16. #include <ESP8266WebServer.h>
  17. #include <ESP8266mDNS.h>
  18. #include <ESP8266HTTPUpdateServer.h>
  19. #include <WiFiUdp.h>
  20. #include <PubSubClient.h>
  21. //TIME MANAGEMENT
  22. #include <NTPtimeESP.h>
  23. #include <Metro.h>
  24. #include <RestartManager.h>
  25. //ACCESORIES
  26. #include <CoreRL.h>
  27. #include <CoreRF.h>
  28. //OTHER
  29. #include <ESPHelper.h>
  30. #include <ArduinoJson.h>
  31. #include <FS.h>
  32. #include <CoreMonitor.h>
  33.  
  34. //CONST SETTINGS
  35.  
  36. #define MQTT_TRANSMITTER_BUFFER_SIZE 300
  37. #define MQTT_PROCESSING_BUFFER_SIZE 300
  38. #define BAUD_RATE 115200
  39. #define INTERNAL_LED 2
  40.  
  41.   //RL RELATED
  42. const bool RL_INSTALLED = true;
  43. //const bool RL_MOD_LATCH = false;  <-- this setting is not available
  44. const bool RL_MOD_FLICK = false;
  45. const uint16_t RL_MOD_FLICK_DELAY = 500;
  46.  
  47.   //RF RELATED
  48.  
  49. const bool RF_INSTALLED = false;
  50.  
  51.   //ENCRYPTION RELATED
  52.  
  53. const bool USE_SSL_ENCRYPTION = false;
  54. const char* fingerprint = "";
  55.  
  56.   //WIFI/CONNECTION RELATED
  57.  
  58. //DELETED PRIVATE INFORMATION
  59.  
  60.  
  61.   netInfo *knownNetworks[3] = {
  62.     &client,
  63.     &home,
  64.     &debug
  65.   };
  66.  
  67.   ESPHelper myESP(knownNetworks, 3);
  68.  
  69. char* host = "omniNode";
  70. const char* update_path = "/update";
  71. const char* update_username = "admin";
  72. const char* update_password = "debug4meESP";
  73.  
  74. const char *HAP_IN = "/HAP_IN";
  75. const char *HAP_OUT = "/HAP_OUT";
  76. const char *ESP_LINK_IN = "/ESP_LINK_IN";
  77. const char *ESP_LINK_OUT = "/ESP_LINK_OUT";
  78. const char *ESP_MQTT_DEBUG_SERIAL = "/ESP_MQTT_DEBUG_SERIAL";
  79.  
  80.   //RESTART SCHEDULE RELATED
  81.  
  82. const uint8_t scheduleRestartAtHour = 2;
  83. const uint8_t scheduleRestartAtDay = 14;
  84.  
  85. const bool useGpioForRestart = false;
  86. const uint8_t restartPin =  NULL;
  87.  
  88.   //GENERIC SETTINGS
  89.  
  90. const bool useSerialDebug = true;
  91. const bool useMQTTDebug = true;
  92.  
  93. const uint32_t monitoringInterval = 30000;
  94.  
  95. const bool useLED = true;
  96.  
  97. const uint32_t PING_INTERVAL = 120000;
  98. const uint32_t OTA_ON_DEFAULT_TIME = 600000;
  99.  
  100. //CODE STARTS HERE ------------
  101.  
  102.   //DECLARING LIBRRY RELATED STUFF
  103.  
  104. ESP8266WebServer httpServer(80);
  105. ESP8266HTTPUpdateServer httpUpdater;
  106. NTPtime NTPch("ch.pool.ntp.org");
  107. strDateTime dateTime;
  108. CoreMonitor MNT(monitoringInterval);
  109. RestartManager RST;
  110. CoreRL RL(13, true);
  111. CoreRF RF(5);
  112.  
  113. Metro pingMetro(PING_INTERVAL);
  114.  
  115.   //DECLARING GLOBAL VARIABLES
  116.  
  117.     //GENERIC FLAGS&VARS
  118.  
  119. String macAdress;
  120. bool updateServerEnabled = false;
  121.  
  122.     //CONNECTION RELATED FLAGS&VARS
  123.  
  124. bool enabledSerialDebug = false;
  125. bool enabledMQTTDebug = false;
  126. bool connectedForFirstTimeSinceBoot = false;
  127. bool connected = false;
  128. uint32_t timeOfLostConnection;
  129.  
  130.   //MQTT BROKER NAMING SCHEME
  131.  
  132. enum MQTT_naming_scheme { BOOTED, CONNECTED, PING, LOG_REPORT, ERR_RESTART_FAILED };
  133.  
  134.  
  135.  
  136. void setup(){
  137.  
  138.     //BEGIN SERIAL && DEBUG RELATED STUFF
  139.  
  140.   if(useSerialDebug){
  141.     Serial.begin(BAUD_RATE);
  142.     enabledSerialDebug = true;
  143.   }
  144.  
  145.   enabledMQTTDebug = false;
  146.  
  147.   DEBUG_MESSAGE(">>>SYSTEM ALIVE<<<<");
  148.  
  149.   DEBUG_MESSAGE("setup(): serial begin successful");
  150.  
  151.   if(useLED){
  152.     pinMode(INTERNAL_LED, OUTPUT);
  153.     digitalWrite(INTERNAL_LED, true);
  154.   }
  155.  
  156.     //BEGIN ESP HELPER & CONNECTION setup
  157.  
  158.   macAdress = WiFi.macAddress();
  159.  
  160.   if(USE_SSL_ENCRYPTION)
  161.     myESP.useSecureClient(fingerprint);
  162.  
  163.     myESP.addSubscription(HAP_IN);
  164.   myESP.addSubscription(ESP_LINK_IN);
  165.     myESP.setMQTTCallback(incomingMQTT);
  166.     myESP.setWifiCallback(wifiConnected);
  167.     myESP.begin();
  168.  
  169.   DEBUG_MESSAGE("setup(): ESP HELPER initialization finished");
  170.  
  171.     //BEGIN Initialization of aceessories
  172.  
  173.   if(RL_INSTALLED)
  174.     RL.begin();
  175.  
  176.   if(RF_INSTALLED)
  177.     RF.begin();
  178.  
  179.   MNT.pauseMonitoring();
  180.  
  181.   DEBUG_MESSAGE("setup(): accessories initialization finished, setup() finished");
  182.  
  183. }
  184.  
  185. void wifiConnected(){
  186.  
  187.   DEBUG_MESSAGE("wifiConnected(): wifi connected, running second stage setup!");
  188.  
  189.     //SENDING "BOOTED" MSG TO MQTT BROKER
  190.  
  191.   //SEND "BOOTED MSG TO BROKER"
  192.   DEBUG_MESSAGE("wifiConnected(): MQTT BOOTED MSG transmitted!");
  193.  
  194.   runLoop(20);
  195.  
  196.     //GET NPT TIME AND SET UP RESTART SCHEDULE
  197.  
  198.   while(true){
  199.  
  200.     dateTime = NTPch.getNTPtime(1.0, 1);
  201.  
  202.     if(dateTime.valid){
  203.  
  204.       RST.setSchedule(((24-dateTime.hour+scheduleRestartAtHour)+scheduleRestartAtDay*24*60*60*1000));
  205.       RST.setBeforeRestartCallback(restartIncoming);
  206.       RST.setScheduledRestartTimeFailCallback(restartScheduleFailed);
  207.       if(useGpioForRestart)
  208.         RST.setGpioRestart(restartPin);
  209.       DEBUG_MESSAGE("wifiConnected(): NTP request get(): success! time calculation finished, RST manager running!");
  210.       break;
  211.  
  212.     }
  213.     else{
  214.       DEBUG_MESSAGE("wifiConnected(): NTP request get(): failed! running loop to maintain the system core");
  215.       runLoop(20);
  216.     }
  217.   }
  218.  
  219.   runLoop(20);
  220.  
  221.     //BEGIN HOSTING THE UPDATE server
  222.  
  223.   MDNS.begin(host);
  224.   httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  225.   httpServer.begin();
  226.   MDNS.addService("http", "tcp", 80);
  227.   DEBUG_MESSAGE("wifiConnected(): update setup finished!");
  228.  
  229.     //STARTING SYSTEM monitoring
  230.  
  231.   MNT.resumeMonitoring();
  232.  
  233.   if(useLED)
  234.     digitalWrite(INTERNAL_LED, false);
  235.  
  236.   DEBUG_MESSAGE("wifiConnected(): generic setup finished, wifiConneced(): finished!");
  237.  
  238. }
  239.  
  240. void loop(){
  241.  
  242.   maintainConnection();
  243.  
  244.   if(updateServerEnabled)
  245.     httpServer.handleClient();
  246.  
  247.   maintainConnection();
  248.  
  249.   MNT.run();
  250.   RST.run();
  251.   RL.run();
  252.  
  253.   maintainConnection();
  254.  
  255.   if(pingMetro.check()){
  256.     //PING
  257.     DEBUG_MESSAGE("pingMetro: MSG transmitted!");
  258.   }
  259.  
  260. }
  261.  
  262. void runLoop(uint8_t cycles){
  263.  
  264.   for(int i = 0; i <= cycles; i++){
  265.     loop();
  266.   }
  267.  
  268. }
  269.  
  270. void maintainConnection(){
  271.  
  272.   if(myESP.loop() == FULL_CONNECTION){
  273.  
  274.     if(!connectedForFirstTimeSinceBoot){
  275.       //SENDING "CONNECTED FROM BOOT" MSG TO MQTT BROKER
  276.       connectedForFirstTimeSinceBoot = true;
  277.       connected = true;
  278.       return;
  279.     }
  280.  
  281.     if(!connected){
  282.       //SENDING "CONNECTED" MSG TO MQTT BROKER
  283.       connected = true;
  284.     }
  285.  
  286.   }
  287.   else{
  288.  
  289.     if(connected){
  290.       connected = false;
  291.       timeOfLostConnection = millis();
  292.  
  293.     }
  294.   }
  295.  
  296. }
  297.  
  298. void restartIncoming(){
  299.  
  300.   DEBUG_MESSAGE("restartIncoming(): triggered, preparing for restart!");
  301.  
  302.   //SENDING "RESTARTING" MSG TO MQTT BROKER
  303.  
  304.   DEBUG_MESSAGE("restartIncoming(): restarting now!");
  305.  
  306. }
  307.  
  308. void restartScheduleFailed(){
  309.  
  310.   //SENDING "RESTART_FAILED" MSG TO MQTT BROKER
  311.   DEBUG_MESSAGE("restartScheduleFailed(): Restart failed, unhalted exception, system stability voided, upcoming: millis() overflow, nothing is certain!!!");
  312.  
  313. }
  314.  
  315. void DEBUG_MESSAGE(char *text){
  316.  
  317.   if(useSerialDebug)
  318.     Serial.println(text);
  319.  
  320.   if(useMQTTDebug)
  321.     myESP.publish(ESP_MQTT_DEBUG_SERIAL, text, false);
  322.  
  323. }
  324.  
  325.  
  326.  
  327.  
  328. // FROM THIS LINE ON IT IS NOT WORKING LOL --------
  329.  
  330.  
  331.  
  332.  
  333.  
  334. void incomingMQTT(char* topic, byte* payload, unsigned int length){
  335.  
  336. }
  337.  
  338. void sendMQTT(int enumInput){
  339.  
  340.  
  341.  
  342. }
  343.  
  344. void sendMQTTjson(uint8_t sendMQTTmsgID, uint8_t MQTTmsgParameter){
  345.  
  346.   Serial.println("Decice is packing the payload to JSON file: sendMQTTmsgID, MQTTmsgParameter");
  347.  
  348.   StaticJsonBuffer<MQTT_TRANSMITTER_BUFFER_SIZE> jsonTransmitterBuffer;
  349.   JsonObject& json = jsonTransmitterBuffer.createObject();
  350.   json["macAdress"] = macAdress;
  351.  
  352.   if(sendMQTTmsgID == 1) {     json["RST_CONFIRM"] = true;                 }
  353.   if(sendMQTTmsgID == 2) {     json["PING_RESPONCE"] = true;               }
  354.   if(sendMQTTmsgID == 3) {     json["OTA_CONFIRM"] = true;                 }
  355.   if(sendMQTTmsgID == 4) {     json["BOOTED"] = true;                      }
  356.   if(sendMQTTmsgID == 5) {     json["ERR"] = true;                         }
  357.   if(sendMQTTmsgID == 6) {     json["RECONNECTED"] = true;                 }
  358.   if(sendMQTTmsgID == 7) {     json["HAP"] = MQTTmsgParameter;             }
  359.  
  360.   char charBuffer[100];
  361.   json.printTo(charBuffer);q
  362.   myESP.publish(ESP_LINK_OUT, charBuffer, false);
  363.   jsonTransmitterBuffer.clear();
  364.  
  365. }
  366.  
  367. void processIncomingMQTTjson(char* topic, byte* payload, unsigned int length){
  368.  
  369.   StaticJsonBuffer<MQTT_PROCESSING_BUFFER_SIZE> jsonPayloadBuffer;
  370.   JsonObject& jsonPayload = jsonPayloadBuffer.parseObject(payload);
  371.  
  372.   String tmpMacAdressJsonString = jsonPayload["macAdress"];
  373.   bool globalMsg = jsonPayload["globalMsg"];
  374.  
  375.   if(!globalMsg && !(tmpMacAdressJsonString == macAdress)){
  376.     Serial.println("this packed is not going to be executed - it isnt adressed to this device...");
  377.     Serial.println(macAdress);
  378.     Serial.println(tmpMacAdressJsonString);
  379.     return;
  380.   }
  381.  
  382.   jsonPayloadBuffer.clear();    //unalloc the json struct
  383.  
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement