IoT sensor/controller using STM32, W5500 ethernet, MQTT

Dependencies:   mbed WIZnet_Library Watchdog DHT MQTT DS1820

Revision:
9:a8098e772b48
Parent:
8:6d7be3bed961
Child:
10:3ad12f8d8b46
--- a/main.cpp	Wed Feb 26 23:36:16 2020 +0000
+++ b/main.cpp	Thu Feb 27 23:05:42 2020 +0000
@@ -5,6 +5,7 @@
 #include "WIZnetInterface.h"
 #include "MQTTSocket.h"
 #include "MQTTClient.h"
+#include "DHT.h"
 
 // ========== PIN DEFINITIONS ============
 // TODO move pin definitions into separate file
@@ -44,6 +45,8 @@
 // ================= *************** ==================
 // sensor inputs
 #define ONEWIRE_PIN     D_37  // pin 15 on Extension
+#define DHT_PIN0         PB_10  // D29 pin 5 on UEXT // pin 7 on Extension
+#define DHT_PIN1         PB_11  // D30 pin 6 on UEXT // pin 8 on Extension
 #define MAX_TEMP_PROBES 4
 // ================= *************** ==================
 #define NODE_NAME "controller03" // TODO just define node number
@@ -61,6 +64,11 @@
 DigitalIn button(BUTTON);
 DigitalOut led(LED_GREEN);
 
+DHT dht0(DHT_PIN0, DHT22);
+DHT dht1(DHT_PIN1, DHT22);
+float temp[2];
+float humidity[2];
+
 Watchdog wd;
 Ticker tick_60sec;
 Ticker tick_5sec;
@@ -68,6 +76,7 @@
 Ticker tick_500ms;
 
 bool flag_publish;
+bool flag_read_temps;
 
 typedef MQTT::Client<MQTTSocket,Countdown> MClient;
 
@@ -211,15 +220,28 @@
 }
 
 
-void read_temp(MClient &client) {/*
+void read_temps(MClient &client) {
+    int error = dht0.readData();
+    if (0 == error) {
+        temp[0]      = dht0.ReadTemperature(CELCIUS);
+        humidity[0]  = dht0.ReadHumidity();
+        pc.printf("Temperature: %3.1f, Humidity: %3.1f\n", temp, humidity);
+    } else {
+        pc.printf("DHT read error: %d\n", error);
+    }
+    // convert to string and publish
+    char temp_str[6];
+    sprintf(temp_str, "%3.1f", temp[0]);
+    connected = publish_value(client,"temp0",temp_str);
+    char humidity_str[6];
+    sprintf(humidity_str, "%3.1f", humidity[0]);
+    connected = publish_value(client,"humidity0",humidity_str);
+/*
     probe.convertTemperature(true, DS1820::all_devices);  //Start temp conversion, wait until ready
     temp = probe.temperature();
     pc.printf("Temp0 is %3.1foC\r\n", temp);
-    // convert to string and publish
-    char temp_str[6];
-    sprintf(temp_str, "%3.1f", temp);
-    connected = publish_value(client,"temp0",temp_str);
-*/}
+*/
+}
 
 
 int networking_init(MQTTSocket &sock, MClient &client, WIZnetInterface &wiz) {
@@ -265,6 +287,7 @@
 void every_5sec() {
     // no waits or blocking routines here please!
     flag_publish = 1;
+    flag_read_temps = 1;
 }
 
 void every_second() {
@@ -272,7 +295,9 @@
     uptime_sec++;
     if(connected == 0) {
         led = !led;
-    }}
+    }
+    wd.Service();       // kick the dog before the timeout
+}
 
 void every_500ms() {
     // no waits or blocking routines here please!
@@ -283,16 +308,16 @@
 
 int main()
 {
-    WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, NC); // SPI1 with no reset
-//    WIZnetInterface wiz(PB_15, PB_14, PB_13, PB_12, NC); // SPI2 with no reset
+//    WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, NC); // SPI1 with no reset
+    WIZnetInterface wiz(PB_15, PB_14, PB_13, PB_12, NC); // SPI2 with no reset
     MQTTSocket sock;
     MClient client(sock);
     
-    wd.Configure(20.0);
     tick_500ms.attach(&every_500ms, 0.5);
     tick_1sec.attach(&every_second, 1.0);
     tick_5sec.attach(&every_5sec, 5.0);
     tick_60sec.attach(&every_60sec, 60);
+    wd.Configure(30.0);
     
     //pulse all outputs
     for(int i=0; i<NUM_OUTPUTS; i++) {
@@ -305,7 +330,6 @@
     connected = networking_init(sock, client, wiz);
     
     while(1) {
-        wd.Service();       // kick the dog before the timeout
         read_inputs(client);
         
         if(connected != 0) {
@@ -319,6 +343,10 @@
                 publish_info(client);
                 flag_publish = 0;
             }
+            else if(flag_read_temps) {
+                read_temps(client);
+                flag_read_temps = 0;
+            }
         }
         
         client.yield(500);  // pause a while, yawn......