a modified code based on the IoTClientEthernet code

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IoTClientEthernet by Zhengguo Sheng

Revision:
20:c90d68fbbb0e
Parent:
19:31b65908c2f3
--- a/main.cpp	Sun Dec 04 14:50:51 2016 +0000
+++ b/main.cpp	Tue Jan 10 18:26:40 2017 +0000
@@ -63,7 +63,6 @@
 char type[30] = TYPE;
 char id[30] = ID;                 // mac without colons
 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
-
 bool connected = false;
 bool mqttConnecting = false;
 bool netConnected = false;
@@ -72,25 +71,19 @@
 int connack_rc = 0; // MQTT connack return code
 int retryAttempt = 0;
 int menuItem = 0;
-
 char* joystickPos = "CENTRE";
 int blink_interval = 0;
-
 char* ip_addr = "";
 char* gateway_addr = "";
 char* host_addr = "";
 int connectTimeout = 1000;
-
 // If we wanted to manually set the MAC address,
 // this is how to do it. In this example, we take
 // the original Mbed Set MAC address and combine it
-// with a prefix of our choosing.
- 
+// with a prefix of our choosing. 
 extern "C" void $Super$$mbed_mac_address(char *s);
 extern "C" void $Sub$$mbed_mac_address(char *s) 
-{
-
-    
+{   
 // define your own MAC Address
   s[0] = 0x68;  
   s[1] = 0xf7;  
@@ -98,11 +91,10 @@
   s[3] = 0x06;  
   s[4] = 0x02;  
   s[5] = 0x63;           
-  
 }
 
 
-
+//---------------------------functions to flash the LED in different colours----------
 void off()
 {
     r = g = b = 1.0;    // 1 is off, 0 is full brightness
@@ -123,17 +115,17 @@
     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;
-    while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD)    // flashing yellow only while connecting 
-    {
+//----------------------------------------------------------------------------------------
+void flashing_yellow(void const *args) 
+{ bool on = false;
+// flashing yellow only while connecting 
+  while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD)    
+   {
         on = !on; 
         if (on)
             yellow();
         else
-            off();   
+           off();   
         wait(0.5);
     }
 }
@@ -156,8 +148,8 @@
 
 void printMenu(int menuItem) 
 {
-    static char last_line1[30] = "", last_line2[30] = "";
-    char line1[30] = "", line2[30] = "";
+    static char last_line1[60] = "", last_line2[60] = "";
+    char line1[60] = "", line2[60] = "";
         
     switch (menuItem)
     {
@@ -227,6 +219,18 @@
             sprintf(line1, "Current Timeout:");
             sprintf(line2, "%d ms", connectTimeout);
             break;
+        case 10: //prints out the temperature on the LCD, after the joystick is scrolled down 10 times
+            sprintf(line1, "Temperature");
+            sprintf(line2, "%0.4f degrees C", sensor.temp());
+            break;
+       case 11: //prints out the accelerometer output on the LCD, after the joystick is scrolled down 11 times
+            sprintf(line1, "accelX  accelY  accelZ");
+            sprintf(line2, "%0.4f,  %0.4f,  %0.4f", MMA.x(), MMA.y(), MMA.z());
+            break;
+       case 12: //prints out the potentiometer values on the LCD, after the joystick is scrolled down 12 times
+            sprintf(line1, "Pot1     Pot2");
+            sprintf(line2, "%0.4f,  %0.4f", ain1.read(), ain2.read());
+            break;
     }
     
     if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0)
@@ -241,15 +245,13 @@
         strncpy(last_line2, line2, sizeof(last_line2));
     }
 }
-
-
 void setMenu()
 {
     
     if (Down)
     {
         joystickPos = "DOWN";
-        if (menuItem >= 0 && menuItem < 9)
+        if (menuItem >= 0 && menuItem < 12)
             printMenu(++menuItem);
     } 
     else if (Left)
@@ -259,7 +261,7 @@
     else if (Up)
     {
         joystickPos = "UP";
-        if (menuItem <= 9 && menuItem > 0)
+        if (menuItem <= 12 && menuItem > 0)
             printMenu(--menuItem);
     }
     else if (Right)
@@ -279,11 +281,7 @@
         Thread::wait(100);
     }
 }
-
-
-/**
- * Display a message on the LCD screen prefixed with IBM IoT Cloud
- */
+ /* Display a message on the LCD screen prefixed with IBM IoT Cloud*/
 void displayMessage(char* message)
 {
     lcd.cls();
@@ -292,8 +290,6 @@
     lcd.locate(0,16);
     lcd.printf(message);
 }
-
-
 int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
 {   
     const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com";
@@ -404,8 +400,11 @@
     char* pubTopic = "iot-2/evt/status/fmt/json";
             
     char buf[250];
+    //sends data from accelerator, temperature sensor,
+    // joystick and potentiometers to the IBM quickstart page
     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}}",
+     "{\"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());
     message.qos = MQTT::QOS0;
     message.retained = false;
@@ -462,15 +461,29 @@
         WARN("Unsupported command: %.*s\n", len, start);
 }
 
-
+PwmOut spkr(D6); //defines D6 as a PWM pin
+DigitalOut redled(LED_RED); //defines LED_RED as digital output pin
+DigitalOut greenled(LED_GREEN); //defines LED_GREEN as digital output pin
 int main()
-{    
+{    //if temperature is not within the set threshld, the the speaker gives 
+  //alarm, which is set by PWM, also red LED of FRDM board turns on 
+    if (sensor.temp()>23 || sensor.temp()<20) //sets temperature threshold
+     {spkr.period(0.010); // set PWM period to 10 ms
+      spkr=0.5; // set duty cycle to 50%
+      greenled=!greenled; //turns green LED off and red LED on
+     }
+     // if temperature is within the threshold set, the the speaker will be turned
+     //off and will not give an alarm, also the green LED of the FRDM board will turn on
+     if (sensor.temp()<23 && sensor.temp()>20)
+     {
+      spkr=0.0; // set duty cycle to 50%
+      redled=!redled; //turns red LED off and green LED on
+     }
     quickstartMode = (strcmp(org, "quickstart") == 0);
 
     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);  
@@ -517,4 +530,5 @@
             led2 = !led2;
         client.yield(10);  // allow the MQTT client to receive messages
     }
-}
+    } 
+