Skip to content

Commit

Permalink
publish status topic, clear with will
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpiatek committed Feb 10, 2018
1 parent 964f9ce commit a286da6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions sample_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// broker password or blank
#define BROKER_PASS ""
// base topic to publish on (will append "/<wifi MAC id>/temp")
// status (1 or 0) published as will to topic (TOPIC_ROOT/<wifi MAC id>/status)
#define TOPIC_ROOT "Devices"
// override default timeout from PubSubClient lib
#define MQTT_KEEPALIVE 60
Expand Down
9 changes: 6 additions & 3 deletions wemos_temp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ WiFiClient espClient;
PubSubClient client(espClient);
String clientId;
String topic;
String willTopic;

void wait10(){
for(int i=0; i<10; i++){
Expand All @@ -72,7 +73,8 @@ void setup(void)
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
topic = String(TOPIC_ROOT)+"/"+WiFi.macAddress()+"/temp";
Serial.print("TOPIC: "+topic);
willTopic = String(TOPIC_ROOT)+"/"+WiFi.macAddress()+"/status";
Serial.println("TOPIC: "+topic);

//set mqtt
clientId = WiFi.macAddress();
Expand Down Expand Up @@ -117,8 +119,9 @@ void checkComms(){
Serial.print("MQTT connecting...");
while (!client.connected()) {
Serial.print(".");
if (client.connect(clientId.c_str(), "use-token-auth", BROKER_PASS)) {
if (client.connect(clientId.c_str(), "use-token-auth", BROKER_PASS, willTopic.c_str(), 0, 1, "0")) {
Serial.println("connected");
client.publish (willTopic.c_str(), "1", true);
}else {
Serial.print("failed, rc=");
Serial.println(client.state());
Expand All @@ -139,7 +142,7 @@ void loop(void)
Serial.println("C");

//TODO something with boolean RC
client.publish (topic.c_str(), String(tempC,2).c_str(), false);
client.publish (topic.c_str(), String(tempC,2).c_str(), true);


//TODO #def - also how much does this affect an accurate reading
Expand Down

0 comments on commit a286da6

Please sign in to comment.