Contains example code from H&N program to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 EthernetInterface LM75B MFRC522 MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Files at this revision

API Documentation at this revision

Comitter:
haziq10
Date:
Tue Mar 01 08:05:27 2016 +0000
Parent:
18:94da9de96d54
Commit message:
Salam aleykoum

Changed in this revision

MFRC522.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MFRC522.lib	Tue Mar 01 08:05:27 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AtomX/code/MFRC522/#63d729186747
--- a/main.cpp	Wed Aug 12 20:50:56 2015 +0000
+++ b/main.cpp	Tue Mar 01 08:05:27 2016 +0000
@@ -28,17 +28,24 @@
 #include "MQTTClient.h"
 #include "MQTTEthernet.h"
 #include "C12832.h"
-#include "Arial12x12.h"
 #include "rtos.h"
+#include "MFRC522.h"    //RFID
+
+#define SPI_MOSI    p5
+#define SPI_MISO    p6
+#define SPI_SCLK    p7
+#define SPI_CS      p21
+// KL25Z Pin for MFRC522 reset
+#define MF_RESET    p8
 
 // Update this to the next number *before* a commit
 #define __APP_SW_REVISION__ "18"
 
 // Configuration values needed to connect to IBM IoT Cloud
-#define ORG "quickstart"             // For a registered connection, replace with your org
-#define ID ""                        // For a registered connection, replace with your id
-#define AUTH_TOKEN ""                // For a registered connection, replace with your auth-token
-#define TYPE DEFAULT_TYPE_NAME       // For a registered connection, replace with your type
+#define ORG "bmdu2g"             // For a registered connection, replace with your org
+#define ID "0002f7f1f9e0"                        // For a registered connection, replace with your id
+#define AUTH_TOKEN "BnpZYb-ih(UDB1ur_i"                // For a registered connection, replace with your auth-token
+#define TYPE "Kiffy"      // For a registered connection, replace with your type
 
 #define MQTT_PORT 1883
 #define MQTT_TLS_PORT 8883
@@ -57,6 +64,10 @@
 #include "K64F.h"
 #endif
 
+MFRC522    RfChip   (SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS, MF_RESET);       //RFID
+
+DigitalOut LedGreen(LED1);
+//Ticker tick;        //RFID
 
 bool quickstartMode = true;
 char org[11] = ORG;  
@@ -73,12 +84,13 @@
 int retryAttempt = 0;
 int menuItem = 0;
 
-char* joystickPos = "CENTRE";
 int blink_interval = 0;
 
 char* ip_addr = "";
 char* gateway_addr = "";
 char* host_addr = "";
+char bnj[20] ="Bienvenue ";
+char badge[50] = "Empty";
 int connectTimeout = 1000;
 
 // If we wanted to manually set the MAC address,
@@ -103,7 +115,7 @@
 }
 */
 
-
+/*
 void off()
 {
     r = g = b = 1.0;    // 1 is off, 0 is full brightness
@@ -122,9 +134,9 @@
 void green()
 {
     r = 1.0; g = 0.7; b = 1.0;    // 1 is off, 0 is full brightness
-}
+}*/
 
-
+/*
 void flashing_yellow(void const *args)
 {
     bool on = false;
@@ -137,9 +149,9 @@
             off();   
         wait(0.5);
     }
-}
+}*/
 
-
+/*
 void flashing_red(void const *args)  // to be used when the connection is lost
 {
     bool on = false;
@@ -152,84 +164,84 @@
             off();
         wait(2.0);
     }
-}
+}*/
 
 
 void printMenu(int menuItem) 
 {
-    static char last_line1[30] = "", last_line2[30] = "";
-    char line1[30] = "", line2[30] = "";
+    /*static char last_line1[30] = "", last_line2[30] = "";
+    char line1[30] = "", line2[30] = "";*/
         
     switch (menuItem)
     {
         case 0:
-            sprintf(line1, "IBM IoT Cloud");
-            sprintf(line2, "Scroll with joystick");
+            puts("IBM IoT Cloud");
+            puts("Scroll with joystick");
             break;
         case 1:
-            sprintf(line1, "Go to:");
-            sprintf(line2, "http://ibm.biz/iotqstart");
+            puts("Go to:");
+            puts("http://ibm.biz/iotqstart");
             break;
         case 2:
-            sprintf(line1, "Device Identity:");
-            sprintf(line2, "%s", id);
+            puts("Device Identity:");
+            printf("%s\n", id);
             break;
         case 3:
-            sprintf(line1, "MQTT Status:");
+            puts("MQTT Status:");
             if (mqttConnecting)
-                sprintf(line2, "Connecting... %d/5", retryAttempt);
+                printf("Connecting... %d/5\n", retryAttempt);
             else
             {
                 if (connected)
-                    sprintf(line2, "Connected");
+                    puts("Connected");
                 else
                 {
                     switch (connack_rc)
                     {
                         case MQTT_CLIENTID_REJECTED:
-                            sprintf(line2, "Clientid rejected");
+                            puts("Clientid rejected");
                             break;
                         case MQTT_BAD_USERNAME_OR_PASSWORD:
-                            sprintf(line2, "Invalid username or password");
+                            puts("Invalid username or password");
                             break;
                         case MQTT_NOT_AUTHORIZED:
-                            sprintf(line2, "Not authorized");
+                            puts("Not authorized");
                             break;
                         default:
-                            sprintf(line2, "Disconnected");
+                            puts("Disconnected");
                     }
                 }
             }
             break;
         case 4:
-            sprintf(line1, "Ethernet State:");
-            sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized");
+            puts("Ethernet State:");
+            puts(ethernetInitialising ? "Initializing..." : "Initialized");
             break;
         case 5:
-            sprintf(line1, "Socket State:");
+            puts("Socket State:");
             if (netConnecting)
-                sprintf(line2, "Connecting... %d/5", retryAttempt);
+                printf("Connecting... %d/5\n", retryAttempt);
             else
-                sprintf(line2, netConnected ? "Connected" : "Disconnected");
+                puts(netConnected ? "Connected" : "Disconnected");
             break;
         case 6:
-            sprintf(line1, "IP Address:");
-            sprintf(line2, "%s", ip_addr);
+            puts("IP Address:");
+            printf("%s\n", ip_addr);
             break;
         case 7:
-            sprintf(line1, "Gateway:");
-            sprintf(line2, "%s", gateway_addr);
+            puts("Gateway:");
+            printf("%s\n", gateway_addr);
             break;
         case 8:
-            sprintf(line1, "App version:");
-            sprintf(line2, "%s", __APP_SW_REVISION__);
+            puts("App version:");
+            printf("%s\n", __APP_SW_REVISION__);
             break;
         case 9:
-            sprintf(line1, "Current Timeout:");
-            sprintf(line2, "%d ms", connectTimeout);
+            puts("Current Timeout:");
+            printf("%d ms\n", connectTimeout);
             break;
     }
-    
+    /*
     if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0)
     {
         lcd.cls(); 
@@ -240,10 +252,10 @@
         lcd.locate(0,16);
         lcd.printf(line2);
         strncpy(last_line2, line2, sizeof(last_line2));
-    }
+    }*/
 }
 
