IoT sensor/controller using STM32, W5500 ethernet, MQTT

Dependencies:   mbed WIZnet_Library Watchdog DHT MQTT DS1820

Committer:
Geekshow
Date:
Wed Mar 04 14:12:34 2020 +0000
Revision:
14:0a3c670b3862
Parent:
12:bcb38c1af703
Reduced DHT measurement to 30sec, reverted MQTT lib to Zhang fork

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zhangyx 0:1170747a672f 1 #include "mbed.h"
Geekshow 7:3c2222251deb 2 #include "Watchdog.h"
Geekshow 3:de9611d75590 3 //#include "rtos.h"
Geekshow 3:de9611d75590 4 //#include "pins.h"
Geekshow 4:ebaf1973d008 5 #include "WIZnetInterface.h"
Geekshow 4:ebaf1973d008 6 #include "MQTTSocket.h"
Geekshow 4:ebaf1973d008 7 #include "MQTTClient.h"
Geekshow 3:de9611d75590 8
Geekshow 14:0a3c670b3862 9 #define VERSION "v12"
Geekshow 3:de9611d75590 10 // ========== PIN DEFINITIONS ============
Geekshow 6:4d30bc5a8076 11 // TODO move pin definitions into separate file
Geekshow 4:ebaf1973d008 12 #define LED_GREEN PA_5
Geekshow 4:ebaf1973d008 13 #define LED_ORANGE PA_1 // Don't use! Shared with D3
Geekshow 3:de9611d75590 14 #define BUTTON PC_9
Geekshow 3:de9611d75590 15
Geekshow 6:4d30bc5a8076 16 #define A_0 PC_0 // Analogue Input 0
Geekshow 6:4d30bc5a8076 17 #define A_1 PC_1 // Analogue Input 1
Geekshow 6:4d30bc5a8076 18 #define A_2 PC_2 // Analogue Input 2
Geekshow 6:4d30bc5a8076 19 #define A_3 PC_3 // Analogue Input 3
Geekshow 6:4d30bc5a8076 20 #define A_4 PC_4 // Analogue Input 4
Geekshow 6:4d30bc5a8076 21 #define A_5 PC_5 // Analogue Input 5
Geekshow 3:de9611d75590 22
Geekshow 6:4d30bc5a8076 23 #define D_0 PA_3 // digital output D0
Geekshow 6:4d30bc5a8076 24 #define D_1 PA_2 // digital output D1
Geekshow 6:4d30bc5a8076 25 #define D_2 PA_0 // digital output D2
Geekshow 6:4d30bc5a8076 26 #define D_3 PA_1 // digital output D3
Geekshow 6:4d30bc5a8076 27 #define D_4 PB_5 // digital output D4
Geekshow 6:4d30bc5a8076 28 #define D_5 PB_6 // digital output D5
Geekshow 6:4d30bc5a8076 29 #define D_6 PA_8 // digital output D6
Geekshow 6:4d30bc5a8076 30 #define D_7 PA_9 // digital output D7
Geekshow 6:4d30bc5a8076 31 #define D_8 PA_10 // digital output D8
Geekshow 6:4d30bc5a8076 32 #define D_9 PB_7 // digital output D9
Geekshow 6:4d30bc5a8076 33 #define D_10 PA_4 // digital output D10 - SPI1 SS
Geekshow 6:4d30bc5a8076 34 #define D_11 PA_4 // digital output D11 - SPI1 MOSI
Geekshow 6:4d30bc5a8076 35 #define D_12 PA_4 // digital output D12 - SPI1 MISO
Geekshow 6:4d30bc5a8076 36 //#define D_13 PA_4 // digital output D13 - SPI1 CLK - GREEN LED
Geekshow 6:4d30bc5a8076 37 #define D_14 PB_8 // digital output D14
Geekshow 8:6d7be3bed961 38 #define D_35 PC_6 // pin 13 on Extension
Geekshow 8:6d7be3bed961 39 #define D_36 PC_7 // pin 14 on Extension
Geekshow 8:6d7be3bed961 40 #define D_37 PC_8 // pin 15 on Extension (last)
Geekshow 3:de9611d75590 41 // ================= *************** ==================
Geekshow 8:6d7be3bed961 42 #define USART3_TX PC_10 // D26 - pin 4 on Extension
Geekshow 6:4d30bc5a8076 43 // ================= *************** ==================
Geekshow 6:4d30bc5a8076 44 // serial output? for uLCD
Geekshow 6:4d30bc5a8076 45 // ================= *************** ==================
Geekshow 8:6d7be3bed961 46 // sensor inputs
Geekshow 11:2a397ea7acc8 47 #include "DS1820.h"
Geekshow 11:2a397ea7acc8 48 #define MAX_PROBES 4
Geekshow 11:2a397ea7acc8 49 #define ONEWIRE_PIN PB_11 // D30 pin 6 on UEXT // pin 8 on Extension
Geekshow 11:2a397ea7acc8 50 DS1820* probe[MAX_PROBES];
Geekshow 11:2a397ea7acc8 51 #include "DHT.h"
Geekshow 11:2a397ea7acc8 52 #define DHT_PIN PB_10 // D29 pin 5 on UEXT // pin 7 on Extension
Geekshow 3:de9611d75590 53 // ================= *************** ==================
Geekshow 4:ebaf1973d008 54 #define NODE_NAME "controller03" // TODO just define node number
Geekshow 3:de9611d75590 55
Geekshow 6:4d30bc5a8076 56 #define NUM_OUTPUTS 8
Geekshow 6:4d30bc5a8076 57 DigitalOut outputs[NUM_OUTPUTS] = {D_0, D_1, D_2, D_3, D_4, D_5, D_6, D_7};
Geekshow 6:4d30bc5a8076 58 #define NUM_INPUTS 6
Geekshow 6:4d30bc5a8076 59 DigitalIn inputs[NUM_INPUTS] = {PC_0, PC_1, PC_2, PC_3, PC_4, PC_5};
Geekshow 6:4d30bc5a8076 60 bool input_state[NUM_INPUTS];
Geekshow 6:4d30bc5a8076 61
Geekshow 6:4d30bc5a8076 62 Serial pc(USART3_TX, NC); // serial debug output on D26 (pin 4 of Extension)
Geekshow 6:4d30bc5a8076 63 //Serial pc(PA_9, NC); // serial debug output on D7
Geekshow 6:4d30bc5a8076 64 //Serial xxxxxx // find a serial port for Amp/uLCD connection
Geekshow 6:4d30bc5a8076 65
Geekshow 6:4d30bc5a8076 66 DigitalIn button(BUTTON);
Geekshow 6:4d30bc5a8076 67 DigitalOut led(LED_GREEN);
Geekshow 6:4d30bc5a8076 68
Geekshow 11:2a397ea7acc8 69 DHT dht0(DHT_PIN, DHT22);
Geekshow 11:2a397ea7acc8 70 float temp[1];
Geekshow 11:2a397ea7acc8 71 float humidity[1];
Geekshow 9:a8098e772b48 72
Geekshow 7:3c2222251deb 73 Watchdog wd;
Geekshow 14:0a3c670b3862 74 Ticker tick_30sec;
Geekshow 5:b2ae1ed8a30e 75 Ticker tick_5sec;
Geekshow 5:b2ae1ed8a30e 76 Ticker tick_1sec;
Geekshow 6:4d30bc5a8076 77 Ticker tick_500ms;
Geekshow 6:4d30bc5a8076 78
Geekshow 6:4d30bc5a8076 79 bool flag_publish;
Geekshow 11:2a397ea7acc8 80 bool flag_read_dht;
Geekshow 11:2a397ea7acc8 81 bool flag_read_ds18b20;
Geekshow 4:ebaf1973d008 82
Geekshow 4:ebaf1973d008 83 typedef MQTT::Client<MQTTSocket,Countdown> MClient;
Geekshow 4:ebaf1973d008 84
Geekshow 6:4d30bc5a8076 85 const char* ONOFF[] = {"ON", "OFF"};
Geekshow 4:ebaf1973d008 86 const char* OPENCLOSED[] = {"CLOSED", "OPEN"};
Geekshow 6:4d30bc5a8076 87 enum IO_STATE{IO_ON, IO_OFF};
Geekshow 4:ebaf1973d008 88
Geekshow 5:b2ae1ed8a30e 89 uint8_t mac_addr[6]={0x00, 0x00, 0x00, 0xBE, 0xEF, 0x03}; // TODO make last byte dynamic
Geekshow 14:0a3c670b3862 90 const char* mqtt_broker = "192.168.10.4";
Geekshow 14:0a3c670b3862 91 //const char* mqtt_broker = "192.168.1.99";
Geekshow 5:b2ae1ed8a30e 92 const int mqtt_port = 1883;
Geekshow 5:b2ae1ed8a30e 93 unsigned long uptime_sec = 0;
Geekshow 6:4d30bc5a8076 94 int connected = -1;
Geekshow 5:b2ae1ed8a30e 95
Geekshow 5:b2ae1ed8a30e 96
Geekshow 4:ebaf1973d008 97
Geekshow 6:4d30bc5a8076 98 void on_control_cmd(const char* topic, const char* message)
zhangyx 0:1170747a672f 99 {
Geekshow 4:ebaf1973d008 100 int new_state = 0;
Geekshow 6:4d30bc5a8076 101 pc.printf("Received CMD %s %s\r\n", topic, message);
Geekshow 6:4d30bc5a8076 102 // find out command first
Geekshow 6:4d30bc5a8076 103 if(strcmp(message, "ON") == 0) {
Geekshow 4:ebaf1973d008 104 pc.printf("ON value requested!\r\n");
Geekshow 6:4d30bc5a8076 105 new_state = IO_ON;
Geekshow 4:ebaf1973d008 106 }
Geekshow 6:4d30bc5a8076 107 else if(strcmp(message, "OFF") == 0) {
Geekshow 4:ebaf1973d008 108 pc.printf("OFF value requested!\r\n");
Geekshow 6:4d30bc5a8076 109 new_state = IO_OFF;
Geekshow 4:ebaf1973d008 110 }
Geekshow 4:ebaf1973d008 111 else {
Geekshow 6:4d30bc5a8076 112 pc.printf("Unknown command value specified!\r\n"); // TODO return current value on no message
Geekshow 4:ebaf1973d008 113 return;
Geekshow 4:ebaf1973d008 114 }
Geekshow 6:4d30bc5a8076 115 // are we updating an output?
Geekshow 6:4d30bc5a8076 116 if(strncmp(topic, "output", 6) == 0) {
Geekshow 6:4d30bc5a8076 117 // find out which output to apply it to
Geekshow 6:4d30bc5a8076 118 int output_num = int(topic[6])-48;
Geekshow 6:4d30bc5a8076 119 if(output_num >= NUM_OUTPUTS) {
Geekshow 6:4d30bc5a8076 120 pc.printf("ERROR: unknown output num %d\r\n", output_num);
Geekshow 6:4d30bc5a8076 121 }
Geekshow 6:4d30bc5a8076 122 else {
Geekshow 6:4d30bc5a8076 123 // turn something on/off!
Geekshow 6:4d30bc5a8076 124 pc.printf("Output: %d updated to %s\r\n", output_num, ONOFF[new_state]);
Geekshow 6:4d30bc5a8076 125 outputs[output_num] = new_state;
Geekshow 10:3ad12f8d8b46 126 flag_publish = 1; // workaround for below
Geekshow 10:3ad12f8d8b46 127 // publish_value(client, topic, ONOFF[new_state], false); // needs to access client :-/
Geekshow 6:4d30bc5a8076 128 }
Geekshow 6:4d30bc5a8076 129 }
Geekshow 6:4d30bc5a8076 130 else {
Geekshow 6:4d30bc5a8076 131 pc.printf("ERROR: Couldn't parse topic: %s\r\n", topic);
Geekshow 4:ebaf1973d008 132 }
Geekshow 4:ebaf1973d008 133 }
Geekshow 4:ebaf1973d008 134
Geekshow 4:ebaf1973d008 135 int publish(MClient& client, const char* msg_type, const char* point,
Geekshow 4:ebaf1973d008 136 const char* payload = NULL, size_t payload_len = 0,
Geekshow 4:ebaf1973d008 137 bool retain = false, MQTT::QoS qos = MQTT::QOS1){
Geekshow 4:ebaf1973d008 138 char topic[64];
Geekshow 4:ebaf1973d008 139 sprintf(topic, "%s/" NODE_NAME "/%s", msg_type, point);
Geekshow 4:ebaf1973d008 140 int ret = client.publish(topic, (void*)payload, payload_len, qos, retain);
Geekshow 4:ebaf1973d008 141 if(ret == -1) {
Geekshow 4:ebaf1973d008 142 pc.printf("ERROR during client.publish() = %d\r\n",ret);
Geekshow 4:ebaf1973d008 143 }
Geekshow 4:ebaf1973d008 144 return ret;
zhangyx 0:1170747a672f 145 }
zhangyx 2:a50b794b8ede 146
Geekshow 4:ebaf1973d008 147
Geekshow 4:ebaf1973d008 148 void messageArrived(MQTT::MessageData& md)
Geekshow 3:de9611d75590 149 {
Geekshow 6:4d30bc5a8076 150 // MQTT callback function
Geekshow 4:ebaf1973d008 151 MQTT::Message &message = md.message;
Geekshow 4:ebaf1973d008 152
Geekshow 4:ebaf1973d008 153 // copy message payload into local char array IMPROVE ME!
Geekshow 4:ebaf1973d008 154 char* payload = new char[message.payloadlen+1];
Geekshow 6:4d30bc5a8076 155 if(!payload) // will this ever happen?
Geekshow 4:ebaf1973d008 156 return;
Geekshow 4:ebaf1973d008 157 memcpy(payload, message.payload, message.payloadlen);
Geekshow 4:ebaf1973d008 158 payload[message.payloadlen]='\0';
Geekshow 4:ebaf1973d008 159
Geekshow 4:ebaf1973d008 160 // copy topic payload into local char array IMPROVE ME!
Geekshow 4:ebaf1973d008 161 char* topic = new char[md.topicName.lenstring.len+1];
Geekshow 6:4d30bc5a8076 162 if(!topic){ // will this ever happen?
Geekshow 4:ebaf1973d008 163 delete[] payload;
Geekshow 4:ebaf1973d008 164 return;
Geekshow 4:ebaf1973d008 165 }
Geekshow 4:ebaf1973d008 166 memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len);
Geekshow 4:ebaf1973d008 167 topic[md.topicName.lenstring.len]='\0';
Geekshow 4:ebaf1973d008 168
Geekshow 4:ebaf1973d008 169 pc.printf("Rcvd: %s : %s\r\n", topic, payload);
Geekshow 4:ebaf1973d008 170
Geekshow 6:4d30bc5a8076 171 // find first delimiter in topic string
Geekshow 4:ebaf1973d008 172 char *topics = strtok (topic,"/");
Geekshow 4:ebaf1973d008 173 for (int tok=0; tok<2 && topics != NULL; tok++) // WARNING! hard coded 2 layer topic!
Geekshow 4:ebaf1973d008 174 {
Geekshow 6:4d30bc5a8076 175 // pc.printf ("Topics %d: %s\r\n",tok, topics);
Geekshow 4:ebaf1973d008 176 topics = strtok (NULL, "/");
Geekshow 4:ebaf1973d008 177 }
Geekshow 4:ebaf1973d008 178 on_control_cmd(topics, payload);
Geekshow 4:ebaf1973d008 179 delete[] topic;
Geekshow 4:ebaf1973d008 180 delete[] payload;
Geekshow 3:de9611d75590 181 }
Geekshow 3:de9611d75590 182
Geekshow 4:ebaf1973d008 183
Geekshow 10:3ad12f8d8b46 184 int publish_value(MClient &client, const char *topic, const char *buf, bool retain = false)
Geekshow 3:de9611d75590 185 {
Geekshow 10:3ad12f8d8b46 186 return publish(client, "stat", topic, buf, strlen(buf), retain);
Geekshow 4:ebaf1973d008 187 }
Geekshow 4:ebaf1973d008 188
Geekshow 6:4d30bc5a8076 189
Geekshow 6:4d30bc5a8076 190 void publish_outputs(MClient &client) {
Geekshow 6:4d30bc5a8076 191 for(int i=0; i<NUM_OUTPUTS; i++) {
Geekshow 6:4d30bc5a8076 192 bool output_state = outputs[i];
Geekshow 6:4d30bc5a8076 193 char topic[] = "outputx";
Geekshow 6:4d30bc5a8076 194 topic[6] = i+48;
Geekshow 6:4d30bc5a8076 195 pc.printf("Output: %s is %s\r\n", topic, ONOFF[output_state]);
Geekshow 10:3ad12f8d8b46 196 connected = publish_value(client, topic, ONOFF[output_state], false);
Geekshow 10:3ad12f8d8b46 197 }
Geekshow 10:3ad12f8d8b46 198 }
Geekshow 10:3ad12f8d8b46 199
Geekshow 10:3ad12f8d8b46 200 void publish_inputs(MClient &client) {
Geekshow 10:3ad12f8d8b46 201 for(int i=0; i<NUM_INPUTS; i++) {
Geekshow 10:3ad12f8d8b46 202 char topic_str[8]; // long enough string for inputx
Geekshow 10:3ad12f8d8b46 203 sprintf(topic_str, "input%d", i);
Geekshow 12:bcb38c1af703 204 publish_value(client,topic_str,OPENCLOSED[input_state[i]], false);
Geekshow 6:4d30bc5a8076 205 }
Geekshow 6:4d30bc5a8076 206 }
Geekshow 6:4d30bc5a8076 207
Geekshow 6:4d30bc5a8076 208
Geekshow 6:4d30bc5a8076 209 void publish_info(MClient &client) {
Geekshow 6:4d30bc5a8076 210 // uptime
Geekshow 6:4d30bc5a8076 211 pc.printf("Uptime %d\r\n", uptime_sec);
Geekshow 6:4d30bc5a8076 212 char uptime_sec_str[12]; // long enough string for a long int
Geekshow 6:4d30bc5a8076 213 sprintf(uptime_sec_str, "%d", uptime_sec);
Geekshow 12:bcb38c1af703 214 publish_value(client,"uptime",uptime_sec_str, false);
Geekshow 10:3ad12f8d8b46 215 // alive
Geekshow 10:3ad12f8d8b46 216 publish_value(client, "alive","ON", false);
Geekshow 6:4d30bc5a8076 217 }
Geekshow 6:4d30bc5a8076 218
Geekshow 6:4d30bc5a8076 219
Geekshow 8:6d7be3bed961 220 void read_inputs(MClient &client) {
Geekshow 6:4d30bc5a8076 221 for(int i=0; i<NUM_INPUTS; i++) {
Geekshow 6:4d30bc5a8076 222 bool old_state = input_state[i]; // save old state
Geekshow 6:4d30bc5a8076 223 input_state[i] = inputs[i]; // read new value
Geekshow 6:4d30bc5a8076 224 // pc.printf("Input %d is %d\r\n", i, input_state[i]);
Geekshow 6:4d30bc5a8076 225 if(input_state[i] != old_state) {
Geekshow 6:4d30bc5a8076 226 // input has changed state
Geekshow 6:4d30bc5a8076 227 pc.printf("Input %d changed to %s\r\n", i, OPENCLOSED[input_state[i]]);
Geekshow 6:4d30bc5a8076 228 char topic_str[8]; // long enough string for inputx
Geekshow 6:4d30bc5a8076 229 sprintf(topic_str, "input%d", i);
Geekshow 12:bcb38c1af703 230 publish_value(client,topic_str,OPENCLOSED[input_state[i]], false);
Geekshow 6:4d30bc5a8076 231 }
Geekshow 6:4d30bc5a8076 232 }
Geekshow 6:4d30bc5a8076 233 }
Geekshow 6:4d30bc5a8076 234
Geekshow 5:b2ae1ed8a30e 235
Geekshow 11:2a397ea7acc8 236 void read_dht(MClient &client) {
Geekshow 9:a8098e772b48 237 int error = dht0.readData();
Geekshow 9:a8098e772b48 238 if (0 == error) {
Geekshow 9:a8098e772b48 239 temp[0] = dht0.ReadTemperature(CELCIUS);
Geekshow 9:a8098e772b48 240 humidity[0] = dht0.ReadHumidity();
Geekshow 11:2a397ea7acc8 241 pc.printf("Temperature: %3.1f, Humidity: %3.1f\n", temp[0], humidity[0]);
Geekshow 9:a8098e772b48 242 } else {
Geekshow 9:a8098e772b48 243 pc.printf("DHT read error: %d\n", error);
Geekshow 10:3ad12f8d8b46 244 return;
Geekshow 9:a8098e772b48 245 }
Geekshow 9:a8098e772b48 246 // convert to string and publish
Geekshow 9:a8098e772b48 247 char temp_str[6];
Geekshow 9:a8098e772b48 248 sprintf(temp_str, "%3.1f", temp[0]);
Geekshow 12:bcb38c1af703 249 publish_value(client,"temp0",temp_str, false);
Geekshow 9:a8098e772b48 250 char humidity_str[6];
Geekshow 9:a8098e772b48 251 sprintf(humidity_str, "%3.1f", humidity[0]);
Geekshow 12:bcb38c1af703 252 publish_value(client,"humidity0",humidity_str, false);
Geekshow 11:2a397ea7acc8 253 }
Geekshow 11:2a397ea7acc8 254
Geekshow 11:2a397ea7acc8 255
Geekshow 11:2a397ea7acc8 256 void read_ds18b20(MClient &client, int num_ds18b20) {
Geekshow 11:2a397ea7acc8 257 // Announce num of DS18B20 found
Geekshow 11:2a397ea7acc8 258 char temp_str[6];
Geekshow 11:2a397ea7acc8 259 char topic_str[6];
Geekshow 11:2a397ea7acc8 260 sprintf(temp_str, "%d", num_ds18b20);
Geekshow 12:bcb38c1af703 261 publish_value(client,"num_ds18b20",temp_str, false);
Geekshow 14:0a3c670b3862 262 if(num_ds18b20 > 0) {
Geekshow 14:0a3c670b3862 263 //Start temperature conversion, wait until ready
Geekshow 14:0a3c670b3862 264 probe[0]->convertTemperature(true, DS1820::all_devices);
Geekshow 14:0a3c670b3862 265 for (int i = 0; i<num_ds18b20; i++) {
Geekshow 14:0a3c670b3862 266 float temp = probe[i]->temperature();
Geekshow 14:0a3c670b3862 267 pc.printf("Device %d returns %3.3foC\r\n", i, temp);
Geekshow 14:0a3c670b3862 268 // convert to string and publish
Geekshow 14:0a3c670b3862 269 sprintf(temp_str, "%3.3f", temp);
Geekshow 14:0a3c670b3862 270 sprintf(topic_str, "probetemp%d", i);
Geekshow 14:0a3c670b3862 271 publish_value(client,topic_str,temp_str, false);
Geekshow 14:0a3c670b3862 272 }
Geekshow 11:2a397ea7acc8 273 }
Geekshow 9:a8098e772b48 274 }
Geekshow 8:6d7be3bed961 275
Geekshow 8:6d7be3bed961 276
Geekshow 5:b2ae1ed8a30e 277 int networking_init(MQTTSocket &sock, MClient &client, WIZnetInterface &wiz) {
Geekshow 4:ebaf1973d008 278 int ret = 0;
Geekshow 4:ebaf1973d008 279 pc.printf("\n\nNode: %s\r\n", NODE_NAME);
Geekshow 4:ebaf1973d008 280 pc.printf("%s attempting ethernet connection...\r\n", NODE_NAME);
Geekshow 4:ebaf1973d008 281 wiz.init(mac_addr); // resets the w5500
Geekshow 4:ebaf1973d008 282 if (wiz.connect() == (-1)) {
Geekshow 4:ebaf1973d008 283 pc.printf("Error getting DHCP address!!\r\n");
Geekshow 4:ebaf1973d008 284 }
Geekshow 4:ebaf1973d008 285
Geekshow 4:ebaf1973d008 286 pc.printf("IP: %s\r\n", wiz.getIPAddress());
Geekshow 5:b2ae1ed8a30e 287
Geekshow 4:ebaf1973d008 288 ret = sock.connect((char*)mqtt_broker,mqtt_port);
Geekshow 4:ebaf1973d008 289 if(ret != 0){
Geekshow 4:ebaf1973d008 290 pc.printf("failed to connect to TCP server\r\n");
Geekshow 4:ebaf1973d008 291 return 1;
Geekshow 4:ebaf1973d008 292 }
Geekshow 4:ebaf1973d008 293 pc.printf("sock.connect()=%d\r\n",ret);
Geekshow 5:b2ae1ed8a30e 294
Geekshow 4:ebaf1973d008 295 if(client.connect() != 0){
Geekshow 4:ebaf1973d008 296 pc.printf("MQTT connect failed\r\n");
Geekshow 4:ebaf1973d008 297 return -1;
Geekshow 4:ebaf1973d008 298 }
Geekshow 4:ebaf1973d008 299 pc.printf("client.connect()=%d\r\n",ret);
Geekshow 4:ebaf1973d008 300
Geekshow 4:ebaf1973d008 301 ret = client.subscribe("cmnd/" NODE_NAME "/+", MQTT::QOS1, messageArrived);
Geekshow 4:ebaf1973d008 302 pc.printf("client.subscribe()=%d\r\n", ret);
Geekshow 6:4d30bc5a8076 303 // TODO add client ID when subscribing
Geekshow 4:ebaf1973d008 304
Geekshow 4:ebaf1973d008 305 // Node online message
Geekshow 10:3ad12f8d8b46 306 publish_value(client, "alive","ON", false);
Geekshow 14:0a3c670b3862 307 publish_value(client, "version", VERSION, true);
Geekshow 10:3ad12f8d8b46 308 publish_value(client, "IPAddress", wiz.getIPAddress(), true);
Geekshow 4:ebaf1973d008 309 pc.printf("Initialization done.\r\n");
Geekshow 4:ebaf1973d008 310
Geekshow 4:ebaf1973d008 311 return 0;
Geekshow 5:b2ae1ed8a30e 312 }
Geekshow 5:b2ae1ed8a30e 313
Geekshow 14:0a3c670b3862 314 void every_30sec() {
Geekshow 6:4d30bc5a8076 315 // no waits or blocking routines here please!
Geekshow 11:2a397ea7acc8 316 flag_read_dht = 1;
Geekshow 11:2a397ea7acc8 317 flag_read_ds18b20 = 1;
Geekshow 6:4d30bc5a8076 318 }
Geekshow 6:4d30bc5a8076 319
Geekshow 6:4d30bc5a8076 320 void every_5sec() {
Geekshow 6:4d30bc5a8076 321 // no waits or blocking routines here please!
Geekshow 6:4d30bc5a8076 322 flag_publish = 1;
Geekshow 3:de9611d75590 323 }
Geekshow 3:de9611d75590 324
Geekshow 5:b2ae1ed8a30e 325 void every_second() {
Geekshow 6:4d30bc5a8076 326 // no waits or blocking routines here please!
Geekshow 5:b2ae1ed8a30e 327 uptime_sec++;
Geekshow 6:4d30bc5a8076 328 if(connected == 0) {
Geekshow 6:4d30bc5a8076 329 led = !led;
Geekshow 9:a8098e772b48 330 }
Geekshow 9:a8098e772b48 331 wd.Service(); // kick the dog before the timeout
Geekshow 9:a8098e772b48 332 }
Geekshow 6:4d30bc5a8076 333
Geekshow 6:4d30bc5a8076 334 void every_500ms() {
Geekshow 6:4d30bc5a8076 335 // no waits or blocking routines here please!
Geekshow 6:4d30bc5a8076 336 if(connected != 0) {
Geekshow 6:4d30bc5a8076 337 led = !led;
Geekshow 6:4d30bc5a8076 338 }
Geekshow 5:b2ae1ed8a30e 339 }
Geekshow 3:de9611d75590 340
Geekshow 3:de9611d75590 341 int main()
Geekshow 3:de9611d75590 342 {
Geekshow 10:3ad12f8d8b46 343 wd.Configure(20.0);
Geekshow 9:a8098e772b48 344 // WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, NC); // SPI1 with no reset
Geekshow 11:2a397ea7acc8 345 WIZnetInterface wiz(PB_15, PB_14, PB_13, PB_12, PC_6); // SPI2 with D35 reset
Geekshow 6:4d30bc5a8076 346 MQTTSocket sock;
Geekshow 6:4d30bc5a8076 347 MClient client(sock);
Geekshow 6:4d30bc5a8076 348
Geekshow 6:4d30bc5a8076 349 tick_500ms.attach(&every_500ms, 0.5);
Geekshow 5:b2ae1ed8a30e 350 tick_1sec.attach(&every_second, 1.0);
Geekshow 11:2a397ea7acc8 351 tick_5sec.attach(&every_5sec, 5.1);
Geekshow 14:0a3c670b3862 352 tick_30sec.attach(&every_30sec, 29.5);
Geekshow 6:4d30bc5a8076 353
Geekshow 6:4d30bc5a8076 354 //pulse all outputs
Geekshow 6:4d30bc5a8076 355 for(int i=0; i<NUM_OUTPUTS; i++) {
Geekshow 6:4d30bc5a8076 356 outputs[i] = IO_OFF;
Geekshow 6:4d30bc5a8076 357 wait(0.2);
Geekshow 6:4d30bc5a8076 358 }
Geekshow 14:0a3c670b3862 359
Geekshow 14:0a3c670b3862 360 // pull high all inputs
Geekshow 14:0a3c670b3862 361 for(int i=0; i<NUM_INPUTS; i++) {
Geekshow 14:0a3c670b3862 362 inputs[i].mode(PullUp);
Geekshow 14:0a3c670b3862 363 }
Geekshow 10:3ad12f8d8b46 364
Geekshow 5:b2ae1ed8a30e 365 pc.printf("\n\nNode: %s\r\n", NODE_NAME);
Geekshow 6:4d30bc5a8076 366
Geekshow 10:3ad12f8d8b46 367 wd.Service(); // kick the dog before the timeout
Geekshow 5:b2ae1ed8a30e 368 connected = networking_init(sock, client, wiz);
Geekshow 11:2a397ea7acc8 369
Geekshow 11:2a397ea7acc8 370 // Initialize DS18B20 probe array to DS1820 objects
Geekshow 11:2a397ea7acc8 371 int num_ds18b20 = 0;
Geekshow 11:2a397ea7acc8 372 while(DS1820::unassignedProbe(ONEWIRE_PIN)) {
Geekshow 11:2a397ea7acc8 373 probe[num_ds18b20] = new DS1820(ONEWIRE_PIN);
Geekshow 11:2a397ea7acc8 374 num_ds18b20++;
Geekshow 11:2a397ea7acc8 375 if (num_ds18b20 == MAX_PROBES)
Geekshow 11:2a397ea7acc8 376 break;
Geekshow 11:2a397ea7acc8 377 }
Geekshow 11:2a397ea7acc8 378 pc.printf("DS18B20: Found %d device(s)\r\n", num_ds18b20);
Geekshow 5:b2ae1ed8a30e 379
Geekshow 3:de9611d75590 380 while(1) {
Geekshow 6:4d30bc5a8076 381 read_inputs(client);
Geekshow 5:b2ae1ed8a30e 382
Geekshow 5:b2ae1ed8a30e 383 if(connected != 0) {
Geekshow 5:b2ae1ed8a30e 384 pc.printf("Restarting network....\r\n");
Geekshow 6:4d30bc5a8076 385 connected = networking_init(sock, client, wiz);
Geekshow 6:4d30bc5a8076 386 }
Geekshow 6:4d30bc5a8076 387 else {
Geekshow 6:4d30bc5a8076 388 // we're connected, do stuff!
Geekshow 6:4d30bc5a8076 389 if(flag_publish) {
Geekshow 6:4d30bc5a8076 390 publish_outputs(client);
Geekshow 10:3ad12f8d8b46 391 publish_inputs(client);
Geekshow 6:4d30bc5a8076 392 publish_info(client);
Geekshow 6:4d30bc5a8076 393 flag_publish = 0;
Geekshow 6:4d30bc5a8076 394 }
Geekshow 11:2a397ea7acc8 395 else if(flag_read_dht) {
Geekshow 11:2a397ea7acc8 396 read_dht(client);
Geekshow 11:2a397ea7acc8 397 flag_read_dht = 0;
Geekshow 11:2a397ea7acc8 398 }
Geekshow 11:2a397ea7acc8 399 else if(flag_read_ds18b20) {
Geekshow 11:2a397ea7acc8 400 read_ds18b20(client, num_ds18b20);
Geekshow 11:2a397ea7acc8 401 flag_read_ds18b20 = 0;
Geekshow 9:a8098e772b48 402 }
zhangyx 0:1170747a672f 403 }
Geekshow 7:3c2222251deb 404
Geekshow 10:3ad12f8d8b46 405 client.yield(50); // pause a while, yawn......
zhangyx 0:1170747a672f 406 }
zhangyx 0:1170747a672f 407 }