A program for IoT demonstration with mbed, EnOcean and MQTT.
Dependencies: C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
main.cpp@12:4fffa4adc257, 2015-08-08 (annotated)
- Committer:
- ytsuboi
- Date:
- Sat Aug 08 04:23:01 2015 +0000
- Revision:
- 12:4fffa4adc257
- Parent:
- 11:7a6df9a2dcdc
Initial version for demonstration at Maker Faire Tokyo 2015
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /******************************************************************************* |
samdanbury | 6:37b6d0d56190 | 2 | * Copyright (c) 2014 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 |
chris | 10:0b5e0dfee08e | 18 | * |
chris | 10:0b5e0dfee08e | 19 | * To do : |
chris | 10:0b5e0dfee08e | 20 | * Add magnetometer sensor output to IoT data stream |
chris | 10:0b5e0dfee08e | 21 | * |
samdanbury | 6:37b6d0d56190 | 22 | *******************************************************************************/ |
samdanbury | 6:37b6d0d56190 | 23 | |
samdanbury | 6:37b6d0d56190 | 24 | #include "MQTTClient.h" |
samdanbury | 6:37b6d0d56190 | 25 | #include "MQTTEthernet.h" |
samdanbury | 6:37b6d0d56190 | 26 | #include "C12832.h" |
samdanbury | 6:37b6d0d56190 | 27 | #include "Arial12x12.h" |
samdanbury | 6:37b6d0d56190 | 28 | #include "rtos.h" |
ytsuboi | 12:4fffa4adc257 | 29 | Serial device(p9, p10); |
samdanbury | 6:37b6d0d56190 | 30 | |
chris | 10:0b5e0dfee08e | 31 | // Update this to the next number *before* a commit |
chris | 10:0b5e0dfee08e | 32 | #define __APP_SW_REVISION__ "10" |
chris | 10:0b5e0dfee08e | 33 | |
samdanbury | 6:37b6d0d56190 | 34 | // Configuration values needed to connect to IBM IoT Cloud |
ytsuboi | 12:4fffa4adc257 | 35 | #define ORG "CHANGE_THIS" // For a registered connection, replace with your org |
ytsuboi | 12:4fffa4adc257 | 36 | #define ID "CHANGE_THIS" // For a registered connection, replace with your id |
ytsuboi | 12:4fffa4adc257 | 37 | #define AUTH_TOKEN "CHANGE_THIS" // For a registered connection, replace with your auth-token |
ytsuboi | 12:4fffa4adc257 | 38 | #define TYPE "CHANGE_THIS" // 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 | |
mazgch | 11:7a6df9a2dcdc | 46 | #if defined(TARGET_UBLOX_C027) |
mazgch | 11:7a6df9a2dcdc | 47 | #warning "Compiling for mbed C027" |
mazgch | 11:7a6df9a2dcdc | 48 | #include "C027.h" |
mazgch | 11:7a6df9a2dcdc | 49 | #elif defined(TARGET_LPC1768) |
samdanbury | 6:37b6d0d56190 | 50 | #warning "Compiling for mbed LPC1768" |
samdanbury | 6:37b6d0d56190 | 51 | #include "LPC1768.h" |
samdanbury | 6:37b6d0d56190 | 52 | #elif defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 53 | #warning "Compiling for mbed K64F" |
samdanbury | 6:37b6d0d56190 | 54 | #include "K64F.h" |
samdanbury | 6:37b6d0d56190 | 55 | #endif |
samdanbury | 6:37b6d0d56190 | 56 | |
ytsuboi | 12:4fffa4adc257 | 57 | bool quickstartMode = false; |
samdanbury | 6:37b6d0d56190 | 58 | char org[11] = ORG; |
samdanbury | 6:37b6d0d56190 | 59 | char type[30] = TYPE; |
samdanbury | 6:37b6d0d56190 | 60 | char id[30] = ID; // mac without colons |
samdanbury | 6:37b6d0d56190 | 61 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
samdanbury | 6:37b6d0d56190 | 62 | |
samdanbury | 6:37b6d0d56190 | 63 | bool connected = false; |
samdanbury | 6:37b6d0d56190 | 64 | char* joystickPos = "CENTRE"; |
samdanbury | 6:37b6d0d56190 | 65 | int blink_interval = 0; |
samdanbury | 6:37b6d0d56190 | 66 | |
ytsuboi | 12:4fffa4adc257 | 67 | int sensor = 0; |
ytsuboi | 12:4fffa4adc257 | 68 | int humidity = 0; |
ytsuboi | 12:4fffa4adc257 | 69 | int temperature = 0; |
ytsuboi | 12:4fffa4adc257 | 70 | |
ytsuboi | 12:4fffa4adc257 | 71 | uint8_t u8CRC8Table[256] = { |
ytsuboi | 12:4fffa4adc257 | 72 | 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, |
ytsuboi | 12:4fffa4adc257 | 73 | 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, |
ytsuboi | 12:4fffa4adc257 | 74 | 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, |
ytsuboi | 12:4fffa4adc257 | 75 | 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, |
ytsuboi | 12:4fffa4adc257 | 76 | 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, |
ytsuboi | 12:4fffa4adc257 | 77 | 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd, |
ytsuboi | 12:4fffa4adc257 | 78 | 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, |
ytsuboi | 12:4fffa4adc257 | 79 | 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd, |
ytsuboi | 12:4fffa4adc257 | 80 | 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, |
ytsuboi | 12:4fffa4adc257 | 81 | 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, |
ytsuboi | 12:4fffa4adc257 | 82 | 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, |
ytsuboi | 12:4fffa4adc257 | 83 | 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a, |
ytsuboi | 12:4fffa4adc257 | 84 | 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, |
ytsuboi | 12:4fffa4adc257 | 85 | 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a, |
ytsuboi | 12:4fffa4adc257 | 86 | 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, |
ytsuboi | 12:4fffa4adc257 | 87 | 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, |
ytsuboi | 12:4fffa4adc257 | 88 | 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, |
ytsuboi | 12:4fffa4adc257 | 89 | 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4, |
ytsuboi | 12:4fffa4adc257 | 90 | 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, |
ytsuboi | 12:4fffa4adc257 | 91 | 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4, |
ytsuboi | 12:4fffa4adc257 | 92 | 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, |
ytsuboi | 12:4fffa4adc257 | 93 | 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, |
ytsuboi | 12:4fffa4adc257 | 94 | 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, |
ytsuboi | 12:4fffa4adc257 | 95 | 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34, |
ytsuboi | 12:4fffa4adc257 | 96 | 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, |
ytsuboi | 12:4fffa4adc257 | 97 | 0x76, 0x71, 0x78, 0x7f, 0x6A, 0x6d, 0x64, 0x63, |
ytsuboi | 12:4fffa4adc257 | 98 | 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, |
ytsuboi | 12:4fffa4adc257 | 99 | 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, |
ytsuboi | 12:4fffa4adc257 | 100 | 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, |
ytsuboi | 12:4fffa4adc257 | 101 | 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8D, 0x84, 0x83, |
ytsuboi | 12:4fffa4adc257 | 102 | 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, |
ytsuboi | 12:4fffa4adc257 | 103 | 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3 |
ytsuboi | 12:4fffa4adc257 | 104 | }; |
ytsuboi | 12:4fffa4adc257 | 105 | |
ytsuboi | 12:4fffa4adc257 | 106 | #define proccrc8(u8CRC, u8Data) (u8CRC8Table[u8CRC ^ u8Data]) |
samdanbury | 6:37b6d0d56190 | 107 | |
samdanbury | 6:37b6d0d56190 | 108 | void off() |
samdanbury | 6:37b6d0d56190 | 109 | { |
samdanbury | 6:37b6d0d56190 | 110 | r = g = b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 111 | } |
samdanbury | 6:37b6d0d56190 | 112 | |
samdanbury | 6:37b6d0d56190 | 113 | void red() |
samdanbury | 6:37b6d0d56190 | 114 | { |
samdanbury | 6:37b6d0d56190 | 115 | r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 116 | } |
samdanbury | 6:37b6d0d56190 | 117 | |
samdanbury | 6:37b6d0d56190 | 118 | void yellow() |
samdanbury | 6:37b6d0d56190 | 119 | { |
samdanbury | 6:37b6d0d56190 | 120 | r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 121 | } |
samdanbury | 6:37b6d0d56190 | 122 | |
samdanbury | 6:37b6d0d56190 | 123 | void green() |
samdanbury | 6:37b6d0d56190 | 124 | { |
samdanbury | 6:37b6d0d56190 | 125 | r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 126 | } |
samdanbury | 6:37b6d0d56190 | 127 | |
samdanbury | 6:37b6d0d56190 | 128 | |
samdanbury | 6:37b6d0d56190 | 129 | void flashing_yellow(void const *args) |
samdanbury | 6:37b6d0d56190 | 130 | { |
samdanbury | 6:37b6d0d56190 | 131 | bool on = false; |
samdanbury | 6:37b6d0d56190 | 132 | while (!connected) // flashing yellow only while connecting |
samdanbury | 6:37b6d0d56190 | 133 | { |
samdanbury | 6:37b6d0d56190 | 134 | on = !on; |
samdanbury | 6:37b6d0d56190 | 135 | if (on) |
samdanbury | 6:37b6d0d56190 | 136 | yellow(); |
samdanbury | 6:37b6d0d56190 | 137 | else |
samdanbury | 6:37b6d0d56190 | 138 | off(); |
samdanbury | 6:37b6d0d56190 | 139 | wait(0.5); |
samdanbury | 6:37b6d0d56190 | 140 | } |
samdanbury | 6:37b6d0d56190 | 141 | } |
samdanbury | 6:37b6d0d56190 | 142 | |
samdanbury | 6:37b6d0d56190 | 143 | |
samdanbury | 6:37b6d0d56190 | 144 | void flashing_red(void const *args) // to be used when the connection is lost |
samdanbury | 6:37b6d0d56190 | 145 | { |
samdanbury | 6:37b6d0d56190 | 146 | bool on = false; |
samdanbury | 6:37b6d0d56190 | 147 | while (!connected) |
samdanbury | 6:37b6d0d56190 | 148 | { |
samdanbury | 6:37b6d0d56190 | 149 | on = !on; |
samdanbury | 6:37b6d0d56190 | 150 | if (on) |
samdanbury | 6:37b6d0d56190 | 151 | red(); |
samdanbury | 6:37b6d0d56190 | 152 | else |
samdanbury | 6:37b6d0d56190 | 153 | off(); |
samdanbury | 6:37b6d0d56190 | 154 | wait(2.0); |
samdanbury | 6:37b6d0d56190 | 155 | } |
samdanbury | 6:37b6d0d56190 | 156 | } |
samdanbury | 6:37b6d0d56190 | 157 | |
samdanbury | 6:37b6d0d56190 | 158 | /** |
samdanbury | 6:37b6d0d56190 | 159 | * Display a message on the LCD screen prefixed with IBM IoT Cloud |
samdanbury | 6:37b6d0d56190 | 160 | */ |
samdanbury | 6:37b6d0d56190 | 161 | void displayMessage(char* message) |
samdanbury | 6:37b6d0d56190 | 162 | { |
samdanbury | 6:37b6d0d56190 | 163 | lcd.cls(); |
samdanbury | 6:37b6d0d56190 | 164 | lcd.locate(0,0); |
samdanbury | 6:37b6d0d56190 | 165 | lcd.printf("IBM IoT Cloud"); |
samdanbury | 6:37b6d0d56190 | 166 | lcd.locate(0,16); |
samdanbury | 6:37b6d0d56190 | 167 | lcd.printf(message); |
samdanbury | 6:37b6d0d56190 | 168 | } |
samdanbury | 6:37b6d0d56190 | 169 | |
samdanbury | 6:37b6d0d56190 | 170 | |
samdanbury | 6:37b6d0d56190 | 171 | int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 172 | { |
samdanbury | 6:37b6d0d56190 | 173 | const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; |
samdanbury | 6:37b6d0d56190 | 174 | |
samdanbury | 6:37b6d0d56190 | 175 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
samdanbury | 6:37b6d0d56190 | 176 | sprintf(hostname, "%s%s", org, iot_ibm); |
ytsuboi | 12:4fffa4adc257 | 177 | // DEBUG("hostname is %s\n", hostname); |
samdanbury | 6:37b6d0d56190 | 178 | int rc = ipstack->connect(hostname, IBM_IOT_PORT); |
samdanbury | 6:37b6d0d56190 | 179 | if (rc != 0) |
samdanbury | 6:37b6d0d56190 | 180 | return rc; |
samdanbury | 6:37b6d0d56190 | 181 | |
samdanbury | 6:37b6d0d56190 | 182 | // Construct clientId - d:org:type:id |
samdanbury | 6:37b6d0d56190 | 183 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
samdanbury | 6:37b6d0d56190 | 184 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
ytsuboi | 12:4fffa4adc257 | 185 | // DEBUG("clientid is %s\n", clientId); |
samdanbury | 6:37b6d0d56190 | 186 | |
samdanbury | 6:37b6d0d56190 | 187 | // MQTT Connect |
samdanbury | 6:37b6d0d56190 | 188 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
samdanbury | 6:37b6d0d56190 | 189 | data.MQTTVersion = 3; |
samdanbury | 6:37b6d0d56190 | 190 | data.clientID.cstring = clientId; |
samdanbury | 6:37b6d0d56190 | 191 | |
samdanbury | 6:37b6d0d56190 | 192 | if (!quickstartMode) |
samdanbury | 6:37b6d0d56190 | 193 | { |
samdanbury | 6:37b6d0d56190 | 194 | data.username.cstring = "use-token-auth"; |
samdanbury | 6:37b6d0d56190 | 195 | data.password.cstring = auth_token; |
samdanbury | 6:37b6d0d56190 | 196 | } |
samdanbury | 6:37b6d0d56190 | 197 | |
icraggs | 8:80d49dd91542 | 198 | if ((rc = client->connect(data)) == 0) |
samdanbury | 6:37b6d0d56190 | 199 | { |
samdanbury | 6:37b6d0d56190 | 200 | connected = true; |
samdanbury | 6:37b6d0d56190 | 201 | green(); |
samdanbury | 6:37b6d0d56190 | 202 | displayMessage("Connected"); |
ytsuboi | 12:4fffa4adc257 | 203 | } |
samdanbury | 6:37b6d0d56190 | 204 | return rc; |
samdanbury | 6:37b6d0d56190 | 205 | } |
samdanbury | 6:37b6d0d56190 | 206 | |
samdanbury | 6:37b6d0d56190 | 207 | |
samdanbury | 6:37b6d0d56190 | 208 | int getConnTimeout(int attemptNumber) |
samdanbury | 6:37b6d0d56190 | 209 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
samdanbury | 6:37b6d0d56190 | 210 | // after 20 attempts, retry every 10 minutes |
samdanbury | 6:37b6d0d56190 | 211 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
samdanbury | 6:37b6d0d56190 | 212 | } |
samdanbury | 6:37b6d0d56190 | 213 | |
samdanbury | 6:37b6d0d56190 | 214 | |
samdanbury | 6:37b6d0d56190 | 215 | void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 216 | { |
samdanbury | 6:37b6d0d56190 | 217 | int retryAttempt = 0; |
samdanbury | 6:37b6d0d56190 | 218 | connected = false; |
icraggs | 8:80d49dd91542 | 219 | |
icraggs | 8:80d49dd91542 | 220 | // make sure a cable is connected before starting to connect |
icraggs | 8:80d49dd91542 | 221 | while (!linkStatus()) { |
icraggs | 8:80d49dd91542 | 222 | wait(1.0f); |
icraggs | 8:80d49dd91542 | 223 | WARN("Ethernet link not present. Check cable connection\n"); |
icraggs | 8:80d49dd91542 | 224 | } |
ytsuboi | 12:4fffa4adc257 | 225 | |
samdanbury | 6:37b6d0d56190 | 226 | while (connect(client, ipstack) != 0) |
samdanbury | 6:37b6d0d56190 | 227 | { |
samdanbury | 6:37b6d0d56190 | 228 | #if defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 229 | red(); |
samdanbury | 6:37b6d0d56190 | 230 | #else |
samdanbury | 6:37b6d0d56190 | 231 | Thread red_thread(flashing_red); |
samdanbury | 6:37b6d0d56190 | 232 | #endif |
samdanbury | 6:37b6d0d56190 | 233 | int timeout = getConnTimeout(++retryAttempt); |
samdanbury | 6:37b6d0d56190 | 234 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
icraggs | 8:80d49dd91542 | 235 | |
icraggs | 8:80d49dd91542 | 236 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
icraggs | 8:80d49dd91542 | 237 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
icraggs | 8:80d49dd91542 | 238 | |
icraggs | 8:80d49dd91542 | 239 | // this works - reset the system when the retry count gets to a threshold |
icraggs | 8:80d49dd91542 | 240 | if (retryAttempt == 5) |
icraggs | 8:80d49dd91542 | 241 | NVIC_SystemReset(); |
icraggs | 8:80d49dd91542 | 242 | else |
icraggs | 8:80d49dd91542 | 243 | wait(timeout); |
samdanbury | 6:37b6d0d56190 | 244 | } |
samdanbury | 6:37b6d0d56190 | 245 | } |
samdanbury | 6:37b6d0d56190 | 246 | |
samdanbury | 6:37b6d0d56190 | 247 | |
samdanbury | 6:37b6d0d56190 | 248 | int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 249 | { |
samdanbury | 6:37b6d0d56190 | 250 | MQTT::Message message; |
samdanbury | 6:37b6d0d56190 | 251 | char* pubTopic = "iot-2/evt/status/fmt/json"; |
samdanbury | 6:37b6d0d56190 | 252 | |
samdanbury | 6:37b6d0d56190 | 253 | char buf[250]; |
ytsuboi | 12:4fffa4adc257 | 254 | // sprintf(buf, |
ytsuboi | 12:4fffa4adc257 | 255 | // "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", |
ytsuboi | 12:4fffa4adc257 | 256 | // MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read()); |
samdanbury | 6:37b6d0d56190 | 257 | sprintf(buf, |
ytsuboi | 12:4fffa4adc257 | 258 | "{\"d\":{\"sensor\":\"%02X\",\"hum\":%3.1f,\"temp\":%2.2f}}", |
ytsuboi | 12:4fffa4adc257 | 259 | sensor, humidity*0.4, temperature*0.16); |
samdanbury | 6:37b6d0d56190 | 260 | message.qos = MQTT::QOS0; |
samdanbury | 6:37b6d0d56190 | 261 | message.retained = false; |
samdanbury | 6:37b6d0d56190 | 262 | message.dup = false; |
samdanbury | 6:37b6d0d56190 | 263 | message.payload = (void*)buf; |
samdanbury | 6:37b6d0d56190 | 264 | message.payloadlen = strlen(buf); |
samdanbury | 6:37b6d0d56190 | 265 | |
samdanbury | 6:37b6d0d56190 | 266 | LOG("Publishing %s\n", buf); |
icraggs | 8:80d49dd91542 | 267 | return client->publish(pubTopic, message); |
samdanbury | 6:37b6d0d56190 | 268 | } |
samdanbury | 6:37b6d0d56190 | 269 | |
samdanbury | 6:37b6d0d56190 | 270 | void messageArrived(MQTT::MessageData& md) |
samdanbury | 6:37b6d0d56190 | 271 | { |
samdanbury | 6:37b6d0d56190 | 272 | MQTT::Message &message = md.message; |
samdanbury | 6:37b6d0d56190 | 273 | char topic[md.topicName.lenstring.len + 1]; |
samdanbury | 6:37b6d0d56190 | 274 | |
samdanbury | 6:37b6d0d56190 | 275 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
samdanbury | 6:37b6d0d56190 | 276 | |
samdanbury | 6:37b6d0d56190 | 277 | LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload); |
samdanbury | 6:37b6d0d56190 | 278 | |
samdanbury | 6:37b6d0d56190 | 279 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
samdanbury | 6:37b6d0d56190 | 280 | char* start = strstr(topic, "/cmd/") + 5; |
samdanbury | 6:37b6d0d56190 | 281 | int len = strstr(topic, "/fmt/") - start; |
samdanbury | 6:37b6d0d56190 | 282 | |
samdanbury | 6:37b6d0d56190 | 283 | if (memcmp(start, "blink", len) == 0) |
samdanbury | 6:37b6d0d56190 | 284 | { |
samdanbury | 6:37b6d0d56190 | 285 | char payload[message.payloadlen + 1]; |
samdanbury | 6:37b6d0d56190 | 286 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
samdanbury | 6:37b6d0d56190 | 287 | |
samdanbury | 6:37b6d0d56190 | 288 | char* pos = strchr(payload, '}'); |
samdanbury | 6:37b6d0d56190 | 289 | if (pos != NULL) |
samdanbury | 6:37b6d0d56190 | 290 | { |
samdanbury | 6:37b6d0d56190 | 291 | *pos = '\0'; |
samdanbury | 6:37b6d0d56190 | 292 | if ((pos = strchr(payload, ':')) != NULL) |
samdanbury | 6:37b6d0d56190 | 293 | { |
samdanbury | 6:37b6d0d56190 | 294 | int blink_rate = atoi(pos + 1); |
samdanbury | 6:37b6d0d56190 | 295 | blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); |
samdanbury | 6:37b6d0d56190 | 296 | } |
samdanbury | 6:37b6d0d56190 | 297 | } |
samdanbury | 6:37b6d0d56190 | 298 | } |
samdanbury | 6:37b6d0d56190 | 299 | else |
samdanbury | 6:37b6d0d56190 | 300 | WARN("Unsupported command: %.*s\n", len, start); |
samdanbury | 6:37b6d0d56190 | 301 | } |
samdanbury | 6:37b6d0d56190 | 302 | |
samdanbury | 6:37b6d0d56190 | 303 | int main() |
ytsuboi | 12:4fffa4adc257 | 304 | { |
ytsuboi | 12:4fffa4adc257 | 305 | // quickstartMode = (strcmp(org, "quickstart") == 0); |
ytsuboi | 12:4fffa4adc257 | 306 | lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen |
ytsuboi | 12:4fffa4adc257 | 307 | led2 = LED2_OFF; // K64F: turn off the main board LED |
ytsuboi | 12:4fffa4adc257 | 308 | displayMessage("Connecting"); |
icraggs | 8:80d49dd91542 | 309 | |
samdanbury | 6:37b6d0d56190 | 310 | #if defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 311 | yellow(); // Don't flash on the K64F, because starting a thread causes the EthernetInterface init call to hang |
samdanbury | 6:37b6d0d56190 | 312 | #else |
samdanbury | 6:37b6d0d56190 | 313 | Thread yellow_thread(flashing_yellow); |
samdanbury | 6:37b6d0d56190 | 314 | #endif |
samdanbury | 6:37b6d0d56190 | 315 | |
samdanbury | 6:37b6d0d56190 | 316 | MQTTEthernet ipstack; |
samdanbury | 6:37b6d0d56190 | 317 | MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
ytsuboi | 12:4fffa4adc257 | 318 | /* |
samdanbury | 6:37b6d0d56190 | 319 | if (quickstartMode) |
samdanbury | 6:37b6d0d56190 | 320 | { |
samdanbury | 6:37b6d0d56190 | 321 | #if defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 322 | getUUID48(id, sizeof(id)); // getMac doesn't work on the K64F |
samdanbury | 6:37b6d0d56190 | 323 | #else |
samdanbury | 6:37b6d0d56190 | 324 | getMac(ipstack.getEth(), id, sizeof(id)); |
samdanbury | 6:37b6d0d56190 | 325 | #endif |
samdanbury | 6:37b6d0d56190 | 326 | } |
ytsuboi | 12:4fffa4adc257 | 327 | */ |
samdanbury | 6:37b6d0d56190 | 328 | attemptConnect(&client, &ipstack); |
ytsuboi | 12:4fffa4adc257 | 329 | |
ytsuboi | 12:4fffa4adc257 | 330 | // if (!quickstartMode) |
ytsuboi | 12:4fffa4adc257 | 331 | // { |
samdanbury | 6:37b6d0d56190 | 332 | int rc = 0; |
samdanbury | 6:37b6d0d56190 | 333 | if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) |
samdanbury | 6:37b6d0d56190 | 334 | WARN("rc from MQTT subscribe is %d\n", rc); |
ytsuboi | 12:4fffa4adc257 | 335 | // } |
ytsuboi | 12:4fffa4adc257 | 336 | |
ytsuboi | 12:4fffa4adc257 | 337 | // USB400Serial usb400; |
ytsuboi | 12:4fffa4adc257 | 338 | device.baud(57600); |
ytsuboi | 12:4fffa4adc257 | 339 | |
ytsuboi | 12:4fffa4adc257 | 340 | int i; |
ytsuboi | 12:4fffa4adc257 | 341 | int dataLength; |
ytsuboi | 12:4fffa4adc257 | 342 | int optionalLength; |
ytsuboi | 12:4fffa4adc257 | 343 | uint8_t c; |
ytsuboi | 12:4fffa4adc257 | 344 | uint8_t crc; |
samdanbury | 6:37b6d0d56190 | 345 | |
ytsuboi | 12:4fffa4adc257 | 346 | while (1) { |
ytsuboi | 12:4fffa4adc257 | 347 | if (device.readable()) { |
ytsuboi | 12:4fffa4adc257 | 348 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 349 | |
ytsuboi | 12:4fffa4adc257 | 350 | if (c == 0x55) { |
ytsuboi | 12:4fffa4adc257 | 351 | crc = 0; |
ytsuboi | 12:4fffa4adc257 | 352 | |
ytsuboi | 12:4fffa4adc257 | 353 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 354 | crc = proccrc8(crc, c); |
ytsuboi | 12:4fffa4adc257 | 355 | dataLength = c; |
ytsuboi | 12:4fffa4adc257 | 356 | |
ytsuboi | 12:4fffa4adc257 | 357 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 358 | crc = proccrc8(crc, c); |
ytsuboi | 12:4fffa4adc257 | 359 | dataLength = (dataLength << 8) | c; |
ytsuboi | 12:4fffa4adc257 | 360 | |
ytsuboi | 12:4fffa4adc257 | 361 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 362 | crc = proccrc8(crc, c); |
ytsuboi | 12:4fffa4adc257 | 363 | optionalLength = c; |
ytsuboi | 12:4fffa4adc257 | 364 | |
ytsuboi | 12:4fffa4adc257 | 365 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 366 | crc = proccrc8(crc, c); // packet type |
ytsuboi | 12:4fffa4adc257 | 367 | |
ytsuboi | 12:4fffa4adc257 | 368 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 369 | crc = proccrc8(crc, c); // CRC |
ytsuboi | 12:4fffa4adc257 | 370 | |
ytsuboi | 12:4fffa4adc257 | 371 | if (crc == 0) { |
ytsuboi | 12:4fffa4adc257 | 372 | crc = 0; |
ytsuboi | 12:4fffa4adc257 | 373 | |
ytsuboi | 12:4fffa4adc257 | 374 | for (i = 0; i < dataLength; i++) { |
ytsuboi | 12:4fffa4adc257 | 375 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 376 | crc = proccrc8(crc, c); |
ytsuboi | 12:4fffa4adc257 | 377 | |
ytsuboi | 12:4fffa4adc257 | 378 | if (i == 4) { |
ytsuboi | 12:4fffa4adc257 | 379 | // printf("Sensor: %02X ", c); |
ytsuboi | 12:4fffa4adc257 | 380 | sensor = c; |
ytsuboi | 12:4fffa4adc257 | 381 | } |
ytsuboi | 12:4fffa4adc257 | 382 | //A5-04-01 |
ytsuboi | 12:4fffa4adc257 | 383 | if (i == 6) { |
ytsuboi | 12:4fffa4adc257 | 384 | humidity = c; |
ytsuboi | 12:4fffa4adc257 | 385 | } |
ytsuboi | 12:4fffa4adc257 | 386 | if (i == 7) { |
ytsuboi | 12:4fffa4adc257 | 387 | temperature = c; |
ytsuboi | 12:4fffa4adc257 | 388 | } |
ytsuboi | 12:4fffa4adc257 | 389 | } |
ytsuboi | 12:4fffa4adc257 | 390 | // printf("hum:%3.1f%% temp:%2.2fC\r\n", humidity*0.4, temperature*0.16); |
ytsuboi | 12:4fffa4adc257 | 391 | |
ytsuboi | 12:4fffa4adc257 | 392 | if (publish(&client, &ipstack) != 0) |
ytsuboi | 12:4fffa4adc257 | 393 | attemptConnect(&client, &ipstack); // if we have lost the connection |
ytsuboi | 12:4fffa4adc257 | 394 | |
ytsuboi | 12:4fffa4adc257 | 395 | for (i = 0; i < optionalLength; i++) { |
ytsuboi | 12:4fffa4adc257 | 396 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 397 | crc = proccrc8(crc, c); |
ytsuboi | 12:4fffa4adc257 | 398 | } |
ytsuboi | 12:4fffa4adc257 | 399 | |
ytsuboi | 12:4fffa4adc257 | 400 | c = device.getc(); |
ytsuboi | 12:4fffa4adc257 | 401 | crc = proccrc8(crc, c); |
ytsuboi | 12:4fffa4adc257 | 402 | |
ytsuboi | 12:4fffa4adc257 | 403 | } |
ytsuboi | 12:4fffa4adc257 | 404 | } |
ytsuboi | 12:4fffa4adc257 | 405 | } |
samdanbury | 6:37b6d0d56190 | 406 | } |
samdanbury | 6:37b6d0d56190 | 407 | } |