Connections to Xively working; has 5 channels on Xively (axl_x, axl_y, axl_z, heater_status, temperature)

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 NTPClient libxively mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
wren301
Date:
Sun May 18 03:50:03 2014 +0000
Parent:
2:cf992d90396e
Child:
4:05986b9ea330
Commit message:
HW5 complete; running code and JSON to websocket server

Changed in this revision

WebSocketClient.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/WebSocketClient.lib	Sun May 18 03:50:03 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
--- a/main.cpp	Sun May 18 03:23:37 2014 +0000
+++ b/main.cpp	Sun May 18 03:50:03 2014 +0000
@@ -4,6 +4,7 @@
 #include "C12832_lcd.h"
 #include "LM75B.h"
 #include "MMA7660.h"
+#include "Websocket.h"
 
 #define PST_OFFSET  7*60*60
 
@@ -20,12 +21,12 @@
 AnalogIn pot2(p19);
 #define LOOP_DELAY_MS 100
 //update time every 10 minutes
-#define UPDATE_TIME 60*1 
+#define UPDATE_TIME 60*1
+MMA7660 acc(p28, p27);// accelerometer
+int updateTimeFromServer = 1;
 
-MMA7660 MMA(p28, p27);
-int updateTimeFromServer = 1;
-    
-void connectToTheInternet() {
+void connectToTheInternet()
+{
     eth.init(); //Init and use DHCP
     wait(2);
     lcd.cls();
@@ -42,7 +43,8 @@
     lcd.cls();
 }
 
-void updateTimeRoutine() {
+void updateTimeRoutine()
+{
     if (ntp.setTime("0.pool.ntp.org") == 0) {
         printf("Time updated!");
     } else {
@@ -53,20 +55,37 @@
     updateTimeFromServer = 0;
 }
 
-void updateTime() {
+void updateTime()
+{
     updateTimeFromServer = 1;
 }
 
+void sendTempJSON()
+{
+    char json_str[100];
+    // See the output on http://sockets.mbed.org/app-board/viewer
+    Websocket ws("ws://sockets.mbed.org:443/ws/app-board/wo");
+    ws.connect();
+    // create json string with acc/tmp data
+    sprintf(json_str, "{\"id\":\"app_board_eth_EW2013\",\"ax\":%d,\"ay\":%d,\"az\":%d, \"tmp\":%d}", (int)(acc.x()*360), (int)(acc.y()*360), (int)(acc.z()*360), (int)tmp.read());
+    // send str
+    ws.send(json_str);
+    ws.close();
+}
+
 //POT values are 0:1, will allow high and low temperatures
 //lets you set temp between 50 and 100 degrees
-void updateTempsFromPots() {
+void updateTempsFromPots()
+{
     float lowTempPot = pot2*50.0+50;
     float highTempPot = pot1*50.0+50;
     //round to the nearest whole number
     lowTempPot = floor(lowTempPot+.5);
     highTempPot = floor(highTempPot+.5);
     //high temp must be at least 1 degree above low temp
-    if (highTempPot <= lowTempPot) {highTempPot = lowTempPot +1;}
+    if (highTempPot <= lowTempPot) {
+        highTempPot = lowTempPot +1;
+    }
     //Refresh the display if the temps have changed by more than a degree
     if ((lowTemp != lowTempPot) || (highTemp != highTempPot)) {
         lowTemp = lowTempPot;
@@ -78,59 +97,61 @@
 }
 
 int main()
-{   
-    connectToTheInternet();    
+{
+    connectToTheInternet();
     //Variable to hold the current minute so we only update the display when the minute changes
     char currentMinute[2];
     currentMinute[1] = 'a';
     char minute[2];
- 
+
     float currentTemp = -200;
-    lcd.printf("Updating time...\r\n");   
-    printf("Updating time...\r\n");   
+    lcd.printf("Updating time...\r\n");
+    printf("Updating time...\r\n");
     if (ntp.setTime("0.pool.ntp.org") == 0) {
-        printf("Set time successfully\r\n");       
+        printf("Set time successfully\r\n");
         lcd.cls();
         timer.attach(&updateTime, UPDATE_TIME);
         lcd.printf("\n\r\n\rHEATER OFF");
-        
-        while(1) {              
+
+        while(1) {
             if(updateTimeFromServer) {
-                updateTimeRoutine();    
-            }      
+                updateTimeRoutine();
+            }
             //Sets temp from POTs
-            updateTempsFromPots();            
-            //Fetch the time 
+            updateTempsFromPots();
+            //Fetch the time
             time_t ctTime;
             ctTime = time(NULL)- PST_OFFSET;
-            char timeBuffer[32];  
-                     
+            char timeBuffer[32];
+
             //See if the minute has changed; set an update display flag if it has
-            strftime(minute, 8, "%M", localtime(&ctTime));           
-            if ( (minute[1] != currentMinute[1]) ) {         
+            strftime(minute, 8, "%M", localtime(&ctTime));
+            if ( (minute[1] != currentMinute[1]) ) {
                 //Formats the time for display
-                strftime(timeBuffer, 32, "%a %b %d %I:%M%p\n\r", localtime(&ctTime));           
+                strftime(timeBuffer, 32, "%a %b %d %I:%M%p\n\r", localtime(&ctTime));
                 printf("%s\r", timeBuffer);
                 lcd.locate(0,0);
                 lcd.printf("%s\r", timeBuffer);
                 currentMinute[1] = minute[1];
-            }          
-            
+            }
+
             //Update the temperature display if the temperature, set temp has changed
-            temp = tmp.read()*9.0/5.0 + 32.0;  
-            //checks if the temperature (rounded to the nearest whole number) has changed 
+            temp = tmp.read()*9.0/5.0 + 32.0;
+
+            //checks if the temperature (rounded to the nearest whole number) has changed
             if (floor(temp+.5) != floor(currentTemp+.5)) {
                 printf("Temp change: %.1f F\n\r", temp);
                 currentTemp = temp;
                 lcd.locate(0,0);
                 lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp);
-            }   
-              
-            lcd.locate(0,0);             
+                sendTempJSON();
+            }
+
+            lcd.locate(0,0);
             //Heater logic: turns off if it has gone over the high temp, on if under the low temp
             if (heaterOn && (temp > highTemp) ) {
                 printf("Heater turned OFF\n\r");
-                heaterOn = 0; 
+                heaterOn = 0;
                 lcd.locate(0,0);
                 lcd.printf("\n\r\n\rHEATER OFF");
             } else if (!heaterOn && (temp < lowTemp) ) {
@@ -138,8 +159,8 @@
                 heaterOn = 1;
                 lcd.locate(0,0);
                 lcd.printf("\n\r\n\rHEATER ON");
-            }      
-            wait(LOOP_DELAY_MS*.001);                                
+            }
+            wait(LOOP_DELAY_MS*.001);
         }
     } else {
         lcd.printf("NTP Error\r\n");