Simple modification to pachube example to allow easy talking to thingspeak database using http commands
Dependencies: EthernetNetIf mbed
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "HTTPClient.h" 00004 #include <string> 00005 00006 00007 EthernetNetIf eth; 00008 HTTPClient http; 00009 00010 // Ethernet LED's 00011 DigitalOut RED(p29); 00012 DigitalOut YEL(p30); 00013 00014 00015 int main() { 00016 00017 printf("Start\n"); 00018 RED = !RED; YEL = !YEL; 00019 00020 printf("\r\nSetting up...\r\n"); 00021 RED = !RED; YEL=!YEL; 00022 00023 EthernetErr ethErr = eth.setup(); 00024 RED = !RED; YEL=!YEL; 00025 00026 if(ethErr) 00027 { 00028 printf("Error %d in setup.\n", ethErr); 00029 return -1; 00030 } 00031 printf("\r\nSetup OK\r\n"); 00032 RED = !RED; YEL=!YEL; 00033 00034 HTTPText txt; 00035 RED = !RED; YEL=!YEL; 00036 00037 int idx = 0; 00038 00039 while(idx < 5) { 00040 00041 printf("\r\n idx = %d \r\n",idx); 00042 00043 // copy API key from settings 00044 char apiKey[17]= "L6MA5ZGH3WPSDALA"; 00045 printf("apiKey = %s \r\n",apiKey); 00046 00047 // use feed ID 00048 char fieldID[8]= "field1"; 00049 printf("fieldID = %s \r\n",fieldID); 00050 00051 // Send data based on the loop idx - this example is 0-4 00052 char data[10]; 00053 sprintf(data,"%d",idx*10); 00054 printf("data = %s \r\n",data); 00055 00056 // uri for post includes feed ID 00057 char uri[256]; 00058 sprintf(uri,"%s%s&%s=%s","http://api.thingspeak.com/update?key=",apiKey,fieldID,data); 00059 printf("uri = %s \r\n",uri); 00060 00061 // result should = - "http://api.thingspeak.com/update?key=L6MA5ZGH3WPSDALA&field1=26" 00062 HTTPResult r = http.get(uri, &txt); 00063 RED = !RED; YEL=!YEL; 00064 00065 if(r==HTTP_OK) 00066 { 00067 printf("Result :\"%s\"\n", txt.gets()); 00068 } 00069 else 00070 { 00071 printf("Error %d\n", r); 00072 } 00073 RED = !RED; YEL=!YEL; 00074 00075 wait(60); //wait 60 seconds 00076 idx = idx + 1; 00077 } 00078 00079 }
Generated on Fri Jul 15 2022 18:15:49 by 1.7.2