IoT sensor/controller using STM32, W5500 ethernet, MQTT
Dependencies: mbed WIZnet_Library Watchdog DHT MQTT DS1820
main.cpp@7:3c2222251deb, 2020-02-26 (annotated)
- Committer:
- Geekshow
- Date:
- Wed Feb 26 17:22:18 2020 +0000
- Revision:
- 7:3c2222251deb
- Parent:
- 6:4d30bc5a8076
- Child:
- 8:6d7be3bed961
added secondary MQTT server address
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:de9611d75590 | 9 | // ========== PIN DEFINITIONS ============ |
Geekshow | 6:4d30bc5a8076 | 10 | // TODO move pin definitions into separate file |
Geekshow | 4:ebaf1973d008 | 11 | #define LED_GREEN PA_5 |
Geekshow | 4:ebaf1973d008 | 12 | #define LED_ORANGE PA_1 // Don't use! Shared with D3 |
Geekshow | 3:de9611d75590 | 13 | #define BUTTON PC_9 |
Geekshow | 3:de9611d75590 | 14 | |
Geekshow | 6:4d30bc5a8076 | 15 | #define A_0 PC_0 // Analogue Input 0 |
Geekshow | 6:4d30bc5a8076 | 16 | #define A_1 PC_1 // Analogue Input 1 |
Geekshow | 6:4d30bc5a8076 | 17 | #define A_2 PC_2 // Analogue Input 2 |
Geekshow | 6:4d30bc5a8076 | 18 | #define A_3 PC_3 // Analogue Input 3 |
Geekshow | 6:4d30bc5a8076 | 19 | #define A_4 PC_4 // Analogue Input 4 |
Geekshow | 6:4d30bc5a8076 | 20 | #define A_5 PC_5 // Analogue Input 5 |
Geekshow | 3:de9611d75590 | 21 | |
Geekshow | 6:4d30bc5a8076 | 22 | #define D_0 PA_3 // digital output D0 |
Geekshow | 6:4d30bc5a8076 | 23 | #define D_1 PA_2 // digital output D1 |
Geekshow | 6:4d30bc5a8076 | 24 | #define D_2 PA_0 // digital output D2 |
Geekshow | 6:4d30bc5a8076 | 25 | #define D_3 PA_1 // digital output D3 |
Geekshow | 6:4d30bc5a8076 | 26 | #define D_4 PB_5 // digital output D4 |
Geekshow | 6:4d30bc5a8076 | 27 | #define D_5 PB_6 // digital output D5 |
Geekshow | 6:4d30bc5a8076 | 28 | #define D_6 PA_8 // digital output D6 |
Geekshow | 6:4d30bc5a8076 | 29 | #define D_7 PA_9 // digital output D7 |
Geekshow | 6:4d30bc5a8076 | 30 | #define D_8 PA_10 // digital output D8 |
Geekshow | 6:4d30bc5a8076 | 31 | #define D_9 PB_7 // digital output D9 |
Geekshow | 6:4d30bc5a8076 | 32 | #define D_10 PA_4 // digital output D10 - SPI1 SS |
Geekshow | 6:4d30bc5a8076 | 33 | #define D_11 PA_4 // digital output D11 - SPI1 MOSI |
Geekshow | 6:4d30bc5a8076 | 34 | #define D_12 PA_4 // digital output D12 - SPI1 MISO |
Geekshow | 6:4d30bc5a8076 | 35 | //#define D_13 PA_4 // digital output D13 - SPI1 CLK - GREEN LED |
Geekshow | 6:4d30bc5a8076 | 36 | #define D_14 PB_8 // digital output D14 |
Geekshow | 3:de9611d75590 | 37 | // ================= *************** ================== |
Geekshow | 6:4d30bc5a8076 | 38 | #define USART3_TX PC_10 // D26 - pin 4 on Extension |
Geekshow | 6:4d30bc5a8076 | 39 | // ================= *************** ================== |
Geekshow | 6:4d30bc5a8076 | 40 | // serial output? for uLCD |
Geekshow | 6:4d30bc5a8076 | 41 | // ================= *************** ================== |
Geekshow | 6:4d30bc5a8076 | 42 | // sensor inputs? |
Geekshow | 3:de9611d75590 | 43 | // ================= *************** ================== |
Geekshow | 4:ebaf1973d008 | 44 | #define NODE_NAME "controller03" // TODO just define node number |
Geekshow | 3:de9611d75590 | 45 | |
Geekshow | 6:4d30bc5a8076 | 46 | #define NUM_OUTPUTS 8 |
Geekshow | 6:4d30bc5a8076 | 47 | DigitalOut outputs[NUM_OUTPUTS] = {D_0, D_1, D_2, D_3, D_4, D_5, D_6, D_7}; |
Geekshow | 6:4d30bc5a8076 | 48 | #define NUM_INPUTS 6 |
Geekshow | 6:4d30bc5a8076 | 49 | DigitalIn inputs[NUM_INPUTS] = {PC_0, PC_1, PC_2, PC_3, PC_4, PC_5}; |
Geekshow | 6:4d30bc5a8076 | 50 | bool input_state[NUM_INPUTS]; |
Geekshow | 6:4d30bc5a8076 | 51 | |
Geekshow | 6:4d30bc5a8076 | 52 | Serial pc(USART3_TX, NC); // serial debug output on D26 (pin 4 of Extension) |
Geekshow | 6:4d30bc5a8076 | 53 | //Serial pc(PA_9, NC); // serial debug output on D7 |
Geekshow | 6:4d30bc5a8076 | 54 | //Serial xxxxxx // find a serial port for Amp/uLCD connection |
Geekshow | 6:4d30bc5a8076 | 55 | |
Geekshow | 6:4d30bc5a8076 | 56 | DigitalIn button(BUTTON); |
Geekshow | 6:4d30bc5a8076 | 57 | DigitalOut led(LED_GREEN); |
Geekshow | 6:4d30bc5a8076 | 58 | |
Geekshow | 7:3c2222251deb | 59 | Watchdog wd; |
Geekshow | 6:4d30bc5a8076 | 60 | Ticker tick_60sec; |
Geekshow | 5:b2ae1ed8a30e | 61 | Ticker tick_5sec; |
Geekshow | 5:b2ae1ed8a30e | 62 | Ticker tick_1sec; |
Geekshow | 6:4d30bc5a8076 | 63 | Ticker tick_500ms; |
Geekshow | 6:4d30bc5a8076 | 64 | |
Geekshow | 6:4d30bc5a8076 | 65 | bool flag_publish; |
Geekshow | 4:ebaf1973d008 | 66 | |
Geekshow | 4:ebaf1973d008 | 67 | typedef MQTT::Client<MQTTSocket,Countdown> MClient; |
Geekshow | 4:ebaf1973d008 | 68 | |
Geekshow | 6:4d30bc5a8076 | 69 | const char* ONOFF[] = {"ON", "OFF"}; |
Geekshow | 4:ebaf1973d008 | 70 | const char* OPENCLOSED[] = {"CLOSED", "OPEN"}; |
Geekshow | 6:4d30bc5a8076 | 71 | enum IO_STATE{IO_ON, IO_OFF}; |
Geekshow | 4:ebaf1973d008 | 72 | |
Geekshow | 5:b2ae1ed8a30e | 73 | uint8_t mac_addr[6]={0x00, 0x00, 0x00, 0xBE, 0xEF, 0x03}; // TODO make last byte dynamic |
Geekshow | 7:3c2222251deb | 74 | const char* mqtt_broker = "192.168.10.4"; |
Geekshow | 7:3c2222251deb | 75 | //const char* mqtt_broker = "192.168.1.99"; |
Geekshow | 5:b2ae1ed8a30e | 76 | const int mqtt_port = 1883; |
Geekshow | 5:b2ae1ed8a30e | 77 | unsigned long uptime_sec = 0; |
Geekshow | 6:4d30bc5a8076 | 78 | int connected = -1; |
Geekshow | 5:b2ae1ed8a30e | 79 | |
Geekshow | 5:b2ae1ed8a30e | 80 | |
Geekshow | 4:ebaf1973d008 | 81 | |
Geekshow | 6:4d30bc5a8076 | 82 | void on_control_cmd(const char* topic, const char* message) |
zhangyx | 0:1170747a672f | 83 | { |
Geekshow | 4:ebaf1973d008 | 84 | int new_state = 0; |
Geekshow | 6:4d30bc5a8076 | 85 | pc.printf("Received CMD %s %s\r\n", topic, message); |
Geekshow | 6:4d30bc5a8076 | 86 | // find out command first |
Geekshow | 6:4d30bc5a8076 | 87 | if(strcmp(message, "ON") == 0) { |
Geekshow | 4:ebaf1973d008 | 88 | pc.printf("ON value requested!\r\n"); |
Geekshow | 6:4d30bc5a8076 | 89 | new_state = IO_ON; |
Geekshow | 4:ebaf1973d008 | 90 | } |
Geekshow | 6:4d30bc5a8076 | 91 | else if(strcmp(message, "OFF") == 0) { |
Geekshow | 4:ebaf1973d008 | 92 | pc.printf("OFF value requested!\r\n"); |
Geekshow | 6:4d30bc5a8076 | 93 | new_state = IO_OFF; |
Geekshow | 4:ebaf1973d008 | 94 | } |
Geekshow | 4:ebaf1973d008 | 95 | else { |
Geekshow | 6:4d30bc5a8076 | 96 | pc.printf("Unknown command value specified!\r\n"); // TODO return current value on no message |
Geekshow | 4:ebaf1973d008 | 97 | return; |
Geekshow | 4:ebaf1973d008 | 98 | } |
Geekshow | 6:4d30bc5a8076 | 99 | // are we updating an output? |
Geekshow | 6:4d30bc5a8076 | 100 | if(strncmp(topic, "output", 6) == 0) { |
Geekshow | 6:4d30bc5a8076 | 101 | // find out which output to apply it to |
Geekshow | 6:4d30bc5a8076 | 102 | int output_num = int(topic[6])-48; |
Geekshow | 6:4d30bc5a8076 | 103 | if(output_num >= NUM_OUTPUTS) { |
Geekshow | 6:4d30bc5a8076 | 104 | pc.printf("ERROR: unknown output num %d\r\n", output_num); |
Geekshow | 6:4d30bc5a8076 | 105 | } |
Geekshow | 6:4d30bc5a8076 | 106 | else { |
Geekshow | 6:4d30bc5a8076 | 107 | // turn something on/off! |
Geekshow | 6:4d30bc5a8076 | 108 | pc.printf("Output: %d updated to %s\r\n", output_num, ONOFF[new_state]); |
Geekshow | 6:4d30bc5a8076 | 109 | outputs[output_num] = new_state; |
Geekshow | 6:4d30bc5a8076 | 110 | // publish_value(client, topic, ONOFF[new_state]); // needs to access client :-/ |
Geekshow | 6:4d30bc5a8076 | 111 | } |
Geekshow | 6:4d30bc5a8076 | 112 | } |
Geekshow | 6:4d30bc5a8076 | 113 | else { |
Geekshow | 6:4d30bc5a8076 | 114 | pc.printf("ERROR: Couldn't parse topic: %s\r\n", topic); |
Geekshow | 4:ebaf1973d008 | 115 | } |
Geekshow | 4:ebaf1973d008 | 116 | } |
Geekshow | 4:ebaf1973d008 | 117 | |
Geekshow | 4:ebaf1973d008 | 118 | int publish(MClient& client, const char* msg_type, const char* point, |
Geekshow | 4:ebaf1973d008 | 119 | const char* payload = NULL, size_t payload_len = 0, |
Geekshow | 4:ebaf1973d008 | 120 | bool retain = false, MQTT::QoS qos = MQTT::QOS1){ |
Geekshow | 4:ebaf1973d008 | 121 | char topic[64]; |
Geekshow | 4:ebaf1973d008 | 122 | sprintf(topic, "%s/" NODE_NAME "/%s", msg_type, point); |
Geekshow | 4:ebaf1973d008 | 123 | int ret = client.publish(topic, (void*)payload, payload_len, qos, retain); |
Geekshow | 4:ebaf1973d008 | 124 | if(ret == -1) { |
Geekshow | 4:ebaf1973d008 | 125 | pc.printf("ERROR during client.publish() = %d\r\n",ret); |
Geekshow | 4:ebaf1973d008 | 126 | } |
Geekshow | 4:ebaf1973d008 | 127 | return ret; |
zhangyx | 0:1170747a672f | 128 | } |
zhangyx | 2:a50b794b8ede | 129 | |
Geekshow | 4:ebaf1973d008 | 130 | |
Geekshow | 4:ebaf1973d008 | 131 | void messageArrived(MQTT::MessageData& md) |
Geekshow | 3:de9611d75590 | 132 | { |
Geekshow | 6:4d30bc5a8076 | 133 | // MQTT callback function |
Geekshow | 4:ebaf1973d008 | 134 | MQTT::Message &message = md.message; |
Geekshow | 4:ebaf1973d008 | 135 | |
Geekshow | 4:ebaf1973d008 | 136 | // copy message payload into local char array IMPROVE ME! |
Geekshow | 4:ebaf1973d008 | 137 | char* payload = new char[message.payloadlen+1]; |
Geekshow | 6:4d30bc5a8076 | 138 | if(!payload) // will this ever happen? |
Geekshow | 4:ebaf1973d008 | 139 | return; |
Geekshow | 4:ebaf1973d008 | 140 | memcpy(payload, message.payload, message.payloadlen); |
Geekshow | 4:ebaf1973d008 | 141 | payload[message.payloadlen]='\0'; |
Geekshow | 4:ebaf1973d008 | 142 | |
Geekshow | 4:ebaf1973d008 | 143 | // copy topic payload into local char array IMPROVE ME! |
Geekshow | 4:ebaf1973d008 | 144 | char* topic = new char[md.topicName.lenstring.len+1]; |
Geekshow | 6:4d30bc5a8076 | 145 | if(!topic){ // will this ever happen? |
Geekshow | 4:ebaf1973d008 | 146 | delete[] payload; |
Geekshow | 4:ebaf1973d008 | 147 | return; |
Geekshow | 4:ebaf1973d008 | 148 | } |
Geekshow | 4:ebaf1973d008 | 149 | memcpy(topic, md.topicName.lenstring.data, md.topicName.lenstring.len); |
Geekshow | 4:ebaf1973d008 | 150 | topic[md.topicName.lenstring.len]='\0'; |
Geekshow | 4:ebaf1973d008 | 151 | |
Geekshow | 4:ebaf1973d008 | 152 | pc.printf("Rcvd: %s : %s\r\n", topic, payload); |
Geekshow | 4:ebaf1973d008 | 153 | |
Geekshow | 6:4d30bc5a8076 | 154 | // find first delimiter in topic string |
Geekshow | 4:ebaf1973d008 | 155 | char *topics = strtok (topic,"/"); |
Geekshow | 4:ebaf1973d008 | 156 | for (int tok=0; tok<2 && topics != NULL; tok++) // WARNING! hard coded 2 layer topic! |
Geekshow | 4:ebaf1973d008 | 157 | { |
Geekshow | 6:4d30bc5a8076 | 158 | // pc.printf ("Topics %d: %s\r\n",tok, topics); |
Geekshow | 4:ebaf1973d008 | 159 | topics = strtok (NULL, "/"); |
Geekshow | 4:ebaf1973d008 | 160 | } |
Geekshow | 4:ebaf1973d008 | 161 | on_control_cmd(topics, payload); |
Geekshow | 4:ebaf1973d008 | 162 | delete[] topic; |
Geekshow | 4:ebaf1973d008 | 163 | delete[] payload; |
Geekshow | 3:de9611d75590 | 164 | } |
Geekshow | 3:de9611d75590 | 165 | |
Geekshow | 4:ebaf1973d008 | 166 | |
Geekshow | 4:ebaf1973d008 | 167 | int publish_value(MClient &client, const char *topic, const char *buf) |
Geekshow | 3:de9611d75590 | 168 | { |
Geekshow | 4:ebaf1973d008 | 169 | return publish(client, "stat", topic, buf, strlen(buf), true); |
Geekshow | 4:ebaf1973d008 | 170 | } |
Geekshow | 4:ebaf1973d008 | 171 | |
Geekshow | 6:4d30bc5a8076 | 172 | |
Geekshow | 6:4d30bc5a8076 | 173 | void publish_outputs(MClient &client) { |
Geekshow | 6:4d30bc5a8076 | 174 | for(int i=0; i<NUM_OUTPUTS; i++) { |
Geekshow | 6:4d30bc5a8076 | 175 | bool output_state = outputs[i]; |
Geekshow | 6:4d30bc5a8076 | 176 | char topic[] = "outputx"; |
Geekshow | 6:4d30bc5a8076 | 177 | topic[6] = i+48; |
Geekshow | 6:4d30bc5a8076 | 178 | pc.printf("Output: %s is %s\r\n", topic, ONOFF[output_state]); |
Geekshow | 6:4d30bc5a8076 | 179 | connected = publish_value(client, topic, ONOFF[output_state]); |
Geekshow | 6:4d30bc5a8076 | 180 | } |
Geekshow | 6:4d30bc5a8076 | 181 | } |
Geekshow | 6:4d30bc5a8076 | 182 | |
Geekshow | 6:4d30bc5a8076 | 183 | |
Geekshow | 6:4d30bc5a8076 | 184 | void publish_info(MClient &client) { |
Geekshow | 6:4d30bc5a8076 | 185 | // uptime |
Geekshow | 6:4d30bc5a8076 | 186 | pc.printf("Uptime %d\r\n", uptime_sec); |
Geekshow | 6:4d30bc5a8076 | 187 | char uptime_sec_str[12]; // long enough string for a long int |
Geekshow | 6:4d30bc5a8076 | 188 | sprintf(uptime_sec_str, "%d", uptime_sec); |
Geekshow | 6:4d30bc5a8076 | 189 | connected = publish_value(client,"uptime",uptime_sec_str); |
Geekshow | 6:4d30bc5a8076 | 190 | } |
Geekshow | 6:4d30bc5a8076 | 191 | |
Geekshow | 6:4d30bc5a8076 | 192 | |
Geekshow | 6:4d30bc5a8076 | 193 | void read_inputs(MClient &client) |
Geekshow | 6:4d30bc5a8076 | 194 | { |
Geekshow | 6:4d30bc5a8076 | 195 | for(int i=0; i<NUM_INPUTS; i++) { |
Geekshow | 6:4d30bc5a8076 | 196 | bool old_state = input_state[i]; // save old state |
Geekshow | 6:4d30bc5a8076 | 197 | input_state[i] = inputs[i]; // read new value |
Geekshow | 6:4d30bc5a8076 | 198 | // pc.printf("Input %d is %d\r\n", i, input_state[i]); |
Geekshow | 6:4d30bc5a8076 | 199 | if(input_state[i] != old_state) { |
Geekshow | 6:4d30bc5a8076 | 200 | // input has changed state |
Geekshow | 6:4d30bc5a8076 | 201 | pc.printf("Input %d changed to %s\r\n", i, OPENCLOSED[input_state[i]]); |
Geekshow | 6:4d30bc5a8076 | 202 | char topic_str[8]; // long enough string for inputx |
Geekshow | 6:4d30bc5a8076 | 203 | sprintf(topic_str, "input%d", i); |
Geekshow | 6:4d30bc5a8076 | 204 | connected = publish_value(client,topic_str,OPENCLOSED[input_state[i]]); |
Geekshow | 6:4d30bc5a8076 | 205 | } |
Geekshow | 6:4d30bc5a8076 | 206 | } |
Geekshow | 6:4d30bc5a8076 | 207 | } |
Geekshow | 6:4d30bc5a8076 | 208 | |
Geekshow | 5:b2ae1ed8a30e | 209 | |
Geekshow | 5:b2ae1ed8a30e | 210 | int networking_init(MQTTSocket &sock, MClient &client, WIZnetInterface &wiz) { |
Geekshow | 4:ebaf1973d008 | 211 | int ret = 0; |
Geekshow | 4:ebaf1973d008 | 212 | pc.printf("\n\nNode: %s\r\n", NODE_NAME); |
Geekshow | 4:ebaf1973d008 | 213 | pc.printf("%s attempting ethernet connection...\r\n", NODE_NAME); |
Geekshow | 4:ebaf1973d008 | 214 | wiz.init(mac_addr); // resets the w5500 |
Geekshow | 4:ebaf1973d008 | 215 | if (wiz.connect() == (-1)) { |
Geekshow | 4:ebaf1973d008 | 216 | pc.printf("Error getting DHCP address!!\r\n"); |
Geekshow | 4:ebaf1973d008 | 217 | } |
Geekshow | 4:ebaf1973d008 | 218 | |
Geekshow | 4:ebaf1973d008 | 219 | pc.printf("IP: %s\r\n", wiz.getIPAddress()); |
Geekshow | 5:b2ae1ed8a30e | 220 | |
Geekshow | 4:ebaf1973d008 | 221 | ret = sock.connect((char*)mqtt_broker,mqtt_port); |
Geekshow | 4:ebaf1973d008 | 222 | if(ret != 0){ |
Geekshow | 4:ebaf1973d008 | 223 | pc.printf("failed to connect to TCP server\r\n"); |
Geekshow | 4:ebaf1973d008 | 224 | return 1; |
Geekshow | 4:ebaf1973d008 | 225 | } |
Geekshow | 4:ebaf1973d008 | 226 | pc.printf("sock.connect()=%d\r\n",ret); |
Geekshow | 5:b2ae1ed8a30e | 227 | |
Geekshow | 4:ebaf1973d008 | 228 | if(client.connect() != 0){ |
Geekshow | 4:ebaf1973d008 | 229 | pc.printf("MQTT connect failed\r\n"); |
Geekshow | 4:ebaf1973d008 | 230 | return -1; |
Geekshow | 4:ebaf1973d008 | 231 | } |
Geekshow | 4:ebaf1973d008 | 232 | pc.printf("client.connect()=%d\r\n",ret); |
Geekshow | 4:ebaf1973d008 | 233 | |
Geekshow | 4:ebaf1973d008 | 234 | ret = client.subscribe("cmnd/" NODE_NAME "/+", MQTT::QOS1, messageArrived); |
Geekshow | 4:ebaf1973d008 | 235 | pc.printf("client.subscribe()=%d\r\n", ret); |
Geekshow | 6:4d30bc5a8076 | 236 | // TODO add client ID when subscribing |
Geekshow | 4:ebaf1973d008 | 237 | |
Geekshow | 4:ebaf1973d008 | 238 | // Node online message |
Geekshow | 5:b2ae1ed8a30e | 239 | publish_value(client, "alive","ON"); |
Geekshow | 5:b2ae1ed8a30e | 240 | publish_value(client, "IPAddress", wiz.getIPAddress()); |
Geekshow | 4:ebaf1973d008 | 241 | pc.printf("Initialization done.\r\n"); |
Geekshow | 4:ebaf1973d008 | 242 | |
Geekshow | 4:ebaf1973d008 | 243 | return 0; |
Geekshow | 5:b2ae1ed8a30e | 244 | } |
Geekshow | 5:b2ae1ed8a30e | 245 | |
Geekshow | 6:4d30bc5a8076 | 246 | void every_60sec() { |
Geekshow | 6:4d30bc5a8076 | 247 | // no waits or blocking routines here please! |
Geekshow | 6:4d30bc5a8076 | 248 | } |
Geekshow | 6:4d30bc5a8076 | 249 | |
Geekshow | 6:4d30bc5a8076 | 250 | void every_5sec() { |
Geekshow | 6:4d30bc5a8076 | 251 | // no waits or blocking routines here please! |
Geekshow | 6:4d30bc5a8076 | 252 | flag_publish = 1; |
Geekshow | 3:de9611d75590 | 253 | } |
Geekshow | 3:de9611d75590 | 254 | |
Geekshow | 5:b2ae1ed8a30e | 255 | void every_second() { |
Geekshow | 6:4d30bc5a8076 | 256 | // no waits or blocking routines here please! |
Geekshow | 5:b2ae1ed8a30e | 257 | uptime_sec++; |
Geekshow | 6:4d30bc5a8076 | 258 | if(connected == 0) { |
Geekshow | 6:4d30bc5a8076 | 259 | led = !led; |
Geekshow | 6:4d30bc5a8076 | 260 | }} |
Geekshow | 6:4d30bc5a8076 | 261 | |
Geekshow | 6:4d30bc5a8076 | 262 | void every_500ms() { |
Geekshow | 6:4d30bc5a8076 | 263 | // no waits or blocking routines here please! |
Geekshow | 6:4d30bc5a8076 | 264 | if(connected != 0) { |
Geekshow | 6:4d30bc5a8076 | 265 | led = !led; |
Geekshow | 6:4d30bc5a8076 | 266 | } |
Geekshow | 5:b2ae1ed8a30e | 267 | } |
Geekshow | 3:de9611d75590 | 268 | |
Geekshow | 3:de9611d75590 | 269 | int main() |
Geekshow | 3:de9611d75590 | 270 | { |
Geekshow | 6:4d30bc5a8076 | 271 | //WIZnetInterface wiz(PA_7, PA_6, PA_5, PA_4, PB_10); // SPI1 with D29 (reset) |
Geekshow | 6:4d30bc5a8076 | 272 | WIZnetInterface wiz(PB_15, PB_14, PB_13, PB_12, NC); // SPI2 with no reset |
Geekshow | 6:4d30bc5a8076 | 273 | MQTTSocket sock; |
Geekshow | 6:4d30bc5a8076 | 274 | MClient client(sock); |
Geekshow | 6:4d30bc5a8076 | 275 | |
Geekshow | 7:3c2222251deb | 276 | wd.Configure(20.0); |
Geekshow | 6:4d30bc5a8076 | 277 | tick_500ms.attach(&every_500ms, 0.5); |
Geekshow | 5:b2ae1ed8a30e | 278 | tick_1sec.attach(&every_second, 1.0); |
Geekshow | 6:4d30bc5a8076 | 279 | tick_5sec.attach(&every_5sec, 5.0); |
Geekshow | 6:4d30bc5a8076 | 280 | tick_60sec.attach(&every_60sec, 60); |
Geekshow | 6:4d30bc5a8076 | 281 | |
Geekshow | 6:4d30bc5a8076 | 282 | //pulse all outputs |
Geekshow | 6:4d30bc5a8076 | 283 | for(int i=0; i<NUM_OUTPUTS; i++) { |
Geekshow | 6:4d30bc5a8076 | 284 | outputs[i] = IO_OFF; |
Geekshow | 6:4d30bc5a8076 | 285 | wait(0.2); |
Geekshow | 6:4d30bc5a8076 | 286 | } |
Geekshow | 6:4d30bc5a8076 | 287 | |
Geekshow | 5:b2ae1ed8a30e | 288 | pc.printf("\n\nNode: %s\r\n", NODE_NAME); |
Geekshow | 6:4d30bc5a8076 | 289 | |
Geekshow | 5:b2ae1ed8a30e | 290 | connected = networking_init(sock, client, wiz); |
Geekshow | 5:b2ae1ed8a30e | 291 | |
Geekshow | 3:de9611d75590 | 292 | while(1) { |
Geekshow | 7:3c2222251deb | 293 | wd.Service(); // kick the dog before the timeout |
Geekshow | 6:4d30bc5a8076 | 294 | read_inputs(client); |
Geekshow | 5:b2ae1ed8a30e | 295 | |
Geekshow | 5:b2ae1ed8a30e | 296 | if(connected != 0) { |
Geekshow | 5:b2ae1ed8a30e | 297 | pc.printf("Restarting network....\r\n"); |
Geekshow | 6:4d30bc5a8076 | 298 | connected = networking_init(sock, client, wiz); |
Geekshow | 6:4d30bc5a8076 | 299 | } |
Geekshow | 6:4d30bc5a8076 | 300 | else { |
Geekshow | 6:4d30bc5a8076 | 301 | // we're connected, do stuff! |
Geekshow | 6:4d30bc5a8076 | 302 | if(flag_publish) { |
Geekshow | 6:4d30bc5a8076 | 303 | publish_outputs(client); |
Geekshow | 6:4d30bc5a8076 | 304 | publish_info(client); |
Geekshow | 6:4d30bc5a8076 | 305 | flag_publish = 0; |
Geekshow | 6:4d30bc5a8076 | 306 | } |
zhangyx | 0:1170747a672f | 307 | } |
Geekshow | 7:3c2222251deb | 308 | |
Geekshow | 7:3c2222251deb | 309 | client.yield(500); // pause a while, yawn...... |
zhangyx | 0:1170747a672f | 310 | } |
zhangyx | 0:1170747a672f | 311 | } |