Freescale FRDM K64F with DeviceHub.net IoT platform demo code

Dependencies:   EthernetInterface FXOS8700Q MQTTS MbedJSONValue mbed-rtos mbed wolfSSL

Fork of HelloMQTTS by wolf SSL

Committer:
tatulea
Date:
Mon Nov 16 19:39:26 2015 +0000
Revision:
21:f456b1a487e0
Parent:
20:0404c7f31c69
Initial revision of freescale FRDM K64F demo code; Publish accelerometer & magnetometer data to devicehub; Publish state of left & right buttons; Control 2 relays connected to GPIO pins, control the state of the RGB led & control LED via PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tatulea 21:f456b1a487e0 1 #include "mbed.h"
icraggs 8:a3e3113054a1 2 #include "MQTTEthernet.h"
icraggs 2:638c854c0695 3 #include "MQTTClient.h"
tatulea 21:f456b1a487e0 4 #include "MbedJSONValue.h"
tatulea 21:f456b1a487e0 5 #include <string>
tatulea 21:f456b1a487e0 6 #include "rtos.h"
tatulea 21:f456b1a487e0 7 #include "FXOS8700Q.h"
icraggs 2:638c854c0695 8
tatulea 21:f456b1a487e0 9 I2C i2c(PTE25, PTE24);
tatulea 21:f456b1a487e0 10
tatulea 21:f456b1a487e0 11 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
tatulea 21:f456b1a487e0 12 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
tatulea 21:f456b1a487e0 13 motion_data_units_t acc_data, mag_data;
tatulea 21:f456b1a487e0 14
tatulea 21:f456b1a487e0 15 DigitalOut red(LED_RED);
tatulea 21:f456b1a487e0 16 DigitalOut green(LED_GREEN);
tatulea 21:f456b1a487e0 17 DigitalOut blue(LED_BLUE);
tatulea 21:f456b1a487e0 18 DigitalOut r1(PTB2);
tatulea 21:f456b1a487e0 19 DigitalOut r2(PTB3);
tatulea 21:f456b1a487e0 20 PwmOut led(PTD1);
tatulea 21:f456b1a487e0 21 InterruptIn sw_left(SW3);
tatulea 21:f456b1a487e0 22 InterruptIn sw_right(SW2);
tatulea 21:f456b1a487e0 23
tatulea 21:f456b1a487e0 24 #define API_KEY "7553e4b7-0930-4b72-b392-890ef25bde0a"
tatulea 21:f456b1a487e0 25 #define PROJECT_ID "5882"
tatulea 21:f456b1a487e0 26 #define DEVICE_UUID "ef51c7bf-105a-408d-ac63-c403d15f7668"
tatulea 21:f456b1a487e0 27
tatulea 21:f456b1a487e0 28 // Publishing topics
tatulea 21:f456b1a487e0 29 char* pubTopic_acc_x = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Accelerometer_X/data";
tatulea 21:f456b1a487e0 30 char* pubTopic_acc_y = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Accelerometer_Y/data";
tatulea 21:f456b1a487e0 31 char* pubTopic_acc_z = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Accelerometer_Z/data";
tatulea 21:f456b1a487e0 32
tatulea 21:f456b1a487e0 33 char* pubTopic_mag_x = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Magnetometer_X/data";
tatulea 21:f456b1a487e0 34 char* pubTopic_mag_y = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Magnetometer_Y/data";
tatulea 21:f456b1a487e0 35 char* pubTopic_mag_z = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Magnetometer_Z/data";
tatulea 21:f456b1a487e0 36
tatulea 21:f456b1a487e0 37 char* pubTopic_sw_left = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Button_left/data";
tatulea 21:f456b1a487e0 38 char* pubTopic_sw_right = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/sensor/Button_right/data";
wolfSSL 17:25584027fae0 39
tatulea 21:f456b1a487e0 40 // Subscription topics
tatulea 21:f456b1a487e0 41 char* subTopic_RLED = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/Red/state";
tatulea 21:f456b1a487e0 42 char* subTopic_GLED = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/Green/state";
tatulea 21:f456b1a487e0 43 char* subTopic_BLED = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/Blue/state";
tatulea 21:f456b1a487e0 44
tatulea 21:f456b1a487e0 45 char* subTopic_Relay1 = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/Relay_1/state";
tatulea 21:f456b1a487e0 46 char* subTopic_Relay2 = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/Relay_2/state";
tatulea 21:f456b1a487e0 47
tatulea 21:f456b1a487e0 48 char* subTopic_LED = "/a/"API_KEY"/p/"PROJECT_ID"/d/"DEVICE_UUID"/actuator/LED/state";
tatulea 21:f456b1a487e0 49
tatulea 21:f456b1a487e0 50 int send = 0;
tatulea 21:f456b1a487e0 51 int pressed_left = 0;
tatulea 21:f456b1a487e0 52 int pressed_right = 0;
tatulea 21:f456b1a487e0 53 int state_left = 0;
tatulea 21:f456b1a487e0 54 int state_right = 0;
icraggs 2:638c854c0695 55
tatulea 21:f456b1a487e0 56 MbedJSONValue JSON;
tatulea 21:f456b1a487e0 57 std::string value;
tatulea 21:f456b1a487e0 58
tatulea 21:f456b1a487e0 59 void readSensors_thread(void const *args) {
tatulea 21:f456b1a487e0 60 while(true) {
tatulea 21:f456b1a487e0 61 Thread::wait(5000);
tatulea 21:f456b1a487e0 62
tatulea 21:f456b1a487e0 63 // unit based results
tatulea 21:f456b1a487e0 64 acc.getAxis(acc_data);
tatulea 21:f456b1a487e0 65 mag.getAxis(mag_data);
tatulea 21:f456b1a487e0 66 //printf("ACC: X=%1.4ff Y=%1.4ff Z=%1.4ff \t MAG: X=%4.1ff Y=%4.1ff Z=%4.1ff\r\n", acc_data.x, acc_data.y, acc_data.z, mag_data.x, mag_data.y, mag_data.z);
tatulea 21:f456b1a487e0 67
tatulea 21:f456b1a487e0 68 send = 1;
tatulea 21:f456b1a487e0 69 }
tatulea 21:f456b1a487e0 70 }
tatulea 21:f456b1a487e0 71 void messageArrived_RLED(MQTT::MessageData& md)
icraggs 2:638c854c0695 72 {
tatulea 21:f456b1a487e0 73 char *message = (char *)malloc(md.message.payloadlen);
tatulea 21:f456b1a487e0 74 int state = 0;
tatulea 21:f456b1a487e0 75 MbedJSONValue dhMessage;
tatulea 21:f456b1a487e0 76 memcpy(message, md.message.payload, md.message.payloadlen);
tatulea 21:f456b1a487e0 77 // printf("message: %s\n", message);
tatulea 21:f456b1a487e0 78
tatulea 21:f456b1a487e0 79 parse(dhMessage, message);
tatulea 21:f456b1a487e0 80 state = atoi(dhMessage["state"].get<std::string>().c_str());
tatulea 21:f456b1a487e0 81 // printf("state: %d\n", state);
tatulea 21:f456b1a487e0 82
tatulea 21:f456b1a487e0 83 red = !state;
tatulea 21:f456b1a487e0 84
tatulea 21:f456b1a487e0 85 free(message);
icraggs 2:638c854c0695 86 }
icraggs 0:0cae29831d01 87
tatulea 21:f456b1a487e0 88 void messageArrived_GLED(MQTT::MessageData& md)
wolfSSL 17:25584027fae0 89 {
tatulea 21:f456b1a487e0 90 char *message = (char *)malloc(md.message.payloadlen);
tatulea 21:f456b1a487e0 91 int state = 0;
tatulea 21:f456b1a487e0 92 MbedJSONValue dhMessage;
tatulea 21:f456b1a487e0 93 memcpy(message, md.message.payload, md.message.payloadlen);
tatulea 21:f456b1a487e0 94 // printf("message: %s\n", message);
tatulea 21:f456b1a487e0 95
tatulea 21:f456b1a487e0 96 parse(dhMessage, message);
tatulea 21:f456b1a487e0 97 state = atoi(dhMessage["state"].get<std::string>().c_str());
tatulea 21:f456b1a487e0 98 // printf("state: %d\n", state);
tatulea 21:f456b1a487e0 99
tatulea 21:f456b1a487e0 100 green = !state;
tatulea 21:f456b1a487e0 101
tatulea 21:f456b1a487e0 102 free(message);
tatulea 21:f456b1a487e0 103 }
wolfSSL 17:25584027fae0 104
tatulea 21:f456b1a487e0 105 void messageArrived_BLED(MQTT::MessageData& md)
tatulea 21:f456b1a487e0 106 {
tatulea 21:f456b1a487e0 107 char *message = (char *)malloc(md.message.payloadlen);
tatulea 21:f456b1a487e0 108 int state = 0;
tatulea 21:f456b1a487e0 109 MbedJSONValue dhMessage;
tatulea 21:f456b1a487e0 110 memcpy(message, md.message.payload, md.message.payloadlen);
tatulea 21:f456b1a487e0 111 // printf("message: %s\n", message);
tatulea 21:f456b1a487e0 112
tatulea 21:f456b1a487e0 113 parse(dhMessage, message);
tatulea 21:f456b1a487e0 114 state = atoi(dhMessage["state"].get<std::string>().c_str());
tatulea 21:f456b1a487e0 115 // printf("state: %d\n", state);
tatulea 21:f456b1a487e0 116
tatulea 21:f456b1a487e0 117 blue = !state;
tatulea 21:f456b1a487e0 118
tatulea 21:f456b1a487e0 119 free(message);
tatulea 21:f456b1a487e0 120 }
tatulea 21:f456b1a487e0 121
tatulea 21:f456b1a487e0 122 void messageArrived_LED(MQTT::MessageData& md)
tatulea 21:f456b1a487e0 123 {
tatulea 21:f456b1a487e0 124 char *message = (char *)malloc(md.message.payloadlen);
tatulea 21:f456b1a487e0 125 int state = 0;
tatulea 21:f456b1a487e0 126 MbedJSONValue dhMessage;
tatulea 21:f456b1a487e0 127 memcpy(message, md.message.payload, md.message.payloadlen);
tatulea 21:f456b1a487e0 128 // printf("message: %s\n", message);
tatulea 21:f456b1a487e0 129
tatulea 21:f456b1a487e0 130 parse(dhMessage, message);
tatulea 21:f456b1a487e0 131 state = atoi(dhMessage["state"].get<std::string>().c_str());
tatulea 21:f456b1a487e0 132 // printf("state: %d\n", state);
tatulea 21:f456b1a487e0 133
tatulea 21:f456b1a487e0 134 led = state/100.0f;
tatulea 21:f456b1a487e0 135
tatulea 21:f456b1a487e0 136 free(message);
wolfSSL 17:25584027fae0 137 }
wolfSSL 17:25584027fae0 138
tatulea 21:f456b1a487e0 139 void messageArrived_Relay1(MQTT::MessageData& md)
tatulea 21:f456b1a487e0 140 {
tatulea 21:f456b1a487e0 141 char *message = (char *)malloc(md.message.payloadlen);
tatulea 21:f456b1a487e0 142 int state = 0;
tatulea 21:f456b1a487e0 143 MbedJSONValue dhMessage;
tatulea 21:f456b1a487e0 144 memcpy(message, md.message.payload, md.message.payloadlen);
tatulea 21:f456b1a487e0 145 // printf("message: %s\n", message);
tatulea 21:f456b1a487e0 146
tatulea 21:f456b1a487e0 147 parse(dhMessage, message);
tatulea 21:f456b1a487e0 148 state = atoi(dhMessage["state"].get<std::string>().c_str());
tatulea 21:f456b1a487e0 149 // printf("state: %d\n", state);
tatulea 21:f456b1a487e0 150
tatulea 21:f456b1a487e0 151 r1 = !state;
tatulea 21:f456b1a487e0 152
tatulea 21:f456b1a487e0 153 free(message);
tatulea 21:f456b1a487e0 154 }
tatulea 21:f456b1a487e0 155
tatulea 21:f456b1a487e0 156 void messageArrived_Relay2(MQTT::MessageData& md)
wolfSSL 17:25584027fae0 157 {
tatulea 21:f456b1a487e0 158 char *message = (char *)malloc(md.message.payloadlen);
tatulea 21:f456b1a487e0 159 int state = 0;
tatulea 21:f456b1a487e0 160 MbedJSONValue dhMessage;
tatulea 21:f456b1a487e0 161 memcpy(message, md.message.payload, md.message.payloadlen);
tatulea 21:f456b1a487e0 162 printf("message: %s\n", message);
tatulea 21:f456b1a487e0 163
tatulea 21:f456b1a487e0 164 parse(dhMessage, message);
tatulea 21:f456b1a487e0 165 state = atoi(dhMessage["state"].get<std::string>().c_str());
tatulea 21:f456b1a487e0 166 printf("state: %d\n", state);
tatulea 21:f456b1a487e0 167
tatulea 21:f456b1a487e0 168 r2 = !state;
tatulea 21:f456b1a487e0 169
tatulea 21:f456b1a487e0 170 free(message);
tatulea 21:f456b1a487e0 171 }
tatulea 21:f456b1a487e0 172
tatulea 21:f456b1a487e0 173 void sw_left_release(void)
tatulea 21:f456b1a487e0 174 {
tatulea 21:f456b1a487e0 175 state_left = !state_left;
tatulea 21:f456b1a487e0 176 pressed_left = 1;
tatulea 21:f456b1a487e0 177 }
tatulea 21:f456b1a487e0 178
tatulea 21:f456b1a487e0 179 void sw_right_release(void)
tatulea 21:f456b1a487e0 180 {
tatulea 21:f456b1a487e0 181 state_right = !state_right;
tatulea 21:f456b1a487e0 182 pressed_right = 1;
tatulea 21:f456b1a487e0 183 }
tatulea 21:f456b1a487e0 184
tatulea 21:f456b1a487e0 185 int main()
tatulea 21:f456b1a487e0 186 {
tatulea 21:f456b1a487e0 187 red = 1;
tatulea 21:f456b1a487e0 188 green = 1;
tatulea 21:f456b1a487e0 189 blue = 1;
tatulea 21:f456b1a487e0 190 r1 = 1;
tatulea 21:f456b1a487e0 191 r2 = 1;
tatulea 21:f456b1a487e0 192 led = 0;
tatulea 21:f456b1a487e0 193
tatulea 21:f456b1a487e0 194 printf("Hello Freedom :)\n");
icraggs 8:a3e3113054a1 195 MQTTEthernet ipstack = MQTTEthernet();
wolfSSL 17:25584027fae0 196
icraggs 8:a3e3113054a1 197 MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
wolfSSL 17:25584027fae0 198 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
wolfSSL 17:25584027fae0 199 data.MQTTVersion = 3;
wolfSSL 17:25584027fae0 200
tatulea 21:f456b1a487e0 201 /* set connect info */
tatulea 21:f456b1a487e0 202 char *hostname = "mqtt.devicehub.net";
tatulea 21:f456b1a487e0 203 char *username = NULL;
tatulea 21:f456b1a487e0 204 char *password = NULL;
tatulea 21:f456b1a487e0 205 int r = rand() % 100;
tatulea 21:f456b1a487e0 206 char buf_client[10] = {0};
tatulea 21:f456b1a487e0 207 sprintf(buf_client, "K64F-%d", r);
tatulea 21:f456b1a487e0 208 char *clientID = buf_client;
tatulea 21:f456b1a487e0 209 int port = 1883;
wolfSSL 17:25584027fae0 210
wolfSSL 17:25584027fae0 211 data.username.cstring = username ;
wolfSSL 17:25584027fae0 212 data.password.cstring = password ;
wolfSSL 17:25584027fae0 213 data.clientID.cstring = clientID ;
tatulea 21:f456b1a487e0 214
tatulea 21:f456b1a487e0 215 // Set publish info
icraggs 2:638c854c0695 216 MQTT::Message message;
icraggs 2:638c854c0695 217 message.qos = MQTT::QOS0;
tatulea 21:f456b1a487e0 218 message.retained = true;
icraggs 2:638c854c0695 219 message.dup = false;
tatulea 21:f456b1a487e0 220
tatulea 21:f456b1a487e0 221 int rc = ipstack.connect(hostname, port, NULL);
tatulea 21:f456b1a487e0 222 if (rc != 0){
tatulea 21:f456b1a487e0 223 printf("rc from TCP connect is %d\r\n", rc);
tatulea 21:f456b1a487e0 224 NVIC_SystemReset();
tatulea 21:f456b1a487e0 225 }
wolfSSL 17:25584027fae0 226
tatulea 21:f456b1a487e0 227 if ((rc = client.connect(data)) != 0){
tatulea 21:f456b1a487e0 228 printf("rc from MQTT connect is %d\r\n", rc);
tatulea 21:f456b1a487e0 229 NVIC_SystemReset();
icraggs 12:086a9314e8a5 230 }
tatulea 21:f456b1a487e0 231 printf("MQTT connected\r\n");
tatulea 21:f456b1a487e0 232
tatulea 21:f456b1a487e0 233 client.subscribe(subTopic_RLED, MQTT::QOS0, messageArrived_RLED);
tatulea 21:f456b1a487e0 234 client.subscribe(subTopic_GLED, MQTT::QOS0, messageArrived_GLED);
tatulea 21:f456b1a487e0 235 client.subscribe(subTopic_BLED, MQTT::QOS0, messageArrived_BLED);
tatulea 21:f456b1a487e0 236 client.subscribe(subTopic_LED, MQTT::QOS0, messageArrived_LED);
tatulea 21:f456b1a487e0 237 client.subscribe(subTopic_Relay1, MQTT::QOS0, messageArrived_Relay1);
tatulea 21:f456b1a487e0 238 client.subscribe(subTopic_Relay2, MQTT::QOS0, messageArrived_Relay2);
tatulea 21:f456b1a487e0 239
tatulea 21:f456b1a487e0 240 printf("Subscribed to MQTT topics\r\n");
tatulea 21:f456b1a487e0 241
tatulea 21:f456b1a487e0 242 acc.enable();
tatulea 21:f456b1a487e0 243 mag.enable();
tatulea 21:f456b1a487e0 244
tatulea 21:f456b1a487e0 245 sw_left.rise(&sw_left_release);
tatulea 21:f456b1a487e0 246 sw_right.rise(&sw_right_release);
tatulea 21:f456b1a487e0 247 Thread thread(readSensors_thread);
tatulea 21:f456b1a487e0 248
tatulea 21:f456b1a487e0 249 while(true) {
tatulea 21:f456b1a487e0 250 client.yield(100);
tatulea 21:f456b1a487e0 251
tatulea 21:f456b1a487e0 252 if(send){
tatulea 21:f456b1a487e0 253 JSON["value"] = acc_data.x;
tatulea 21:f456b1a487e0 254 value = JSON.serialize();
tatulea 21:f456b1a487e0 255 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 256 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 257 client.publish(pubTopic_acc_x, message);
tatulea 21:f456b1a487e0 258
tatulea 21:f456b1a487e0 259 JSON["value"] = acc_data.y;
tatulea 21:f456b1a487e0 260 value = JSON.serialize();
tatulea 21:f456b1a487e0 261 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 262 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 263 client.publish(pubTopic_acc_y, message);
tatulea 21:f456b1a487e0 264
tatulea 21:f456b1a487e0 265 JSON["value"] = acc_data.z;
tatulea 21:f456b1a487e0 266 value = JSON.serialize();
tatulea 21:f456b1a487e0 267 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 268 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 269 client.publish(pubTopic_acc_z, message);
tatulea 21:f456b1a487e0 270
tatulea 21:f456b1a487e0 271 JSON["value"] = mag_data.x;
tatulea 21:f456b1a487e0 272 value = JSON.serialize();
tatulea 21:f456b1a487e0 273 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 274 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 275 client.publish(pubTopic_mag_x, message);
tatulea 21:f456b1a487e0 276
tatulea 21:f456b1a487e0 277 JSON["value"] = mag_data.y;
tatulea 21:f456b1a487e0 278 value = JSON.serialize();
tatulea 21:f456b1a487e0 279 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 280 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 281 client.publish(pubTopic_mag_y, message);
tatulea 21:f456b1a487e0 282
tatulea 21:f456b1a487e0 283 JSON["value"] = mag_data.z;
tatulea 21:f456b1a487e0 284 value = JSON.serialize();
tatulea 21:f456b1a487e0 285 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 286 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 287 client.publish(pubTopic_mag_z, message);
tatulea 21:f456b1a487e0 288
tatulea 21:f456b1a487e0 289 send = 0;
tatulea 21:f456b1a487e0 290 }
tatulea 21:f456b1a487e0 291
tatulea 21:f456b1a487e0 292 if(pressed_left){
tatulea 21:f456b1a487e0 293 JSON["value"] = state_left;
tatulea 21:f456b1a487e0 294 value = JSON.serialize();
tatulea 21:f456b1a487e0 295 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 296 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 297 client.publish(pubTopic_sw_left, message);
tatulea 21:f456b1a487e0 298
tatulea 21:f456b1a487e0 299 pressed_left = 0;
tatulea 21:f456b1a487e0 300 }
tatulea 21:f456b1a487e0 301
tatulea 21:f456b1a487e0 302 if(pressed_right){
tatulea 21:f456b1a487e0 303 JSON["value"] = state_right;
tatulea 21:f456b1a487e0 304 value = JSON.serialize();
tatulea 21:f456b1a487e0 305 message.payload = (void*)value.c_str();
tatulea 21:f456b1a487e0 306 message.payloadlen = strlen(value.c_str());
tatulea 21:f456b1a487e0 307 client.publish(pubTopic_sw_right, message);
tatulea 21:f456b1a487e0 308
tatulea 21:f456b1a487e0 309 pressed_right = 0;
tatulea 21:f456b1a487e0 310 }
tatulea 21:f456b1a487e0 311 };
icraggs 0:0cae29831d01 312 }