Skip to content

Commit 0a8a035

Browse files
committed
exchanging data between the boards
1 parent 57db559 commit 0a8a035

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
platform = espressif8266
1313
board = esp01_1m
1414
framework = arduino
15-
monitor_speed = 9600
15+
monitor_speed = 19200
1616
lib_extra_dirs = ~/Documents/Arduino/libraries
1717
lib_deps = plerup/EspSoftwareSerial@^6.9.0

src/main.ino

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
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;
1114
const char MQTT_SUB_TOPIC[] = "$aws/things/" THINGNAME "/shadow/sub";
1215
const char MQTT_PUB_TOPIC[] = "$aws/things/" THINGNAME "/shadow/pub";
1316

1417
uint8_t DST = 0;
1518
WiFiClientSecure 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

1723
BearSSL::X509List cert(cacert);
1824
BearSSL::X509List client_crt(client_cert);
@@ -27,6 +33,8 @@ time_t nowish = 1510592825;
2733
void 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)
6069
void 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)
99111
void 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+
129164
void 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

Comments
 (0)