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 IBM Watson IoT

Committer:
skoda
Date:
Thu Nov 19 12:50:08 2015 +0000
Revision:
21:bb0d9f12da01
Parent:
20:14a61a65afba
Child:
22:e6b69714b59a
v2.0 working i2c only publishing when gets data from i2c

Who changed what in which revision?

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