It can be run only with mbed LPC1768, LAN socket and DHT11 sensor. mbed application board is not needed.
Dependencies: DHT11 EthernetInterface MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
Example Photo
- 水魚堂さんの回路図エディタ BSch3V を利用して作成しました。回路図データ /media/uploads/strysd/simpleiot.ce3
- DHT11 は、masahiro's electronic lab さんで配布されている部品ライブラリを利用しました。http://www.eleclabo.com/denshi/doc/bschlib.html
main.cpp@20:d8387eff818d, 2016-10-15 (annotated)
- Committer:
- strysd
- Date:
- Sat Oct 15 05:55:27 2016 +0000
- Revision:
- 20:d8387eff818d
- Parent:
- 18:94da9de96d54
first commit after fork
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /******************************************************************************* |
icraggs | 16:2420bfbf5f1c | 2 | * Copyright (c) 2014, 2015 IBM Corp. |
samdanbury | 6:37b6d0d56190 | 3 | * |
samdanbury | 6:37b6d0d56190 | 4 | * All rights reserved. This program and the accompanying materials |
samdanbury | 6:37b6d0d56190 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
samdanbury | 6:37b6d0d56190 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
samdanbury | 6:37b6d0d56190 | 7 | * |
samdanbury | 6:37b6d0d56190 | 8 | * The Eclipse Public License is available at |
samdanbury | 6:37b6d0d56190 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
samdanbury | 6:37b6d0d56190 | 10 | * and the Eclipse Distribution License is available at |
samdanbury | 6:37b6d0d56190 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
samdanbury | 6:37b6d0d56190 | 12 | * |
samdanbury | 6:37b6d0d56190 | 13 | * Contributors: |
samdanbury | 6:37b6d0d56190 | 14 | * Sam Danbury - initial implementation |
samdanbury | 6:37b6d0d56190 | 15 | * Ian Craggs - refactoring to remove STL and other changes |
icraggs | 8:80d49dd91542 | 16 | * Sam Grove - added check for Ethernet cable. |
chris | 10:0b5e0dfee08e | 17 | * Chris Styles - Added additional menu screen for software revision |
icraggs | 16:2420bfbf5f1c | 18 | * James Sutton - Mac fix and extra debug |
icraggs | 16:2420bfbf5f1c | 19 | * Ian Craggs - add not authorized messages |
strysd | 20:d8387eff818d | 20 | ******************************************************************************* |
strysd | 20:d8387eff818d | 21 | * Forked by Satoru Yoshida 2016/Oct |
strysd | 20:d8387eff818d | 22 | * Removed support for mbed application board, this program can run only |
strysd | 20:d8387eff818d | 23 | * set of mbed LPC1768, LAN socket and DHT11 sensor |
samdanbury | 6:37b6d0d56190 | 24 | *******************************************************************************/ |
samdanbury | 6:37b6d0d56190 | 25 | |
strysd | 20:d8387eff818d | 26 | |
samdanbury | 6:37b6d0d56190 | 27 | #include "MQTTClient.h" |
samdanbury | 6:37b6d0d56190 | 28 | #include "MQTTEthernet.h" |
samdanbury | 6:37b6d0d56190 | 29 | #include "rtos.h" |
samdanbury | 6:37b6d0d56190 | 30 | |
chris | 10:0b5e0dfee08e | 31 | // Update this to the next number *before* a commit |
strysd | 20:d8387eff818d | 32 | #define __APP_SW_REVISION__ "1" |
chris | 10:0b5e0dfee08e | 33 | |
samdanbury | 6:37b6d0d56190 | 34 | // Configuration values needed to connect to IBM IoT Cloud |
strysd | 20:d8387eff818d | 35 | #define ORG "" // For a registered connection, replace with your org |
strysd | 20:d8387eff818d | 36 | #define ID "" // For a registered connection, replace with your device id, not MAC address |
strysd | 20:d8387eff818d | 37 | #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token |
strysd | 20:d8387eff818d | 38 | #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type |
samdanbury | 6:37b6d0d56190 | 39 | |
samdanbury | 6:37b6d0d56190 | 40 | #define MQTT_PORT 1883 |
samdanbury | 6:37b6d0d56190 | 41 | #define MQTT_TLS_PORT 8883 |
samdanbury | 6:37b6d0d56190 | 42 | #define IBM_IOT_PORT MQTT_PORT |
samdanbury | 6:37b6d0d56190 | 43 | |
samdanbury | 6:37b6d0d56190 | 44 | #define MQTT_MAX_PACKET_SIZE 250 |
samdanbury | 6:37b6d0d56190 | 45 | |
samdanbury | 6:37b6d0d56190 | 46 | #include "LPC1768.h" |
samdanbury | 6:37b6d0d56190 | 47 | |
jsutton | 14:1f961d19f3cf | 48 | |
icraggs | 8:80d49dd91542 | 49 | bool quickstartMode = true; |
samdanbury | 6:37b6d0d56190 | 50 | char org[11] = ORG; |
samdanbury | 6:37b6d0d56190 | 51 | char type[30] = TYPE; |
strysd | 20:d8387eff818d | 52 | char id[30] = ID; // device id |
samdanbury | 6:37b6d0d56190 | 53 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
samdanbury | 6:37b6d0d56190 | 54 | |
samdanbury | 6:37b6d0d56190 | 55 | bool connected = false; |
jsutton | 14:1f961d19f3cf | 56 | bool mqttConnecting = false; |
jsutton | 14:1f961d19f3cf | 57 | bool netConnected = false; |
jsutton | 14:1f961d19f3cf | 58 | bool netConnecting = false; |
jsutton | 14:1f961d19f3cf | 59 | bool ethernetInitialising = true; |
icraggs | 16:2420bfbf5f1c | 60 | int connack_rc = 0; // MQTT connack return code |
jsutton | 14:1f961d19f3cf | 61 | int retryAttempt = 0; |
jsutton | 14:1f961d19f3cf | 62 | |
samdanbury | 6:37b6d0d56190 | 63 | int blink_interval = 0; |
samdanbury | 6:37b6d0d56190 | 64 | |
jsutton | 13:85801e3b83d3 | 65 | char* ip_addr = ""; |
jsutton | 13:85801e3b83d3 | 66 | char* gateway_addr = ""; |
jsutton | 14:1f961d19f3cf | 67 | char* host_addr = ""; |
jsutton | 14:1f961d19f3cf | 68 | int connectTimeout = 1000; |
jsutton | 14:1f961d19f3cf | 69 | |
jsutton | 14:1f961d19f3cf | 70 | // If we wanted to manually set the MAC address, |
jsutton | 14:1f961d19f3cf | 71 | // this is how to do it. In this example, we take |
jsutton | 14:1f961d19f3cf | 72 | // the original Mbed Set MAC address and combine it |
jsutton | 14:1f961d19f3cf | 73 | // with a prefix of our choosing. |
jsutton | 14:1f961d19f3cf | 74 | /* |
jsutton | 14:1f961d19f3cf | 75 | extern "C" void $Super$$mbed_mac_address(char *s); |
jsutton | 14:1f961d19f3cf | 76 | extern "C" void $Sub$$mbed_mac_address(char *s) |
jsutton | 14:1f961d19f3cf | 77 | { |
jsutton | 15:09458079f4bb | 78 | char originalMAC[6] = ""; |
jsutton | 15:09458079f4bb | 79 | $Super$$mbed_mac_address(originalMAC); |
jsutton | 14:1f961d19f3cf | 80 | |
jsutton | 14:1f961d19f3cf | 81 | char mac[6]; |
jsutton | 14:1f961d19f3cf | 82 | mac[0] = 0x00; |
jsutton | 14:1f961d19f3cf | 83 | mac[1] = 0x08; |
jsutton | 14:1f961d19f3cf | 84 | mac[2] = 0xdc; |
jsutton | 15:09458079f4bb | 85 | mac[3] = originalMAC[3]; |
jsutton | 15:09458079f4bb | 86 | mac[4] = originalMAC[4]; |
jsutton | 15:09458079f4bb | 87 | mac[5] = originalMAC[5]; |
jsutton | 14:1f961d19f3cf | 88 | memcpy(s, mac, 6); |
jsutton | 14:1f961d19f3cf | 89 | } |
jsutton | 14:1f961d19f3cf | 90 | */ |
jsutton | 13:85801e3b83d3 | 91 | |
samdanbury | 6:37b6d0d56190 | 92 | /** |
samdanbury | 6:37b6d0d56190 | 93 | * Display a message on the LCD screen prefixed with IBM IoT Cloud |
samdanbury | 6:37b6d0d56190 | 94 | */ |
samdanbury | 6:37b6d0d56190 | 95 | int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 96 | { |
samdanbury | 6:37b6d0d56190 | 97 | const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; |
samdanbury | 6:37b6d0d56190 | 98 | |
samdanbury | 6:37b6d0d56190 | 99 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
samdanbury | 6:37b6d0d56190 | 100 | sprintf(hostname, "%s%s", org, iot_ibm); |
jsutton | 13:85801e3b83d3 | 101 | EthernetInterface& eth = ipstack->getEth(); |
jsutton | 13:85801e3b83d3 | 102 | ip_addr = eth.getIPAddress(); |
jsutton | 13:85801e3b83d3 | 103 | gateway_addr = eth.getGateway(); |
jsutton | 14:1f961d19f3cf | 104 | |
jsutton | 14:1f961d19f3cf | 105 | // Construct clientId - d:org:type:id |
jsutton | 14:1f961d19f3cf | 106 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
jsutton | 14:1f961d19f3cf | 107 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
jsutton | 14:1f961d19f3cf | 108 | |
icraggs | 16:2420bfbf5f1c | 109 | // Network debug statements |
jsutton | 14:1f961d19f3cf | 110 | LOG("=====================================\n"); |
jsutton | 14:1f961d19f3cf | 111 | LOG("Connecting Ethernet.\n"); |
jsutton | 14:1f961d19f3cf | 112 | LOG("IP ADDRESS: %s\n", eth.getIPAddress()); |
jsutton | 14:1f961d19f3cf | 113 | LOG("MAC ADDRESS: %s\n", eth.getMACAddress()); |
jsutton | 14:1f961d19f3cf | 114 | LOG("Gateway: %s\n", eth.getGateway()); |
jsutton | 14:1f961d19f3cf | 115 | LOG("Network Mask: %s\n", eth.getNetworkMask()); |
jsutton | 14:1f961d19f3cf | 116 | LOG("Server Hostname: %s\n", hostname); |
jsutton | 14:1f961d19f3cf | 117 | LOG("Client ID: %s\n", clientId); |
jsutton | 14:1f961d19f3cf | 118 | LOG("=====================================\n"); |
jsutton | 14:1f961d19f3cf | 119 | |
jsutton | 14:1f961d19f3cf | 120 | netConnecting = true; |
jsutton | 14:1f961d19f3cf | 121 | int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); |
icraggs | 16:2420bfbf5f1c | 122 | if (rc != 0) |
icraggs | 16:2420bfbf5f1c | 123 | { |
icraggs | 18:94da9de96d54 | 124 | WARN("IP Stack connect returned: %d\n", rc); |
samdanbury | 6:37b6d0d56190 | 125 | return rc; |
jsutton | 13:85801e3b83d3 | 126 | } |
jsutton | 13:85801e3b83d3 | 127 | netConnected = true; |
jsutton | 14:1f961d19f3cf | 128 | netConnecting = false; |
jsutton | 14:1f961d19f3cf | 129 | |
samdanbury | 6:37b6d0d56190 | 130 | // MQTT Connect |
jsutton | 14:1f961d19f3cf | 131 | mqttConnecting = true; |
samdanbury | 6:37b6d0d56190 | 132 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
samdanbury | 6:37b6d0d56190 | 133 | data.MQTTVersion = 3; |
samdanbury | 6:37b6d0d56190 | 134 | data.clientID.cstring = clientId; |
samdanbury | 6:37b6d0d56190 | 135 | |
samdanbury | 6:37b6d0d56190 | 136 | if (!quickstartMode) |
samdanbury | 6:37b6d0d56190 | 137 | { |
samdanbury | 6:37b6d0d56190 | 138 | data.username.cstring = "use-token-auth"; |
samdanbury | 6:37b6d0d56190 | 139 | data.password.cstring = auth_token; |
samdanbury | 6:37b6d0d56190 | 140 | } |
samdanbury | 6:37b6d0d56190 | 141 | |
icraggs | 8:80d49dd91542 | 142 | if ((rc = client->connect(data)) == 0) |
samdanbury | 6:37b6d0d56190 | 143 | { |
samdanbury | 6:37b6d0d56190 | 144 | connected = true; |
strysd | 20:d8387eff818d | 145 | led2 = LED2_ON; |
strysd | 20:d8387eff818d | 146 | LOG("Connected\n"); |
jsutton | 13:85801e3b83d3 | 147 | wait(1); |
samdanbury | 6:37b6d0d56190 | 148 | } |
icraggs | 16:2420bfbf5f1c | 149 | else |
icraggs | 16:2420bfbf5f1c | 150 | WARN("MQTT connect returned %d\n", rc); |
strysd | 20:d8387eff818d | 151 | led2 = LED2_OFF; |
icraggs | 16:2420bfbf5f1c | 152 | if (rc >= 0) |
icraggs | 16:2420bfbf5f1c | 153 | connack_rc = rc; |
jsutton | 14:1f961d19f3cf | 154 | mqttConnecting = false; |
samdanbury | 6:37b6d0d56190 | 155 | return rc; |
samdanbury | 6:37b6d0d56190 | 156 | } |
samdanbury | 6:37b6d0d56190 | 157 | |
samdanbury | 6:37b6d0d56190 | 158 | |
samdanbury | 6:37b6d0d56190 | 159 | int getConnTimeout(int attemptNumber) |
samdanbury | 6:37b6d0d56190 | 160 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
samdanbury | 6:37b6d0d56190 | 161 | // after 20 attempts, retry every 10 minutes |
samdanbury | 6:37b6d0d56190 | 162 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
samdanbury | 6:37b6d0d56190 | 163 | } |
samdanbury | 6:37b6d0d56190 | 164 | |
samdanbury | 6:37b6d0d56190 | 165 | |
samdanbury | 6:37b6d0d56190 | 166 | void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 167 | { |
samdanbury | 6:37b6d0d56190 | 168 | connected = false; |
icraggs | 16:2420bfbf5f1c | 169 | |
icraggs | 8:80d49dd91542 | 170 | // make sure a cable is connected before starting to connect |
icraggs | 16:2420bfbf5f1c | 171 | while (!linkStatus()) |
icraggs | 16:2420bfbf5f1c | 172 | { |
icraggs | 8:80d49dd91542 | 173 | wait(1.0f); |
icraggs | 8:80d49dd91542 | 174 | WARN("Ethernet link not present. Check cable connection\n"); |
icraggs | 8:80d49dd91542 | 175 | } |
samdanbury | 6:37b6d0d56190 | 176 | |
icraggs | 16:2420bfbf5f1c | 177 | while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) |
samdanbury | 6:37b6d0d56190 | 178 | { |
icraggs | 16:2420bfbf5f1c | 179 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
icraggs | 16:2420bfbf5f1c | 180 | return; // don't reattempt to connect if credentials are wrong |
icraggs | 16:2420bfbf5f1c | 181 | |
samdanbury | 6:37b6d0d56190 | 182 | int timeout = getConnTimeout(++retryAttempt); |
samdanbury | 6:37b6d0d56190 | 183 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
icraggs | 8:80d49dd91542 | 184 | |
icraggs | 8:80d49dd91542 | 185 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
icraggs | 8:80d49dd91542 | 186 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
icraggs | 8:80d49dd91542 | 187 | |
icraggs | 8:80d49dd91542 | 188 | // this works - reset the system when the retry count gets to a threshold |
icraggs | 8:80d49dd91542 | 189 | if (retryAttempt == 5) |
icraggs | 8:80d49dd91542 | 190 | NVIC_SystemReset(); |
icraggs | 8:80d49dd91542 | 191 | else |
icraggs | 8:80d49dd91542 | 192 | wait(timeout); |
samdanbury | 6:37b6d0d56190 | 193 | } |
samdanbury | 6:37b6d0d56190 | 194 | } |
samdanbury | 6:37b6d0d56190 | 195 | |
samdanbury | 6:37b6d0d56190 | 196 | int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 197 | { |
samdanbury | 6:37b6d0d56190 | 198 | MQTT::Message message; |
samdanbury | 6:37b6d0d56190 | 199 | char* pubTopic = "iot-2/evt/status/fmt/json"; |
samdanbury | 6:37b6d0d56190 | 200 | |
samdanbury | 6:37b6d0d56190 | 201 | char buf[250]; |
samdanbury | 6:37b6d0d56190 | 202 | sprintf(buf, |
strysd | 20:d8387eff818d | 203 | "{\"d\":{\"temp\":%d,\"humidity\":%d}}", dht.readTemperature(), dht.readHumidity()); |
samdanbury | 6:37b6d0d56190 | 204 | message.qos = MQTT::QOS0; |
samdanbury | 6:37b6d0d56190 | 205 | message.retained = false; |
samdanbury | 6:37b6d0d56190 | 206 | message.dup = false; |
samdanbury | 6:37b6d0d56190 | 207 | message.payload = (void*)buf; |
samdanbury | 6:37b6d0d56190 | 208 | message.payloadlen = strlen(buf); |
samdanbury | 6:37b6d0d56190 | 209 | |
samdanbury | 6:37b6d0d56190 | 210 | LOG("Publishing %s\n", buf); |
icraggs | 8:80d49dd91542 | 211 | return client->publish(pubTopic, message); |
samdanbury | 6:37b6d0d56190 | 212 | } |
samdanbury | 6:37b6d0d56190 | 213 | |
samdanbury | 6:37b6d0d56190 | 214 | |
samdanbury | 6:37b6d0d56190 | 215 | char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address |
samdanbury | 6:37b6d0d56190 | 216 | { |
samdanbury | 6:37b6d0d56190 | 217 | strncpy(buf, eth.getMACAddress(), buflen); |
samdanbury | 6:37b6d0d56190 | 218 | |
samdanbury | 6:37b6d0d56190 | 219 | char* pos; // Remove colons from mac address |
samdanbury | 6:37b6d0d56190 | 220 | while ((pos = strchr(buf, ':')) != NULL) |
samdanbury | 6:37b6d0d56190 | 221 | memmove(pos, pos + 1, strlen(pos) + 1); |
samdanbury | 6:37b6d0d56190 | 222 | return buf; |
samdanbury | 6:37b6d0d56190 | 223 | } |
chris | 12:8b480eb8a496 | 224 | |
samdanbury | 6:37b6d0d56190 | 225 | |
samdanbury | 6:37b6d0d56190 | 226 | void messageArrived(MQTT::MessageData& md) |
samdanbury | 6:37b6d0d56190 | 227 | { |
samdanbury | 6:37b6d0d56190 | 228 | MQTT::Message &message = md.message; |
samdanbury | 6:37b6d0d56190 | 229 | char topic[md.topicName.lenstring.len + 1]; |
samdanbury | 6:37b6d0d56190 | 230 | |
samdanbury | 6:37b6d0d56190 | 231 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
samdanbury | 6:37b6d0d56190 | 232 | |
samdanbury | 6:37b6d0d56190 | 233 | LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload); |
samdanbury | 6:37b6d0d56190 | 234 | |
samdanbury | 6:37b6d0d56190 | 235 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
samdanbury | 6:37b6d0d56190 | 236 | char* start = strstr(topic, "/cmd/") + 5; |
samdanbury | 6:37b6d0d56190 | 237 | int len = strstr(topic, "/fmt/") - start; |
strysd | 20:d8387eff818d | 238 | |
strysd | 20:d8387eff818d | 239 | //Sample for blink command. |
strysd | 20:d8387eff818d | 240 | /* |
samdanbury | 6:37b6d0d56190 | 241 | if (memcmp(start, "blink", len) == 0) |
samdanbury | 6:37b6d0d56190 | 242 | { |
samdanbury | 6:37b6d0d56190 | 243 | char payload[message.payloadlen + 1]; |
samdanbury | 6:37b6d0d56190 | 244 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
samdanbury | 6:37b6d0d56190 | 245 | |
samdanbury | 6:37b6d0d56190 | 246 | char* pos = strchr(payload, '}'); |
samdanbury | 6:37b6d0d56190 | 247 | if (pos != NULL) |
samdanbury | 6:37b6d0d56190 | 248 | { |
samdanbury | 6:37b6d0d56190 | 249 | *pos = '\0'; |
samdanbury | 6:37b6d0d56190 | 250 | if ((pos = strchr(payload, ':')) != NULL) |
samdanbury | 6:37b6d0d56190 | 251 | { |
samdanbury | 6:37b6d0d56190 | 252 | int blink_rate = atoi(pos + 1); |
samdanbury | 6:37b6d0d56190 | 253 | blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); |
samdanbury | 6:37b6d0d56190 | 254 | } |
samdanbury | 6:37b6d0d56190 | 255 | } |
samdanbury | 6:37b6d0d56190 | 256 | } |
samdanbury | 6:37b6d0d56190 | 257 | else |
samdanbury | 6:37b6d0d56190 | 258 | WARN("Unsupported command: %.*s\n", len, start); |
strysd | 20:d8387eff818d | 259 | */ |
strysd | 20:d8387eff818d | 260 | |
samdanbury | 6:37b6d0d56190 | 261 | } |
samdanbury | 6:37b6d0d56190 | 262 | |
samdanbury | 6:37b6d0d56190 | 263 | |
samdanbury | 6:37b6d0d56190 | 264 | int main() |
strysd | 20:d8387eff818d | 265 | { |
icraggs | 8:80d49dd91542 | 266 | quickstartMode = (strcmp(org, "quickstart") == 0); |
icraggs | 8:80d49dd91542 | 267 | |
strysd | 20:d8387eff818d | 268 | led2 = LED2_OFF; |
samdanbury | 6:37b6d0d56190 | 269 | |
strysd | 20:d8387eff818d | 270 | LOG("Connecting\n"); |
jsutton | 13:85801e3b83d3 | 271 | |
jsutton | 14:1f961d19f3cf | 272 | LOG("***** IBM IoT Client Ethernet Example *****\n"); |
samdanbury | 6:37b6d0d56190 | 273 | MQTTEthernet ipstack; |
jsutton | 14:1f961d19f3cf | 274 | ethernetInitialising = false; |
samdanbury | 6:37b6d0d56190 | 275 | MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
icraggs | 18:94da9de96d54 | 276 | LOG("Ethernet Initialized\n"); |
jsutton | 13:85801e3b83d3 | 277 | |
samdanbury | 6:37b6d0d56190 | 278 | if (quickstartMode) |
icraggs | 16:2420bfbf5f1c | 279 | getMac(ipstack.getEth(), id, sizeof(id)); |
icraggs | 16:2420bfbf5f1c | 280 | |
icraggs | 16:2420bfbf5f1c | 281 | attemptConnect(&client, &ipstack); |
icraggs | 16:2420bfbf5f1c | 282 | |
icraggs | 16:2420bfbf5f1c | 283 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
samdanbury | 6:37b6d0d56190 | 284 | { |
strysd | 20:d8387eff818d | 285 | |
icraggs | 16:2420bfbf5f1c | 286 | while (true) |
strysd | 20:d8387eff818d | 287 | wait(1); // Permanent failures - don't retry |
samdanbury | 6:37b6d0d56190 | 288 | } |
icraggs | 16:2420bfbf5f1c | 289 | |
samdanbury | 6:37b6d0d56190 | 290 | if (!quickstartMode) |
samdanbury | 6:37b6d0d56190 | 291 | { |
samdanbury | 6:37b6d0d56190 | 292 | int rc = 0; |
samdanbury | 6:37b6d0d56190 | 293 | if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) |
samdanbury | 6:37b6d0d56190 | 294 | WARN("rc from MQTT subscribe is %d\n", rc); |
samdanbury | 6:37b6d0d56190 | 295 | } |
samdanbury | 6:37b6d0d56190 | 296 | |
samdanbury | 6:37b6d0d56190 | 297 | int count = 0; |
strysd | 20:d8387eff818d | 298 | int state = 0; |
samdanbury | 6:37b6d0d56190 | 299 | while (true) |
samdanbury | 6:37b6d0d56190 | 300 | { |
strysd | 20:d8387eff818d | 301 | if (++count == 600) |
strysd | 20:d8387eff818d | 302 | { // Publish a message every 6 seconds = 600 * 10ms by yield |
strysd | 20:d8387eff818d | 303 | |
strysd | 20:d8387eff818d | 304 | state = dht.readData(); |
strysd | 20:d8387eff818d | 305 | |
strysd | 20:d8387eff818d | 306 | if (state != DHT11::OK) { |
strysd | 20:d8387eff818d | 307 | LOG("DHT11 Error: %d\n", state); |
strysd | 20:d8387eff818d | 308 | LOG("{\"d\":{\"temp\":%d,\"humidity\":%d}}", dht.readTemperature(), dht.readHumidity()); |
strysd | 20:d8387eff818d | 309 | } else { |
strysd | 20:d8387eff818d | 310 | if (publish(&client, &ipstack) != 0) { |
strysd | 20:d8387eff818d | 311 | attemptConnect(&client, &ipstack); // if we have lost the connection |
strysd | 20:d8387eff818d | 312 | } |
strysd | 20:d8387eff818d | 313 | } |
samdanbury | 6:37b6d0d56190 | 314 | count = 0; |
strysd | 20:d8387eff818d | 315 | led2 = !led2; |
samdanbury | 6:37b6d0d56190 | 316 | } |
samdanbury | 6:37b6d0d56190 | 317 | |
strysd | 20:d8387eff818d | 318 | client.yield(10); // allow the MQTT client to receive messages within 10 msec. |
samdanbury | 6:37b6d0d56190 | 319 | } |
strysd | 20:d8387eff818d | 320 | } |