-
+/*
 void setMenu()
 {
     
@@ -267,8 +279,8 @@
         joystickPos = "RIGHT";
     else
         joystickPos = "CENTRE";
-}
-
+}*/
+/*
 void menu_loop(void const *args)
 {
     int count = 0;
@@ -279,7 +291,7 @@
             printMenu(menuItem);
         Thread::wait(100);
     }
-}
+}*/
 
 
 /**
@@ -287,11 +299,10 @@
  */
 void displayMessage(char* message)
 {
-    lcd.cls();
-    lcd.locate(0,0);        
-    lcd.printf("IBM IoT Cloud");
-    lcd.locate(0,16);
-    lcd.printf(message);
+    /*lcd.cls();
+    lcd.locate(0,0);   */     
+    puts("IBM IoT Cloud");
+    puts(message);
 }
 
 
@@ -345,7 +356,7 @@
     if ((rc = client->connect(data)) == 0) 
     {       
         connected = true;
-        green();    
+        //green();    
         displayMessage("Connected");
         wait(1);
         displayMessage("Scroll with joystick");
@@ -382,7 +393,7 @@
         if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
             return; // don't reattempt to connect if credentials are wrong
             
-        Thread red_thread(flashing_red);
+        //Thread red_thread(flashing_red);
 
         int timeout = getConnTimeout(++retryAttempt);
         WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
@@ -406,8 +417,7 @@
             
     char buf[250];
     sprintf(buf,
-     "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
-            MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
+     "{\"d\":{\"myName\":\"IoT mbed\",\"mbedMsg\":\"%s\",\"mbedBadge\":\"%s\"}}",bnj,badge);
     message.qos = MQTT::QOS0;
     message.retained = false;
     message.dup = false;
@@ -464,17 +474,65 @@
 }
 
 
+
+void threadRFID(void const *argsvoid){
+    while(true){
+        
+        LedGreen = 1;
+    
+        // Look for new cards
+        if ( ! RfChip.PICC_IsNewCardPresent())
+        {
+          wait_ms(500);
+          continue;
+        }
+    
+        // Select one of the cards
+        if ( ! RfChip.PICC_ReadCardSerial())
+        {
+          wait_ms(500);
+          continue;
+        }
+    
+        LedGreen = 0;
+        
+        strcpy (badge, "");
+        // Print Card UID
+        printf("Card UID: ");
+        for (uint8_t i = 0; i < RfChip.uid.size; i++)
+        {
+          printf(" %X", RfChip.uid.uidByte[i]);
+        }
+        printf("\n\r");
+        
+        sprintf(badge,"%02X%02X%02X%02X", RfChip.uid.uidByte[0],RfChip.uid.uidByte[1],RfChip.uid.uidByte[2],RfChip.uid.uidByte[3]);
+        
+        Thread::wait(100);
+    }
+    
+}
+
+
+
 int main()
 {    
+
+    // Init. RC522 Chip
+    RfChip.PCD_Init();
+    
+    //tick.attach(&tickRFID,100);
+    Thread thread(threadRFID);
+  
+  
     quickstartMode = (strcmp(org, "quickstart") == 0);
 
-    lcd.set_font((unsigned char*) Arial12x12);  // Set a nice font for the LCD screen
+    //lcd.set_font((unsigned char*) Arial12x12);  // Set a nice font for the LCD screen
     
     led2 = LED2_OFF; // K64F: turn off the main board LED 
     
     displayMessage("Connecting");
-    Thread yellow_thread(flashing_yellow);
-    Thread menu_thread(menu_loop);  
+    /*Thread yellow_thread(flashing_yellow);
+    Thread menu_thread(menu_loop);  */
     
     LOG("***** IBM IoT Client Ethernet Example *****\n");
     MQTTEthernet ipstack;
@@ -489,7 +547,7 @@
     
     if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)    
     {
-        red();
+        //red();
         while (true)
             wait(1.0); // Permanent failures - don't retry
     }
@@ -518,4 +576,9 @@
             led2 = !led2;
         client.yield(10);  // allow the MQTT client to receive messages
     }
+    
+    
+    
+    
+    
 }