Local copy

Dependencies:   C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed

Fork of IBMIoTClientExampleForLPC1768 by Sam Danbury

Committer:
rajathishere
Date:
Wed Jul 02 10:37:56 2014 +0000
Revision:
11:0fa63599f5a0
Parent:
9:cbabd85f2d5d
Child:
13:65e87bd958bd
Read blink count from Json

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 0:6276e9f72327 1 /*******************************************************************************
samdanbury 0:6276e9f72327 2 * Copyright (c) 2014 IBM Corporation and other Contributors.
samdanbury 0:6276e9f72327 3 *
samdanbury 0:6276e9f72327 4 * All rights reserved. This program and the accompanying materials
samdanbury 0:6276e9f72327 5 * are made available under the terms of the Eclipse Public License v1.0
samdanbury 0:6276e9f72327 6 * which accompanies this distribution, and is available at
samdanbury 0:6276e9f72327 7 * http://www.eclipse.org/legal/epl-v10.html
samdanbury 0:6276e9f72327 8 *
samdanbury 0:6276e9f72327 9 * Contributors: Sam Danbury
samdanbury 0:6276e9f72327 10 * IBM - Initial Contribution
samdanbury 0:6276e9f72327 11 *******************************************************************************/
samdanbury 0:6276e9f72327 12
samdanbury 0:6276e9f72327 13 #include "stdio.h"
samdanbury 0:6276e9f72327 14 #include "mbed.h"
samdanbury 0:6276e9f72327 15 #include "rtos.h"
samdanbury 0:6276e9f72327 16 #include "EthernetInterface.h"
samdanbury 0:6276e9f72327 17 #include "C12832_lcd.h"
samdanbury 0:6276e9f72327 18 #include "LM75B.h"
samdanbury 0:6276e9f72327 19 #include "MMA7660.h"
samdanbury 0:6276e9f72327 20 #include "C12832_lcd.h"
samdanbury 0:6276e9f72327 21 #include "QuickstartClient.h"
rajathishere 11:0fa63599f5a0 22 #include "cJSON.h"
samdanbury 0:6276e9f72327 23
samdanbury 0:6276e9f72327 24 #include <string>
samdanbury 0:6276e9f72327 25
samdanbury 0:6276e9f72327 26 using namespace std;
samdanbury 0:6276e9f72327 27
samdanbury 0:6276e9f72327 28 //LCD
samdanbury 0:6276e9f72327 29 C12832_LCD lcd;
samdanbury 0:6276e9f72327 30
samdanbury 0:6276e9f72327 31 //LED
samdanbury 0:6276e9f72327 32 DigitalOut led1(LED1);
rajathishere 9:cbabd85f2d5d 33 DigitalOut led4(LED4);
samdanbury 0:6276e9f72327 34
samdanbury 0:6276e9f72327 35 //Accelerometer
samdanbury 0:6276e9f72327 36 MMA7660 MMA(p28, p27);
samdanbury 0:6276e9f72327 37
samdanbury 0:6276e9f72327 38 //Temperature sensor
samdanbury 0:6276e9f72327 39 LM75B tmp(p28, p27);
samdanbury 0:6276e9f72327 40
samdanbury 0:6276e9f72327 41 //Joystick
samdanbury 0:6276e9f72327 42 BusIn Down(p12);
samdanbury 0:6276e9f72327 43 BusIn Left(p13);
samdanbury 0:6276e9f72327 44 BusOut Click(p14);
samdanbury 0:6276e9f72327 45 BusIn Up(p15);
samdanbury 0:6276e9f72327 46 BusIn Right(p16);
samdanbury 0:6276e9f72327 47 string joystickPos;
samdanbury 0:6276e9f72327 48 void joystickThread(void const *args);
rajathishere 11:0fa63599f5a0 49 int getCount(char* message);
samdanbury 0:6276e9f72327 50
samdanbury 0:6276e9f72327 51 //Potentiometer
samdanbury 0:6276e9f72327 52 AnalogIn ain1(p19);
samdanbury 0:6276e9f72327 53 AnalogIn ain2(p20);
samdanbury 0:6276e9f72327 54 float pot1;
samdanbury 0:6276e9f72327 55 float pot2;
rajathishere 9:cbabd85f2d5d 56 QuickstartClient* c;
rajathishere 9:cbabd85f2d5d 57 LocalFileSystem local("local");
samdanbury 0:6276e9f72327 58
rajathishere 9:cbabd85f2d5d 59 int getdata(char* buf, int count)
rajathishere 9:cbabd85f2d5d 60 {
rajathishere 9:cbabd85f2d5d 61 return (c->mysock).receive(buf, (size_t)count);
rajathishere 9:cbabd85f2d5d 62 }
samdanbury 2:25ddff75a8c7 63
samdanbury 0:6276e9f72327 64 int main()
samdanbury 0:6276e9f72327 65 {
rajathishere 9:cbabd85f2d5d 66 char buf[250];
rajathishere 9:cbabd85f2d5d 67 int buflen = sizeof(buf);
samdanbury 0:6276e9f72327 68 //Connect to the network
samdanbury 0:6276e9f72327 69 EthernetInterface eth;
samdanbury 0:6276e9f72327 70 eth.init();
samdanbury 0:6276e9f72327 71 eth.connect();
samdanbury 0:6276e9f72327 72
samdanbury 0:6276e9f72327 73 //Obtain mac address of mbed
samdanbury 0:6276e9f72327 74 string mac = eth.getMACAddress();
samdanbury 0:6276e9f72327 75
samdanbury 0:6276e9f72327 76 //Remove colons from mac address
samdanbury 0:6276e9f72327 77 mac.erase(remove(mac.begin(), mac.end(), ':'), mac.end());
samdanbury 0:6276e9f72327 78
samdanbury 0:6276e9f72327 79 //Start thread to read data from joystick
samdanbury 0:6276e9f72327 80 Thread jThd(joystickThread);
samdanbury 0:6276e9f72327 81 joystickPos = "CENTRE";
samdanbury 0:6276e9f72327 82
rajathishere 9:cbabd85f2d5d 83 //QuickstartClient* c = new QuickstartClient(mac);
rajathishere 9:cbabd85f2d5d 84 c = new QuickstartClient(mac);
rajathishere 11:0fa63599f5a0 85 (c->mysock).set_blocking(true, 50000);
rajathishere 9:cbabd85f2d5d 86 int res = MQTTPacket_read(buf, buflen, getdata);
rajathishere 9:cbabd85f2d5d 87 lcd.printf("res, %d\n", res);
rajathishere 11:0fa63599f5a0 88 if ( res == CONNACK)
rajathishere 11:0fa63599f5a0 89 {
rajathishere 11:0fa63599f5a0 90 int connack_rc;
rajathishere 11:0fa63599f5a0 91
rajathishere 11:0fa63599f5a0 92 if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
rajathishere 11:0fa63599f5a0 93 {
rajathishere 11:0fa63599f5a0 94 lcd.printf("Unable to connect, return code %d\n", connack_rc);
rajathishere 11:0fa63599f5a0 95 goto exit;
rajathishere 11:0fa63599f5a0 96 }
rajathishere 11:0fa63599f5a0 97 }
rajathishere 11:0fa63599f5a0 98 else
rajathishere 11:0fa63599f5a0 99 {
rajathishere 11:0fa63599f5a0 100 lcd.printf("rc conn, %d\n", res);
rajathishere 11:0fa63599f5a0 101 goto exit;
rajathishere 11:0fa63599f5a0 102 }
samdanbury 0:6276e9f72327 103
rajathishere 9:cbabd85f2d5d 104 int rc = c->subscribe();
rajathishere 9:cbabd85f2d5d 105 res = MQTTPacket_read(buf, buflen, getdata);
rajathishere 9:cbabd85f2d5d 106 lcd.printf("res, %d\n", res);
rajathishere 9:cbabd85f2d5d 107 wait(2.0);
rajathishere 9:cbabd85f2d5d 108 if (res == SUBACK) /* wait for suback */
rajathishere 9:cbabd85f2d5d 109 {
rajathishere 9:cbabd85f2d5d 110 int submsgid;
rajathishere 9:cbabd85f2d5d 111 int subcount;
rajathishere 9:cbabd85f2d5d 112 int granted_qos;
rajathishere 9:cbabd85f2d5d 113
rajathishere 9:cbabd85f2d5d 114 rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
rajathishere 9:cbabd85f2d5d 115 if (granted_qos != 0)
rajathishere 9:cbabd85f2d5d 116 {
rajathishere 9:cbabd85f2d5d 117 lcd.printf("granted qos != 0, %d\n", granted_qos);
rajathishere 9:cbabd85f2d5d 118 goto exit;
rajathishere 9:cbabd85f2d5d 119 }
rajathishere 9:cbabd85f2d5d 120 }
rajathishere 9:cbabd85f2d5d 121 else {
rajathishere 9:cbabd85f2d5d 122 lcd.printf("rc subs 2, %d\n", rc);
rajathishere 9:cbabd85f2d5d 123 goto exit;
rajathishere 9:cbabd85f2d5d 124 }
rajathishere 9:cbabd85f2d5d 125
rajathishere 7:2c5d0dbd7985 126 char value[10];
rajathishere 7:2c5d0dbd7985 127 //string data_points = "";
samdanbury 0:6276e9f72327 128 while(1)
samdanbury 0:6276e9f72327 129 {
samdanbury 0:6276e9f72327 130 //Initialize lcd
rajathishere 8:e58e10ca4352 131 lcd.cls();
rajathishere 8:e58e10ca4352 132 lcd.locate(0,0);
rajathishere 8:e58e10ca4352 133 lcd.printf("Mac address: %s\n", mac);
samdanbury 0:6276e9f72327 134 //Flash led to show message has been sent successfully
samdanbury 0:6276e9f72327 135 led1 = 1;
rajathishere 7:2c5d0dbd7985 136 string data_points = "\"myName\": \"IoT mbed\"";
rajathishere 6:a022f983f94b 137
rajathishere 7:2c5d0dbd7985 138 sprintf(value, "%0.4f", MMA.x());
rajathishere 7:2c5d0dbd7985 139 data_points+=",\"accelX\":";
rajathishere 7:2c5d0dbd7985 140 data_points+=value;
rajathishere 7:2c5d0dbd7985 141 sprintf(value, "%0.4f", MMA.y());
rajathishere 7:2c5d0dbd7985 142 data_points+=",\"accelY\":";
rajathishere 7:2c5d0dbd7985 143 data_points+=value;
rajathishere 7:2c5d0dbd7985 144 sprintf(value, "%0.4f", MMA.z());
rajathishere 7:2c5d0dbd7985 145 data_points+=",\"accelZ\":";
rajathishere 7:2c5d0dbd7985 146 data_points+=value;
rajathishere 7:2c5d0dbd7985 147 sprintf(value, "%0.4f", tmp.read());
rajathishere 7:2c5d0dbd7985 148 data_points+=",\"temp\":";
rajathishere 7:2c5d0dbd7985 149 data_points+=value;
rajathishere 7:2c5d0dbd7985 150 data_points+=",\"joystick\":" + joystickPos;
rajathishere 7:2c5d0dbd7985 151 pot1 = ain1;
rajathishere 7:2c5d0dbd7985 152 pot2 = ain2;
rajathishere 7:2c5d0dbd7985 153 sprintf(value, "%0.4f", pot1);
rajathishere 7:2c5d0dbd7985 154 data_points+=",\"potentiometer1\":";
rajathishere 7:2c5d0dbd7985 155 data_points+=value;
rajathishere 7:2c5d0dbd7985 156 sprintf(value, "%0.4f", pot1);
rajathishere 7:2c5d0dbd7985 157 data_points+=",\"potentiometer1\":";
rajathishere 7:2c5d0dbd7985 158 data_points+=value;
rajathishere 7:2c5d0dbd7985 159
rajathishere 7:2c5d0dbd7985 160 string message = "{\"d\":{" + data_points + "}}";
samdanbury 0:6276e9f72327 161
rajathishere 9:cbabd85f2d5d 162 //TODO Get both publish and subscribe working togather
rajathishere 9:cbabd85f2d5d 163 //c->publish(message);//c->publish(m->convertToJson());
rajathishere 9:cbabd85f2d5d 164
samdanbury 0:6276e9f72327 165 led1 = 0;
rajathishere 9:cbabd85f2d5d 166 //TODO get subscription working consistently
rajathishere 9:cbabd85f2d5d 167 res = MQTTPacket_read(buf, buflen, getdata);
rajathishere 9:cbabd85f2d5d 168 lcd.printf("pub res : %d",res);
rajathishere 9:cbabd85f2d5d 169 if ( res == PUBLISH )
rajathishere 9:cbabd85f2d5d 170 {
rajathishere 9:cbabd85f2d5d 171 int dup;
rajathishere 9:cbabd85f2d5d 172 int qos;
rajathishere 9:cbabd85f2d5d 173 int retained;
rajathishere 9:cbabd85f2d5d 174 int msgid;
rajathishere 9:cbabd85f2d5d 175 int payloadlen_in;
rajathishere 9:cbabd85f2d5d 176 char* payload_in;
rajathishere 9:cbabd85f2d5d 177 int rc;
rajathishere 9:cbabd85f2d5d 178 MQTTString receivedTopic;
rajathishere 9:cbabd85f2d5d 179
rajathishere 9:cbabd85f2d5d 180 rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
rajathishere 9:cbabd85f2d5d 181 &payload_in, &payloadlen_in, buf, buflen);
rajathishere 9:cbabd85f2d5d 182 lcd.printf("message arrived %s\n", payload_in);
rajathishere 11:0fa63599f5a0 183 wait(4.0);
rajathishere 11:0fa63599f5a0 184 char message[50];
rajathishere 11:0fa63599f5a0 185 int i =0, charNums = 0;
rajathishere 11:0fa63599f5a0 186 while(*payload_in && charNums <2) {
rajathishere 11:0fa63599f5a0 187 charNums = 0;
rajathishere 11:0fa63599f5a0 188 if(*payload_in == '\'') {
rajathishere 11:0fa63599f5a0 189 charNums++;
rajathishere 11:0fa63599f5a0 190 while(*payload_in != '\'') {
rajathishere 11:0fa63599f5a0 191 message[i] = *payload_in;
rajathishere 11:0fa63599f5a0 192 i++; payload_in++;
rajathishere 11:0fa63599f5a0 193 }
rajathishere 11:0fa63599f5a0 194 charNums++;
rajathishere 11:0fa63599f5a0 195 }
rajathishere 11:0fa63599f5a0 196 payload_in++;
rajathishere 11:0fa63599f5a0 197 }
rajathishere 11:0fa63599f5a0 198 message[i] = '\0';
rajathishere 11:0fa63599f5a0 199 lcd.cls();
rajathishere 11:0fa63599f5a0 200 lcd.locate(0,0);
rajathishere 11:0fa63599f5a0 201 lcd.printf(" Message %s", message);
rajathishere 11:0fa63599f5a0 202 wait(4.0);
rajathishere 11:0fa63599f5a0 203 int count = getCount(payload_in);
rajathishere 11:0fa63599f5a0 204 lcd.printf("Count %d\n", count);
rajathishere 9:cbabd85f2d5d 205 while(count) {
rajathishere 9:cbabd85f2d5d 206 led4 = 1;
rajathishere 9:cbabd85f2d5d 207 wait(0.5);
rajathishere 9:cbabd85f2d5d 208 led4 = 0;
rajathishere 9:cbabd85f2d5d 209 wait(0.5);
rajathishere 9:cbabd85f2d5d 210 count--;
rajathishere 9:cbabd85f2d5d 211 }
rajathishere 9:cbabd85f2d5d 212
rajathishere 9:cbabd85f2d5d 213 }
rajathishere 11:0fa63599f5a0 214 wait(4.0);
samdanbury 0:6276e9f72327 215 }
rajathishere 9:cbabd85f2d5d 216 exit:
samdanbury 0:6276e9f72327 217 eth.disconnect();
samdanbury 0:6276e9f72327 218 }
samdanbury 0:6276e9f72327 219
rajathishere 11:0fa63599f5a0 220 int getCount(char* message) {
rajathishere 11:0fa63599f5a0 221 cJSON *json;
rajathishere 11:0fa63599f5a0 222 int count = 2;
rajathishere 11:0fa63599f5a0 223 json = cJSON_Parse(message);
rajathishere 11:0fa63599f5a0 224 if (!json) {
rajathishere 11:0fa63599f5a0 225 lcd.printf("Error before: [%s]\n", cJSON_GetErrorPtr());
rajathishere 11:0fa63599f5a0 226 wait(2.0);
rajathishere 11:0fa63599f5a0 227 } else {
rajathishere 11:0fa63599f5a0 228 count = cJSON_GetObjectItem(json, "count")->valueint;
rajathishere 11:0fa63599f5a0 229 cJSON_Delete(json);
rajathishere 11:0fa63599f5a0 230 }
rajathishere 11:0fa63599f5a0 231 return count;
rajathishere 11:0fa63599f5a0 232 }
rajathishere 11:0fa63599f5a0 233
samdanbury 0:6276e9f72327 234 void joystickThread(void const *args) {
samdanbury 0:6276e9f72327 235 while (1) {
samdanbury 0:6276e9f72327 236 if (Down)
samdanbury 0:6276e9f72327 237 joystickPos = "DOWN";
samdanbury 0:6276e9f72327 238 else if (Left)
samdanbury 0:6276e9f72327 239 joystickPos = "LEFT";
samdanbury 0:6276e9f72327 240 else if (Click)
samdanbury 0:6276e9f72327 241 joystickPos = "CLICK";
samdanbury 0:6276e9f72327 242 else if (Up)
samdanbury 0:6276e9f72327 243 joystickPos = "UP";
samdanbury 0:6276e9f72327 244 else if (Right)
samdanbury 0:6276e9f72327 245 joystickPos = "RIGHT";
samdanbury 0:6276e9f72327 246 else
samdanbury 0:6276e9f72327 247 joystickPos = "CENTRE";
samdanbury 0:6276e9f72327 248 }
rajathishere 6:a022f983f94b 249 }