
This is a program that turns your mbed device into a FireFly gateway, that publishes data from FireFly BLE modules to the IBM Watson IoT Platform.
Dependencies: C12832 EthernetInterface MQTT mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
main.cpp@23:03835082a0de, 2015-12-15 (annotated)
- Committer:
- skoda
- Date:
- Tue Dec 15 09:37:25 2015 +0000
- Revision:
- 23:03835082a0de
- Parent:
- 22:e6b69714b59a
potentially problematic code for i2c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /******************************************************************************* |
icraggs | 16:2420bfbf5f1c | 2 | * Copyright (c) 2014, 2015 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 |
icraggs | 16:2420bfbf5f1c | 18 | * James Sutton - Mac fix and extra debug |
icraggs | 16:2420bfbf5f1c | 19 | * Ian Craggs - add not authorized messages |
chris | 10:0b5e0dfee08e | 20 | * |
chris | 10:0b5e0dfee08e | 21 | * To do : |
chris | 10:0b5e0dfee08e | 22 | * Add magnetometer sensor output to IoT data stream |
chris | 10:0b5e0dfee08e | 23 | * |
samdanbury | 6:37b6d0d56190 | 24 | *******************************************************************************/ |
samdanbury | 6:37b6d0d56190 | 25 | |
samdanbury | 6:37b6d0d56190 | 26 | #include "LM75B.h" |
samdanbury | 6:37b6d0d56190 | 27 | #include "MMA7660.h" |
samdanbury | 6:37b6d0d56190 | 28 | #include "MQTTClient.h" |
samdanbury | 6:37b6d0d56190 | 29 | #include "MQTTEthernet.h" |
samdanbury | 6:37b6d0d56190 | 30 | #include "C12832.h" |
samdanbury | 6:37b6d0d56190 | 31 | #include "Arial12x12.h" |
samdanbury | 6:37b6d0d56190 | 32 | #include "rtos.h" |
samdanbury | 6:37b6d0d56190 | 33 | |
chris | 10:0b5e0dfee08e | 34 | // Update this to the next number *before* a commit |
icraggs | 18:94da9de96d54 | 35 | #define __APP_SW_REVISION__ "18" |
chris | 10:0b5e0dfee08e | 36 | |
samdanbury | 6:37b6d0d56190 | 37 | // Configuration values needed to connect to IBM IoT Cloud |
skoda | 23:03835082a0de | 38 | #define ORG "gpr4zb" // For a registered connection, replace with your org |
skoda | 23:03835082a0de | 39 | #define ID "FFgateway" // For a registered connection, replace with your id |
skoda | 19:33804f085577 | 40 | #define AUTH_TOKEN "123456789" // For a registered connection, replace with your auth-token |
skoda | 19:33804f085577 | 41 | #define TYPE "FireFly" // For a registered connection, replace with your type |
samdanbury | 6:37b6d0d56190 | 42 | |
samdanbury | 6:37b6d0d56190 | 43 | #define MQTT_PORT 1883 |
samdanbury | 6:37b6d0d56190 | 44 | #define MQTT_TLS_PORT 8883 |
samdanbury | 6:37b6d0d56190 | 45 | #define IBM_IOT_PORT MQTT_PORT |
samdanbury | 6:37b6d0d56190 | 46 | |
samdanbury | 6:37b6d0d56190 | 47 | #define MQTT_MAX_PACKET_SIZE 250 |
samdanbury | 6:37b6d0d56190 | 48 | |
mazgch | 11:7a6df9a2dcdc | 49 | #if defined(TARGET_UBLOX_C027) |
mazgch | 11:7a6df9a2dcdc | 50 | #warning "Compiling for mbed C027" |
mazgch | 11:7a6df9a2dcdc | 51 | #include "C027.h" |
mazgch | 11:7a6df9a2dcdc | 52 | #elif defined(TARGET_LPC1768) |
samdanbury | 6:37b6d0d56190 | 53 | #warning "Compiling for mbed LPC1768" |
samdanbury | 6:37b6d0d56190 | 54 | #include "LPC1768.h" |
samdanbury | 6:37b6d0d56190 | 55 | #elif defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 56 | #warning "Compiling for mbed K64F" |
samdanbury | 6:37b6d0d56190 | 57 | #include "K64F.h" |
samdanbury | 6:37b6d0d56190 | 58 | #endif |
samdanbury | 6:37b6d0d56190 | 59 | |
jsutton | 14:1f961d19f3cf | 60 | |
icraggs | 8:80d49dd91542 | 61 | bool quickstartMode = true; |
samdanbury | 6:37b6d0d56190 | 62 | char org[11] = ORG; |
samdanbury | 6:37b6d0d56190 | 63 | char type[30] = TYPE; |
samdanbury | 6:37b6d0d56190 | 64 | char id[30] = ID; // mac without colons |
samdanbury | 6:37b6d0d56190 | 65 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
samdanbury | 6:37b6d0d56190 | 66 | |
samdanbury | 6:37b6d0d56190 | 67 | bool connected = false; |
jsutton | 14:1f961d19f3cf | 68 | bool mqttConnecting = false; |
jsutton | 14:1f961d19f3cf | 69 | bool netConnected = false; |
jsutton | 14:1f961d19f3cf | 70 | bool netConnecting = false; |
jsutton | 14:1f961d19f3cf | 71 | bool ethernetInitialising = true; |
icraggs | 16:2420bfbf5f1c | 72 | int connack_rc = 0; // MQTT connack return code |
jsutton | 14:1f961d19f3cf | 73 | int retryAttempt = 0; |
jsutton | 14:1f961d19f3cf | 74 | int menuItem = 0; |
jsutton | 14:1f961d19f3cf | 75 | |
samdanbury | 6:37b6d0d56190 | 76 | char* joystickPos = "CENTRE"; |
samdanbury | 6:37b6d0d56190 | 77 | int blink_interval = 0; |
skoda | 20:14a61a65afba | 78 | int respond = 0; |
skoda | 20:14a61a65afba | 79 | char* response = "00000000000000000000"; |
samdanbury | 6:37b6d0d56190 | 80 | |
jsutton | 13:85801e3b83d3 | 81 | char* ip_addr = ""; |
jsutton | 13:85801e3b83d3 | 82 | char* gateway_addr = ""; |
jsutton | 14:1f961d19f3cf | 83 | char* host_addr = ""; |
jsutton | 14:1f961d19f3cf | 84 | int connectTimeout = 1000; |
skoda | 20:14a61a65afba | 85 | |
skoda | 20:14a61a65afba | 86 | uint16_t lux = 0; |
skoda | 20:14a61a65afba | 87 | uint16_t rx = 0; |
skoda | 20:14a61a65afba | 88 | uint16_t t = 0; |
skoda | 20:14a61a65afba | 89 | int16_t ax = 0; |
skoda | 20:14a61a65afba | 90 | int16_t ay = 0; |
skoda | 20:14a61a65afba | 91 | int16_t az = 0; |
skoda | 20:14a61a65afba | 92 | int16_t deviceID = 0; |
skoda | 22:e6b69714b59a | 93 | const int BUFFER_SIZE = 30; |
skoda | 22:e6b69714b59a | 94 | const int DATA_LENGTH = 20; |
skoda | 23:03835082a0de | 95 | |
skoda | 22:e6b69714b59a | 96 | int indexBufWrite = 0; |
skoda | 22:e6b69714b59a | 97 | int indexBufRead = 0; |
skoda | 22:e6b69714b59a | 98 | char IoTbuffer[BUFFER_SIZE][DATA_LENGTH]; |
skoda | 22:e6b69714b59a | 99 | |
skoda | 22:e6b69714b59a | 100 | int DELETE = 0; |
skoda | 20:14a61a65afba | 101 | |
skoda | 20:14a61a65afba | 102 | |
skoda | 20:14a61a65afba | 103 | Serial pc(USBTX, USBRX); |
skoda | 20:14a61a65afba | 104 | |
skoda | 20:14a61a65afba | 105 | |
skoda | 20:14a61a65afba | 106 | |
jsutton | 14:1f961d19f3cf | 107 | |
jsutton | 14:1f961d19f3cf | 108 | // If we wanted to manually set the MAC address, |
jsutton | 14:1f961d19f3cf | 109 | // this is how to do it. In this example, we take |
jsutton | 14:1f961d19f3cf | 110 | // the original Mbed Set MAC address and combine it |
jsutton | 14:1f961d19f3cf | 111 | // with a prefix of our choosing. |
jsutton | 14:1f961d19f3cf | 112 | /* |
jsutton | 14:1f961d19f3cf | 113 | extern "C" void $Super$$mbed_mac_address(char *s); |
jsutton | 14:1f961d19f3cf | 114 | extern "C" void $Sub$$mbed_mac_address(char *s) |
jsutton | 14:1f961d19f3cf | 115 | { |
jsutton | 15:09458079f4bb | 116 | char originalMAC[6] = ""; |
jsutton | 15:09458079f4bb | 117 | $Super$$mbed_mac_address(originalMAC); |
jsutton | 14:1f961d19f3cf | 118 | |
jsutton | 14:1f961d19f3cf | 119 | char mac[6]; |
jsutton | 14:1f961d19f3cf | 120 | mac[0] = 0x00; |
jsutton | 14:1f961d19f3cf | 121 | mac[1] = 0x08; |
jsutton | 14:1f961d19f3cf | 122 | mac[2] = 0xdc; |
jsutton | 15:09458079f4bb | 123 | mac[3] = originalMAC[3]; |
jsutton | 15:09458079f4bb | 124 | mac[4] = originalMAC[4]; |
jsutton | 15:09458079f4bb | 125 | mac[5] = originalMAC[5]; |
jsutton | 14:1f961d19f3cf | 126 | memcpy(s, mac, 6); |
jsutton | 14:1f961d19f3cf | 127 | } |
jsutton | 14:1f961d19f3cf | 128 | */ |
jsutton | 13:85801e3b83d3 | 129 | |
samdanbury | 6:37b6d0d56190 | 130 | |
samdanbury | 6:37b6d0d56190 | 131 | void off() |
samdanbury | 6:37b6d0d56190 | 132 | { |
samdanbury | 6:37b6d0d56190 | 133 | r = g = b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 134 | } |
samdanbury | 6:37b6d0d56190 | 135 | |
samdanbury | 6:37b6d0d56190 | 136 | void red() |
samdanbury | 6:37b6d0d56190 | 137 | { |
samdanbury | 6:37b6d0d56190 | 138 | r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 139 | } |
samdanbury | 6:37b6d0d56190 | 140 | |
samdanbury | 6:37b6d0d56190 | 141 | void yellow() |
samdanbury | 6:37b6d0d56190 | 142 | { |
samdanbury | 6:37b6d0d56190 | 143 | r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 144 | } |
samdanbury | 6:37b6d0d56190 | 145 | |
samdanbury | 6:37b6d0d56190 | 146 | void green() |
samdanbury | 6:37b6d0d56190 | 147 | { |
samdanbury | 6:37b6d0d56190 | 148 | r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 149 | } |
samdanbury | 6:37b6d0d56190 | 150 | |
samdanbury | 6:37b6d0d56190 | 151 | |
samdanbury | 6:37b6d0d56190 | 152 | void flashing_yellow(void const *args) |
samdanbury | 6:37b6d0d56190 | 153 | { |
samdanbury | 6:37b6d0d56190 | 154 | bool on = false; |
icraggs | 16:2420bfbf5f1c | 155 | while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD) // flashing yellow only while connecting |
samdanbury | 6:37b6d0d56190 | 156 | { |
samdanbury | 6:37b6d0d56190 | 157 | on = !on; |
samdanbury | 6:37b6d0d56190 | 158 | if (on) |
samdanbury | 6:37b6d0d56190 | 159 | yellow(); |
samdanbury | 6:37b6d0d56190 | 160 | else |
samdanbury | 6:37b6d0d56190 | 161 | off(); |
samdanbury | 6:37b6d0d56190 | 162 | wait(0.5); |
samdanbury | 6:37b6d0d56190 | 163 | } |
samdanbury | 6:37b6d0d56190 | 164 | } |
samdanbury | 6:37b6d0d56190 | 165 | |
samdanbury | 6:37b6d0d56190 | 166 | |
samdanbury | 6:37b6d0d56190 | 167 | void flashing_red(void const *args) // to be used when the connection is lost |
samdanbury | 6:37b6d0d56190 | 168 | { |
samdanbury | 6:37b6d0d56190 | 169 | bool on = false; |
samdanbury | 6:37b6d0d56190 | 170 | while (!connected) |
samdanbury | 6:37b6d0d56190 | 171 | { |
samdanbury | 6:37b6d0d56190 | 172 | on = !on; |
samdanbury | 6:37b6d0d56190 | 173 | if (on) |
samdanbury | 6:37b6d0d56190 | 174 | red(); |
samdanbury | 6:37b6d0d56190 | 175 | else |
samdanbury | 6:37b6d0d56190 | 176 | off(); |
samdanbury | 6:37b6d0d56190 | 177 | wait(2.0); |
samdanbury | 6:37b6d0d56190 | 178 | } |
samdanbury | 6:37b6d0d56190 | 179 | } |
samdanbury | 6:37b6d0d56190 | 180 | |
skoda | 20:14a61a65afba | 181 | //matjaz dodamo nit za komunikacijo preko i2c |
skoda | 20:14a61a65afba | 182 | void firefly_com (void const *args) |
skoda | 20:14a61a65afba | 183 | { |
skoda | 20:14a61a65afba | 184 | |
skoda | 20:14a61a65afba | 185 | char buf[100]; |
skoda | 20:14a61a65afba | 186 | int i = 0; |
skoda | 20:14a61a65afba | 187 | |
skoda | 20:14a61a65afba | 188 | |
skoda | 20:14a61a65afba | 189 | |
skoda | 20:14a61a65afba | 190 | |
skoda | 23:03835082a0de | 191 | LOG("Slave thread initiated!\r\n"); |
skoda | 20:14a61a65afba | 192 | |
skoda | 20:14a61a65afba | 193 | /* |
skoda | 20:14a61a65afba | 194 | while(1){ |
skoda | 20:14a61a65afba | 195 | |
skoda | 20:14a61a65afba | 196 | masterI2C.write(addr, send_test_byte, 1); //write (int address, const char *data, int length, bool repeated=false) |
skoda | 20:14a61a65afba | 197 | Thread::wait(100); //da ima cas slave odgovorit |
skoda | 20:14a61a65afba | 198 | masterI2C.read(addr, recv_test_byte, 1); |
skoda | 20:14a61a65afba | 199 | LOG("I2C send 0x56 slave reflect 0x%x:\n", recv_test_byte[0]); |
skoda | 20:14a61a65afba | 200 | Thread::wait(1000); |
skoda | 20:14a61a65afba | 201 | } |
skoda | 20:14a61a65afba | 202 | */ |
skoda | 20:14a61a65afba | 203 | |
skoda | 20:14a61a65afba | 204 | while(1){ |
skoda | 20:14a61a65afba | 205 | |
skoda | 20:14a61a65afba | 206 | |
skoda | 20:14a61a65afba | 207 | i = slave.receive(); |
skoda | 22:e6b69714b59a | 208 | DELETE++; |
skoda | 22:e6b69714b59a | 209 | /*if(i>0) |
skoda | 22:e6b69714b59a | 210 | LOG("i: %d\r\n",i);*/ |
skoda | 23:03835082a0de | 211 | //TRY to reset i2c if there is no receive>0 for a longer period of time |
skoda | 23:03835082a0de | 212 | //set slave to master sent stop signal and set it back to slave mode |
skoda | 23:03835082a0de | 213 | //TRY ALSO set LPC as master and FRDM as slave and scan all addresses on LPC to see how many devices you can find |
skoda | 23:03835082a0de | 214 | //if FRDM isnt on the spectre |
skoda | 20:14a61a65afba | 215 | switch (i) |
skoda | 20:14a61a65afba | 216 | { |
skoda | 20:14a61a65afba | 217 | case I2CSlave::ReadAddressed: |
skoda | 22:e6b69714b59a | 218 | if(indexBufRead != indexBufWrite){ |
skoda | 22:e6b69714b59a | 219 | slave.write(IoTbuffer[indexBufRead],20); |
skoda | 22:e6b69714b59a | 220 | LOG("Slave sent mesage: %s\r\n",IoTbuffer[indexBufRead]); |
skoda | 22:e6b69714b59a | 221 | indexBufRead++; |
skoda | 22:e6b69714b59a | 222 | if(indexBufRead == BUFFER_SIZE){ |
skoda | 22:e6b69714b59a | 223 | indexBufRead = 0; |
skoda | 22:e6b69714b59a | 224 | } |
skoda | 22:e6b69714b59a | 225 | }else{ |
skoda | 22:e6b69714b59a | 226 | response = "00000000000000000000"; |
skoda | 22:e6b69714b59a | 227 | slave.write(response,20); |
skoda | 22:e6b69714b59a | 228 | //LOG("Slave sent mesage: %s\r\n",response); |
skoda | 22:e6b69714b59a | 229 | } |
skoda | 20:14a61a65afba | 230 | break; |
skoda | 20:14a61a65afba | 231 | |
skoda | 20:14a61a65afba | 232 | case I2CSlave::WriteGeneral: |
skoda | 20:14a61a65afba | 233 | |
skoda | 22:e6b69714b59a | 234 | slave.read(buf, 20); |
skoda | 20:14a61a65afba | 235 | //LOG("Napacen case\r\n"); |
skoda | 20:14a61a65afba | 236 | break; |
skoda | 20:14a61a65afba | 237 | |
skoda | 20:14a61a65afba | 238 | case I2CSlave::WriteAddressed: |
skoda | 20:14a61a65afba | 239 | |
skoda | 22:e6b69714b59a | 240 | slave.read(buf, 20); |
skoda | 20:14a61a65afba | 241 | //parsanje |
skoda | 20:14a61a65afba | 242 | lux = ((uint16_t)buf[11] << 8) | buf[12]; //že skalirano v luxe |
skoda | 20:14a61a65afba | 243 | t = ((uint16_t)buf[13] << 8) | buf[14]; //raw |
skoda | 20:14a61a65afba | 244 | |
skoda | 20:14a61a65afba | 245 | //buf[4] = 0x55; |
skoda | 20:14a61a65afba | 246 | //buf[5] = 0xff; |
skoda | 20:14a61a65afba | 247 | ax = ((uint16_t)buf[15] << 8) | buf[16]; //pospeškometer ay ax = (((buf[4]&0xFF) << 8) | (buf[5]&0xFF)); //pospeškometer ax |
skoda | 20:14a61a65afba | 248 | ax = (ax >> 6); |
skoda | 20:14a61a65afba | 249 | ay = ((uint16_t)buf[17] << 8) | buf[18]; //pospeškometer ay |
skoda | 20:14a61a65afba | 250 | ay = (ay >> 6); |
skoda | 20:14a61a65afba | 251 | az = ((uint16_t)buf[19] << 8) | buf[20]; //pospeškometer az |
skoda | 20:14a61a65afba | 252 | az = (az >> 6); |
skoda | 20:14a61a65afba | 253 | |
skoda | 21:bb0d9f12da01 | 254 | int tmp = 48; |
skoda | 21:bb0d9f12da01 | 255 | deviceID = 0; |
skoda | 21:bb0d9f12da01 | 256 | //sprintf(tmp, "%c%c", buf[3],buf[4]); |
skoda | 21:bb0d9f12da01 | 257 | deviceID = (buf[3]-tmp)*10; //device ID from whom the message was received |
skoda | 21:bb0d9f12da01 | 258 | deviceID += (buf[4]-tmp); |
skoda | 20:14a61a65afba | 259 | respond = 1; |
skoda | 20:14a61a65afba | 260 | //rh = ((uint16_t)buf[4] << 8) | buf[5]; |
skoda | 20:14a61a65afba | 261 | |
skoda | 23:03835082a0de | 262 | /*if(deviceID == 12){ |
skoda | 22:e6b69714b59a | 263 | respond = 0; |
skoda | 23:03835082a0de | 264 | }*/ |
skoda | 20:14a61a65afba | 265 | |
skoda | 20:14a61a65afba | 266 | //LOG("Slave read 0x:%d\r\n", lux); |
skoda | 20:14a61a65afba | 267 | //LOG("Slave read 0x:%x%x%x%x\r\n", buf[0],buf[1],buf[2],buf[3]); |
skoda | 20:14a61a65afba | 268 | LOG("Slave Received %s \r\n", buf); |
skoda | 20:14a61a65afba | 269 | |
skoda | 20:14a61a65afba | 270 | break; |
skoda | 20:14a61a65afba | 271 | |
skoda | 20:14a61a65afba | 272 | } // switch .. |
skoda | 23:03835082a0de | 273 | //Thread::wait(1); |
skoda | 23:03835082a0de | 274 | slave.write(IoTbuffer[0],20); |
skoda | 20:14a61a65afba | 275 | |
skoda | 20:14a61a65afba | 276 | } |
skoda | 20:14a61a65afba | 277 | |
skoda | 20:14a61a65afba | 278 | |
skoda | 20:14a61a65afba | 279 | } |
skoda | 20:14a61a65afba | 280 | |
samdanbury | 6:37b6d0d56190 | 281 | |
samdanbury | 6:37b6d0d56190 | 282 | void printMenu(int menuItem) |
samdanbury | 6:37b6d0d56190 | 283 | { |
icraggs | 18:94da9de96d54 | 284 | static char last_line1[30] = "", last_line2[30] = ""; |
icraggs | 18:94da9de96d54 | 285 | char line1[30] = "", line2[30] = ""; |
icraggs | 18:94da9de96d54 | 286 | |
samdanbury | 6:37b6d0d56190 | 287 | switch (menuItem) |
samdanbury | 6:37b6d0d56190 | 288 | { |
samdanbury | 6:37b6d0d56190 | 289 | case 0: |
icraggs | 18:94da9de96d54 | 290 | sprintf(line1, "IBM IoT Cloud"); |
icraggs | 18:94da9de96d54 | 291 | sprintf(line2, "Scroll with joystick"); |
samdanbury | 6:37b6d0d56190 | 292 | break; |
samdanbury | 6:37b6d0d56190 | 293 | case 1: |
icraggs | 18:94da9de96d54 | 294 | sprintf(line1, "Go to:"); |
icraggs | 18:94da9de96d54 | 295 | sprintf(line2, "http://ibm.biz/iotqstart"); |
samdanbury | 6:37b6d0d56190 | 296 | break; |
samdanbury | 6:37b6d0d56190 | 297 | case 2: |
icraggs | 18:94da9de96d54 | 298 | sprintf(line1, "Device Identity:"); |
icraggs | 18:94da9de96d54 | 299 | sprintf(line2, "%s", id); |
samdanbury | 6:37b6d0d56190 | 300 | break; |
samdanbury | 6:37b6d0d56190 | 301 | case 3: |
icraggs | 18:94da9de96d54 | 302 | sprintf(line1, "MQTT Status:"); |
icraggs | 16:2420bfbf5f1c | 303 | if (mqttConnecting) |
icraggs | 18:94da9de96d54 | 304 | sprintf(line2, "Connecting... %d/5", retryAttempt); |
icraggs | 16:2420bfbf5f1c | 305 | else |
icraggs | 16:2420bfbf5f1c | 306 | { |
icraggs | 16:2420bfbf5f1c | 307 | if (connected) |
icraggs | 18:94da9de96d54 | 308 | sprintf(line2, "Connected"); |
icraggs | 16:2420bfbf5f1c | 309 | else |
icraggs | 16:2420bfbf5f1c | 310 | { |
icraggs | 16:2420bfbf5f1c | 311 | switch (connack_rc) |
icraggs | 16:2420bfbf5f1c | 312 | { |
icraggs | 16:2420bfbf5f1c | 313 | case MQTT_CLIENTID_REJECTED: |
icraggs | 18:94da9de96d54 | 314 | sprintf(line2, "Clientid rejected"); |
icraggs | 16:2420bfbf5f1c | 315 | break; |
icraggs | 16:2420bfbf5f1c | 316 | case MQTT_BAD_USERNAME_OR_PASSWORD: |
icraggs | 18:94da9de96d54 | 317 | sprintf(line2, "Invalid username or password"); |
icraggs | 16:2420bfbf5f1c | 318 | break; |
icraggs | 16:2420bfbf5f1c | 319 | case MQTT_NOT_AUTHORIZED: |
icraggs | 18:94da9de96d54 | 320 | sprintf(line2, "Not authorized"); |
icraggs | 16:2420bfbf5f1c | 321 | break; |
icraggs | 16:2420bfbf5f1c | 322 | default: |
icraggs | 18:94da9de96d54 | 323 | sprintf(line2, "Disconnected"); |
icraggs | 16:2420bfbf5f1c | 324 | } |
icraggs | 16:2420bfbf5f1c | 325 | } |
jsutton | 14:1f961d19f3cf | 326 | } |
samdanbury | 6:37b6d0d56190 | 327 | break; |
chris | 10:0b5e0dfee08e | 328 | case 4: |
icraggs | 18:94da9de96d54 | 329 | sprintf(line1, "Ethernet State:"); |
icraggs | 18:94da9de96d54 | 330 | sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized"); |
jsutton | 14:1f961d19f3cf | 331 | break; |
jsutton | 14:1f961d19f3cf | 332 | case 5: |
icraggs | 18:94da9de96d54 | 333 | sprintf(line1, "Socket State:"); |
icraggs | 16:2420bfbf5f1c | 334 | if (netConnecting) |
icraggs | 18:94da9de96d54 | 335 | sprintf(line2, "Connecting... %d/5", retryAttempt); |
icraggs | 16:2420bfbf5f1c | 336 | else |
icraggs | 18:94da9de96d54 | 337 | sprintf(line2, netConnected ? "Connected" : "Disconnected"); |
jsutton | 13:85801e3b83d3 | 338 | break; |
jsutton | 14:1f961d19f3cf | 339 | case 6: |
icraggs | 18:94da9de96d54 | 340 | sprintf(line1, "IP Address:"); |
icraggs | 18:94da9de96d54 | 341 | sprintf(line2, "%s", ip_addr); |
jsutton | 13:85801e3b83d3 | 342 | break; |
jsutton | 14:1f961d19f3cf | 343 | case 7: |
icraggs | 18:94da9de96d54 | 344 | sprintf(line1, "Gateway:"); |
icraggs | 18:94da9de96d54 | 345 | sprintf(line2, "%s", gateway_addr); |
jsutton | 13:85801e3b83d3 | 346 | break; |
jsutton | 14:1f961d19f3cf | 347 | case 8: |
icraggs | 18:94da9de96d54 | 348 | sprintf(line1, "App version:"); |
icraggs | 18:94da9de96d54 | 349 | sprintf(line2, "%s", __APP_SW_REVISION__); |
chris | 10:0b5e0dfee08e | 350 | break; |
jsutton | 14:1f961d19f3cf | 351 | case 9: |
icraggs | 18:94da9de96d54 | 352 | sprintf(line1, "Current Timeout:"); |
icraggs | 18:94da9de96d54 | 353 | sprintf(line2, "%d ms", connectTimeout); |
jsutton | 14:1f961d19f3cf | 354 | break; |
samdanbury | 6:37b6d0d56190 | 355 | } |
icraggs | 18:94da9de96d54 | 356 | |
icraggs | 18:94da9de96d54 | 357 | if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0) |
icraggs | 18:94da9de96d54 | 358 | { |
icraggs | 18:94da9de96d54 | 359 | lcd.cls(); |
icraggs | 18:94da9de96d54 | 360 | lcd.locate(0, 0); |
icraggs | 18:94da9de96d54 | 361 | lcd.printf(line1); |
icraggs | 18:94da9de96d54 | 362 | strncpy(last_line1, line1, sizeof(last_line1)); |
icraggs | 18:94da9de96d54 | 363 | |
icraggs | 18:94da9de96d54 | 364 | lcd.locate(0,16); |
icraggs | 18:94da9de96d54 | 365 | lcd.printf(line2); |
icraggs | 18:94da9de96d54 | 366 | strncpy(last_line2, line2, sizeof(last_line2)); |
icraggs | 18:94da9de96d54 | 367 | } |
samdanbury | 6:37b6d0d56190 | 368 | } |
samdanbury | 6:37b6d0d56190 | 369 | |
samdanbury | 6:37b6d0d56190 | 370 | |
samdanbury | 6:37b6d0d56190 | 371 | void setMenu() |
samdanbury | 6:37b6d0d56190 | 372 | { |
jsutton | 14:1f961d19f3cf | 373 | |
samdanbury | 6:37b6d0d56190 | 374 | if (Down) |
samdanbury | 6:37b6d0d56190 | 375 | { |
samdanbury | 6:37b6d0d56190 | 376 | joystickPos = "DOWN"; |
skoda | 20:14a61a65afba | 377 | if (menuItem >= 0 && menuItem < 9) |
samdanbury | 6:37b6d0d56190 | 378 | printMenu(++menuItem); |
samdanbury | 6:37b6d0d56190 | 379 | } |
samdanbury | 6:37b6d0d56190 | 380 | else if (Left) |
samdanbury | 6:37b6d0d56190 | 381 | joystickPos = "LEFT"; |
samdanbury | 6:37b6d0d56190 | 382 | else if (Click) |
samdanbury | 6:37b6d0d56190 | 383 | joystickPos = "CLICK"; |
samdanbury | 6:37b6d0d56190 | 384 | else if (Up) |
samdanbury | 6:37b6d0d56190 | 385 | { |
samdanbury | 6:37b6d0d56190 | 386 | joystickPos = "UP"; |
skoda | 20:14a61a65afba | 387 | if (menuItem <= 9 && menuItem > 0) |
samdanbury | 6:37b6d0d56190 | 388 | printMenu(--menuItem); |
samdanbury | 6:37b6d0d56190 | 389 | } |
samdanbury | 6:37b6d0d56190 | 390 | else if (Right) |
samdanbury | 6:37b6d0d56190 | 391 | joystickPos = "RIGHT"; |
samdanbury | 6:37b6d0d56190 | 392 | else |
samdanbury | 6:37b6d0d56190 | 393 | joystickPos = "CENTRE"; |
samdanbury | 6:37b6d0d56190 | 394 | } |
samdanbury | 6:37b6d0d56190 | 395 | |
jsutton | 13:85801e3b83d3 | 396 | void menu_loop(void const *args) |
jsutton | 13:85801e3b83d3 | 397 | { |
jsutton | 14:1f961d19f3cf | 398 | int count = 0; |
icraggs | 16:2420bfbf5f1c | 399 | while(true) |
icraggs | 16:2420bfbf5f1c | 400 | { |
jsutton | 13:85801e3b83d3 | 401 | setMenu(); |
icraggs | 16:2420bfbf5f1c | 402 | if (++count % 10 == 0) |
jsutton | 14:1f961d19f3cf | 403 | printMenu(menuItem); |
jsutton | 14:1f961d19f3cf | 404 | Thread::wait(100); |
jsutton | 13:85801e3b83d3 | 405 | } |
jsutton | 13:85801e3b83d3 | 406 | } |
jsutton | 13:85801e3b83d3 | 407 | |
samdanbury | 6:37b6d0d56190 | 408 | |
samdanbury | 6:37b6d0d56190 | 409 | /** |
samdanbury | 6:37b6d0d56190 | 410 | * Display a message on the LCD screen prefixed with IBM IoT Cloud |
samdanbury | 6:37b6d0d56190 | 411 | */ |
samdanbury | 6:37b6d0d56190 | 412 | void displayMessage(char* message) |
samdanbury | 6:37b6d0d56190 | 413 | { |
samdanbury | 6:37b6d0d56190 | 414 | lcd.cls(); |
samdanbury | 6:37b6d0d56190 | 415 | lcd.locate(0,0); |
samdanbury | 6:37b6d0d56190 | 416 | lcd.printf("IBM IoT Cloud"); |
samdanbury | 6:37b6d0d56190 | 417 | lcd.locate(0,16); |
samdanbury | 6:37b6d0d56190 | 418 | lcd.printf(message); |
samdanbury | 6:37b6d0d56190 | 419 | } |
samdanbury | 6:37b6d0d56190 | 420 | |
samdanbury | 6:37b6d0d56190 | 421 | |
samdanbury | 6:37b6d0d56190 | 422 | int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 423 | { |
samdanbury | 6:37b6d0d56190 | 424 | const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; |
samdanbury | 6:37b6d0d56190 | 425 | |
samdanbury | 6:37b6d0d56190 | 426 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
samdanbury | 6:37b6d0d56190 | 427 | sprintf(hostname, "%s%s", org, iot_ibm); |
jsutton | 13:85801e3b83d3 | 428 | EthernetInterface& eth = ipstack->getEth(); |
jsutton | 13:85801e3b83d3 | 429 | ip_addr = eth.getIPAddress(); |
jsutton | 13:85801e3b83d3 | 430 | gateway_addr = eth.getGateway(); |
jsutton | 14:1f961d19f3cf | 431 | |
jsutton | 14:1f961d19f3cf | 432 | // Construct clientId - d:org:type:id |
jsutton | 14:1f961d19f3cf | 433 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
jsutton | 14:1f961d19f3cf | 434 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
jsutton | 14:1f961d19f3cf | 435 | |
icraggs | 16:2420bfbf5f1c | 436 | // Network debug statements |
jsutton | 14:1f961d19f3cf | 437 | LOG("=====================================\n"); |
jsutton | 14:1f961d19f3cf | 438 | LOG("Connecting Ethernet.\n"); |
jsutton | 14:1f961d19f3cf | 439 | LOG("IP ADDRESS: %s\n", eth.getIPAddress()); |
jsutton | 14:1f961d19f3cf | 440 | LOG("MAC ADDRESS: %s\n", eth.getMACAddress()); |
jsutton | 14:1f961d19f3cf | 441 | LOG("Gateway: %s\n", eth.getGateway()); |
jsutton | 14:1f961d19f3cf | 442 | LOG("Network Mask: %s\n", eth.getNetworkMask()); |
jsutton | 14:1f961d19f3cf | 443 | LOG("Server Hostname: %s\n", hostname); |
jsutton | 14:1f961d19f3cf | 444 | LOG("Client ID: %s\n", clientId); |
jsutton | 14:1f961d19f3cf | 445 | LOG("=====================================\n"); |
jsutton | 14:1f961d19f3cf | 446 | |
jsutton | 14:1f961d19f3cf | 447 | netConnecting = true; |
jsutton | 14:1f961d19f3cf | 448 | int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); |
icraggs | 16:2420bfbf5f1c | 449 | if (rc != 0) |
icraggs | 16:2420bfbf5f1c | 450 | { |
icraggs | 18:94da9de96d54 | 451 | WARN("IP Stack connect returned: %d\n", rc); |
samdanbury | 6:37b6d0d56190 | 452 | return rc; |
jsutton | 13:85801e3b83d3 | 453 | } |
jsutton | 13:85801e3b83d3 | 454 | netConnected = true; |
jsutton | 14:1f961d19f3cf | 455 | netConnecting = false; |
jsutton | 14:1f961d19f3cf | 456 | |
samdanbury | 6:37b6d0d56190 | 457 | // MQTT Connect |
jsutton | 14:1f961d19f3cf | 458 | mqttConnecting = true; |
samdanbury | 6:37b6d0d56190 | 459 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
samdanbury | 6:37b6d0d56190 | 460 | data.MQTTVersion = 3; |
samdanbury | 6:37b6d0d56190 | 461 | data.clientID.cstring = clientId; |
samdanbury | 6:37b6d0d56190 | 462 | |
samdanbury | 6:37b6d0d56190 | 463 | if (!quickstartMode) |
samdanbury | 6:37b6d0d56190 | 464 | { |
samdanbury | 6:37b6d0d56190 | 465 | data.username.cstring = "use-token-auth"; |
samdanbury | 6:37b6d0d56190 | 466 | data.password.cstring = auth_token; |
samdanbury | 6:37b6d0d56190 | 467 | } |
samdanbury | 6:37b6d0d56190 | 468 | |
icraggs | 8:80d49dd91542 | 469 | if ((rc = client->connect(data)) == 0) |
samdanbury | 6:37b6d0d56190 | 470 | { |
samdanbury | 6:37b6d0d56190 | 471 | connected = true; |
skoda | 20:14a61a65afba | 472 | //green(); |
skoda | 20:14a61a65afba | 473 | //matjaz zelja je da je off |
skoda | 20:14a61a65afba | 474 | off(); |
samdanbury | 6:37b6d0d56190 | 475 | displayMessage("Connected"); |
jsutton | 13:85801e3b83d3 | 476 | wait(1); |
samdanbury | 6:37b6d0d56190 | 477 | displayMessage("Scroll with joystick"); |
samdanbury | 6:37b6d0d56190 | 478 | } |
icraggs | 16:2420bfbf5f1c | 479 | else |
icraggs | 16:2420bfbf5f1c | 480 | WARN("MQTT connect returned %d\n", rc); |
icraggs | 16:2420bfbf5f1c | 481 | if (rc >= 0) |
icraggs | 16:2420bfbf5f1c | 482 | connack_rc = rc; |
jsutton | 14:1f961d19f3cf | 483 | mqttConnecting = false; |
samdanbury | 6:37b6d0d56190 | 484 | return rc; |
samdanbury | 6:37b6d0d56190 | 485 | } |
samdanbury | 6:37b6d0d56190 | 486 | |
samdanbury | 6:37b6d0d56190 | 487 | |
samdanbury | 6:37b6d0d56190 | 488 | int getConnTimeout(int attemptNumber) |
samdanbury | 6:37b6d0d56190 | 489 | { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
samdanbury | 6:37b6d0d56190 | 490 | // after 20 attempts, retry every 10 minutes |
samdanbury | 6:37b6d0d56190 | 491 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
samdanbury | 6:37b6d0d56190 | 492 | } |
samdanbury | 6:37b6d0d56190 | 493 | |
samdanbury | 6:37b6d0d56190 | 494 | |
samdanbury | 6:37b6d0d56190 | 495 | void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 496 | { |
samdanbury | 6:37b6d0d56190 | 497 | connected = false; |
icraggs | 16:2420bfbf5f1c | 498 | |
icraggs | 8:80d49dd91542 | 499 | // make sure a cable is connected before starting to connect |
icraggs | 16:2420bfbf5f1c | 500 | while (!linkStatus()) |
icraggs | 16:2420bfbf5f1c | 501 | { |
icraggs | 8:80d49dd91542 | 502 | wait(1.0f); |
icraggs | 8:80d49dd91542 | 503 | WARN("Ethernet link not present. Check cable connection\n"); |
icraggs | 8:80d49dd91542 | 504 | } |
samdanbury | 6:37b6d0d56190 | 505 | |
icraggs | 16:2420bfbf5f1c | 506 | while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) |
samdanbury | 6:37b6d0d56190 | 507 | { |
icraggs | 16:2420bfbf5f1c | 508 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
icraggs | 16:2420bfbf5f1c | 509 | return; // don't reattempt to connect if credentials are wrong |
icraggs | 16:2420bfbf5f1c | 510 | |
samdanbury | 6:37b6d0d56190 | 511 | Thread red_thread(flashing_red); |
chris | 12:8b480eb8a496 | 512 | |
samdanbury | 6:37b6d0d56190 | 513 | int timeout = getConnTimeout(++retryAttempt); |
samdanbury | 6:37b6d0d56190 | 514 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
icraggs | 8:80d49dd91542 | 515 | |
icraggs | 8:80d49dd91542 | 516 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
icraggs | 8:80d49dd91542 | 517 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
icraggs | 8:80d49dd91542 | 518 | |
icraggs | 8:80d49dd91542 | 519 | // this works - reset the system when the retry count gets to a threshold |
icraggs | 8:80d49dd91542 | 520 | if (retryAttempt == 5) |
icraggs | 8:80d49dd91542 | 521 | NVIC_SystemReset(); |
icraggs | 8:80d49dd91542 | 522 | else |
icraggs | 8:80d49dd91542 | 523 | wait(timeout); |
samdanbury | 6:37b6d0d56190 | 524 | } |
samdanbury | 6:37b6d0d56190 | 525 | } |
samdanbury | 6:37b6d0d56190 | 526 | |
samdanbury | 6:37b6d0d56190 | 527 | |
samdanbury | 6:37b6d0d56190 | 528 | int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 529 | { |
skoda | 20:14a61a65afba | 530 | //moje float spremenljivke |
skoda | 20:14a61a65afba | 531 | float tempC = 0.0f; |
skoda | 20:14a61a65afba | 532 | float fax = 0.0f; |
skoda | 20:14a61a65afba | 533 | float fay = 0.0f; |
skoda | 20:14a61a65afba | 534 | float faz = 0.0f; |
skoda | 20:14a61a65afba | 535 | |
samdanbury | 6:37b6d0d56190 | 536 | MQTT::Message message; |
samdanbury | 6:37b6d0d56190 | 537 | char* pubTopic = "iot-2/evt/status/fmt/json"; |
samdanbury | 6:37b6d0d56190 | 538 | |
samdanbury | 6:37b6d0d56190 | 539 | char buf[250]; |
skoda | 20:14a61a65afba | 540 | /* |
samdanbury | 6:37b6d0d56190 | 541 | sprintf(buf, |
samdanbury | 6:37b6d0d56190 | 542 | "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}", |
samdanbury | 6:37b6d0d56190 | 543 | MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read()); |
skoda | 20:14a61a65afba | 544 | */ |
skoda | 20:14a61a65afba | 545 | //moj float preracun |
skoda | 20:14a61a65afba | 546 | // Scaled by 100 |
skoda | 20:14a61a65afba | 547 | tempC = (((float)175.72 * t) / 65536) - 46.85; |
skoda | 20:14a61a65afba | 548 | |
skoda | 20:14a61a65afba | 549 | fax = (float) ax / 256.0f; |
skoda | 20:14a61a65afba | 550 | fax = abs(fax); |
skoda | 20:14a61a65afba | 551 | fay = (float) ay / 256.0f; |
skoda | 20:14a61a65afba | 552 | fay = abs(fay); |
skoda | 20:14a61a65afba | 553 | faz = (float) az / 256.0f; |
skoda | 20:14a61a65afba | 554 | faz = abs(faz); |
skoda | 20:14a61a65afba | 555 | |
skoda | 22:e6b69714b59a | 556 | /*sprintf(buf, |
skoda | 22:e6b69714b59a | 557 | "{\"d\":{\"myName\":\"IoT mbed\",\"lux\":%d,\"accelX\":%0.3f,\"accelY\":%0.3f,\"accelZ\":%0.3f,\"temp\":%0.1f,\"deviceID\":%d}}", |
skoda | 22:e6b69714b59a | 558 | lux, fax, fay, faz, tempC, deviceID);*/ |
skoda | 22:e6b69714b59a | 559 | |
skoda | 20:14a61a65afba | 560 | sprintf(buf, |
skoda | 22:e6b69714b59a | 561 | "{\"d\":{\"myName\":\"IoT mbed\",\"lux\":%d,\"temp\":%0.1f,\"deviceID\":%d}}", |
skoda | 22:e6b69714b59a | 562 | lux, tempC, deviceID); |
samdanbury | 6:37b6d0d56190 | 563 | message.qos = MQTT::QOS0; |
samdanbury | 6:37b6d0d56190 | 564 | message.retained = false; |
samdanbury | 6:37b6d0d56190 | 565 | message.dup = false; |
samdanbury | 6:37b6d0d56190 | 566 | message.payload = (void*)buf; |
samdanbury | 6:37b6d0d56190 | 567 | message.payloadlen = strlen(buf); |
samdanbury | 6:37b6d0d56190 | 568 | |
samdanbury | 6:37b6d0d56190 | 569 | LOG("Publishing %s\n", buf); |
icraggs | 8:80d49dd91542 | 570 | return client->publish(pubTopic, message); |
samdanbury | 6:37b6d0d56190 | 571 | } |
samdanbury | 6:37b6d0d56190 | 572 | |
samdanbury | 6:37b6d0d56190 | 573 | |
samdanbury | 6:37b6d0d56190 | 574 | char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address |
samdanbury | 6:37b6d0d56190 | 575 | { |
samdanbury | 6:37b6d0d56190 | 576 | strncpy(buf, eth.getMACAddress(), buflen); |
samdanbury | 6:37b6d0d56190 | 577 | |
samdanbury | 6:37b6d0d56190 | 578 | char* pos; // Remove colons from mac address |
samdanbury | 6:37b6d0d56190 | 579 | while ((pos = strchr(buf, ':')) != NULL) |
samdanbury | 6:37b6d0d56190 | 580 | memmove(pos, pos + 1, strlen(pos) + 1); |
samdanbury | 6:37b6d0d56190 | 581 | return buf; |
samdanbury | 6:37b6d0d56190 | 582 | } |
chris | 12:8b480eb8a496 | 583 | |
samdanbury | 6:37b6d0d56190 | 584 | |
samdanbury | 6:37b6d0d56190 | 585 | void messageArrived(MQTT::MessageData& md) |
samdanbury | 6:37b6d0d56190 | 586 | { |
samdanbury | 6:37b6d0d56190 | 587 | MQTT::Message &message = md.message; |
samdanbury | 6:37b6d0d56190 | 588 | char topic[md.topicName.lenstring.len + 1]; |
samdanbury | 6:37b6d0d56190 | 589 | |
samdanbury | 6:37b6d0d56190 | 590 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
samdanbury | 6:37b6d0d56190 | 591 | |
samdanbury | 6:37b6d0d56190 | 592 | LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload); |
skoda | 22:e6b69714b59a | 593 | LOG("Slave i2c working %d\n", DELETE); |
skoda | 20:14a61a65afba | 594 | |
samdanbury | 6:37b6d0d56190 | 595 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
samdanbury | 6:37b6d0d56190 | 596 | char* start = strstr(topic, "/cmd/") + 5; |
samdanbury | 6:37b6d0d56190 | 597 | int len = strstr(topic, "/fmt/") - start; |
samdanbury | 6:37b6d0d56190 | 598 | |
samdanbury | 6:37b6d0d56190 | 599 | if (memcmp(start, "blink", len) == 0) |
samdanbury | 6:37b6d0d56190 | 600 | { |
samdanbury | 6:37b6d0d56190 | 601 | char payload[message.payloadlen + 1]; |
samdanbury | 6:37b6d0d56190 | 602 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
skoda | 20:14a61a65afba | 603 | |
samdanbury | 6:37b6d0d56190 | 604 | char* pos = strchr(payload, '}'); |
samdanbury | 6:37b6d0d56190 | 605 | if (pos != NULL) |
samdanbury | 6:37b6d0d56190 | 606 | { |
samdanbury | 6:37b6d0d56190 | 607 | *pos = '\0'; |
samdanbury | 6:37b6d0d56190 | 608 | if ((pos = strchr(payload, ':')) != NULL) |
samdanbury | 6:37b6d0d56190 | 609 | { |
samdanbury | 6:37b6d0d56190 | 610 | int blink_rate = atoi(pos + 1); |
samdanbury | 6:37b6d0d56190 | 611 | blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate); |
samdanbury | 6:37b6d0d56190 | 612 | } |
samdanbury | 6:37b6d0d56190 | 613 | } |
skoda | 20:14a61a65afba | 614 | }else if (memcmp(start, "respond", len) == 0) |
skoda | 20:14a61a65afba | 615 | { |
skoda | 20:14a61a65afba | 616 | char payload[message.payloadlen + 1]; |
skoda | 20:14a61a65afba | 617 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
skoda | 20:14a61a65afba | 618 | |
skoda | 20:14a61a65afba | 619 | char* pos = strchr(payload, '}'); |
skoda | 20:14a61a65afba | 620 | if (pos != NULL) |
skoda | 20:14a61a65afba | 621 | { |
skoda | 20:14a61a65afba | 622 | *pos = '\0'; |
skoda | 20:14a61a65afba | 623 | if ((pos = strchr(payload, ':')) != NULL) |
skoda | 20:14a61a65afba | 624 | { |
skoda | 23:03835082a0de | 625 | response = (pos+2); // CHANGED +2 instead of +1 due to " at the start of the message |
skoda | 22:e6b69714b59a | 626 | |
skoda | 22:e6b69714b59a | 627 | strcpy(IoTbuffer[indexBufWrite], response); |
skoda | 22:e6b69714b59a | 628 | //LOG("%d sporocilo v bufferju: %s size of IoTbuffer: %d \r\n", indexBufWrite, IoTbuffer[indexBufWrite], response); |
skoda | 22:e6b69714b59a | 629 | indexBufWrite++; |
skoda | 22:e6b69714b59a | 630 | if(indexBufWrite == BUFFER_SIZE){ |
skoda | 22:e6b69714b59a | 631 | indexBufWrite = 0; |
skoda | 22:e6b69714b59a | 632 | } |
skoda | 22:e6b69714b59a | 633 | |
skoda | 22:e6b69714b59a | 634 | //LOG("%s \n", response); |
skoda | 20:14a61a65afba | 635 | } |
skoda | 20:14a61a65afba | 636 | } |
skoda | 20:14a61a65afba | 637 | }else |
samdanbury | 6:37b6d0d56190 | 638 | WARN("Unsupported command: %.*s\n", len, start); |
samdanbury | 6:37b6d0d56190 | 639 | } |
samdanbury | 6:37b6d0d56190 | 640 | |
samdanbury | 6:37b6d0d56190 | 641 | |
samdanbury | 6:37b6d0d56190 | 642 | int main() |
skoda | 22:e6b69714b59a | 643 | { |
skoda | 20:14a61a65afba | 644 | pc.baud(115200); |
skoda | 20:14a61a65afba | 645 | |
icraggs | 8:80d49dd91542 | 646 | quickstartMode = (strcmp(org, "quickstart") == 0); |
icraggs | 8:80d49dd91542 | 647 | |
samdanbury | 6:37b6d0d56190 | 648 | lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen |
samdanbury | 6:37b6d0d56190 | 649 | |
samdanbury | 6:37b6d0d56190 | 650 | led2 = LED2_OFF; // K64F: turn off the main board LED |
samdanbury | 6:37b6d0d56190 | 651 | |
skoda | 20:14a61a65afba | 652 | //matjaz set I2C parameters |
skoda | 20:14a61a65afba | 653 | slave.address(0xC2); |
skoda | 20:14a61a65afba | 654 | slave.frequency(400000); |
skoda | 20:14a61a65afba | 655 | |
samdanbury | 6:37b6d0d56190 | 656 | displayMessage("Connecting"); |
jsutton | 13:85801e3b83d3 | 657 | Thread yellow_thread(flashing_yellow); |
jsutton | 13:85801e3b83d3 | 658 | Thread menu_thread(menu_loop); |
skoda | 20:14a61a65afba | 659 | Thread firefly (firefly_com); |
jsutton | 13:85801e3b83d3 | 660 | |
jsutton | 14:1f961d19f3cf | 661 | LOG("***** IBM IoT Client Ethernet Example *****\n"); |
samdanbury | 6:37b6d0d56190 | 662 | MQTTEthernet ipstack; |
jsutton | 14:1f961d19f3cf | 663 | ethernetInitialising = false; |
samdanbury | 6:37b6d0d56190 | 664 | MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
icraggs | 18:94da9de96d54 | 665 | LOG("Ethernet Initialized\n"); |
jsutton | 13:85801e3b83d3 | 666 | |
samdanbury | 6:37b6d0d56190 | 667 | if (quickstartMode) |
icraggs | 16:2420bfbf5f1c | 668 | getMac(ipstack.getEth(), id, sizeof(id)); |
icraggs | 16:2420bfbf5f1c | 669 | |
icraggs | 16:2420bfbf5f1c | 670 | attemptConnect(&client, &ipstack); |
icraggs | 16:2420bfbf5f1c | 671 | |
icraggs | 16:2420bfbf5f1c | 672 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
samdanbury | 6:37b6d0d56190 | 673 | { |
icraggs | 16:2420bfbf5f1c | 674 | red(); |
icraggs | 16:2420bfbf5f1c | 675 | while (true) |
icraggs | 16:2420bfbf5f1c | 676 | wait(1.0); // Permanent failures - don't retry |
samdanbury | 6:37b6d0d56190 | 677 | } |
icraggs | 16:2420bfbf5f1c | 678 | |
samdanbury | 6:37b6d0d56190 | 679 | if (!quickstartMode) |
samdanbury | 6:37b6d0d56190 | 680 | { |
samdanbury | 6:37b6d0d56190 | 681 | int rc = 0; |
samdanbury | 6:37b6d0d56190 | 682 | if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) |
samdanbury | 6:37b6d0d56190 | 683 | WARN("rc from MQTT subscribe is %d\n", rc); |
samdanbury | 6:37b6d0d56190 | 684 | } |
samdanbury | 6:37b6d0d56190 | 685 | |
samdanbury | 6:37b6d0d56190 | 686 | blink_interval = 0; |
skoda | 23:03835082a0de | 687 | //int count = 0; |
skoda | 20:14a61a65afba | 688 | |
skoda | 20:14a61a65afba | 689 | pc.baud(115200); |
samdanbury | 6:37b6d0d56190 | 690 | while (true) |
samdanbury | 6:37b6d0d56190 | 691 | { |
skoda | 20:14a61a65afba | 692 | if (respond>0) |
skoda | 23:03835082a0de | 693 | { // Publish a message when you get data over i2c |
samdanbury | 6:37b6d0d56190 | 694 | if (publish(&client, &ipstack) != 0) |
samdanbury | 6:37b6d0d56190 | 695 | attemptConnect(&client, &ipstack); // if we have lost the connection |
skoda | 20:14a61a65afba | 696 | respond = 0; |
samdanbury | 6:37b6d0d56190 | 697 | } |
samdanbury | 6:37b6d0d56190 | 698 | |
skoda | 20:14a61a65afba | 699 | /*if (blink_interval == 0) |
samdanbury | 6:37b6d0d56190 | 700 | led2 = LED2_OFF; |
samdanbury | 6:37b6d0d56190 | 701 | else if (count % blink_interval == 0) |
skoda | 20:14a61a65afba | 702 | led2 = !led2;*/ |
skoda | 20:14a61a65afba | 703 | |
samdanbury | 6:37b6d0d56190 | 704 | client.yield(10); // allow the MQTT client to receive messages |
samdanbury | 6:37b6d0d56190 | 705 | } |
skoda | 23:03835082a0de | 706 | |
samdanbury | 6:37b6d0d56190 | 707 | } |