Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ThingSpeak.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "HTTPClient.h" 00004 #include "ThingSpeak.h" 00005 00006 Serial pc(USBTX, USBRX); 00007 00008 ThingSpeak::ThingSpeak(char* Key) : thingSpeakKey(Key) { 00009 thingSpeakUrl = "https://api.thingspeak.com/update"; 00010 thingSpeakRead = "https://api.thingspeak.com/channels/"; 00011 urlBuffer[0] = 0; 00012 fieldBuffer[0] = 0; 00013 } 00014 00015 float ThingSpeak::pull(long int readKey, int field) { 00016 sprintf(urlBuffer, "%s%d/fields/%d/last", thingSpeakRead, readKey, field ); 00017 pc.printf("URL Buffer request: %s ", urlBuffer); 00018 res = http.get(urlBuffer, &resp); 00019 if (res == HTTP_OK){ 00020 pc.printf(" Result :\"%s\"\r\n,", resp.gets(), res); 00021 float resp2 = atoi(resp.gets()); 00022 return resp2; 00023 } 00024 else { 00025 pc.printf(" Error %d\r\n", res); 00026 return false; 00027 } 00028 } 00029 00030 void ThingSpeak::connect() { 00031 pc.printf("Setting up Ethernet...\r\n"); 00032 EthernetErr ethErr = eth.setup(); 00033 if(ethErr){ 00034 pc.printf("Error %d in ethernet setup.\r\n", ethErr); 00035 connect(); 00036 } 00037 pc.printf("Ethernet setup OK\r\n"); 00038 } 00039 00040 void ThingSpeak::setField(float field, int i) { 00041 char fieldi; 00042 char fieldShortBuff[8]; 00043 sprintf(fieldShortBuff, "%f", field); 00044 sprintf(&fieldi, "%i", i); 00045 strncat(fieldBuffer, "&field", 6 ); 00046 strncat(fieldBuffer, &fieldi , 1); 00047 strncat(fieldBuffer, "=", 1); 00048 strncat(fieldBuffer, fieldShortBuff , 6); 00049 } 00050 00051 void ThingSpeak::putUp() { 00052 sprintf(urlBuffer, "%s?key=%s%s", thingSpeakUrl, thingSpeakKey, fieldBuffer); 00053 pc.printf("URL Buffer request: %s ", urlBuffer); 00054 res = http.get(urlBuffer, &resp); 00055 if (res == HTTP_OK) 00056 pc.printf(" Result :\"%s\"\r\n", resp.gets()); 00057 else 00058 pc.printf(" Error %d\r\n", res); 00059 fieldBuffer[0] = 0; 00060 }
Generated on Sat Jul 16 2022 20:38:28 by
1.7.2