IoT sensor/controller using STM32, W5500 ethernet, MQTT
Dependencies: mbed WIZnet_Library Watchdog DHT MQTT DS1820
Diff: main.cpp
- Revision:
- 11:2a397ea7acc8
- Parent:
- 10:3ad12f8d8b46
- Child:
- 12:bcb38c1af703
diff -r 3ad12f8d8b46 -r 2a397ea7acc8 main.cpp --- a/main.cpp Sun Mar 01 11:11:22 2020 +0000 +++ b/main.cpp Tue Mar 03 00:15:32 2020 +0000 @@ -5,7 +5,6 @@ #include "WIZnetInterface.h" #include "MQTTSocket.h" #include "MQTTClient.h" -#include "DHT.h" // ========== PIN DEFINITIONS ============ // TODO move pin definitions into separate file @@ -44,10 +43,12 @@ // serial output? for uLCD // ================= *************** ================== // 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 +#include "DS1820.h" +#define MAX_PROBES 4 +#define ONEWIRE_PIN PB_11 // D30 pin 6 on UEXT // pin 8 on Extension +DS1820* probe[MAX_PROBES]; +#include "DHT.h" +#define DHT_PIN PB_10 // D29 pin 5 on UEXT // pin 7 on Extension // ================= *************** ================== #define NODE_NAME "controller03" // TODO just define node number @@ -64,10 +65,9 @@ DigitalIn button(BUTTON); DigitalOut led(LED_GREEN); -DHT dht0(DHT_PIN0, DHT22); -DHT dht1(DHT_PIN1, DHT22); -float temp[2]; -float humidity[2]; +DHT dht0(DHT_PIN, DHT22); +float temp[1]; +float humidity[1]; Watchdog wd; Ticker tick_60sec; @@ -76,7 +76,8 @@ Ticker tick_500ms; bool flag_publish; -bool flag_read_temps; +bool flag_read_dht; +bool flag_read_ds18b20; typedef MQTT::Client<MQTTSocket,Countdown> MClient; @@ -231,12 +232,12 @@ } -void read_temps(MClient &client) { +void read_dht(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); + pc.printf("Temperature: %3.1f, Humidity: %3.1f\n", temp[0], humidity[0]); } else { pc.printf("DHT read error: %d\n", error); return; @@ -248,11 +249,25 @@ char humidity_str[6]; sprintf(humidity_str, "%3.1f", humidity[0]); connected = publish_value(client,"humidity0",humidity_str, false); -/* - probe.convertTemperature(true, DS1820::all_devices); //Start temp conversion, wait until ready - temp = probe.temperature(); - pc.printf("Temp0 is %3.1foC\r\n", temp); -*/ +} + + +void read_ds18b20(MClient &client, int num_ds18b20) { + // Announce num of DS18B20 found + char temp_str[6]; + char topic_str[6]; + sprintf(temp_str, "%d", num_ds18b20); + connected = publish_value(client,"num_ds18b20",temp_str, false); + //Start temperature conversion, wait until ready + probe[0]->convertTemperature(true, DS1820::all_devices); + for (int i = 0; i<num_ds18b20; i++) { + float temp = probe[i]->temperature(); + pc.printf("Device %d returns %3.3foC\r\n", i, temp); + // convert to string and publish + sprintf(temp_str, "%3.3f", temp); + sprintf(topic_str, "probetemp%d", i); + connected = publish_value(client,topic_str,temp_str, false); + } } @@ -294,7 +309,8 @@ void every_60sec() { // no waits or blocking routines here please! - flag_read_temps = 1; + flag_read_dht = 1; + flag_read_ds18b20 = 1; } void every_5sec() { @@ -322,14 +338,14 @@ { wd.Configure(20.0); // 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(PB_15, PB_14, PB_13, PB_12, PC_6); // SPI2 with D35 reset MQTTSocket sock; MClient client(sock); 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); + tick_5sec.attach(&every_5sec, 5.1); + tick_60sec.attach(&every_60sec, 59); //pulse all outputs for(int i=0; i<NUM_OUTPUTS; i++) { @@ -341,6 +357,16 @@ wd.Service(); // kick the dog before the timeout connected = networking_init(sock, client, wiz); + + // Initialize DS18B20 probe array to DS1820 objects + int num_ds18b20 = 0; + while(DS1820::unassignedProbe(ONEWIRE_PIN)) { + probe[num_ds18b20] = new DS1820(ONEWIRE_PIN); + num_ds18b20++; + if (num_ds18b20 == MAX_PROBES) + break; + } + pc.printf("DS18B20: Found %d device(s)\r\n", num_ds18b20); while(1) { read_inputs(client); @@ -357,9 +383,13 @@ publish_info(client); flag_publish = 0; } - else if(flag_read_temps) { - read_temps(client); - flag_read_temps = 0; + else if(flag_read_dht) { + read_dht(client); + flag_read_dht = 0; + } + else if(flag_read_ds18b20) { + read_ds18b20(client, num_ds18b20); + flag_read_ds18b20 = 0; } }