Proximity sensor mqtt version
Dependencies: MQTT-JMF WNCInterface mbed-rtos mbed
Fork of WNCInterface_MQTT_hivemq by
main.cpp@1:1b516e98444f, 2016-09-27 (annotated)
- Committer:
- elmkom
- Date:
- Tue Sep 27 15:47:49 2016 +0000
- Revision:
- 1:1b516e98444f
- Parent:
- 0:73334e2a82be
Att proximity sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:73334e2a82be | 1 | //#define MQTT_DEBUG |
JMF | 0:73334e2a82be | 2 | |
JMF | 0:73334e2a82be | 3 | #include "mbed.h" |
JMF | 0:73334e2a82be | 4 | #include "MQTTClient.h" |
JMF | 0:73334e2a82be | 5 | #include "MQTTFormat.h" |
JMF | 0:73334e2a82be | 6 | |
JMF | 0:73334e2a82be | 7 | //#include "MQTTEthernet.h" |
JMF | 0:73334e2a82be | 8 | #include "MQTTWNCInterface.h" |
JMF | 0:73334e2a82be | 9 | #include "rtos.h" |
JMF | 0:73334e2a82be | 10 | #include "k64f.h" |
elmkom | 1:1b516e98444f | 11 | #include "Proximity.h" |
JMF | 0:73334e2a82be | 12 | |
JMF | 0:73334e2a82be | 13 | |
JMF | 0:73334e2a82be | 14 | // connect options for MQTT broker |
elmkom | 1:1b516e98444f | 15 | #define BROKER "iot.foundry.att.com" // MQTT broker URL |
elmkom | 1:1b516e98444f | 16 | #define PORT 1880 // MQTT broker port number |
JMF | 0:73334e2a82be | 17 | #define CLIENTID "96430312d8f7" // use K64F MAC address without colons |
JMF | 0:73334e2a82be | 18 | #define USERNAME "" // not required for MQTT Dashboard public broker |
JMF | 0:73334e2a82be | 19 | #define PASSWORD "" // not required for MQTT Dashboard public broker |
elmkom | 1:1b516e98444f | 20 | #define TOPIC "proximity" // MQTT topic |
JMF | 0:73334e2a82be | 21 | |
elmkom | 1:1b516e98444f | 22 | Proximity proximityStrip; |
JMF | 0:73334e2a82be | 23 | Queue<uint32_t, 6> messageQ; |
JMF | 0:73334e2a82be | 24 | |
JMF | 0:73334e2a82be | 25 | struct rcvd_errs{ |
JMF | 0:73334e2a82be | 26 | int err; |
JMF | 0:73334e2a82be | 27 | char *er; |
JMF | 0:73334e2a82be | 28 | }; |
JMF | 0:73334e2a82be | 29 | |
JMF | 0:73334e2a82be | 30 | rcvd_errs response[] = { |
JMF | 0:73334e2a82be | 31 | 200, "200 OK - Request has succeeded.", |
JMF | 0:73334e2a82be | 32 | 201, "201 Created - Request has been fulfilled and a new resource created.", |
JMF | 0:73334e2a82be | 33 | 202, "202 Accepted - The request has been accepted for processing, but the processing will be completed asynchronously", |
JMF | 0:73334e2a82be | 34 | 204, "204 No Content - The server has fulfilled the request but does not need to return an entity-body.", |
JMF | 0:73334e2a82be | 35 | 400, "400 Bad Request - Bad request (e.g. sending an array when the API is expecting a hash.)", |
JMF | 0:73334e2a82be | 36 | 401, "401 Unauthorized - No valid API key provided.", |
JMF | 0:73334e2a82be | 37 | 403, "403 Forbidden - You tried to access a disabled device, or your API key is not allowed to access that resource, etc.", |
JMF | 0:73334e2a82be | 38 | 404, "404 Not Found - The requested item could not be found.", |
JMF | 0:73334e2a82be | 39 | 405, "405 Method Not Allowed - The HTTP method specified is not allowed.", |
JMF | 0:73334e2a82be | 40 | 415, "415 Unsupported Media Type - The requested media type is currently not supported.", |
JMF | 0:73334e2a82be | 41 | 422, "422 Unprocessable Entity - Can result from sending invalid fields.", |
JMF | 0:73334e2a82be | 42 | 429, "429 Too Many Requests - The user has sent too many requests in a given period of time.", |
JMF | 0:73334e2a82be | 43 | 500, "500 Server errors - Something went wrong in the M2X server", |
JMF | 0:73334e2a82be | 44 | 502, "502 Server errors - Something went wrong in the M2X server", |
JMF | 0:73334e2a82be | 45 | 503, "503 Server errors - Something went wrong in the M2X server", |
JMF | 0:73334e2a82be | 46 | 504, "504 Server errors - Something went wrong in the M2X server", |
JMF | 0:73334e2a82be | 47 | }; |
JMF | 0:73334e2a82be | 48 | #define RCMAX sizeof(response)/sizeof(rcvd_errs) |
JMF | 0:73334e2a82be | 49 | |
JMF | 0:73334e2a82be | 50 | char * response_str(int rc) { |
JMF | 0:73334e2a82be | 51 | static char *unkown = "Unknown error code..."; |
JMF | 0:73334e2a82be | 52 | int i=0; |
JMF | 0:73334e2a82be | 53 | while( response[i].err != rc && i < RCMAX) |
JMF | 0:73334e2a82be | 54 | i++; |
JMF | 0:73334e2a82be | 55 | return (i<RCMAX? response[i].er : unkown); |
JMF | 0:73334e2a82be | 56 | } |
JMF | 0:73334e2a82be | 57 | |
JMF | 0:73334e2a82be | 58 | // LED color control function |
JMF | 0:73334e2a82be | 59 | void controlLED(color_t led_color) { |
JMF | 0:73334e2a82be | 60 | switch(led_color) { |
JMF | 0:73334e2a82be | 61 | case red : |
JMF | 0:73334e2a82be | 62 | greenLED = blueLED = 1; |
JMF | 0:73334e2a82be | 63 | redLED = 0.7; |
JMF | 0:73334e2a82be | 64 | break; |
JMF | 0:73334e2a82be | 65 | case green : |
JMF | 0:73334e2a82be | 66 | redLED = blueLED = 1; |
JMF | 0:73334e2a82be | 67 | greenLED = 0.7; |
JMF | 0:73334e2a82be | 68 | break; |
JMF | 0:73334e2a82be | 69 | case blue : |
JMF | 0:73334e2a82be | 70 | redLED = greenLED = 1; |
JMF | 0:73334e2a82be | 71 | blueLED = 0.7; |
JMF | 0:73334e2a82be | 72 | break; |
JMF | 0:73334e2a82be | 73 | case off : |
JMF | 0:73334e2a82be | 74 | redLED = greenLED = blueLED = 1; |
JMF | 0:73334e2a82be | 75 | break; |
JMF | 0:73334e2a82be | 76 | } |
JMF | 0:73334e2a82be | 77 | } |
JMF | 0:73334e2a82be | 78 | |
JMF | 0:73334e2a82be | 79 | // Switch 2 interrupt handler |
JMF | 0:73334e2a82be | 80 | void sw2_ISR(void) { |
JMF | 0:73334e2a82be | 81 | messageQ.put((uint32_t*)22); |
JMF | 0:73334e2a82be | 82 | } |
JMF | 0:73334e2a82be | 83 | |
JMF | 0:73334e2a82be | 84 | // Switch3 interrupt handler |
JMF | 0:73334e2a82be | 85 | void sw3_ISR(void) { |
JMF | 0:73334e2a82be | 86 | messageQ.put((uint32_t*)33); |
JMF | 0:73334e2a82be | 87 | } |
JMF | 0:73334e2a82be | 88 | |
JMF | 0:73334e2a82be | 89 | // MQTT message arrived callback function |
JMF | 0:73334e2a82be | 90 | void messageArrived(MQTT::MessageData& md) { |
JMF | 0:73334e2a82be | 91 | MQTT::Message &message = md.message; |
JMF | 0:73334e2a82be | 92 | printf("Receiving MQTT message: %.*s\r\n", message.payloadlen, (char*)message.payload); |
JMF | 0:73334e2a82be | 93 | |
JMF | 0:73334e2a82be | 94 | if (message.payloadlen == 3) { |
JMF | 0:73334e2a82be | 95 | if (strncmp((char*)message.payload, "red", 3) == 0) |
JMF | 0:73334e2a82be | 96 | controlLED(red); |
JMF | 0:73334e2a82be | 97 | |
JMF | 0:73334e2a82be | 98 | else if(strncmp((char*)message.payload, "grn", 3) == 0) |
JMF | 0:73334e2a82be | 99 | controlLED(green); |
JMF | 0:73334e2a82be | 100 | |
JMF | 0:73334e2a82be | 101 | else if(strncmp((char*)message.payload, "blu", 3) == 0) |
JMF | 0:73334e2a82be | 102 | controlLED(blue); |
JMF | 0:73334e2a82be | 103 | |
JMF | 0:73334e2a82be | 104 | else if(strncmp((char*)message.payload, "off", 3) == 0) |
JMF | 0:73334e2a82be | 105 | controlLED(off); |
JMF | 0:73334e2a82be | 106 | } |
JMF | 0:73334e2a82be | 107 | } |
JMF | 0:73334e2a82be | 108 | |
JMF | 0:73334e2a82be | 109 | int main() { |
JMF | 0:73334e2a82be | 110 | int rc, good = 0; |
JMF | 0:73334e2a82be | 111 | Timer tmr; |
JMF | 0:73334e2a82be | 112 | char* topic = TOPIC; |
elmkom | 1:1b516e98444f | 113 | int count = 0; |
JMF | 0:73334e2a82be | 114 | // turn off LED |
elmkom | 1:1b516e98444f | 115 | controlLED(red); |
JMF | 0:73334e2a82be | 116 | |
JMF | 0:73334e2a82be | 117 | // set SW2 and SW3 to generate interrupt on falling edge |
JMF | 0:73334e2a82be | 118 | switch2.fall(&sw2_ISR); |
JMF | 0:73334e2a82be | 119 | switch3.fall(&sw3_ISR); |
JMF | 0:73334e2a82be | 120 | |
JMF | 0:73334e2a82be | 121 | // initialize ethernet interface |
JMF | 0:73334e2a82be | 122 | MQTTwnc ipstack = MQTTwnc(); |
JMF | 0:73334e2a82be | 123 | |
JMF | 0:73334e2a82be | 124 | // get and display client network info |
JMF | 0:73334e2a82be | 125 | WNCInterface& eth = ipstack.getEth(); |
JMF | 0:73334e2a82be | 126 | |
JMF | 0:73334e2a82be | 127 | // construct the MQTT client |
JMF | 0:73334e2a82be | 128 | MQTT::Client<MQTTwnc, Countdown> client = MQTT::Client<MQTTwnc, Countdown>(ipstack); |
JMF | 0:73334e2a82be | 129 | |
JMF | 0:73334e2a82be | 130 | char* hostname = BROKER; |
JMF | 0:73334e2a82be | 131 | int port = PORT; |
JMF | 0:73334e2a82be | 132 | |
JMF | 0:73334e2a82be | 133 | printf("\r\n\r\nWelcome to the K64F MQTT Demo!\r\n"); |
JMF | 0:73334e2a82be | 134 | printf("\r\nConnected to local network...\r\n"); |
JMF | 0:73334e2a82be | 135 | printf("IP address is %s\r\n", eth.getIPAddress()); |
JMF | 0:73334e2a82be | 136 | printf("MAC address is %s\r\n", eth.getMACAddress()); |
JMF | 0:73334e2a82be | 137 | printf("Gateway address is %s\r\n", eth.getGateway()); |
JMF | 0:73334e2a82be | 138 | |
JMF | 0:73334e2a82be | 139 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
JMF | 0:73334e2a82be | 140 | int tries; |
JMF | 0:73334e2a82be | 141 | |
JMF | 0:73334e2a82be | 142 | while( !good ) { |
JMF | 0:73334e2a82be | 143 | tries=0; |
JMF | 0:73334e2a82be | 144 | // connect to TCP socket and check return code |
JMF | 0:73334e2a82be | 145 | tmr.start(); |
JMF | 0:73334e2a82be | 146 | rc = 1; |
JMF | 0:73334e2a82be | 147 | while( rc && tries < 3) { |
JMF | 0:73334e2a82be | 148 | printf("\r\n\r\nAttempting TCP connect to %s:%d: ", hostname, port); |
JMF | 0:73334e2a82be | 149 | rc = ipstack.connect(hostname, port); |
JMF | 0:73334e2a82be | 150 | if( rc ) { |
JMF | 0:73334e2a82be | 151 | printf("Failed!!\r\n"); |
JMF | 0:73334e2a82be | 152 | while( tmr.read_ms() < 5000 ) ; |
JMF | 0:73334e2a82be | 153 | tries++; |
JMF | 0:73334e2a82be | 154 | tmr.reset(); |
JMF | 0:73334e2a82be | 155 | } |
JMF | 0:73334e2a82be | 156 | else { |
JMF | 0:73334e2a82be | 157 | printf("Success!\r\n"); |
JMF | 0:73334e2a82be | 158 | rc = 0; |
JMF | 0:73334e2a82be | 159 | } |
JMF | 0:73334e2a82be | 160 | } |
JMF | 0:73334e2a82be | 161 | |
JMF | 0:73334e2a82be | 162 | data.MQTTVersion = 3; |
JMF | 0:73334e2a82be | 163 | data.clientID.cstring = eth.getMACAddress(); |
JMF | 0:73334e2a82be | 164 | // data.username.cstring = USERNAME; |
JMF | 0:73334e2a82be | 165 | // data.password.cstring = PASSWORD; |
JMF | 0:73334e2a82be | 166 | |
JMF | 0:73334e2a82be | 167 | // send MQTT connect packet and check return code |
JMF | 0:73334e2a82be | 168 | rc = 1; |
JMF | 0:73334e2a82be | 169 | tmr.reset(); |
JMF | 0:73334e2a82be | 170 | while( !client.isConnected() && rc && tries < 3) { |
JMF | 0:73334e2a82be | 171 | printf("Attempting (%d) MQTT connect to %s:%d: ", tries, hostname, port); |
JMF | 0:73334e2a82be | 172 | rc = client.connect(data); |
JMF | 0:73334e2a82be | 173 | if( rc ) { |
JMF | 0:73334e2a82be | 174 | printf("Connection Failed!\r\n"); |
JMF | 0:73334e2a82be | 175 | while( tmr.read_ms() < 5000 ); |
JMF | 0:73334e2a82be | 176 | tmr.reset(); |
JMF | 0:73334e2a82be | 177 | tries++; |
JMF | 0:73334e2a82be | 178 | } |
JMF | 0:73334e2a82be | 179 | else |
JMF | 0:73334e2a82be | 180 | printf("Connected!\r\n"); |
JMF | 0:73334e2a82be | 181 | } |
JMF | 0:73334e2a82be | 182 | |
JMF | 0:73334e2a82be | 183 | // subscribe to MQTT topic |
JMF | 0:73334e2a82be | 184 | tmr.reset(); |
JMF | 0:73334e2a82be | 185 | rc = 1; |
JMF | 0:73334e2a82be | 186 | while( rc && client.isConnected() && tries < 3) { |
JMF | 0:73334e2a82be | 187 | printf("We are %s, Subscribing to MQTT topic %s (%d): ", client.isConnected()?"connected":"NOT CONNECTED",topic,tries); |
JMF | 0:73334e2a82be | 188 | rc = client.subscribe(topic, MQTT::QOS0, messageArrived); |
JMF | 0:73334e2a82be | 189 | if( rc ) { |
JMF | 0:73334e2a82be | 190 | printf("Subscribe request failed!\r\n"); |
JMF | 0:73334e2a82be | 191 | while( tmr.read_ms() < 5000 ); |
JMF | 0:73334e2a82be | 192 | tries++; |
JMF | 0:73334e2a82be | 193 | tmr.reset(); |
JMF | 0:73334e2a82be | 194 | } |
JMF | 0:73334e2a82be | 195 | else { |
JMF | 0:73334e2a82be | 196 | good=1; |
JMF | 0:73334e2a82be | 197 | printf("Subscribe successful!\r\n"); |
JMF | 0:73334e2a82be | 198 | } |
JMF | 0:73334e2a82be | 199 | } |
JMF | 0:73334e2a82be | 200 | } |
elmkom | 1:1b516e98444f | 201 | controlLED(blue); |
JMF | 0:73334e2a82be | 202 | MQTT::Message message; |
elmkom | 1:1b516e98444f | 203 | char buf[512]; |
JMF | 0:73334e2a82be | 204 | message.qos = MQTT::QOS0; |
JMF | 0:73334e2a82be | 205 | message.retained = false; |
JMF | 0:73334e2a82be | 206 | message.dup = false; |
JMF | 0:73334e2a82be | 207 | message.payload = (void*)buf; |
JMF | 0:73334e2a82be | 208 | message.payloadlen = strlen(buf)+1; |
JMF | 0:73334e2a82be | 209 | |
elmkom | 1:1b516e98444f | 210 | sprintf(buf, "[\"Hi\"]"); |
elmkom | 1:1b516e98444f | 211 | message.payloadlen = strlen(buf)+1; |
elmkom | 1:1b516e98444f | 212 | rc = client.publish(topic, message); |
elmkom | 1:1b516e98444f | 213 | if( rc ) { |
elmkom | 1:1b516e98444f | 214 | printf("Publish request failed! (%d)\r\n",rc); |
elmkom | 1:1b516e98444f | 215 | } |
elmkom | 1:1b516e98444f | 216 | else { |
elmkom | 1:1b516e98444f | 217 | printf("Publish successful!\r\n"); |
elmkom | 1:1b516e98444f | 218 | client.yield(100); |
elmkom | 1:1b516e98444f | 219 | } |
elmkom | 1:1b516e98444f | 220 | |
elmkom | 1:1b516e98444f | 221 | proximityStrip.init(); |
elmkom | 1:1b516e98444f | 222 | proximityStrip.on(); |
elmkom | 1:1b516e98444f | 223 | |
JMF | 0:73334e2a82be | 224 | while(true) { |
elmkom | 1:1b516e98444f | 225 | count++; |
elmkom | 1:1b516e98444f | 226 | if(count >= 30) |
elmkom | 1:1b516e98444f | 227 | { |
elmkom | 1:1b516e98444f | 228 | count = 0; |
elmkom | 1:1b516e98444f | 229 | sprintf(buf, "wake"); |
elmkom | 1:1b516e98444f | 230 | message.payloadlen = strlen(buf)+1; |
elmkom | 1:1b516e98444f | 231 | controlLED(red); |
elmkom | 1:1b516e98444f | 232 | rc = client.publish(topic, message); |
elmkom | 1:1b516e98444f | 233 | controlLED(off); |
elmkom | 1:1b516e98444f | 234 | if( rc ) { |
elmkom | 1:1b516e98444f | 235 | printf("Publish request failed! (%d)\r\n",rc); |
elmkom | 1:1b516e98444f | 236 | } |
elmkom | 1:1b516e98444f | 237 | else { |
elmkom | 1:1b516e98444f | 238 | printf("Publish successful!\r\n"); |
elmkom | 1:1b516e98444f | 239 | client.yield(100); |
elmkom | 1:1b516e98444f | 240 | } |
elmkom | 1:1b516e98444f | 241 | } |
elmkom | 1:1b516e98444f | 242 | |
JMF | 0:73334e2a82be | 243 | osEvent switchEvent = messageQ.get(100); |
elmkom | 1:1b516e98444f | 244 | controlLED(green); |
elmkom | 1:1b516e98444f | 245 | proximityStrip.scan(); |
elmkom | 1:1b516e98444f | 246 | controlLED(off); |
elmkom | 1:1b516e98444f | 247 | if(proximityStrip.changed(50)) |
elmkom | 1:1b516e98444f | 248 | { |
elmkom | 1:1b516e98444f | 249 | controlLED(blue); |
elmkom | 1:1b516e98444f | 250 | char* data = proximityStrip.getDataStr(); |
elmkom | 1:1b516e98444f | 251 | printf("data:%s",data); |
elmkom | 1:1b516e98444f | 252 | sprintf(buf, data); |
elmkom | 1:1b516e98444f | 253 | //sprintf(buf, "[{\"s\":\"1\"},{\"s\":\"2\"}]"); |
elmkom | 1:1b516e98444f | 254 | message.payloadlen = strlen(buf)+1; |
elmkom | 1:1b516e98444f | 255 | rc = client.publish(topic, message); |
elmkom | 1:1b516e98444f | 256 | if( rc ) { |
elmkom | 1:1b516e98444f | 257 | printf("Publish request failed! (%d)\r\n",rc); |
elmkom | 1:1b516e98444f | 258 | } |
elmkom | 1:1b516e98444f | 259 | else { |
elmkom | 1:1b516e98444f | 260 | printf("Publish successful!\r\n"); |
elmkom | 1:1b516e98444f | 261 | count=0; |
elmkom | 1:1b516e98444f | 262 | client.yield(100); |
elmkom | 1:1b516e98444f | 263 | } |
elmkom | 1:1b516e98444f | 264 | } |
JMF | 0:73334e2a82be | 265 | if (switchEvent.value.v == 22 || switchEvent.value.v == 33) { |
JMF | 0:73334e2a82be | 266 | switch(switchEvent.value.v) { |
JMF | 0:73334e2a82be | 267 | case 22 : |
JMF | 0:73334e2a82be | 268 | sprintf(buf, "sw2"); |
elmkom | 1:1b516e98444f | 269 | message.payloadlen = strlen(buf)+1; |
JMF | 0:73334e2a82be | 270 | break; |
JMF | 0:73334e2a82be | 271 | case 33 : |
JMF | 0:73334e2a82be | 272 | sprintf(buf, "sw3"); |
elmkom | 1:1b516e98444f | 273 | message.payloadlen = strlen(buf)+1; |
JMF | 0:73334e2a82be | 274 | break; |
JMF | 0:73334e2a82be | 275 | } |
JMF | 0:73334e2a82be | 276 | printf("Publishing MQTT message: %s (%d)\r\n", (char*)message.payload, |
JMF | 0:73334e2a82be | 277 | message.payloadlen); |
JMF | 0:73334e2a82be | 278 | rc = client.publish(topic, message); |
JMF | 0:73334e2a82be | 279 | if( rc ) { |
JMF | 0:73334e2a82be | 280 | printf("Publish request failed! (%d)\r\n",rc); |
JMF | 0:73334e2a82be | 281 | } |
JMF | 0:73334e2a82be | 282 | else { |
elmkom | 1:1b516e98444f | 283 | printf("Publish successful!\r\n"); |
elmkom | 1:1b516e98444f | 284 | count=0; |
JMF | 0:73334e2a82be | 285 | client.yield(100); |
JMF | 0:73334e2a82be | 286 | } |
JMF | 0:73334e2a82be | 287 | } |
JMF | 0:73334e2a82be | 288 | else { |
JMF | 0:73334e2a82be | 289 | client.yield(100); |
JMF | 0:73334e2a82be | 290 | } |
JMF | 0:73334e2a82be | 291 | } |
JMF | 0:73334e2a82be | 292 | } |
JMF | 0:73334e2a82be | 293 |