final

Dependencies:   NetServices-Traffic mbed spxml

Fork of LiveTrafficDisplayFinal by Rahol Rajan

Revision:
1:c1df4cf13f16
Parent:
0:88e082c58797
--- a/main.cpp	Wed Mar 06 19:15:22 2013 +0000
+++ b/main.cpp	Sun Feb 22 12:02:41 2015 +0000
@@ -1,79 +1,59 @@
 #include "mbed.h"
 #include "EthernetNetIf.h"
+#include "NTPClient.h"
 #include "HTTPClient.h"
-#include "NokiaLCD.h"
-//Mapquest traffic RSS Feed - get web page with XML
-// displays Road diversions/closures on LCD  from XML "<fullDesc>.... text...</fullDesc>"
-NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
+Serial pc(USBTX, USBRX);
+// networking stuff
 EthernetNetIf eth;
+NTPClient ntp;
 HTTPClient http;
-HTTPResult result;
-bool completed = false;
-
-void request_callback(HTTPResult r) {
-    result = r;
-    completed = true;
-}
-
+ 
+char* thingSpeakUrl = "https://api.thingspeak.com/apps/thingtweet/1/statuses/update";
+char* thingSpeakKey = "Z0EQH6OQ8HLNNL6G";
+ 
+char urlBuffer[256];
+char timeBuffer[64];
+ 
+// pin defs
+AnalogIn temp(p20);
+ 
 int main() {
-    char *tstartXML = "<fullDesc>"; //RSS XML start title
-    char *tendXML = "</fullDesc>";  //RSS XML end title
-    char *tsptr;                    //Start Pointer
-    char *teptr;                    //End Pointer
-    int i=0,j=0;
-    lcd.cls();                      //Clear LCD Screen
-    lcd.locate(0,1);
-    lcd.printf("net setup");
-    EthernetErr ethErr = eth.setup();
-    if (ethErr) {
-        lcd.printf("net error");
+ 
+    pc.printf("Start\r\n");
+ 
+    pc.printf("Setting up Ethernet...\r\n");
+   EthernetErr ethErr = eth.setup();
+    if(ethErr)
+    {
+        pc.printf("Error %d in ethernet setup.\r\n", ethErr);
         return -1;
     }
-    lcd.locate(0,2);
-    lcd.printf("net ok");
-    wait(1);
-    lcd.cls();                      //Clear LCD Screen
-    HTTPStream stream;
-    char BigBuf[2048 + 1] = {0};
-    stream.readNext((byte*)BigBuf, 2048); //Point to buffer for the first read
-    //Get web page with XML
-    HTTPResult r = http.get("http://www.mapquestapi.com/traffic/v1/incidents?key=Fmjtd%7Cluub2l6z2u%2C75%3Do5-96twuy&callback=handleIncidentsResponse&boundingBox=33.977698,-84.662833,33.520957,-84.113516&filters=construction,incidents&inFormat=kvp&outFormat=xml", &stream, request_callback);
-    while (!completed) {
-        Net::poll();                // Polls the Networking stack
-        if (stream.readable()) {    // check for end of file
-            BigBuf[stream.readLen()] = 0; // Transform this buffer in a zero-terminated char* string
-            tsptr = BigBuf;
-            // displays titles on LCD  from XML "<title>....title text...</title>"
-            do {
-                tsptr = strstr(tsptr,tstartXML); // find <fullDesc> in string - NULL if not
-                teptr = strstr(tsptr,tendXML);   // find <\fullDesc> in string - NULL if not
-                if (tsptr!=NULL) tsptr = tsptr + strlen(tstartXML);// move to char after "<fullDesc>"
-                if ((tsptr!=NULL)&&(teptr!=NULL)) {
-                    i=0;
-                    // loop to display lines on LCD
-                    for (j=0; (j)<(strlen(tsptr)-strlen(teptr)); j=j+16) {
-  //                      lcd.cls(); // clear screen before writing a new line
-                        lcd.locate(0,(2+(j/16)));
-                        // loop to output a line on the LCD
-                        for (i=0; ((i<16)&&(tsptr[i+j] != '<')); i++) {
-                            lcd.putc(tsptr[i+j]);
-                        }
-                    }
-                    wait(5);
-                    lcd.cls(); //clear LCD between traffic updates
-                    lcd.locate(0,2);
-                }
-            } while (tsptr!=NULL); // No more "<fullDesc>"s in BigBuf to display
-            stream.readNext((byte*)BigBuf, 2048); //Buffer read, now we can put more data in it
+    pc.printf("Ethernet setup OK\r\n");
+   
+    while(1)
+    {
+        float temperature = temp.read();
+        temperature=temperature*150;
+       
+        // for debug
+        pc.printf("Time: %s, Temperature: %f\r\n", timeBuffer, temperature);
+       
+        // format url here
+        urlBuffer[0] = 0;
+        sprintf(urlBuffer, "%s?api_key=%s&status= %s %f", thingSpeakUrl, thingSpeakKey, "Temperature",temperature);
+        pc.printf("Request to %s\r\n", urlBuffer);
+       
+        HTTPText resp;
+        HTTPResult res = http.get(urlBuffer, &resp);
+        if (res == HTTP_OK)
+        {
+            pc.printf("Result :\"%s\"\r\n", resp.gets());
         }
-    }
-    lcd.cls();
-    if (result == HTTP_OK) {
-        lcd.cls();
-        lcd.locate(0,1);
-        lcd.printf(" Read complete");
-    } else {
-        lcd.printf(" Error %d\", result");
-        return -1;
-    }
-}
+        else
+        {
+            pc.printf("Error %d\r\n", res);
+        }
+       
+        wait(16); // limited by ThingSpeak's API
+   }
+ }  
\ No newline at end of file