Skip to content

Commit c5e115d

Browse files
committed
code cleaning
1 parent d6d261d commit c5e115d

File tree

3 files changed

+20
-50
lines changed

3 files changed

+20
-50
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
6+
src/secrets.h
7+
scripts/flash.sh
8+
scripts/loadcode.txt

.vscode/extensions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
]
7+
}

src/main.ino

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void NTPConnect(void)
3636
now = time(nullptr);
3737
}
3838

39-
Serial.println("done!");
39+
Serial.println(" done!");
4040
struct tm timeinfo;
4141
gmtime_r(&now, &timeinfo);
4242

@@ -57,42 +57,18 @@ void messageReceived(char *topic, byte *payload, unsigned int length)
5757
Serial.println();
5858
}
5959

60-
void pubSubErr(int8_t MQTTErr)
61-
{
62-
if (MQTTErr == MQTT_CONNECTION_TIMEOUT)
63-
Serial.print("Connection tiemout");
64-
else if (MQTTErr == MQTT_CONNECTION_LOST)
65-
Serial.print("Connection lost");
66-
else if (MQTTErr == MQTT_CONNECT_FAILED)
67-
Serial.print("Connect failed");
68-
else if (MQTTErr == MQTT_DISCONNECTED)
69-
Serial.print("Disconnected");
70-
else if (MQTTErr == MQTT_CONNECTED)
71-
Serial.print("Connected");
72-
else if (MQTTErr == MQTT_CONNECT_BAD_PROTOCOL)
73-
Serial.print("Connect bad protocol");
74-
else if (MQTTErr == MQTT_CONNECT_BAD_CLIENT_ID)
75-
Serial.print("Connect bad Client-ID");
76-
else if (MQTTErr == MQTT_CONNECT_UNAVAILABLE)
77-
Serial.print("Connect unavailable");
78-
else if (MQTTErr == MQTT_CONNECT_BAD_CREDENTIALS)
79-
Serial.print("Connect bad credentials");
80-
else if (MQTTErr == MQTT_CONNECT_UNAUTHORIZED)
81-
Serial.print("Connect unauthorized");
82-
}
83-
8460
void connectToMqtt(bool nonBlocking = false)
8561
{
8662
Serial.print("MQTT connecting ");
8763
while (!client.connected()) {
8864
if (client.connect(THINGNAME)) {
8965
Serial.println("connected!");
9066
if (!client.subscribe(MQTT_SUB_TOPIC)) {
91-
pubSubErr(client.state());
67+
Serial.println(client.state());
9268
}
9369
} else {
9470
Serial.print("failed, reason -> ");
95-
pubSubErr(client.state());
71+
Serial.println(client.state());
9672
if (!nonBlocking) {
9773
Serial.println(" < try again in 5 seconds");
9874
delay(5000);
@@ -116,7 +92,7 @@ void connectToWiFi(String init_str)
11692
delay(1000);
11793
}
11894
if (init_str != emptyString) {
119-
Serial.println("ok!");
95+
Serial.println(" ok!");
12096
}
12197
}
12298

@@ -126,25 +102,6 @@ void checkWiFiThenMQTT(void)
126102
connectToMqtt();
127103
}
128104

129-
unsigned long previousMillis = 0;
130-
const long interval = 5000;
131-
132-
void checkWiFiThenMQTTNonBlocking(void)
133-
{
134-
connectToWiFi(emptyString);
135-
if (millis() - previousMillis >= interval && !client.connected()) {
136-
previousMillis = millis();
137-
connectToMqtt(true);
138-
}
139-
}
140-
141-
void checkWiFiThenReboot(void)
142-
{
143-
connectToWiFi("Checking WiFi");
144-
Serial.print("Rebooting");
145-
ESP.restart();
146-
}
147-
148105
void sendData(void)
149106
{
150107
DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(3) + 100);
@@ -164,7 +121,7 @@ void sendData(void)
164121

165122
serializeJson(root, shadow, sizeof(shadow));
166123
if (!client.publish(MQTT_PUB_TOPIC, shadow, false)) {
167-
pubSubErr(client.state());
124+
Serial.println(client.state());
168125
}
169126
}
170127

@@ -197,8 +154,6 @@ void loop()
197154
now = time(nullptr);
198155
if (!client.connected()) {
199156
checkWiFiThenMQTT();
200-
//checkWiFiThenMQTTNonBlocking();
201-
//checkWiFiThenReboot();
202157
} else {
203158
client.loop();
204159
if (millis() - lastMillis > 5000) {

0 commit comments

Comments
 (0)