Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
src/main.cpp@13:65e87bd958bd, 2014-07-02 (annotated)
- Committer:
- rajathishere
- Date:
- Wed Jul 02 15:07:40 2014 +0000
- Revision:
- 13:65e87bd958bd
- Parent:
- 11:0fa63599f5a0
Rename QuickstartClient to ExampleClient
Who changed what in which revision?
User | Revision | Line number | New 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" |
rajathishere | 13:65e87bd958bd | 21 | #include "ExampleClient.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 | 13:65e87bd958bd | 56 | ExampleClient* 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 | 13:65e87bd958bd | 83 | //ExampleClient* c = new ExampleClient(mac); |
rajathishere | 13:65e87bd958bd | 84 | c = new ExampleClient(mac); |
rajathishere | 11:0fa63599f5a0 | 85 | (c->mysock).set_blocking(true, 50000); |
rajathishere | 9:cbabd85f2d5d | 86 | int res = MQTTPacket_read(buf, buflen, getdata); |
rajathishere | 11:0fa63599f5a0 | 87 | if ( res == CONNACK) |
rajathishere | 11:0fa63599f5a0 | 88 | { |
rajathishere | 11:0fa63599f5a0 | 89 | int connack_rc; |
rajathishere | 11:0fa63599f5a0 | 90 | |
rajathishere | 11:0fa63599f5a0 | 91 | if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) |
rajathishere | 11:0fa63599f5a0 | 92 | { |
rajathishere | 11:0fa63599f5a0 | 93 | lcd.printf("Unable to connect, return code %d\n", connack_rc); |
rajathishere | 11:0fa63599f5a0 | 94 | goto exit; |
rajathishere | 11:0fa63599f5a0 | 95 | } |
rajathishere | 11:0fa63599f5a0 | 96 | } |
rajathishere | 11:0fa63599f5a0 | 97 | else |
rajathishere | 11:0fa63599f5a0 | 98 | { |
rajathishere | 11:0fa63599f5a0 | 99 | lcd.printf("rc conn, %d\n", res); |
rajathishere | 11:0fa63599f5a0 | 100 | goto exit; |
rajathishere | 11:0fa63599f5a0 | 101 | } |
samdanbury | 0:6276e9f72327 | 102 | |
rajathishere | 9:cbabd85f2d5d | 103 | int rc = c->subscribe(); |
rajathishere | 9:cbabd85f2d5d | 104 | res = MQTTPacket_read(buf, buflen, getdata); |
rajathishere | 9:cbabd85f2d5d | 105 | if (res == SUBACK) /* wait for suback */ |
rajathishere | 9:cbabd85f2d5d | 106 | { |
rajathishere | 9:cbabd85f2d5d | 107 | int submsgid; |
rajathishere | 9:cbabd85f2d5d | 108 | int subcount; |
rajathishere | 9:cbabd85f2d5d | 109 | int granted_qos; |
rajathishere | 9:cbabd85f2d5d | 110 | |
rajathishere | 9:cbabd85f2d5d | 111 | rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); |
rajathishere | 9:cbabd85f2d5d | 112 | if (granted_qos != 0) |
rajathishere | 9:cbabd85f2d5d | 113 | { |
rajathishere | 9:cbabd85f2d5d | 114 | lcd.printf("granted qos != 0, %d\n", granted_qos); |
rajathishere | 9:cbabd85f2d5d | 115 | goto exit; |
rajathishere | 9:cbabd85f2d5d | 116 | } |
rajathishere | 9:cbabd85f2d5d | 117 | } |
rajathishere | 9:cbabd85f2d5d | 118 | else { |
rajathishere | 9:cbabd85f2d5d | 119 | lcd.printf("rc subs 2, %d\n", rc); |
rajathishere | 9:cbabd85f2d5d | 120 | goto exit; |
rajathishere | 9:cbabd85f2d5d | 121 | } |
rajathishere | 9:cbabd85f2d5d | 122 | |
rajathishere | 7:2c5d0dbd7985 | 123 | char value[10]; |
rajathishere | 7:2c5d0dbd7985 | 124 | //string data_points = ""; |
samdanbury | 0:6276e9f72327 | 125 | while(1) |
samdanbury | 0:6276e9f72327 | 126 | { |
samdanbury | 0:6276e9f72327 | 127 | //Initialize lcd |
rajathishere | 8:e58e10ca4352 | 128 | lcd.cls(); |
rajathishere | 8:e58e10ca4352 | 129 | lcd.locate(0,0); |
rajathishere | 8:e58e10ca4352 | 130 | lcd.printf("Mac address: %s\n", mac); |
samdanbury | 0:6276e9f72327 | 131 | //Flash led to show message has been sent successfully |
samdanbury | 0:6276e9f72327 | 132 | led1 = 1; |
rajathishere | 7:2c5d0dbd7985 | 133 | string data_points = "\"myName\": \"IoT mbed\""; |
rajathishere | 6:a022f983f94b | 134 | |
rajathishere | 7:2c5d0dbd7985 | 135 | sprintf(value, "%0.4f", MMA.x()); |
rajathishere | 7:2c5d0dbd7985 | 136 | data_points+=",\"accelX\":"; |
rajathishere | 7:2c5d0dbd7985 | 137 | data_points+=value; |
rajathishere | 7:2c5d0dbd7985 | 138 | sprintf(value, "%0.4f", MMA.y()); |
rajathishere | 7:2c5d0dbd7985 | 139 | data_points+=",\"accelY\":"; |
rajathishere | 7:2c5d0dbd7985 | 140 | data_points+=value; |
rajathishere | 7:2c5d0dbd7985 | 141 | sprintf(value, "%0.4f", MMA.z()); |
rajathishere | 7:2c5d0dbd7985 | 142 | data_points+=",\"accelZ\":"; |
rajathishere | 7:2c5d0dbd7985 | 143 | data_points+=value; |
rajathishere | 7:2c5d0dbd7985 | 144 | sprintf(value, "%0.4f", tmp.read()); |
rajathishere | 7:2c5d0dbd7985 | 145 | data_points+=",\"temp\":"; |
rajathishere | 7:2c5d0dbd7985 | 146 | data_points+=value; |
rajathishere | 7:2c5d0dbd7985 | 147 | data_points+=",\"joystick\":" + joystickPos; |
rajathishere | 7:2c5d0dbd7985 | 148 | pot1 = ain1; |
rajathishere | 7:2c5d0dbd7985 | 149 | pot2 = ain2; |
rajathishere | 7:2c5d0dbd7985 | 150 | sprintf(value, "%0.4f", pot1); |
rajathishere | 7:2c5d0dbd7985 | 151 | data_points+=",\"potentiometer1\":"; |
rajathishere | 7:2c5d0dbd7985 | 152 | data_points+=value; |
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 | |
rajathishere | 7:2c5d0dbd7985 | 157 | string message = "{\"d\":{" + data_points + "}}"; |
samdanbury | 0:6276e9f72327 | 158 | |
rajathishere | 9:cbabd85f2d5d | 159 | //TODO Get both publish and subscribe working togather |
rajathishere | 9:cbabd85f2d5d | 160 | //c->publish(message);//c->publish(m->convertToJson()); |
rajathishere | 9:cbabd85f2d5d | 161 | |
samdanbury | 0:6276e9f72327 | 162 | led1 = 0; |
rajathishere | 9:cbabd85f2d5d | 163 | //TODO get subscription working consistently |
rajathishere | 9:cbabd85f2d5d | 164 | res = MQTTPacket_read(buf, buflen, getdata); |
rajathishere | 9:cbabd85f2d5d | 165 | if ( res == PUBLISH ) |
rajathishere | 9:cbabd85f2d5d | 166 | { |
rajathishere | 9:cbabd85f2d5d | 167 | int dup; |
rajathishere | 9:cbabd85f2d5d | 168 | int qos; |
rajathishere | 9:cbabd85f2d5d | 169 | int retained; |
rajathishere | 9:cbabd85f2d5d | 170 | int msgid; |
rajathishere | 9:cbabd85f2d5d | 171 | int payloadlen_in; |
rajathishere | 9:cbabd85f2d5d | 172 | char* payload_in; |
rajathishere | 9:cbabd85f2d5d | 173 | int rc; |
rajathishere | 9:cbabd85f2d5d | 174 | MQTTString receivedTopic; |
rajathishere | 9:cbabd85f2d5d | 175 | |
rajathishere | 9:cbabd85f2d5d | 176 | rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, |
rajathishere | 9:cbabd85f2d5d | 177 | &payload_in, &payloadlen_in, buf, buflen); |
rajathishere | 11:0fa63599f5a0 | 178 | char message[50]; |
rajathishere | 11:0fa63599f5a0 | 179 | int i =0, charNums = 0; |
rajathishere | 11:0fa63599f5a0 | 180 | while(*payload_in && charNums <2) { |
rajathishere | 11:0fa63599f5a0 | 181 | charNums = 0; |
rajathishere | 11:0fa63599f5a0 | 182 | if(*payload_in == '\'') { |
rajathishere | 11:0fa63599f5a0 | 183 | charNums++; |
rajathishere | 11:0fa63599f5a0 | 184 | while(*payload_in != '\'') { |
rajathishere | 11:0fa63599f5a0 | 185 | message[i] = *payload_in; |
rajathishere | 11:0fa63599f5a0 | 186 | i++; payload_in++; |
rajathishere | 11:0fa63599f5a0 | 187 | } |
rajathishere | 11:0fa63599f5a0 | 188 | charNums++; |
rajathishere | 11:0fa63599f5a0 | 189 | } |
rajathishere | 11:0fa63599f5a0 | 190 | payload_in++; |
rajathishere | 11:0fa63599f5a0 | 191 | } |
rajathishere | 11:0fa63599f5a0 | 192 | message[i] = '\0'; |
rajathishere | 11:0fa63599f5a0 | 193 | int count = getCount(payload_in); |
rajathishere | 11:0fa63599f5a0 | 194 | lcd.printf("Count %d\n", count); |
rajathishere | 9:cbabd85f2d5d | 195 | while(count) { |
rajathishere | 9:cbabd85f2d5d | 196 | led4 = 1; |
rajathishere | 9:cbabd85f2d5d | 197 | wait(0.5); |
rajathishere | 9:cbabd85f2d5d | 198 | led4 = 0; |
rajathishere | 9:cbabd85f2d5d | 199 | wait(0.5); |
rajathishere | 9:cbabd85f2d5d | 200 | count--; |
rajathishere | 9:cbabd85f2d5d | 201 | } |
rajathishere | 9:cbabd85f2d5d | 202 | |
rajathishere | 9:cbabd85f2d5d | 203 | } |
rajathishere | 13:65e87bd958bd | 204 | wait(1.0); |
samdanbury | 0:6276e9f72327 | 205 | } |
rajathishere | 9:cbabd85f2d5d | 206 | exit: |
samdanbury | 0:6276e9f72327 | 207 | eth.disconnect(); |
samdanbury | 0:6276e9f72327 | 208 | } |
samdanbury | 0:6276e9f72327 | 209 | |
rajathishere | 11:0fa63599f5a0 | 210 | int getCount(char* message) { |
rajathishere | 11:0fa63599f5a0 | 211 | cJSON *json; |
rajathishere | 11:0fa63599f5a0 | 212 | int count = 2; |
rajathishere | 11:0fa63599f5a0 | 213 | json = cJSON_Parse(message); |
rajathishere | 11:0fa63599f5a0 | 214 | if (!json) { |
rajathishere | 11:0fa63599f5a0 | 215 | lcd.printf("Error before: [%s]\n", cJSON_GetErrorPtr()); |
rajathishere | 11:0fa63599f5a0 | 216 | wait(2.0); |
rajathishere | 11:0fa63599f5a0 | 217 | } else { |
rajathishere | 11:0fa63599f5a0 | 218 | count = cJSON_GetObjectItem(json, "count")->valueint; |
rajathishere | 11:0fa63599f5a0 | 219 | cJSON_Delete(json); |
rajathishere | 11:0fa63599f5a0 | 220 | } |
rajathishere | 11:0fa63599f5a0 | 221 | return count; |
rajathishere | 11:0fa63599f5a0 | 222 | } |
rajathishere | 11:0fa63599f5a0 | 223 | |
samdanbury | 0:6276e9f72327 | 224 | void joystickThread(void const *args) { |
samdanbury | 0:6276e9f72327 | 225 | while (1) { |
samdanbury | 0:6276e9f72327 | 226 | if (Down) |
samdanbury | 0:6276e9f72327 | 227 | joystickPos = "DOWN"; |
samdanbury | 0:6276e9f72327 | 228 | else if (Left) |
samdanbury | 0:6276e9f72327 | 229 | joystickPos = "LEFT"; |
samdanbury | 0:6276e9f72327 | 230 | else if (Click) |
samdanbury | 0:6276e9f72327 | 231 | joystickPos = "CLICK"; |
samdanbury | 0:6276e9f72327 | 232 | else if (Up) |
samdanbury | 0:6276e9f72327 | 233 | joystickPos = "UP"; |
samdanbury | 0:6276e9f72327 | 234 | else if (Right) |
samdanbury | 0:6276e9f72327 | 235 | joystickPos = "RIGHT"; |
samdanbury | 0:6276e9f72327 | 236 | else |
samdanbury | 0:6276e9f72327 | 237 | joystickPos = "CENTRE"; |
samdanbury | 0:6276e9f72327 | 238 | } |
rajathishere | 6:a022f983f94b | 239 | } |