11#include < ESP8266WiFi.h>
22#include < WiFiClientSecure.h>
3+ #include < SoftwareSerial.h>
34#include < PubSubClient.h>
45#include < ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson (use v6.xx)
56#include < time.h>
7+
8+ #define DEBUG true
69#define emptyString String ()
710
811#include " secrets.h"
912
10- const int MQTT_PORT = 8883 ;
13+ const int MQTT_PORT = 8883 ;
1114const char MQTT_SUB_TOPIC[] = " $aws/things/" THINGNAME " /shadow/sub" ;
1215const char MQTT_PUB_TOPIC[] = " $aws/things/" THINGNAME " /shadow/pub" ;
1316
1417uint8_t DST = 0 ;
1518WiFiClientSecure net;
19+ SoftwareSerial UnoBoard (10 , 11 ); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
20+ // This means that you need to connect the TX line from the esp to the Arduino's pin 2
21+ // and the RX line from the esp to the Arduino's pin 3
1622
1723BearSSL::X509List cert (cacert);
1824BearSSL::X509List client_crt (client_cert);
@@ -27,6 +33,8 @@ time_t nowish = 1510592825;
2733void NTPConnect (void )
2834{
2935 Serial.print (" Setting time using SNTP" );
36+ sendDataToUno (" Setting time using SNTP\r\n " , 1000 , DEBUG);
37+
3038 configTime (TIME_ZONE * 3600 , DST * 3600 , " pool.ntp.org" , " time.nist.gov" );
3139 now = time (nullptr );
3240
@@ -37,6 +45,7 @@ void NTPConnect(void)
3745 }
3846
3947 Serial.println (" done!" );
48+ sendDataToUno (" done!\r\n " , 1000 , DEBUG);
4049 struct tm timeinfo;
4150 gmtime_r (&now, &timeinfo);
4251
@@ -60,9 +69,12 @@ void messageReceived(char *topic, byte *payload, unsigned int length)
6069void connectToMqtt (bool nonBlocking = false )
6170{
6271 Serial.print (" MQTT connecting " );
72+ sendDataToUno (" MQTT connecting \r\n " , 1000 , DEBUG);
73+
6374 while (!client.connected ()) {
6475 if (client.connect (THINGNAME)) {
6576 Serial.println (" connected!" );
77+ sendDataToUno (" connected! \r\n " , 1000 , DEBUG);
6678 if (!client.subscribe (MQTT_SUB_TOPIC)) {
6779 Serial.println (client.state ());
6880 }
@@ -99,10 +111,12 @@ void connectToWiFi(String init_str)
99111void checkWiFiThenMQTT (void )
100112{
101113 connectToWiFi (" Checking WiFi" );
114+ sendDataToUno (" Checking WiFi \r\n " , 1000 , DEBUG);
115+
102116 connectToMqtt ();
103117}
104118
105- void sendData (void )
119+ void sendDataToAWS (void )
106120{
107121 DynamicJsonDocument jsonBuffer (JSON_OBJECT_SIZE (3 ) + 100 );
108122
@@ -111,9 +125,8 @@ void sendData(void)
111125 JsonObject state_reported = state.createNestedObject (" reported" );
112126
113127 // read data coming from Uno board
114- // state_reported["values"] = Serial.read();
115- state_reported[" values" ] = Serial.readString ();
116- // state_reported["values"] = Serial.readStringUntil('\r\n');
128+
129+ state_reported[" values" ] = Serial.readStringUntil (' \r\n ' );
117130
118131 Serial.printf (" Sending [%s]: " , MQTT_PUB_TOPIC);
119132 serializeJson (root, Serial);
@@ -126,9 +139,34 @@ void sendData(void)
126139 }
127140}
128141
142+ String sendDataToUno (String command, const int timeout, boolean debug)
143+ {
144+ String response = " " ;
145+ UnoBoard.print (command); // send the read character to the Uno
146+ long int time = millis ();
147+
148+ while ( (time+timeout) > millis ()) {
149+ while (UnoBoard.available ()) {
150+ // The esp has data so display its output to the serial window
151+ char c = UnoBoard.read (); // read the next character.
152+ response+=c;
153+ }
154+ }
155+
156+ if (debug) {
157+ Serial.print (response);
158+ }
159+
160+ return response;
161+ }
162+
163+
129164void setup ()
130165{
131166 Serial.begin (9600 );
167+ Serial.println (" starting setup" );
168+
169+ UnoBoard.begin (9600 ); // your esp's baud rate might be different
132170 delay (5000 );
133171
134172 WiFi.hostname (THINGNAME);
@@ -157,7 +195,7 @@ void loop()
157195 client.loop ();
158196 if (millis () - lastMillis > 5000 ) {
159197 lastMillis = millis ();
160- sendData ();
198+ sendDataToAWS ();
161199 }
162200 }
163201}
0 commit comments