![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
bluemix rfid
Dependencies: C12832 EthernetInterface IBMIoTClientEthernetExample LM75B MMA7660 MQTT iBMrfid mbed-rtos mbed
Fork of IBMIoTClientEthernetExample by
main.cpp@19:0cbe652c98cc, 2016-03-18 (annotated)
- Committer:
- muhammadimran
- Date:
- Fri Mar 18 10:28:03 2016 +0000
- Revision:
- 19:0cbe652c98cc
- Parent:
- 18:94da9de96d54
IBM blue mix rfid
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" |
muhammadimran | 19:0cbe652c98cc | 30 | //#include "C12832.h" |
samdanbury | 6:37b6d0d56190 | 31 | #include "Arial12x12.h" |
samdanbury | 6:37b6d0d56190 | 32 | #include "rtos.h" |
samdanbury | 6:37b6d0d56190 | 33 | |
muhammadimran | 19:0cbe652c98cc | 34 | #include "MFRC522.h" |
muhammadimran | 19:0cbe652c98cc | 35 | |
chris | 10:0b5e0dfee08e | 36 | // Update this to the next number *before* a commit |
icraggs | 18:94da9de96d54 | 37 | #define __APP_SW_REVISION__ "18" |
chris | 10:0b5e0dfee08e | 38 | |
samdanbury | 6:37b6d0d56190 | 39 | // Configuration values needed to connect to IBM IoT Cloud |
muhammadimran | 19:0cbe652c98cc | 40 | #define ORG "nqnqo3" // For a registered connection, replace with your org |
muhammadimran | 19:0cbe652c98cc | 41 | #define ID "0002f7f260cc" // For a registered connection, replace with your id |
muhammadimran | 19:0cbe652c98cc | 42 | #define AUTH_TOKEN "xXgpv8_f4TM8*h&voy" // For a registered connection, replace with your auth-token |
muhammadimran | 19:0cbe652c98cc | 43 | #define TYPE "mbed-ers4" // For a registered connection, replace with your type |
samdanbury | 6:37b6d0d56190 | 44 | |
samdanbury | 6:37b6d0d56190 | 45 | #define MQTT_PORT 1883 |
samdanbury | 6:37b6d0d56190 | 46 | #define MQTT_TLS_PORT 8883 |
samdanbury | 6:37b6d0d56190 | 47 | #define IBM_IOT_PORT MQTT_PORT |
samdanbury | 6:37b6d0d56190 | 48 | |
samdanbury | 6:37b6d0d56190 | 49 | #define MQTT_MAX_PACKET_SIZE 250 |
samdanbury | 6:37b6d0d56190 | 50 | |
mazgch | 11:7a6df9a2dcdc | 51 | #if defined(TARGET_UBLOX_C027) |
mazgch | 11:7a6df9a2dcdc | 52 | #warning "Compiling for mbed C027" |
mazgch | 11:7a6df9a2dcdc | 53 | #include "C027.h" |
mazgch | 11:7a6df9a2dcdc | 54 | #elif defined(TARGET_LPC1768) |
samdanbury | 6:37b6d0d56190 | 55 | #warning "Compiling for mbed LPC1768" |
samdanbury | 6:37b6d0d56190 | 56 | #include "LPC1768.h" |
samdanbury | 6:37b6d0d56190 | 57 | #elif defined(TARGET_K64F) |
samdanbury | 6:37b6d0d56190 | 58 | #warning "Compiling for mbed K64F" |
samdanbury | 6:37b6d0d56190 | 59 | #include "K64F.h" |
samdanbury | 6:37b6d0d56190 | 60 | #endif |
samdanbury | 6:37b6d0d56190 | 61 | |
jsutton | 14:1f961d19f3cf | 62 | |
muhammadimran | 19:0cbe652c98cc | 63 | #define SPI_MOSI p5 |
muhammadimran | 19:0cbe652c98cc | 64 | #define SPI_MISO p6 |
muhammadimran | 19:0cbe652c98cc | 65 | #define SPI_SCK p7 |
muhammadimran | 19:0cbe652c98cc | 66 | #define SPI_CS p21 |
muhammadimran | 19:0cbe652c98cc | 67 | #define MF_RESET p8 |
muhammadimran | 19:0cbe652c98cc | 68 | #define UART_RX p10 |
muhammadimran | 19:0cbe652c98cc | 69 | #define UART_TX p9 |
muhammadimran | 19:0cbe652c98cc | 70 | |
muhammadimran | 19:0cbe652c98cc | 71 | //Serial connection to PC for output |
muhammadimran | 19:0cbe652c98cc | 72 | Serial pc(p9,p10); |
muhammadimran | 19:0cbe652c98cc | 73 | MFRC522 RfChip (SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, MF_RESET); |
muhammadimran | 19:0cbe652c98cc | 74 | |
icraggs | 8:80d49dd91542 | 75 | bool quickstartMode = true; |
muhammadimran | 19:0cbe652c98cc | 76 | char org[11] = ORG; |
samdanbury | 6:37b6d0d56190 | 77 | char type[30] = TYPE; |
samdanbury | 6:37b6d0d56190 | 78 | char id[30] = ID; // mac without colons |
samdanbury | 6:37b6d0d56190 | 79 | char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode |
samdanbury | 6:37b6d0d56190 | 80 | |
samdanbury | 6:37b6d0d56190 | 81 | bool connected = false; |
jsutton | 14:1f961d19f3cf | 82 | bool mqttConnecting = false; |
jsutton | 14:1f961d19f3cf | 83 | bool netConnected = false; |
jsutton | 14:1f961d19f3cf | 84 | bool netConnecting = false; |
jsutton | 14:1f961d19f3cf | 85 | bool ethernetInitialising = true; |
icraggs | 16:2420bfbf5f1c | 86 | int connack_rc = 0; // MQTT connack return code |
jsutton | 14:1f961d19f3cf | 87 | int retryAttempt = 0; |
jsutton | 14:1f961d19f3cf | 88 | int menuItem = 0; |
jsutton | 14:1f961d19f3cf | 89 | |
muhammadimran | 19:0cbe652c98cc | 90 | //char RFID[25]="ECD0D334"; |
muhammadimran | 19:0cbe652c98cc | 91 | //char* RFID_str = (char*) malloc (2*RfChip.uid.size + 1); |
muhammadimran | 19:0cbe652c98cc | 92 | char RFID_new[9]; |
muhammadimran | 19:0cbe652c98cc | 93 | char RFID_old[9]; |
samdanbury | 6:37b6d0d56190 | 94 | char* joystickPos = "CENTRE"; |
samdanbury | 6:37b6d0d56190 | 95 | int blink_interval = 0; |
samdanbury | 6:37b6d0d56190 | 96 | |
jsutton | 13:85801e3b83d3 | 97 | char* ip_addr = ""; |
jsutton | 13:85801e3b83d3 | 98 | char* gateway_addr = ""; |
jsutton | 14:1f961d19f3cf | 99 | char* host_addr = ""; |
jsutton | 14:1f961d19f3cf | 100 | int connectTimeout = 1000; |
jsutton | 14:1f961d19f3cf | 101 | |
muhammadimran | 19:0cbe652c98cc | 102 | bool tag_EN = true; |
muhammadimran | 19:0cbe652c98cc | 103 | bool New_RFID = false ; |
muhammadimran | 19:0cbe652c98cc | 104 | |
jsutton | 14:1f961d19f3cf | 105 | // If we wanted to manually set the MAC address, |
jsutton | 14:1f961d19f3cf | 106 | // this is how to do it. In this example, we take |
jsutton | 14:1f961d19f3cf | 107 | // the original Mbed Set MAC address and combine it |
jsutton | 14:1f961d19f3cf | 108 | // with a prefix of our choosing. |
muhammadimran | 19:0cbe652c98cc | 109 | /* |
jsutton | 14:1f961d19f3cf | 110 | extern "C" void $Super$$mbed_mac_address(char *s); |
muhammadimran | 19:0cbe652c98cc | 111 | extern "C" void $Sub$$mbed_mac_address(char *s) |
jsutton | 14:1f961d19f3cf | 112 | { |
muhammadimran | 19:0cbe652c98cc | 113 | char originalMAC[6] = ""; |
muhammadimran | 19:0cbe652c98cc | 114 | $Super$$mbed_mac_address(originalMAC); |
muhammadimran | 19:0cbe652c98cc | 115 | |
muhammadimran | 19:0cbe652c98cc | 116 | char mac[6]; |
muhammadimran | 19:0cbe652c98cc | 117 | mac[0] = 0x00; |
muhammadimran | 19:0cbe652c98cc | 118 | mac[1] = 0x08; |
muhammadimran | 19:0cbe652c98cc | 119 | mac[2] = 0xdc; |
muhammadimran | 19:0cbe652c98cc | 120 | mac[3] = originalMAC[3]; |
muhammadimran | 19:0cbe652c98cc | 121 | mac[4] = originalMAC[4]; |
muhammadimran | 19:0cbe652c98cc | 122 | mac[5] = originalMAC[5]; |
muhammadimran | 19:0cbe652c98cc | 123 | memcpy(s, mac, 6); |
jsutton | 14:1f961d19f3cf | 124 | } |
jsutton | 14:1f961d19f3cf | 125 | */ |
jsutton | 13:85801e3b83d3 | 126 | |
muhammadimran | 19:0cbe652c98cc | 127 | void tagRFID(void const *args) |
muhammadimran | 19:0cbe652c98cc | 128 | { |
muhammadimran | 19:0cbe652c98cc | 129 | |
muhammadimran | 19:0cbe652c98cc | 130 | |
muhammadimran | 19:0cbe652c98cc | 131 | while(tag_EN) |
muhammadimran | 19:0cbe652c98cc | 132 | { |
muhammadimran | 19:0cbe652c98cc | 133 | while( !RfChip.PICC_IsNewCardPresent()) { |
muhammadimran | 19:0cbe652c98cc | 134 | Thread::wait(10); |
muhammadimran | 19:0cbe652c98cc | 135 | continue; |
muhammadimran | 19:0cbe652c98cc | 136 | } |
muhammadimran | 19:0cbe652c98cc | 137 | if ( ! RfChip.PICC_ReadCardSerial()) { |
muhammadimran | 19:0cbe652c98cc | 138 | //wait_ms(100); |
muhammadimran | 19:0cbe652c98cc | 139 | //continue; |
muhammadimran | 19:0cbe652c98cc | 140 | } |
muhammadimran | 19:0cbe652c98cc | 141 | |
muhammadimran | 19:0cbe652c98cc | 142 | uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
muhammadimran | 19:0cbe652c98cc | 143 | |
muhammadimran | 19:0cbe652c98cc | 144 | sprintf(RFID_new,"%02X%02X%02X%02X",RfChip.uid.uidByte[0],RfChip.uid.uidByte[1],RfChip.uid.uidByte[2],RfChip.uid.uidByte[3]); |
muhammadimran | 19:0cbe652c98cc | 145 | } |
muhammadimran | 19:0cbe652c98cc | 146 | |
muhammadimran | 19:0cbe652c98cc | 147 | for(int i=0;i<9;i++) |
muhammadimran | 19:0cbe652c98cc | 148 | { |
muhammadimran | 19:0cbe652c98cc | 149 | RFID_old[i]=RFID_new[i]; |
muhammadimran | 19:0cbe652c98cc | 150 | } |
muhammadimran | 19:0cbe652c98cc | 151 | |
muhammadimran | 19:0cbe652c98cc | 152 | } |
samdanbury | 6:37b6d0d56190 | 153 | |
samdanbury | 6:37b6d0d56190 | 154 | void off() |
samdanbury | 6:37b6d0d56190 | 155 | { |
muhammadimran | 19:0cbe652c98cc | 156 | //r = g = b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 157 | } |
samdanbury | 6:37b6d0d56190 | 158 | |
samdanbury | 6:37b6d0d56190 | 159 | void red() |
samdanbury | 6:37b6d0d56190 | 160 | { |
muhammadimran | 19:0cbe652c98cc | 161 | //r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 162 | } |
samdanbury | 6:37b6d0d56190 | 163 | |
samdanbury | 6:37b6d0d56190 | 164 | void yellow() |
samdanbury | 6:37b6d0d56190 | 165 | { |
muhammadimran | 19:0cbe652c98cc | 166 | //r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 167 | } |
samdanbury | 6:37b6d0d56190 | 168 | |
samdanbury | 6:37b6d0d56190 | 169 | void green() |
samdanbury | 6:37b6d0d56190 | 170 | { |
muhammadimran | 19:0cbe652c98cc | 171 | //r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness |
samdanbury | 6:37b6d0d56190 | 172 | } |
samdanbury | 6:37b6d0d56190 | 173 | |
samdanbury | 6:37b6d0d56190 | 174 | |
samdanbury | 6:37b6d0d56190 | 175 | void flashing_yellow(void const *args) |
samdanbury | 6:37b6d0d56190 | 176 | { |
samdanbury | 6:37b6d0d56190 | 177 | bool on = false; |
muhammadimran | 19:0cbe652c98cc | 178 | while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD) { // flashing yellow only while connecting |
muhammadimran | 19:0cbe652c98cc | 179 | on = !on; |
samdanbury | 6:37b6d0d56190 | 180 | if (on) |
samdanbury | 6:37b6d0d56190 | 181 | yellow(); |
samdanbury | 6:37b6d0d56190 | 182 | else |
muhammadimran | 19:0cbe652c98cc | 183 | off(); |
samdanbury | 6:37b6d0d56190 | 184 | wait(0.5); |
samdanbury | 6:37b6d0d56190 | 185 | } |
samdanbury | 6:37b6d0d56190 | 186 | } |
samdanbury | 6:37b6d0d56190 | 187 | |
samdanbury | 6:37b6d0d56190 | 188 | |
samdanbury | 6:37b6d0d56190 | 189 | void flashing_red(void const *args) // to be used when the connection is lost |
samdanbury | 6:37b6d0d56190 | 190 | { |
samdanbury | 6:37b6d0d56190 | 191 | bool on = false; |
muhammadimran | 19:0cbe652c98cc | 192 | while (!connected) { |
samdanbury | 6:37b6d0d56190 | 193 | on = !on; |
samdanbury | 6:37b6d0d56190 | 194 | if (on) |
samdanbury | 6:37b6d0d56190 | 195 | red(); |
samdanbury | 6:37b6d0d56190 | 196 | else |
samdanbury | 6:37b6d0d56190 | 197 | off(); |
samdanbury | 6:37b6d0d56190 | 198 | wait(2.0); |
samdanbury | 6:37b6d0d56190 | 199 | } |
samdanbury | 6:37b6d0d56190 | 200 | } |
samdanbury | 6:37b6d0d56190 | 201 | |
samdanbury | 6:37b6d0d56190 | 202 | |
muhammadimran | 19:0cbe652c98cc | 203 | void printMenu(int menuItem) |
samdanbury | 6:37b6d0d56190 | 204 | { |
icraggs | 18:94da9de96d54 | 205 | static char last_line1[30] = "", last_line2[30] = ""; |
icraggs | 18:94da9de96d54 | 206 | char line1[30] = "", line2[30] = ""; |
muhammadimran | 19:0cbe652c98cc | 207 | |
muhammadimran | 19:0cbe652c98cc | 208 | switch (menuItem) { |
samdanbury | 6:37b6d0d56190 | 209 | case 0: |
icraggs | 18:94da9de96d54 | 210 | sprintf(line1, "IBM IoT Cloud"); |
icraggs | 18:94da9de96d54 | 211 | sprintf(line2, "Scroll with joystick"); |
samdanbury | 6:37b6d0d56190 | 212 | break; |
samdanbury | 6:37b6d0d56190 | 213 | case 1: |
icraggs | 18:94da9de96d54 | 214 | sprintf(line1, "Go to:"); |
icraggs | 18:94da9de96d54 | 215 | sprintf(line2, "http://ibm.biz/iotqstart"); |
samdanbury | 6:37b6d0d56190 | 216 | break; |
samdanbury | 6:37b6d0d56190 | 217 | case 2: |
icraggs | 18:94da9de96d54 | 218 | sprintf(line1, "Device Identity:"); |
icraggs | 18:94da9de96d54 | 219 | sprintf(line2, "%s", id); |
samdanbury | 6:37b6d0d56190 | 220 | break; |
samdanbury | 6:37b6d0d56190 | 221 | case 3: |
icraggs | 18:94da9de96d54 | 222 | sprintf(line1, "MQTT Status:"); |
icraggs | 16:2420bfbf5f1c | 223 | if (mqttConnecting) |
icraggs | 18:94da9de96d54 | 224 | sprintf(line2, "Connecting... %d/5", retryAttempt); |
muhammadimran | 19:0cbe652c98cc | 225 | else { |
icraggs | 16:2420bfbf5f1c | 226 | if (connected) |
icraggs | 18:94da9de96d54 | 227 | sprintf(line2, "Connected"); |
muhammadimran | 19:0cbe652c98cc | 228 | else { |
muhammadimran | 19:0cbe652c98cc | 229 | switch (connack_rc) { |
icraggs | 16:2420bfbf5f1c | 230 | case MQTT_CLIENTID_REJECTED: |
icraggs | 18:94da9de96d54 | 231 | sprintf(line2, "Clientid rejected"); |
icraggs | 16:2420bfbf5f1c | 232 | break; |
icraggs | 16:2420bfbf5f1c | 233 | case MQTT_BAD_USERNAME_OR_PASSWORD: |
icraggs | 18:94da9de96d54 | 234 | sprintf(line2, "Invalid username or password"); |
icraggs | 16:2420bfbf5f1c | 235 | break; |
icraggs | 16:2420bfbf5f1c | 236 | case MQTT_NOT_AUTHORIZED: |
icraggs | 18:94da9de96d54 | 237 | sprintf(line2, "Not authorized"); |
icraggs | 16:2420bfbf5f1c | 238 | break; |
icraggs | 16:2420bfbf5f1c | 239 | default: |
icraggs | 18:94da9de96d54 | 240 | sprintf(line2, "Disconnected"); |
icraggs | 16:2420bfbf5f1c | 241 | } |
icraggs | 16:2420bfbf5f1c | 242 | } |
jsutton | 14:1f961d19f3cf | 243 | } |
samdanbury | 6:37b6d0d56190 | 244 | break; |
chris | 10:0b5e0dfee08e | 245 | case 4: |
icraggs | 18:94da9de96d54 | 246 | sprintf(line1, "Ethernet State:"); |
icraggs | 18:94da9de96d54 | 247 | sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized"); |
jsutton | 14:1f961d19f3cf | 248 | break; |
jsutton | 14:1f961d19f3cf | 249 | case 5: |
icraggs | 18:94da9de96d54 | 250 | sprintf(line1, "Socket State:"); |
icraggs | 16:2420bfbf5f1c | 251 | if (netConnecting) |
icraggs | 18:94da9de96d54 | 252 | sprintf(line2, "Connecting... %d/5", retryAttempt); |
icraggs | 16:2420bfbf5f1c | 253 | else |
icraggs | 18:94da9de96d54 | 254 | sprintf(line2, netConnected ? "Connected" : "Disconnected"); |
jsutton | 13:85801e3b83d3 | 255 | break; |
jsutton | 14:1f961d19f3cf | 256 | case 6: |
icraggs | 18:94da9de96d54 | 257 | sprintf(line1, "IP Address:"); |
icraggs | 18:94da9de96d54 | 258 | sprintf(line2, "%s", ip_addr); |
jsutton | 13:85801e3b83d3 | 259 | break; |
jsutton | 14:1f961d19f3cf | 260 | case 7: |
icraggs | 18:94da9de96d54 | 261 | sprintf(line1, "Gateway:"); |
icraggs | 18:94da9de96d54 | 262 | sprintf(line2, "%s", gateway_addr); |
jsutton | 13:85801e3b83d3 | 263 | break; |
jsutton | 14:1f961d19f3cf | 264 | case 8: |
icraggs | 18:94da9de96d54 | 265 | sprintf(line1, "App version:"); |
icraggs | 18:94da9de96d54 | 266 | sprintf(line2, "%s", __APP_SW_REVISION__); |
chris | 10:0b5e0dfee08e | 267 | break; |
jsutton | 14:1f961d19f3cf | 268 | case 9: |
icraggs | 18:94da9de96d54 | 269 | sprintf(line1, "Current Timeout:"); |
icraggs | 18:94da9de96d54 | 270 | sprintf(line2, "%d ms", connectTimeout); |
jsutton | 14:1f961d19f3cf | 271 | break; |
samdanbury | 6:37b6d0d56190 | 272 | } |
muhammadimran | 19:0cbe652c98cc | 273 | |
muhammadimran | 19:0cbe652c98cc | 274 | if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0) { |
muhammadimran | 19:0cbe652c98cc | 275 | //lcd.cls(); |
muhammadimran | 19:0cbe652c98cc | 276 | //lcd.locate(0, 0); |
muhammadimran | 19:0cbe652c98cc | 277 | //lcd.printf(line1); |
icraggs | 18:94da9de96d54 | 278 | strncpy(last_line1, line1, sizeof(last_line1)); |
icraggs | 18:94da9de96d54 | 279 | |
muhammadimran | 19:0cbe652c98cc | 280 | //lcd.locate(0,16); |
muhammadimran | 19:0cbe652c98cc | 281 | //lcd.printf(line2); |
icraggs | 18:94da9de96d54 | 282 | strncpy(last_line2, line2, sizeof(last_line2)); |
icraggs | 18:94da9de96d54 | 283 | } |
samdanbury | 6:37b6d0d56190 | 284 | } |
samdanbury | 6:37b6d0d56190 | 285 | |
samdanbury | 6:37b6d0d56190 | 286 | |
samdanbury | 6:37b6d0d56190 | 287 | void setMenu() |
samdanbury | 6:37b6d0d56190 | 288 | { |
muhammadimran | 19:0cbe652c98cc | 289 | |
muhammadimran | 19:0cbe652c98cc | 290 | /*if (Down) |
samdanbury | 6:37b6d0d56190 | 291 | { |
samdanbury | 6:37b6d0d56190 | 292 | joystickPos = "DOWN"; |
jsutton | 14:1f961d19f3cf | 293 | if (menuItem >= 0 && menuItem < 9) |
samdanbury | 6:37b6d0d56190 | 294 | printMenu(++menuItem); |
muhammadimran | 19:0cbe652c98cc | 295 | } |
samdanbury | 6:37b6d0d56190 | 296 | else if (Left) |
samdanbury | 6:37b6d0d56190 | 297 | joystickPos = "LEFT"; |
samdanbury | 6:37b6d0d56190 | 298 | else if (Click) |
samdanbury | 6:37b6d0d56190 | 299 | joystickPos = "CLICK"; |
samdanbury | 6:37b6d0d56190 | 300 | else if (Up) |
samdanbury | 6:37b6d0d56190 | 301 | { |
samdanbury | 6:37b6d0d56190 | 302 | joystickPos = "UP"; |
jsutton | 14:1f961d19f3cf | 303 | if (menuItem <= 9 && menuItem > 0) |
samdanbury | 6:37b6d0d56190 | 304 | printMenu(--menuItem); |
samdanbury | 6:37b6d0d56190 | 305 | } |
samdanbury | 6:37b6d0d56190 | 306 | else if (Right) |
samdanbury | 6:37b6d0d56190 | 307 | joystickPos = "RIGHT"; |
samdanbury | 6:37b6d0d56190 | 308 | else |
muhammadimran | 19:0cbe652c98cc | 309 | joystickPos = "CENTRE";*/ |
samdanbury | 6:37b6d0d56190 | 310 | } |
samdanbury | 6:37b6d0d56190 | 311 | |
jsutton | 13:85801e3b83d3 | 312 | void menu_loop(void const *args) |
jsutton | 13:85801e3b83d3 | 313 | { |
jsutton | 14:1f961d19f3cf | 314 | int count = 0; |
muhammadimran | 19:0cbe652c98cc | 315 | while(true) { |
jsutton | 13:85801e3b83d3 | 316 | setMenu(); |
icraggs | 16:2420bfbf5f1c | 317 | if (++count % 10 == 0) |
jsutton | 14:1f961d19f3cf | 318 | printMenu(menuItem); |
jsutton | 14:1f961d19f3cf | 319 | Thread::wait(100); |
jsutton | 13:85801e3b83d3 | 320 | } |
jsutton | 13:85801e3b83d3 | 321 | } |
jsutton | 13:85801e3b83d3 | 322 | |
samdanbury | 6:37b6d0d56190 | 323 | |
samdanbury | 6:37b6d0d56190 | 324 | /** |
samdanbury | 6:37b6d0d56190 | 325 | * Display a message on the LCD screen prefixed with IBM IoT Cloud |
samdanbury | 6:37b6d0d56190 | 326 | */ |
muhammadimran | 19:0cbe652c98cc | 327 | |
muhammadimran | 19:0cbe652c98cc | 328 | /*void displayMessage(char* message) |
samdanbury | 6:37b6d0d56190 | 329 | { |
samdanbury | 6:37b6d0d56190 | 330 | lcd.cls(); |
muhammadimran | 19:0cbe652c98cc | 331 | lcd.locate(0,0); |
samdanbury | 6:37b6d0d56190 | 332 | lcd.printf("IBM IoT Cloud"); |
samdanbury | 6:37b6d0d56190 | 333 | lcd.locate(0,16); |
samdanbury | 6:37b6d0d56190 | 334 | lcd.printf(message); |
muhammadimran | 19:0cbe652c98cc | 335 | }*/ |
samdanbury | 6:37b6d0d56190 | 336 | |
samdanbury | 6:37b6d0d56190 | 337 | |
samdanbury | 6:37b6d0d56190 | 338 | int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
muhammadimran | 19:0cbe652c98cc | 339 | { |
samdanbury | 6:37b6d0d56190 | 340 | const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com"; |
muhammadimran | 19:0cbe652c98cc | 341 | |
samdanbury | 6:37b6d0d56190 | 342 | char hostname[strlen(org) + strlen(iot_ibm) + 1]; |
samdanbury | 6:37b6d0d56190 | 343 | sprintf(hostname, "%s%s", org, iot_ibm); |
jsutton | 13:85801e3b83d3 | 344 | EthernetInterface& eth = ipstack->getEth(); |
jsutton | 13:85801e3b83d3 | 345 | ip_addr = eth.getIPAddress(); |
jsutton | 13:85801e3b83d3 | 346 | gateway_addr = eth.getGateway(); |
muhammadimran | 19:0cbe652c98cc | 347 | |
jsutton | 14:1f961d19f3cf | 348 | // Construct clientId - d:org:type:id |
jsutton | 14:1f961d19f3cf | 349 | char clientId[strlen(org) + strlen(type) + strlen(id) + 5]; |
jsutton | 14:1f961d19f3cf | 350 | sprintf(clientId, "d:%s:%s:%s", org, type, id); |
muhammadimran | 19:0cbe652c98cc | 351 | |
muhammadimran | 19:0cbe652c98cc | 352 | // Network debug statements |
jsutton | 14:1f961d19f3cf | 353 | LOG("=====================================\n"); |
jsutton | 14:1f961d19f3cf | 354 | LOG("Connecting Ethernet.\n"); |
jsutton | 14:1f961d19f3cf | 355 | LOG("IP ADDRESS: %s\n", eth.getIPAddress()); |
jsutton | 14:1f961d19f3cf | 356 | LOG("MAC ADDRESS: %s\n", eth.getMACAddress()); |
jsutton | 14:1f961d19f3cf | 357 | LOG("Gateway: %s\n", eth.getGateway()); |
jsutton | 14:1f961d19f3cf | 358 | LOG("Network Mask: %s\n", eth.getNetworkMask()); |
jsutton | 14:1f961d19f3cf | 359 | LOG("Server Hostname: %s\n", hostname); |
jsutton | 14:1f961d19f3cf | 360 | LOG("Client ID: %s\n", clientId); |
jsutton | 14:1f961d19f3cf | 361 | LOG("=====================================\n"); |
muhammadimran | 19:0cbe652c98cc | 362 | |
jsutton | 14:1f961d19f3cf | 363 | netConnecting = true; |
jsutton | 14:1f961d19f3cf | 364 | int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout); |
muhammadimran | 19:0cbe652c98cc | 365 | if (rc != 0) { |
muhammadimran | 19:0cbe652c98cc | 366 | WARN("IP Stack connect returned: %d\n", rc); |
samdanbury | 6:37b6d0d56190 | 367 | return rc; |
jsutton | 13:85801e3b83d3 | 368 | } |
jsutton | 13:85801e3b83d3 | 369 | netConnected = true; |
jsutton | 14:1f961d19f3cf | 370 | netConnecting = false; |
jsutton | 14:1f961d19f3cf | 371 | |
samdanbury | 6:37b6d0d56190 | 372 | // MQTT Connect |
jsutton | 14:1f961d19f3cf | 373 | mqttConnecting = true; |
samdanbury | 6:37b6d0d56190 | 374 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
samdanbury | 6:37b6d0d56190 | 375 | data.MQTTVersion = 3; |
samdanbury | 6:37b6d0d56190 | 376 | data.clientID.cstring = clientId; |
muhammadimran | 19:0cbe652c98cc | 377 | |
muhammadimran | 19:0cbe652c98cc | 378 | if (!quickstartMode) { |
samdanbury | 6:37b6d0d56190 | 379 | data.username.cstring = "use-token-auth"; |
samdanbury | 6:37b6d0d56190 | 380 | data.password.cstring = auth_token; |
samdanbury | 6:37b6d0d56190 | 381 | } |
muhammadimran | 19:0cbe652c98cc | 382 | |
muhammadimran | 19:0cbe652c98cc | 383 | if ((rc = client->connect(data)) == 0) { |
samdanbury | 6:37b6d0d56190 | 384 | connected = true; |
muhammadimran | 19:0cbe652c98cc | 385 | green(); |
muhammadimran | 19:0cbe652c98cc | 386 | //displayMessage("Connected"); |
jsutton | 13:85801e3b83d3 | 387 | wait(1); |
muhammadimran | 19:0cbe652c98cc | 388 | //displayMessage("Scroll with joystick"); |
muhammadimran | 19:0cbe652c98cc | 389 | } else |
icraggs | 16:2420bfbf5f1c | 390 | WARN("MQTT connect returned %d\n", rc); |
icraggs | 16:2420bfbf5f1c | 391 | if (rc >= 0) |
icraggs | 16:2420bfbf5f1c | 392 | connack_rc = rc; |
jsutton | 14:1f961d19f3cf | 393 | mqttConnecting = false; |
samdanbury | 6:37b6d0d56190 | 394 | return rc; |
samdanbury | 6:37b6d0d56190 | 395 | } |
samdanbury | 6:37b6d0d56190 | 396 | |
samdanbury | 6:37b6d0d56190 | 397 | |
samdanbury | 6:37b6d0d56190 | 398 | int getConnTimeout(int attemptNumber) |
muhammadimran | 19:0cbe652c98cc | 399 | { |
muhammadimran | 19:0cbe652c98cc | 400 | // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute |
muhammadimran | 19:0cbe652c98cc | 401 | // after 20 attempts, retry every 10 minutes |
samdanbury | 6:37b6d0d56190 | 402 | return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; |
samdanbury | 6:37b6d0d56190 | 403 | } |
samdanbury | 6:37b6d0d56190 | 404 | |
samdanbury | 6:37b6d0d56190 | 405 | |
samdanbury | 6:37b6d0d56190 | 406 | void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 407 | { |
samdanbury | 6:37b6d0d56190 | 408 | connected = false; |
muhammadimran | 19:0cbe652c98cc | 409 | |
icraggs | 8:80d49dd91542 | 410 | // make sure a cable is connected before starting to connect |
muhammadimran | 19:0cbe652c98cc | 411 | while (!linkStatus()) { |
icraggs | 8:80d49dd91542 | 412 | wait(1.0f); |
icraggs | 8:80d49dd91542 | 413 | WARN("Ethernet link not present. Check cable connection\n"); |
icraggs | 8:80d49dd91542 | 414 | } |
muhammadimran | 19:0cbe652c98cc | 415 | |
muhammadimran | 19:0cbe652c98cc | 416 | while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) { |
icraggs | 16:2420bfbf5f1c | 417 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
icraggs | 16:2420bfbf5f1c | 418 | return; // don't reattempt to connect if credentials are wrong |
muhammadimran | 19:0cbe652c98cc | 419 | |
samdanbury | 6:37b6d0d56190 | 420 | Thread red_thread(flashing_red); |
chris | 12:8b480eb8a496 | 421 | |
samdanbury | 6:37b6d0d56190 | 422 | int timeout = getConnTimeout(++retryAttempt); |
samdanbury | 6:37b6d0d56190 | 423 | WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); |
muhammadimran | 19:0cbe652c98cc | 424 | |
icraggs | 8:80d49dd91542 | 425 | // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed |
icraggs | 8:80d49dd91542 | 426 | // or maybe just add the proper members to do this disconnect and call attemptConnect(...) |
muhammadimran | 19:0cbe652c98cc | 427 | |
icraggs | 8:80d49dd91542 | 428 | // this works - reset the system when the retry count gets to a threshold |
icraggs | 8:80d49dd91542 | 429 | if (retryAttempt == 5) |
icraggs | 8:80d49dd91542 | 430 | NVIC_SystemReset(); |
icraggs | 8:80d49dd91542 | 431 | else |
icraggs | 8:80d49dd91542 | 432 | wait(timeout); |
samdanbury | 6:37b6d0d56190 | 433 | } |
samdanbury | 6:37b6d0d56190 | 434 | } |
samdanbury | 6:37b6d0d56190 | 435 | |
samdanbury | 6:37b6d0d56190 | 436 | |
samdanbury | 6:37b6d0d56190 | 437 | int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack) |
samdanbury | 6:37b6d0d56190 | 438 | { |
samdanbury | 6:37b6d0d56190 | 439 | MQTT::Message message; |
samdanbury | 6:37b6d0d56190 | 440 | char* pubTopic = "iot-2/evt/status/fmt/json"; |
muhammadimran | 19:0cbe652c98cc | 441 | |
samdanbury | 6:37b6d0d56190 | 442 | char buf[250]; |
muhammadimran | 19:0cbe652c98cc | 443 | /*printf(buf, |
muhammadimran | 19:0cbe652c98cc | 444 | "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f,\"badge\":\"%s\"}}", |
muhammadimran | 19:0cbe652c98cc | 445 | 0.5, 0.5, 0.5,0.5,RFID, 0.5, 0.5,RFID_str);*/ |
muhammadimran | 19:0cbe652c98cc | 446 | |
muhammadimran | 19:0cbe652c98cc | 447 | sprintf(buf,"{\"d\":{\"myName\":\"IoT mbed\",\"key\":\"%s\"}}",RFID_old); |
samdanbury | 6:37b6d0d56190 | 448 | message.qos = MQTT::QOS0; |
samdanbury | 6:37b6d0d56190 | 449 | message.retained = false; |
samdanbury | 6:37b6d0d56190 | 450 | message.dup = false; |
samdanbury | 6:37b6d0d56190 | 451 | message.payload = (void*)buf; |
samdanbury | 6:37b6d0d56190 | 452 | message.payloadlen = strlen(buf); |
muhammadimran | 19:0cbe652c98cc | 453 | |
samdanbury | 6:37b6d0d56190 | 454 | LOG("Publishing %s\n", buf); |
icraggs | 8:80d49dd91542 | 455 | return client->publish(pubTopic, message); |
samdanbury | 6:37b6d0d56190 | 456 | } |
samdanbury | 6:37b6d0d56190 | 457 | |
samdanbury | 6:37b6d0d56190 | 458 | |
samdanbury | 6:37b6d0d56190 | 459 | char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address |
muhammadimran | 19:0cbe652c98cc | 460 | { |
samdanbury | 6:37b6d0d56190 | 461 | strncpy(buf, eth.getMACAddress(), buflen); |
samdanbury | 6:37b6d0d56190 | 462 | |
samdanbury | 6:37b6d0d56190 | 463 | char* pos; // Remove colons from mac address |
samdanbury | 6:37b6d0d56190 | 464 | while ((pos = strchr(buf, ':')) != NULL) |
samdanbury | 6:37b6d0d56190 | 465 | memmove(pos, pos + 1, strlen(pos) + 1); |
samdanbury | 6:37b6d0d56190 | 466 | return buf; |
samdanbury | 6:37b6d0d56190 | 467 | } |
chris | 12:8b480eb8a496 | 468 | |
samdanbury | 6:37b6d0d56190 | 469 | |
samdanbury | 6:37b6d0d56190 | 470 | void messageArrived(MQTT::MessageData& md) |
samdanbury | 6:37b6d0d56190 | 471 | { |
samdanbury | 6:37b6d0d56190 | 472 | MQTT::Message &message = md.message; |
samdanbury | 6:37b6d0d56190 | 473 | char topic[md.topicName.lenstring.len + 1]; |
muhammadimran | 19:0cbe652c98cc | 474 | |
samdanbury | 6:37b6d0d56190 | 475 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
muhammadimran | 19:0cbe652c98cc | 476 | |
muhammadimran | 19:0cbe652c98cc | 477 | LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload); |
samdanbury | 6:37b6d0d56190 | 478 | |
samdanbury | 6:37b6d0d56190 | 479 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
samdanbury | 6:37b6d0d56190 | 480 | char* start = strstr(topic, "/cmd/") + 5; |
samdanbury | 6:37b6d0d56190 | 481 | int len = strstr(topic, "/fmt/") - start; |
muhammadimran | 19:0cbe652c98cc | 482 | |
muhammadimran | 19:0cbe652c98cc | 483 | if (memcmp(start, "blink", len) == 0) { |
samdanbury | 6:37b6d0d56190 | 484 | char payload[message.payloadlen + 1]; |
samdanbury | 6:37b6d0d56190 | 485 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
muhammadimran | 19:0cbe652c98cc | 486 | |
muhammadimran | 19:0cbe652c98cc | 487 | |
samdanbury | 6:37b6d0d56190 | 488 | char* pos = strchr(payload, '}'); |
muhammadimran | 19:0cbe652c98cc | 489 | if (pos != NULL) { |
samdanbury | 6:37b6d0d56190 | 490 | *pos = '\0'; |
muhammadimran | 19:0cbe652c98cc | 491 | if ((pos = strchr(payload, ':')) != NULL) { |
muhammadimran | 19:0cbe652c98cc | 492 | int blink_rate = atoi(pos + 1); |
muhammadimran | 19:0cbe652c98cc | 493 | blink_interval = blink_rate; |
samdanbury | 6:37b6d0d56190 | 494 | } |
samdanbury | 6:37b6d0d56190 | 495 | } |
muhammadimran | 19:0cbe652c98cc | 496 | } else |
samdanbury | 6:37b6d0d56190 | 497 | WARN("Unsupported command: %.*s\n", len, start); |
samdanbury | 6:37b6d0d56190 | 498 | } |
samdanbury | 6:37b6d0d56190 | 499 | |
samdanbury | 6:37b6d0d56190 | 500 | |
samdanbury | 6:37b6d0d56190 | 501 | int main() |
muhammadimran | 19:0cbe652c98cc | 502 | { |
muhammadimran | 19:0cbe652c98cc | 503 | RfChip.PCD_Init(); |
icraggs | 8:80d49dd91542 | 504 | quickstartMode = (strcmp(org, "quickstart") == 0); |
icraggs | 8:80d49dd91542 | 505 | |
muhammadimran | 19:0cbe652c98cc | 506 | //lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen |
muhammadimran | 19:0cbe652c98cc | 507 | |
muhammadimran | 19:0cbe652c98cc | 508 | led2 = LED2_OFF; // K64F: turn off the main board LED |
muhammadimran | 19:0cbe652c98cc | 509 | |
muhammadimran | 19:0cbe652c98cc | 510 | //displayMessage("Connecting"); |
samdanbury | 6:37b6d0d56190 | 511 | |
jsutton | 13:85801e3b83d3 | 512 | Thread yellow_thread(flashing_yellow); |
muhammadimran | 19:0cbe652c98cc | 513 | Thread menu_thread(menu_loop); |
muhammadimran | 19:0cbe652c98cc | 514 | Thread tag_thread(tagRFID); |
muhammadimran | 19:0cbe652c98cc | 515 | |
jsutton | 14:1f961d19f3cf | 516 | LOG("***** IBM IoT Client Ethernet Example *****\n"); |
samdanbury | 6:37b6d0d56190 | 517 | MQTTEthernet ipstack; |
jsutton | 14:1f961d19f3cf | 518 | ethernetInitialising = false; |
samdanbury | 6:37b6d0d56190 | 519 | MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); |
muhammadimran | 19:0cbe652c98cc | 520 | LOG("Ethernet Initialized\n"); |
muhammadimran | 19:0cbe652c98cc | 521 | |
samdanbury | 6:37b6d0d56190 | 522 | if (quickstartMode) |
icraggs | 16:2420bfbf5f1c | 523 | getMac(ipstack.getEth(), id, sizeof(id)); |
muhammadimran | 19:0cbe652c98cc | 524 | |
icraggs | 16:2420bfbf5f1c | 525 | attemptConnect(&client, &ipstack); |
muhammadimran | 19:0cbe652c98cc | 526 | |
muhammadimran | 19:0cbe652c98cc | 527 | if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { |
icraggs | 16:2420bfbf5f1c | 528 | red(); |
icraggs | 16:2420bfbf5f1c | 529 | while (true) |
icraggs | 16:2420bfbf5f1c | 530 | wait(1.0); // Permanent failures - don't retry |
samdanbury | 6:37b6d0d56190 | 531 | } |
muhammadimran | 19:0cbe652c98cc | 532 | |
muhammadimran | 19:0cbe652c98cc | 533 | if (!quickstartMode) { |
samdanbury | 6:37b6d0d56190 | 534 | int rc = 0; |
samdanbury | 6:37b6d0d56190 | 535 | if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) |
muhammadimran | 19:0cbe652c98cc | 536 | WARN("rc from MQTT subscribe is %d\n", rc); |
samdanbury | 6:37b6d0d56190 | 537 | } |
muhammadimran | 19:0cbe652c98cc | 538 | |
samdanbury | 6:37b6d0d56190 | 539 | blink_interval = 0; |
samdanbury | 6:37b6d0d56190 | 540 | int count = 0; |
muhammadimran | 19:0cbe652c98cc | 541 | while (true) { |
muhammadimran | 19:0cbe652c98cc | 542 | if (true) { |
muhammadimran | 19:0cbe652c98cc | 543 | client.yield(100); // allow the MQTT client to receive messages |
muhammadimran | 19:0cbe652c98cc | 544 | // Publish a message every second |
muhammadimran | 19:0cbe652c98cc | 545 | /*if ( ! RfChip.PICC_IsNewCardPresent()) { |
muhammadimran | 19:0cbe652c98cc | 546 | wait_ms(100); |
muhammadimran | 19:0cbe652c98cc | 547 | continue; |
muhammadimran | 19:0cbe652c98cc | 548 | } |
muhammadimran | 19:0cbe652c98cc | 549 | |
muhammadimran | 19:0cbe652c98cc | 550 | if ( ! RfChip.PICC_ReadCardSerial()) { |
muhammadimran | 19:0cbe652c98cc | 551 | wait_ms(100); |
muhammadimran | 19:0cbe652c98cc | 552 | continue; |
muhammadimran | 19:0cbe652c98cc | 553 | }*/ |
muhammadimran | 19:0cbe652c98cc | 554 | |
muhammadimran | 19:0cbe652c98cc | 555 | //LedGreen = 0; |
muhammadimran | 19:0cbe652c98cc | 556 | // Print Card UID |
muhammadimran | 19:0cbe652c98cc | 557 | //intf("Card UID: "); |
muhammadimran | 19:0cbe652c98cc | 558 | //char* RFID_str = (char*) malloc (2*RfChip.uid.size + 1); |
muhammadimran | 19:0cbe652c98cc | 559 | /* char* RFID_ptr = RFID_str; |
muhammadimran | 19:0cbe652c98cc | 560 | for (uint8_t i = 0; i < RfChip.uid.size; i++) { |
muhammadimran | 19:0cbe652c98cc | 561 | printf("%2X", RfChip.uid.uidByte[i]); |
muhammadimran | 19:0cbe652c98cc | 562 | RFID_ptr += sprintf(RFID_ptr, "%02X", RfChip.uid.uidByte[i]); |
muhammadimran | 19:0cbe652c98cc | 563 | }*/ |
muhammadimran | 19:0cbe652c98cc | 564 | |
muhammadimran | 19:0cbe652c98cc | 565 | //sprintf(RFID_str,"%02X%02X%02X%02X",RfChip.uid.uidByte[0],RfChip.uid.uidByte[1],RfChip.uid.uidByte[2],RfChip.uid.uidByte[3]); |
muhammadimran | 19:0cbe652c98cc | 566 | /* |
muhammadimran | 19:0cbe652c98cc | 567 | sprintf(RFID_ptr,"\n\t"); |
muhammadimran | 19:0cbe652c98cc | 568 | *(RFID_ptr + 1) = '\0'; |
muhammadimran | 19:0cbe652c98cc | 569 | //printf("badge no : %s\n", RFID_str); |
muhammadimran | 19:0cbe652c98cc | 570 | printf("\n"); |
muhammadimran | 19:0cbe652c98cc | 571 | //RFID2 = RFID_str; |
muhammadimran | 19:0cbe652c98cc | 572 | */ |
muhammadimran | 19:0cbe652c98cc | 573 | // Print Card type |
muhammadimran | 19:0cbe652c98cc | 574 | //uint8_t piccType = RfChip.PICC_GetType(RfChip.uid.sak); |
muhammadimran | 19:0cbe652c98cc | 575 | //printf("PICC Type: %s \n\r", RfChip.PICC_GetTypeName(piccType)); |
muhammadimran | 19:0cbe652c98cc | 576 | |
muhammadimran | 19:0cbe652c98cc | 577 | wait_ms(100); |
muhammadimran | 19:0cbe652c98cc | 578 | |
muhammadimran | 19:0cbe652c98cc | 579 | //if(New_RFID == true ) |
muhammadimran | 19:0cbe652c98cc | 580 | //{ |
muhammadimran | 19:0cbe652c98cc | 581 | if (publish(&client, &ipstack) != 0) |
samdanbury | 6:37b6d0d56190 | 582 | attemptConnect(&client, &ipstack); // if we have lost the connection |
muhammadimran | 19:0cbe652c98cc | 583 | count = 0; |
muhammadimran | 19:0cbe652c98cc | 584 | |
muhammadimran | 19:0cbe652c98cc | 585 | /*if (blink_interval == 1) |
muhammadimran | 19:0cbe652c98cc | 586 | { |
muhammadimran | 19:0cbe652c98cc | 587 | if ( RfChip.PICC_IsNewCardPresent()) {led2.write(1);} |
muhammadimran | 19:0cbe652c98cc | 588 | else |
muhammadimran | 19:0cbe652c98cc | 589 | {led2.write(0);} |
muhammadimran | 19:0cbe652c98cc | 590 | } |
muhammadimran | 19:0cbe652c98cc | 591 | else |
muhammadimran | 19:0cbe652c98cc | 592 | {led2.write(0);}*/ |
muhammadimran | 19:0cbe652c98cc | 593 | //} |
muhammadimran | 19:0cbe652c98cc | 594 | |
muhammadimran | 19:0cbe652c98cc | 595 | // client.yield(100); // allow the MQTT client to receive messages |
muhammadimran | 19:0cbe652c98cc | 596 | |
muhammadimran | 19:0cbe652c98cc | 597 | //LedGreen = 1; |
muhammadimran | 19:0cbe652c98cc | 598 | |
muhammadimran | 19:0cbe652c98cc | 599 | //Partie RFID |
muhammadimran | 19:0cbe652c98cc | 600 | // |
muhammadimran | 19:0cbe652c98cc | 601 | // Look for new cards |
muhammadimran | 19:0cbe652c98cc | 602 | |
muhammadimran | 19:0cbe652c98cc | 603 | |
muhammadimran | 19:0cbe652c98cc | 604 | // Select one of the cards |
samdanbury | 6:37b6d0d56190 | 605 | } |
samdanbury | 6:37b6d0d56190 | 606 | } |
samdanbury | 6:37b6d0d56190 | 607 | } |
muhammadimran | 19:0cbe652c98cc | 608 | |
muhammadimran | 19:0cbe652c98cc | 609 |