From d58cfdac8ac8849c1c80da69b5996d0556d49c80 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Wed, 3 Jul 2019 14:55:21 -0400 Subject: [PATCH 1/5] write microcontroller code --- .../area_dht11_esp8266/area_dht11_esp8266.ino | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino diff --git a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino new file mode 100644 index 00000000..b2e74ab2 --- /dev/null +++ b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino @@ -0,0 +1,60 @@ +#include +#include +#include +// TODO did we alter MQTT_KEEPALIVE ? See ph_temp_sensor_esp8266 +#include + +// WIFI SETUP +const char* ssid = "ssid"; +const char* password = "password"; +WiFiClient wifi_client; + +// MQTT SETUP +const char* mqtt_broker = "broker"; +const char* mqtt_topic = "namespace/sensors"; +PubSubClient mqtt_client(wifi_client); + +#define MQTT_MESSAGE_SIZE 128 +char mqtt_message[MQTT_MESSAGE_SIZE]; + +// HUMIDITY & TEMP SENSOR +DHTesp dht; + +const int MEASUREMENT_FREQ_MS = dht.getMinimumSamplingPeriod() * 2; + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println(ARDUINO_BOARD); + Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)"); + + + dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 4 +} + +void loop() +{ + delay(MEASUREMENT_FREQ_MS); + + float humidity = dht.getHumidity(); + float temperature = dht.getTemperature(); + + Serial.print(dht.getStatusString()); + Serial.print("\t"); + Serial.print(humidity, 1); + Serial.print(" H%"); + Serial.print("\t\t"); + Serial.print(temperature, 1); + Serial.print(" C"); + Serial.print("\t\t"); + Serial.print(dht.toFahrenheit(temperature), 1); + Serial.print(" F"); + Serial.print("\t\t"); + Serial.print(dht.computeHeatIndex(temperature, humidity, false), 1); + Serial.print(" H%C"); + Serial.print("\t\t"); + Serial.print(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1); + Serial.println(" H%F"); +} + From c961dddad2edae0ed6f68e987377696c1769a270 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Wed, 3 Jul 2019 15:15:57 -0400 Subject: [PATCH 2/5] weave in wifi and pubsub code --- .../area_dht11_esp8266/area_dht11_esp8266.ino | 107 ++++++++++++++++-- 1 file changed, 98 insertions(+), 9 deletions(-) diff --git a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino index b2e74ab2..853fa507 100644 --- a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino +++ b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino @@ -4,27 +4,53 @@ // TODO did we alter MQTT_KEEPALIVE ? See ph_temp_sensor_esp8266 #include -// WIFI SETUP + +// WARNING! YOU MUST REPLACE THE EXAMPLE VALUE BELOW +const char* DEVICE_ID = "b1c8ae88-8622-415f-951e-27a21888fe19"; + + +// WIFI CONFIGURABLE CONSTANTS const char* ssid = "ssid"; const char* password = "password"; -WiFiClient wifi_client; -// MQTT SETUP + +// MQTT CONFIGURABLE CONSTANTS const char* mqtt_broker = "broker"; +const int mqtt_port = 1883; const char* mqtt_topic = "namespace/sensors"; -PubSubClient mqtt_client(wifi_client); + +// WIFI VARS +WiFiClient wifi_client; + +// PUBSUB VARS +PubSubClient mqtt_client(wifi_client); #define MQTT_MESSAGE_SIZE 128 char mqtt_message[MQTT_MESSAGE_SIZE]; +#define MQTT_RETRY_MS 5000 -// HUMIDITY & TEMP SENSOR +// HUMIDITY & TEMP SENSOR VARS DHTesp dht; const int MEASUREMENT_FREQ_MS = dht.getMinimumSamplingPeriod() * 2; -void setup() -{ - Serial.begin(115200); +void setup_wifi(void) { + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(333); + Serial.println("...waiting for Wi-Fi connection..."); + } + + Serial.println(""); + Serial.print("Wireless network SSID: "); + Serial.println(ssid); + Serial.print("Local IP address: "); + Serial.println(WiFi.localIP()); + Serial.println(""); +} + +void setup_dht_sensor(void) { Serial.println(); Serial.println(ARDUINO_BOARD); Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)"); @@ -33,7 +59,49 @@ void setup() dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 4 } -void loop() +void setup_mqtt(void) { + // We'll need to randomly generate a client ID later. + randomSeed(micros()); + + mqtt_client.setServer(mqtt_broker, mqtt_port); +} + + +// Thanks to https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_esp8266/mqtt_esp8266.ino +void connect_mqtt(void) { + // Loop until we're connected + while (!mqtt_client.connected()) { + Serial.print("Connecting to MQTT broker..."); + // Create a random client ID + String clientId = "dht11_"; + clientId += String(random(0xffff), HEX); + + // Attempt to connect + if (mqtt_client.connect(clientId.c_str())) { + Serial.println("MQTT connected"); + } else { + Serial.print("MQTT connection failed, rc="); + Serial.print(mqtt_client.state()); + Serial.println(" ...trying again in 5 seconds"); + + delay(MQTT_RETRY_MS); + } + } +} + + +void setup(void) +{ + Serial.begin(115200); + + setup_wifi(); + + setup_mqtt(); + + setup_dht_sensor(); +} + +void loop(void) { delay(MEASUREMENT_FREQ_MS); @@ -56,5 +124,26 @@ void loop() Serial.print("\t\t"); Serial.print(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1); Serial.println(" H%F"); + + + if (!mqtt_client.connected()) { + connect_mqtt(); + } + mqtt_client.loop(); + + // publish formatted message to MQTT topic + snprintf( + mqtt_message, + MQTT_MESSAGE_SIZE, + "{ \"device_id\": \"%s\", \"temp_c\": %.2f, \"temp_f\": %.2f, \"humidity\": %.2f, \"heat_index_c\": %.2f, \"heat_index_f\": %.2f }", + DEVICE_ID, // snprintf wants a const char* + temperature, + dht.toFahrenheit(temperature), + humidity, + dht.computeHeatIndex(temperature, humidity, false), + dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true) + ); + + mqtt_client.publish(mqtt_topic, mqtt_message); } From f4eaa503fca6d943e57ff47c7bcd6b9000b031e4 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Wed, 3 Jul 2019 18:05:48 -0400 Subject: [PATCH 3/5] nits --- microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino | 3 +++ .../ph_temp_sensor_esp8266/ph_temp_sensor_esp8266.ino | 2 ++ 2 files changed, 5 insertions(+) diff --git a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino index 853fa507..0af95a45 100644 --- a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino +++ b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino @@ -99,6 +99,9 @@ void setup(void) setup_mqtt(); setup_dht_sensor(); + + Serial.print("device_id "); + Serial.println(DEVICE_ID); } void loop(void) diff --git a/microcontrollers/ph_temp_sensor_esp8266/ph_temp_sensor_esp8266.ino b/microcontrollers/ph_temp_sensor_esp8266/ph_temp_sensor_esp8266.ino index f38a90ae..fc1b32c9 100644 --- a/microcontrollers/ph_temp_sensor_esp8266/ph_temp_sensor_esp8266.ino +++ b/microcontrollers/ph_temp_sensor_esp8266/ph_temp_sensor_esp8266.ino @@ -10,6 +10,8 @@ // to be set as `#define MQTT_KEEPALIVE 60`. This is done in an effort // to allow the time-intensive scrolling process not take so long that // it exceeds the default keepalive in PubSubClient.h. +// If you're using the Arduino IDE, this can usually be found in +// ~/Documents/Arduino/libraries/PubSubClient/PubSubClient.h #include // WIFI SETUP From 0c238371683afac7b6cf9c288033f51335f33acc Mon Sep 17 00:00:00 2001 From: Terkwood Date: Wed, 3 Jul 2019 18:07:53 -0400 Subject: [PATCH 4/5] rearrange things, add note --- .../area_dht11_esp8266/area_dht11_esp8266.ino | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino index 0af95a45..1a07e9a9 100644 --- a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino +++ b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino @@ -1,7 +1,14 @@ #include #include #include -// TODO did we alter MQTT_KEEPALIVE ? See ph_temp_sensor_esp8266 +// NB. We have altered the `#define MQTT_KEEPALIVE 15` in PubSubClient.h +// to be set as `#define MQTT_KEEPALIVE 60`. This is done in an effort +// to allow the time-intensive scrolling process not take so long that +// it exceeds the default keepalive in PubSubClient.h. +// If you're using the Arduino IDE, this can usually be found in +// ~/Documents/Arduino/libraries/PubSubClient/PubSubClient.h +// WE ALSO MODIFY MQTT MAX PACKET SIZE +// `#define MQTT_MAX_PACKET_SIZE 256` #include @@ -25,7 +32,7 @@ WiFiClient wifi_client; // PUBSUB VARS PubSubClient mqtt_client(wifi_client); -#define MQTT_MESSAGE_SIZE 128 +#define MQTT_MESSAGE_SIZE 256 char mqtt_message[MQTT_MESSAGE_SIZE]; #define MQTT_RETRY_MS 5000 @@ -51,11 +58,6 @@ void setup_wifi(void) { } void setup_dht_sensor(void) { - Serial.println(); - Serial.println(ARDUINO_BOARD); - Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)"); - - dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 4 } @@ -64,6 +66,13 @@ void setup_mqtt(void) { randomSeed(micros()); mqtt_client.setServer(mqtt_broker, mqtt_port); + + Serial.print("Publishing to broker "); + Serial.print(mqtt_broker); + Serial.print(":"); + Serial.print(mqtt_port); + Serial.print(" with topic "); + Serial.println(mqtt_topic); } @@ -100,46 +109,31 @@ void setup(void) setup_dht_sensor(); - Serial.print("device_id "); + Serial.print("prawnalith device_id "); Serial.println(DEVICE_ID); + Serial.println(); + Serial.println(ARDUINO_BOARD); } void loop(void) { delay(MEASUREMENT_FREQ_MS); - float humidity = dht.getHumidity(); - float temperature = dht.getTemperature(); - - Serial.print(dht.getStatusString()); - Serial.print("\t"); - Serial.print(humidity, 1); - Serial.print(" H%"); - Serial.print("\t\t"); - Serial.print(temperature, 1); - Serial.print(" C"); - Serial.print("\t\t"); - Serial.print(dht.toFahrenheit(temperature), 1); - Serial.print(" F"); - Serial.print("\t\t"); - Serial.print(dht.computeHeatIndex(temperature, humidity, false), 1); - Serial.print(" H%C"); - Serial.print("\t\t"); - Serial.print(dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true), 1); - Serial.println(" H%F"); - - if (!mqtt_client.connected()) { connect_mqtt(); } mqtt_client.loop(); + float humidity = dht.getHumidity(); + float temperature = dht.getTemperature(); + // publish formatted message to MQTT topic snprintf( mqtt_message, MQTT_MESSAGE_SIZE, - "{ \"device_id\": \"%s\", \"temp_c\": %.2f, \"temp_f\": %.2f, \"humidity\": %.2f, \"heat_index_c\": %.2f, \"heat_index_f\": %.2f }", + "{ \"device_id\": \"%s\", \"status\": \"%s\", \"temp_c\": %.2f, \"temp_f\": %.2f, \"humidity\": %.2f, \"heat_index_c\": %.2f, \"heat_index_f\": %.2f }", DEVICE_ID, // snprintf wants a const char* + dht.getStatusString(), temperature, dht.toFahrenheit(temperature), humidity, @@ -147,6 +141,12 @@ void loop(void) dht.computeHeatIndex(dht.toFahrenheit(temperature), humidity, true) ); - mqtt_client.publish(mqtt_topic, mqtt_message); -} + bool publish_result = mqtt_client.publish(mqtt_topic, mqtt_message); + + if (!publish_result) { + Serial.println("MQTT publish failed. Message too large? Check that PubSubClient.h has the following workaround:"); + Serial.println("#define MQTT_MAX_PACKET_SIZE 256"); + } + Serial.println(mqtt_message); +} From af7e08e419b92b4ca7dbb82c8c5115f1475bcb36 Mon Sep 17 00:00:00 2001 From: Terkwood Date: Wed, 3 Jul 2019 18:10:15 -0400 Subject: [PATCH 5/5] add warning --- microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino index 1a07e9a9..a8822410 100644 --- a/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino +++ b/microcontrollers/area_dht11_esp8266/area_dht11_esp8266.ino @@ -32,6 +32,11 @@ WiFiClient wifi_client; // PUBSUB VARS PubSubClient mqtt_client(wifi_client); +// WARNING WARNING WARNING +// WARNING WARNING WARNING +// PubSubClient.h must be modified to support the next setting! +// ~/Documents/Arduino/libraries/PubSubClient/PubSubClient.h +// #define MQTT_MAX_PACKET_SIZE 256 #define MQTT_MESSAGE_SIZE 256 char mqtt_message[MQTT_MESSAGE_SIZE]; #define MQTT_RETRY_MS 5000