Simple modification to pachube example to allow easy talking to thingspeak database using http commands

Dependencies:   EthernetNetIf mbed

Committer:
ohararp
Date:
Fri Feb 04 19:52:42 2011 +0000
Revision:
0:31f1fc2cf08b
thing_speak_a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ohararp 0:31f1fc2cf08b 1 #include "mbed.h"
ohararp 0:31f1fc2cf08b 2 #include "EthernetNetIf.h"
ohararp 0:31f1fc2cf08b 3 #include "HTTPClient.h"
ohararp 0:31f1fc2cf08b 4 #include <string>
ohararp 0:31f1fc2cf08b 5
ohararp 0:31f1fc2cf08b 6
ohararp 0:31f1fc2cf08b 7 EthernetNetIf eth;
ohararp 0:31f1fc2cf08b 8 HTTPClient http;
ohararp 0:31f1fc2cf08b 9
ohararp 0:31f1fc2cf08b 10 // Ethernet LED's
ohararp 0:31f1fc2cf08b 11 DigitalOut RED(p29);
ohararp 0:31f1fc2cf08b 12 DigitalOut YEL(p30);
ohararp 0:31f1fc2cf08b 13
ohararp 0:31f1fc2cf08b 14
ohararp 0:31f1fc2cf08b 15 int main() {
ohararp 0:31f1fc2cf08b 16
ohararp 0:31f1fc2cf08b 17 printf("Start\n");
ohararp 0:31f1fc2cf08b 18 RED = !RED; YEL = !YEL;
ohararp 0:31f1fc2cf08b 19
ohararp 0:31f1fc2cf08b 20 printf("\r\nSetting up...\r\n");
ohararp 0:31f1fc2cf08b 21 RED = !RED; YEL=!YEL;
ohararp 0:31f1fc2cf08b 22
ohararp 0:31f1fc2cf08b 23 EthernetErr ethErr = eth.setup();
ohararp 0:31f1fc2cf08b 24 RED = !RED; YEL=!YEL;
ohararp 0:31f1fc2cf08b 25
ohararp 0:31f1fc2cf08b 26 if(ethErr)
ohararp 0:31f1fc2cf08b 27 {
ohararp 0:31f1fc2cf08b 28 printf("Error %d in setup.\n", ethErr);
ohararp 0:31f1fc2cf08b 29 return -1;
ohararp 0:31f1fc2cf08b 30 }
ohararp 0:31f1fc2cf08b 31 printf("\r\nSetup OK\r\n");
ohararp 0:31f1fc2cf08b 32 RED = !RED; YEL=!YEL;
ohararp 0:31f1fc2cf08b 33
ohararp 0:31f1fc2cf08b 34 HTTPText txt;
ohararp 0:31f1fc2cf08b 35 RED = !RED; YEL=!YEL;
ohararp 0:31f1fc2cf08b 36
ohararp 0:31f1fc2cf08b 37 int idx = 0;
ohararp 0:31f1fc2cf08b 38
ohararp 0:31f1fc2cf08b 39 while(idx < 5) {
ohararp 0:31f1fc2cf08b 40
ohararp 0:31f1fc2cf08b 41 printf("\r\n idx = %d \r\n",idx);
ohararp 0:31f1fc2cf08b 42
ohararp 0:31f1fc2cf08b 43 // copy API key from settings
ohararp 0:31f1fc2cf08b 44 char apiKey[17]= "L6MA5ZGH3WPSDALA";
ohararp 0:31f1fc2cf08b 45 printf("apiKey = %s \r\n",apiKey);
ohararp 0:31f1fc2cf08b 46
ohararp 0:31f1fc2cf08b 47 // use feed ID
ohararp 0:31f1fc2cf08b 48 char fieldID[8]= "field1";
ohararp 0:31f1fc2cf08b 49 printf("fieldID = %s \r\n",fieldID);
ohararp 0:31f1fc2cf08b 50
ohararp 0:31f1fc2cf08b 51 // Send data based on the loop idx - this example is 0-4
ohararp 0:31f1fc2cf08b 52 char data[10];
ohararp 0:31f1fc2cf08b 53 sprintf(data,"%d",idx*10);
ohararp 0:31f1fc2cf08b 54 printf("data = %s \r\n",data);
ohararp 0:31f1fc2cf08b 55
ohararp 0:31f1fc2cf08b 56 // uri for post includes feed ID
ohararp 0:31f1fc2cf08b 57 char uri[256];
ohararp 0:31f1fc2cf08b 58 sprintf(uri,"%s%s&%s=%s","http://api.thingspeak.com/update?key=",apiKey,fieldID,data);
ohararp 0:31f1fc2cf08b 59 printf("uri = %s \r\n",uri);
ohararp 0:31f1fc2cf08b 60
ohararp 0:31f1fc2cf08b 61 // result should = - "http://api.thingspeak.com/update?key=L6MA5ZGH3WPSDALA&field1=26"
ohararp 0:31f1fc2cf08b 62 HTTPResult r = http.get(uri, &txt);
ohararp 0:31f1fc2cf08b 63 RED = !RED; YEL=!YEL;
ohararp 0:31f1fc2cf08b 64
ohararp 0:31f1fc2cf08b 65 if(r==HTTP_OK)
ohararp 0:31f1fc2cf08b 66 {
ohararp 0:31f1fc2cf08b 67 printf("Result :\"%s\"\n", txt.gets());
ohararp 0:31f1fc2cf08b 68 }
ohararp 0:31f1fc2cf08b 69 else
ohararp 0:31f1fc2cf08b 70 {
ohararp 0:31f1fc2cf08b 71 printf("Error %d\n", r);
ohararp 0:31f1fc2cf08b 72 }
ohararp 0:31f1fc2cf08b 73 RED = !RED; YEL=!YEL;
ohararp 0:31f1fc2cf08b 74
ohararp 0:31f1fc2cf08b 75 wait(60); //wait 60 seconds
ohararp 0:31f1fc2cf08b 76 idx = idx + 1;
ohararp 0:31f1fc2cf08b 77 }
ohararp 0:31f1fc2cf08b 78
ohararp 0:31f1fc2cf08b 79 }