-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This hopefully fixes many confusing errors regarding undefined functions. Moving away from .h files makes it a lot easier for those files to call each other. Unfortunately, this comes at a cost of code size: link-time optimization (inlining etc. across compilation units) hasn't been enabled for the ESP8266 port yet. See: esp8266/Arduino#23
- Loading branch information
Showing
20 changed files
with
541 additions
and
470 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
|
||
#pragma once | ||
|
||
#include <ArduinoJson.h> | ||
|
||
void colorLightSend(uint8_t *arg); | ||
void colorLightReceive(JsonObject &value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
|
||
#include <ArduinoJson.h> | ||
|
||
#include "mqtt.h" | ||
#include "config.h" | ||
#include "colorlight.h" | ||
|
||
WiFiClient mqttClient; | ||
PubSubClient mqtt(MQTT_HOST, MQTT_PORT, mqttCallback, mqttClient); | ||
|
||
const char *MQTT_MSG_ONLINE = "online"; | ||
const char *MQTT_MSG_OFFLINE = "offline"; | ||
|
||
bool mqttWasConnected = false; | ||
uint32_t mqttLastTry = 0; | ||
|
||
void log(String line) { | ||
Serial.println(line); | ||
#ifdef MQTT_LOG | ||
line = String(F(CLIENT_ID ": ")) + line; | ||
mqtt.publish(MQTT_LOG, line.c_str()); | ||
#endif | ||
} | ||
|
||
void mqttLoop() { | ||
if (!mqtt.loop()) { | ||
// Not connected, but connection available. | ||
// Try to reconnect, with a second delay between each try. | ||
uint32_t currentMillis = millis(); | ||
if (mqttLastTry == 0 || currentMillis - mqttLastTry > 5000) { | ||
mqttLastTry = currentMillis; | ||
if (mqtt.connect(CLIENT_ID, MQTT_LOG, 1, false, MQTT_MSG_OFFLINE)) { | ||
mqtt.publish(MQTT_LOG, MQTT_MSG_ONLINE); | ||
if (!mqtt.subscribe(MQTT_PREFIX "a/+", 1)) { | ||
log(F("failed to subscribe")); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void mqttCallback(char *topic, byte *payload, unsigned int length) { | ||
if (length < 1) { | ||
// Odd packet | ||
Serial.println("MQTT: got zero-length payload"); | ||
return; | ||
} | ||
|
||
// Turn it into a C string (needed for the JSON parser). | ||
char json[length+1]; | ||
json[length] = 0; | ||
memcpy(json, payload, length); | ||
|
||
DynamicJsonBuffer jsonBuffer; | ||
JsonObject& root = jsonBuffer.parseObject(json); | ||
if (!root.success()) { | ||
log(F("colorlight: could not parse JSON")); | ||
return; | ||
} | ||
JsonObject& value = root["value"]; | ||
if (!value.success()) { | ||
log(F("colorlight: no 'value' key")); | ||
return; | ||
} | ||
|
||
const char *origin = root["origin"]; | ||
if (origin != NULL && strcmp(origin, CLIENT_ID) == 0) { | ||
// Received my own message. | ||
return; | ||
} | ||
|
||
if (strcmp(topic, MQTT_PREFIX "a/colorlight") == 0) { | ||
colorLightReceive(value); | ||
} else { | ||
log(String("unknown actuator: ") + topic); | ||
} | ||
} |
Oops, something went wrong.