Skip to content

Commit 46e6da8

Browse files
committed
write in C++ for better IDE support
1 parent a8001b8 commit 46e6da8

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
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 = 19200
15+
monitor_speed = 9600
1616
lib_extra_dirs = ~/Documents/Arduino/libraries
1717
lib_deps = plerup/EspSoftwareSerial@^6.9.0

src/main.ino renamed to src/main.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
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

1918
const int MQTT_PORT = 8883;
2019
const char MQTT_SUB_TOPIC[] = "$aws/things/" THINGNAME "/shadow/sub";
@@ -37,6 +36,15 @@ unsigned long lastMillis = 0;
3736
time_t now;
3837
time_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+
4048
void 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

105106
void 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

119118
void checkWiFiThenMQTT(void)

0 commit comments

Comments
 (0)