IoT sensor/controller using STM32, W5500 ethernet, MQTT
Dependencies: mbed WIZnet_Library Watchdog DHT MQTT DS1820
Revision 14:0a3c670b3862, committed 2020-03-04
- Comitter:
- Geekshow
- Date:
- Wed Mar 04 14:12:34 2020 +0000
- Parent:
- 12:bcb38c1af703
- Commit message:
- Reduced DHT measurement to 30sec, reverted MQTT lib to Zhang fork
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Mar 03 00:22:19 2020 +0000
+++ b/main.cpp Wed Mar 04 14:12:34 2020 +0000
@@ -6,6 +6,7 @@
#include "MQTTSocket.h"
#include "MQTTClient.h"
+#define VERSION "v12"
// ========== PIN DEFINITIONS ============
// TODO move pin definitions into separate file
#define LED_GREEN PA_5
@@ -70,7 +71,7 @@
float humidity[1];
Watchdog wd;
-Ticker tick_60sec;
+Ticker tick_30sec;
Ticker tick_5sec;
Ticker tick_1sec;
Ticker tick_500ms;
@@ -86,8 +87,8 @@
enum IO_STATE{IO_ON, IO_OFF};
uint8_t mac_addr[6]={0x00, 0x00, 0x00, 0xBE, 0xEF, 0x03}; // TODO make last byte dynamic
-//const char* mqtt_broker = "192.168.10.4";
-const char* mqtt_broker = "192.168.1.99";
+const char* mqtt_broker = "192.168.10.4";
+//const char* mqtt_broker = "192.168.1.99";
const int mqtt_port = 1883;
unsigned long uptime_sec = 0;
int connected = -1;
@@ -258,15 +259,17 @@
char topic_str[6];
sprintf(temp_str, "%d", num_ds18b20);
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);
- publish_value(client,topic_str,temp_str, false);
+ if(num_ds18b20 > 0) {
+ //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);
+ publish_value(client,topic_str,temp_str, false);
+ }
}
}
@@ -301,13 +304,14 @@
// Node online message
publish_value(client, "alive","ON", false);
+ publish_value(client, "version", VERSION, true);
publish_value(client, "IPAddress", wiz.getIPAddress(), true);
pc.printf("Initialization done.\r\n");
return 0;
}
-void every_60sec() {
+void every_30sec() {
// no waits or blocking routines here please!
flag_read_dht = 1;
flag_read_ds18b20 = 1;
@@ -345,13 +349,18 @@
tick_500ms.attach(&every_500ms, 0.5);
tick_1sec.attach(&every_second, 1.0);
tick_5sec.attach(&every_5sec, 5.1);
- tick_60sec.attach(&every_60sec, 59);
+ tick_30sec.attach(&every_30sec, 29.5);
//pulse all outputs
for(int i=0; i<NUM_OUTPUTS; i++) {
outputs[i] = IO_OFF;
wait(0.2);
}
+
+ // pull high all inputs
+ for(int i=0; i<NUM_INPUTS; i++) {
+ inputs[i].mode(PullUp);
+ }
pc.printf("\n\nNode: %s\r\n", NODE_NAME);