44 @version 3.0
55*/
66
7+ #include < time.h>
8+ #include " secrets.h"
9+ #include < Arduino.h>
710#include < ESP8266WiFi.h>
8- #include < WiFiClientSecure.h>
9- #include < SoftwareSerial.h>
10- #include < PubSubClient.h>
1111#include < ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson (use v6.xx)
12- #include < time.h>
12+ #include < PubSubClient.h>
13+ #include < SoftwareSerial.h>
14+ #include < WiFiClientSecure.h>
1315
1416#define DEBUG true
15- #define emptyString String ()
16-
17- #include " secrets.h"
1817
1918const int MQTT_PORT = 8883 ;
2019const char MQTT_SUB_TOPIC[] = " $aws/things/" THINGNAME " /shadow/sub" ;
@@ -37,6 +36,15 @@ unsigned long lastMillis = 0;
3736time_t now;
3837time_t nowish = 1510592825 ;
3938
39+ // declaring custom function to follow C++ validation rules
40+ void connectToMqtt ();
41+ void NTPConnect (void );
42+ void sendDataToAWS (void );
43+ void checkWiFiThenMQTT (void );
44+ void connectToWiFi (String init_str);
45+ void messageReceived (char *topic, byte *payload, unsigned int length);
46+ String sendDataToUno (String command, const int timeout, boolean debug);
47+
4048void NTPConnect (void )
4149{
4250 Serial.print (" Setting time using SNTP" );
@@ -74,7 +82,7 @@ void messageReceived(char *topic, byte *payload, unsigned int length)
7482 Serial.println ();
7583}
7684
77- void connectToMqtt (bool nonBlocking = false )
85+ void connectToMqtt ()
7886{
7987 Serial.print (" MQTT connecting " );
8088 sendDataToUno (" MQTT connecting \r\n " , 1000 , DEBUG);
@@ -89,31 +97,22 @@ void connectToMqtt(bool nonBlocking = false)
8997 } else {
9098 Serial.print (" failed, reason -> " );
9199 Serial.println (client.state ());
92- if (!nonBlocking) {
93100 Serial.println (" < try again in 5 seconds" );
94101 delay (5000 );
95- } else {
96- Serial.println (" <" );
97- }
98- }
99- if (nonBlocking) {
100- break ;
101102 }
102103 }
103104}
104105
105106void connectToWiFi (String init_str)
106107{
107- if (init_str != emptyString) {
108- Serial.print (init_str);
109- }
108+ Serial.print (init_str);
109+
110110 while (WiFi.status () != WL_CONNECTED) {
111111 Serial.print (" ." );
112112 delay (1000 );
113113 }
114- if (init_str != emptyString) {
115- Serial.println (" ok!" );
116- }
114+
115+ Serial.println (" ok!" );
117116}
118117
119118void checkWiFiThenMQTT (void )
0 commit comments