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