IoT sensor/controller using STM32, W5500 ethernet, MQTT

Dependencies:   mbed WIZnet_Library Watchdog DHT MQTT DS1820

Committer:
Geekshow
Date:
Thu Feb 27 23:05:42 2020 +0000
Revision:
9:a8098e772b48
Parent:
8:6d7be3bed961
Child:
10:3ad12f8d8b46
Added DHT temp measurement

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 9:a8098e772b48 8 #include "DHT.h"
Geekshow 3:de9611d75590 9
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 8:6d7be3bed961 47 #define ONEWIRE_PIN D_37 // pin 15 on Extension
Geekshow 9:a8098e772b48 48 #define DHT_PIN0 PB_10 // D29 pin 5 on UEXT // pin 7 on Extension
Geekshow 9:a8098e772b48 49 #define DHT_PIN1 PB_11 // D30 pin 6 on UEXT // pin 8 on Extension
Geekshow 8:6d7be3bed961 50 #define MAX_TEMP_PROBES 4
Geekshow 3:de9611d75590 51 // ================= *************** ==================
Geekshow 4:ebaf1973d008 52 #define NODE_NAME "controller03" // TODO just define node number
Geekshow 3:de9611d75590 53
Geekshow 6:4d30bc5a8076 54 #define NUM_OUTPUTS 8
Geekshow 6:4d30bc5a8076 55 DigitalOut outputs[NUM_OUTPUTS] = {D_0, D_1, D_2, D_3, D_4, D_5, D_6, D_7};
Geekshow 6:4d30bc5a8076 56 #define NUM_INPUTS 6
Geekshow 6:4d30bc5a8076 57 DigitalIn inputs[NUM_INPUTS] = {PC_0, PC_1, PC_2, PC_3, PC_4, PC_5};
Geekshow 6:4d30bc5a8076 58 bool input_state[NUM_INPUTS];
Geekshow 6:4d30bc5a8076 59
Geekshow 6:4d30bc5a8076 60 Serial pc(USART3_TX, NC); // serial debug output on D26 (pin 4 of Extension)
Geekshow 6:4d30bc5a8076 61 //Serial pc(PA_9, NC); // serial debug output on D7
Geekshow 6:4d30bc5a8076 62 //Serial xxxxxx // find a serial port for Amp/uLCD connection
Geekshow 6:4d30bc5a8076 63
Geekshow 6:4d30bc5a8076 64 DigitalIn button(BUTTON);
Geekshow 6:4d30bc5a8076 65 DigitalOut led(LED_GREEN);
Geekshow 6:4d30bc5a8076 66
Geekshow 9:a8098e772b48 67 DHT dht0(DHT_PIN0, DHT22);
Geekshow 9:a8098e772b48 68 DHT dht1(DHT_PIN1, DHT22);
Geekshow 9:a8098e772b48 69 float temp[2];
Geekshow 9:a8098e772b48 70 float humidity[2];
Geekshow 9:a8098e772b48 71
Geekshow 7:3c2222251deb 72 Watchdog wd;
Geekshow 6:4d30bc5a8076 73 Ticker tick_60sec;
Geekshow 5:b2ae1ed8a30e 74 Ticker tick_5sec;
Geekshow 5:b2ae1ed8a30e 75 Ticker tick_1sec;
Geekshow 6:4d30bc5a8076 76 Ticker tick_500ms;
Geekshow 6:4d30bc5a8076 77
Geekshow 6:4d30bc5a8076 78 bool flag_publish;
Geekshow 9:a8098e772b48 79 bool flag_read_temps;
Geekshow 4:ebaf1973d008 80
Geekshow 4:ebaf1973d008 81 typedef MQTT::Client<MQTTSocket,Countdown> MClient;
Geekshow 4:ebaf1973d008 82
Geekshow 6:4d30bc5a8076 83 const char* ONOFF[] = {"ON", "OFF"};
Geekshow 4:ebaf1973d008 84 const char* OPENCLOSED[] = {"CLOSED", "OPEN"};
Geekshow 6:4d30bc5a8076 85 enum IO_STATE{IO_ON, IO_OFF};
Geekshow 4:ebaf1973d008 86
Geekshow 5:b2ae1ed8a30e 87 uint8_t mac_addr[6]={0x00, 0x00, 0x00, 0xBE, 0xEF, 0x03}; // TODO make last byte dynamic
Geekshow 8:6d7be3bed961 88 //const char* mqtt_broker = "192.168.10.4";
Geekshow 8:6d7be3bed961 89 const char* mqtt_broker = "192.168.1.99";
Geekshow 5:b2ae1ed8a30e 90 const int mqtt_port = 1883;
Geekshow 5:b2ae1ed8a30e 91 unsigned long uptime_sec = 0;
Geekshow 6:4d30bc5a8076 92 int connected = -1;
Geekshow 5:b2ae1ed8a30e 93
Geekshow 5:b2ae1ed8a30e 94
Geekshow 4:ebaf1973d008 95
Geekshow 6:4d30bc5a8076 96 void on_control_cmd(const char* topic, const char* message)
zhangyx 0:1170747a672f 97 {
Geekshow 4:ebaf1973d008 98 int new_state = 0;
Geekshow 6:4d30bc5a8076 99 pc.printf("Received CMD %s %s\r\n", topic, message);
Geekshow 6:4d30bc5a8076 100 // find out command first
Geekshow 6:4d30bc5a8076 101 if(strcmp(message, "ON") == 0) {
Geekshow 4:ebaf1973d008 102 pc.printf("ON value requested!\r\n");
Geekshow 6:4d30bc5a8076 103 new_state = IO_ON;
Geekshow 4:ebaf1973d008 104 }
Geekshow 6:4d30bc5a8076 105 else if(strcmp(message, "OFF") == 0) {
Geekshow 4:ebaf1973d008 106 pc.printf("OFF value requested!\r\n");
Geekshow 6:4d30bc5a8076 107 new_state = IO_OFF;
Geekshow 4:ebaf1973d008 108 }
Geekshow 4:ebaf1973d008 109 else {
Geekshow 6:4d30bc5a8076 110 pc.printf("Unknown command value specified!\r\n"); // TODO return current value on no message
Geekshow 4:ebaf1973d008 111 return;
Geekshow 4:ebaf1973d008 112 }
Geekshow 6:4d30bc5a8076 113 // are we updating an output?
Geekshow 6:4d30bc5a8076 114 if(strncmp(topic, "output", 6) == 0) {
Geekshow 6:4d30bc5a8076 115 // find out which output to apply it to
Geekshow 6:4d30bc5a8076 116 int output_num = int(topic[6])-48;
Geekshow 6:4d30bc5a8076 117 if(output_num >= NUM_OUTPUTS) {
Geekshow 6:4d30bc5a8076 118 pc.printf("ERROR: unknown output num %d\r\n", output_num);
Geekshow 6:4d30bc5a8076 119 }
Geekshow 6:4d30bc5a8076 120 else {
Geekshow 6:4d30bc5a8076 121 // turn something on/off!
Geekshow 6:4d30bc5a8076 122 pc.printf("Output: %d updated to %s\r\n", output_num, ONOFF[new_state]);
Geekshow 6:4d30bc5a8076 123 outputs[output_num] = new_state;
Geekshow 6:4d30bc5a8076 124 // publish_value(client, topic, ONOFF[new_state]); // needs to access client :-/
Geekshow 6:4d30bc5a8076 125 }
Geekshow 6:4d30bc5a8076 126 }
Geekshow 6:4d30bc5a8076 127 else {
Geekshow 6:4d30bc5a8076 128 pc.printf("ERROR: Couldn't parse topic: %s\r\n", topic);
Geekshow 4:ebaf1973d008 129 }
Geekshow 4:ebaf1973d008 130 }
Geekshow 4:ebaf1973d008 131
Geekshow 4:ebaf1973d008 132 int publish(MClient& client, const char* msg_type, const char* point,
Geekshow 4:ebaf1973d008 133 const char* payload = NULL, size_t payload_len = 0,
Geekshow 4:ebaf1973d008 134 bool retain = false, MQTT::QoS qos = MQTT::QOS1){
Geekshow 4:ebaf1973d008 135 char topic[64];
Geekshow 4:ebaf1973d008 136 sprintf(topic, "%s/" NODE_NAME "/%s", msg_type, point);
Geekshow 4:ebaf1973d008 137 int ret = client.publish(topic, (void*)payload, payload_len, qos, retain);
Geekshow 4:ebaf1973d008 138 if(ret == -1) {
Geekshow 4:ebaf1973d008 139 pc.printf("ERROR during client.publish() = %d\r\n",ret);
Geekshow 4:ebaf1973d008 140 }
Geekshow 4:ebaf1973d008 141 return ret;
zhangyx 0:1170747a672f 142 }
zhangyx 2:a50b794b8ede 143
Geekshow 4:ebaf1973d008 144
Geekshow 4:ebaf1973d008 145 void messageArrived(MQTT::MessageData& md)
Geekshow 3:de9611d75590 146 {
Geekshow 6:4d30bc5a8076 147 // MQTT callback function
Geekshow 4:ebaf1973d008 148 MQTT::Message &message = md.message;
Geekshow 4:ebaf1973d008 149
Geekshow 4:ebaf1973d008 150 // copy message payload into local char array IMPROVE ME!
Geekshow 4:ebaf1973d008 151 char* payload = new char[message.payloadlen+1];
Geekshow 6:4d30bc5a8076 152 if(!payload) // will this ever happen?
Geekshow 4:ebaf1973d008 153 return;
Geekshow 4:ebaf1973d008 154 memcpy(payload, message.payload, message.payloadlen);
Geekshow 4:ebaf1973d008 155 payload[message.payloadlen]='\0';
Geekshow 4:ebaf1973d008 156
Geekshow 4:ebaf1973d008 157 // copy topic payload into local char array IMPROVE ME!
Geekshow 4:ebaf1973d008 158 char* topic = new char[md.topicName.lenstring.len+1];
Geekshow 6:4d30bc5a8076 159 if(!topic){ // will this ever happen?
Geekshow 4:ebaf1973d008 160 delete[] payload;
Geekshow 4:ebaf1973d008 161 return;
Geekshow 4:ebaf1973d008 162 }
Geekshow 4:ebaf1973d008 163 memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len);
Geekshow 4:ebaf1973d008 164 topic[md.topicName.lenstring.len]='\0';
Geekshow 4:ebaf1973d008 165
Geekshow 4:ebaf1973d008 166 pc.printf("Rcvd: %s : %s\r\n", topic, payload);
Geekshow 4:ebaf1973d008 167
Geekshow 6:4d30bc5a8076 168 // find first delimiter in topic string
Geekshow 4:ebaf1973d008 169 char *topics = strtok (topic,"/");
Geekshow 4:ebaf1973d008 170 for (int tok=0; tok<2 && topics != NULL; tok++) // WARNING! hard coded 2 layer topic!
Geekshow 4:ebaf1973d008 171 {
Geekshow 6:4d30bc5a8076 172 // pc.printf ("Topics %d: %s\r\n",tok, topics);
Geekshow 4:ebaf1973d008 173 topics = strtok (NULL, "/");
Geekshow 4:ebaf1973d008 174 }
Geekshow 4:ebaf1973d008 175 on_control_cmd(topics, payload);
Geekshow 4:ebaf1973d008 176 delete[] topic;
Geekshow 4:ebaf1973d008 177 delete[] payload;
Geekshow 3:de9611d75590 178 }
Geekshow 3:de9611d75590 179
Geekshow 4:ebaf1973d008 180
Geekshow 4:ebaf1973d008 181 int publish_value(MClient &client, const char *topic, const char *buf)
Geekshow 3:de9611d75590 182 {
Geekshow 4:ebaf1973d008 183 return publish(client, "stat", topic, buf, strlen(buf), true);
Geekshow 4:ebaf1973d008 184 }
Geekshow 4:ebaf1973d008 185
Geekshow 6:4d30bc5a8076 186
Geekshow 6:4d30bc5a8076 187 void publish_outputs(MClient &client) {
Geekshow 6:4d30bc5a8076 188 for(int i=0; i<NUM_OUTPUTS; i++) {
Geekshow 6:4d30bc5a8076 189 bool output_state = outputs[i];
Geekshow 6:4d30bc5a8076 190 char topic[] = "outputx";
Geekshow 6:4d30bc5a8076 191 topic[6] = i+48;
Geekshow 6:4d30bc5a8076 192 pc.printf("Output: %s is %s\r\n", topic, ONOFF[output_state]);
Geekshow 6:4d30bc5a8076 193 connected = publish_value(client, topic, ONOFF[output_state]);
Geekshow 6:4d30bc5a8076 194 }
Geekshow 6:4d30bc5a8076 195 }
Geekshow 6:4d30bc5a8076 196
Geekshow 6:4d30bc5a8076 197
Geekshow 6:4d30bc5a8076 198 void publish_info(MClient &client) {
Geekshow 6:4d30bc5a8076 199 // uptime
Geekshow 6:4d30bc5a8076 200 pc.printf("Uptime %d\r\n", uptime_sec);
Geekshow 6:4d30bc5a8076 201 char uptime_sec_str[12]; // long enough string for a long int
Geekshow 6:4d30bc5a8076 202 sprintf(uptime_sec_str, "%d", uptime_sec);
Geekshow 6:4d30bc5a8076 203 connected = publish_value(client,"uptime",uptime_sec_str);
Geekshow 6:4d30bc5a8076 204 }
Geekshow 6:4d30bc5a8076 205
Geekshow 6:4d30bc5a8076 206
Geekshow 8:6d7be3bed961 207 void read_inputs(MClient &client) {
Geekshow 6:4d30bc5a8076 208 for(int i=0; i<NUM_INPUTS; i++) {
Geekshow 6:4d30bc5a8076 209 bool old_state = input_state[i]; // save old state
Geekshow 6:4d30bc5a8076 210 input_state[i] = inputs[i]; // read new value
Geekshow 6:4d30bc5a8076 211 // pc.printf("Input %d is %d\r\n", i, input_state[i]);
Geekshow 6:4d30bc5a8076 212 if(input_state[i] != old_state) {
Geekshow 6:4d30bc5a8076 213 // input has changed state
Geekshow 6:4d30bc5a8076 214 pc.printf("Input %d changed to %s\r\n", i, OPENCLOSED[input_state[i]]);
Geekshow 6:4d30bc5a8076 215 char topic_str[8]; // long enough string for inputx
Geekshow 6:4d30bc5a8076 216 sprintf(topic_str, "input%d", i);
Geekshow 6:4d30bc5a8076 217 connected = publish_value(client,topic_str,OPENCLOSED[input_state[i]]);
Geekshow 6:4d30bc5a8076 218 }
Geekshow 6:4d30bc5a8076 219 }
Geekshow 6:4d30bc5a8076 220 }
Geekshow 6:4d30bc5a8076 221
Geekshow 5:b2ae1ed8a30e 222
Geekshow 9:a8098e772b48 223 void read_temps(MClient &client) {
Geekshow 9:a8098e772b48 224 int error = dht0.readData();
Geekshow 9:a8098e772b48 225 if (0 == error) {
Geekshow 9:a8098e772b48 226 temp[0] = dht0.ReadTemperature(CELCIUS);
Geekshow 9:a8098e772b48 227 humidity[0] = dht0.ReadHumidity();
Geekshow 9:a8098e772b48 228 pc.printf("Temperature: %3.1f, Humidity: %3.1f\n", temp, humidity);
Geekshow 9:a8098e772b48 229 } else {
Geekshow 9:a8098e772b48 230 pc.printf("DHT read error: %d\n", error);
Geekshow 9:a8098e772b48 231 }
Geekshow 9:a8098e772b48 232 // convert to string and publish
Geekshow 9:a8098e772b48 233 char temp_str[6];
Geekshow 9:a8098e772b48 234 sprintf(temp_str, "%3.1f", temp[0]);
Geekshow 9:a8098e772b48 235 connected = publish_value(client,"temp0",temp_str);
Geekshow 9:a8098e772b48 236 char humidity_str[6];
Geekshow 9:a8098e772b48 237 sprintf(humidity_str, "%3.1f", humidity[0]);
Geekshow 9:a8098e772b48 238 connected = publish_value(client,"humidity0",humidity_str);
Geekshow 9:a8098e772b48 239 /*
Geekshow 8:6d7be3bed961 240 probe.convertTemperature(true, DS1820::all_devices); //Start temp conversion, wait until ready
Geekshow 8:6d7be3bed961 241 temp = probe.temperature();
Geekshow 8:6d7be3bed961 242 pc.printf("Temp0 is %3.1foC\r\n", temp);
Geekshow 9:a8098e772b48 243 */
Geekshow 9:a8098e772b48 244 }
Geekshow 8:6d7be3bed961 245
Geekshow 8:6d7be3bed961 246
Geekshow 5:b2ae1ed8a30e 247 int networking_init(MQTTSocket &sock, MClient &client, WIZnetInterface &wiz) {
Geekshow 4:ebaf1973d008 248 int ret = 0;
Geekshow 4:ebaf1973d008 249 pc.printf("\n\nNode: %s\r\n", NODE_NAME);
Geekshow 4:ebaf1973d008 250 pc.printf("%s attempting ethernet connection...\r\n", NODE_NAME);
Geekshow 4:ebaf1973d008 251 wiz.init(mac_addr); // resets the w5500
Geekshow 4:ebaf1973d008 252 if (wiz.connect() == (-1)) {
Geekshow 4:ebaf1973d008 253 pc.printf("Error getting DHCP address!!\r\n");
Geekshow 4:ebaf1973d008 254 }
Geekshow 4:ebaf1973d008 255
Geekshow 4:ebaf1973d008 256 pc.printf("IP: %s\r\n", wiz.getIPAddress());
Geekshow 5:b2ae1ed8a30e 257
Geekshow 4:ebaf1973d008 258 ret = sock.connect((char*)mqtt_broker,mqtt_port);
Geekshow 4:ebaf1973d008 259 if(ret != 0){
Geekshow 4:ebaf1973d008 260 pc.printf("failed to connect to TCP server\r\n");
Geekshow 4:ebaf1973d008 261 return 1;
Geekshow 4:ebaf1973d008 262 }
Geekshow 4:ebaf1973d008 263 pc.printf("sock.connect()=%d\r\n",ret);
Geekshow 5:b2ae1ed8a30e 264
Geekshow 4:ebaf1973d008 265 if(client.connect() != 0){
Geekshow 4:ebaf1973d008 266 pc.printf("MQTT connect failed\r\n");
Geekshow 4:ebaf1973d008 267 return -1;
Geekshow 4:ebaf1973d008 268 }
Geekshow 4:ebaf1973d008 269 pc.printf("client.connect()=%d\r\n",ret);
Geekshow 4:ebaf1973d008 270
Geekshow 4:ebaf1973d008 271 ret = client.subscribe("cmnd/" NODE_NAME "/+", MQTT::QOS1, messageArrived);
Geekshow 4:ebaf1973d008 272 pc.printf("client.subscribe()=%d\r\n", ret);
Geekshow 6:4d30bc5a8076 273 // TODO add client ID when subscribing
Geekshow 4:ebaf1973d008 274
Geekshow 4:ebaf1973d008 275 // Node online message
Geekshow 5:b2ae1ed8a30e 276 publish_value(client, "alive","ON");
Geekshow 5:b2ae1ed8a30e 277 publish_value(client, "IPAddress", wiz.getIPAddress());
Geekshow 4:ebaf1973d008 278 pc.printf("Initialization done.\r\n");
Geekshow 4:ebaf1973d008 279
Geekshow 4:ebaf1973d008 280 return 0;
Geekshow 5:b2ae1ed8a30e 281 }
Geekshow 5:b2ae1ed8a30e 282
Geekshow 6:4d30bc5a8076 283 void every_60sec() {
Geekshow 6:4d30bc5a8076 284 // no waits or blocking routines here please!
Geekshow 6:4d30bc5a8076 285 }
Geekshow 6:4d30bc5a8076 286
Geekshow 6:4d30bc5a8076 287 void every_5sec() {
Geekshow 6:4d30bc5a8076 288 // no waits or blocking routines here please!
Geekshow 6:4d30bc5a8076 289 flag_publish = 1;
Geekshow 9:a8098e772b48 290 flag_read_temps = 1;
Geekshow 3:de9611d75590 291 }
Geekshow 3:de9611d75590 292
Geekshow 5:b2ae1ed8a30e 293 void every_second() {
Geekshow 6:4d30bc5a8076 294 // no waits or blocking routines here please!
Geekshow 5:b2ae1ed8a30e 295 uptime_sec++;
Geekshow 6:4d30bc5a8076 296 if(connected == 0) {
Geekshow 6:4d30bc5a8076 297 led = !led;
Geekshow 9:a8098e772b48 298 }
Geekshow 9:a8098e772b48 299 wd.Service(); // kick the dog before the timeout
Geekshow 9:a8098e772b48 300 }
Geekshow 6:4d30bc5a8076 301
Geekshow 6:4d30bc5a8076 302 void every_500ms() {
Geekshow 6:4d30bc5a8076 303 // no waits or blocking routines here please!
Geekshow 6:4d30bc5a8076 304 if(connected != 0) {
Geekshow 6:4d30bc5a8076 305 led = !led;
Geekshow 6:4d30bc5a8076 306 }
Geekshow 5:b2ae1ed8a30e 307 }
Geekshow 3:de9611d75590 308
Geekshow 3:de9611d75590 309 int main()
Geekshow 3:de9611d75590 310 {
Geekshow 9:a8098e772b48 311 // WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, NC); // SPI1 with no reset
Geekshow 9:a8098e772b48 312 WIZnetInterface wiz(PB_15, PB_14, PB_13, PB_12, NC); // SPI2 with no reset
Geekshow 6:4d30bc5a8076 313 MQTTSocket sock;
Geekshow 6:4d30bc5a8076 314 MClient client(sock);
Geekshow 6:4d30bc5a8076 315
Geekshow 6:4d30bc5a8076 316 tick_500ms.attach(&every_500ms, 0.5);
Geekshow 5:b2ae1ed8a30e 317 tick_1sec.attach(&every_second, 1.0);
Geekshow 6:4d30bc5a8076 318 tick_5sec.attach(&every_5sec, 5.0);
Geekshow 6:4d30bc5a8076 319 tick_60sec.attach(&every_60sec, 60);
Geekshow 9:a8098e772b48 320 wd.Configure(30.0);
Geekshow 6:4d30bc5a8076 321
Geekshow 6:4d30bc5a8076 322 //pulse all outputs
Geekshow 6:4d30bc5a8076 323 for(int i=0; i<NUM_OUTPUTS; i++) {
Geekshow 6:4d30bc5a8076 324 outputs[i] = IO_OFF;
Geekshow 6:4d30bc5a8076 325 wait(0.2);
Geekshow 6:4d30bc5a8076 326 }
Geekshow 6:4d30bc5a8076 327
Geekshow 5:b2ae1ed8a30e 328 pc.printf("\n\nNode: %s\r\n", NODE_NAME);
Geekshow 6:4d30bc5a8076 329
Geekshow 5:b2ae1ed8a30e 330 connected = networking_init(sock, client, wiz);
Geekshow 5:b2ae1ed8a30e 331
Geekshow 3:de9611d75590 332 while(1) {
Geekshow 6:4d30bc5a8076 333 read_inputs(client);
Geekshow 5:b2ae1ed8a30e 334
Geekshow 5:b2ae1ed8a30e 335 if(connected != 0) {
Geekshow 5:b2ae1ed8a30e 336 pc.printf("Restarting network....\r\n");
Geekshow 6:4d30bc5a8076 337 connected = networking_init(sock, client, wiz);
Geekshow 6:4d30bc5a8076 338 }
Geekshow 6:4d30bc5a8076 339 else {
Geekshow 6:4d30bc5a8076 340 // we're connected, do stuff!
Geekshow 6:4d30bc5a8076 341 if(flag_publish) {
Geekshow 6:4d30bc5a8076 342 publish_outputs(client);
Geekshow 6:4d30bc5a8076 343 publish_info(client);
Geekshow 6:4d30bc5a8076 344 flag_publish = 0;
Geekshow 6:4d30bc5a8076 345 }
Geekshow 9:a8098e772b48 346 else if(flag_read_temps) {
Geekshow 9:a8098e772b48 347 read_temps(client);
Geekshow 9:a8098e772b48 348 flag_read_temps = 0;
Geekshow 9:a8098e772b48 349 }
zhangyx 0:1170747a672f 350 }
Geekshow 7:3c2222251deb 351
Geekshow 7:3c2222251deb 352 client.yield(500); // pause a while, yawn......
zhangyx 0:1170747a672f 353 }
zhangyx 0:1170747a672f 354 }