Library for writing and reading on ThingSpeak with ethernet
Diff: ThingSpeak.cpp
- Revision:
- 0:a7bce9e88175
- Child:
- 1:ea7f0ef29ef5
diff -r 000000000000 -r a7bce9e88175 ThingSpeak.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ThingSpeak.cpp Mon Jun 05 08:17:19 2017 +0000 @@ -0,0 +1,62 @@ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "HTTPClient.h" +#include "ThingSpeak.h" + +Serial pc(USBTX, USBRX); + +ThingSpeak::ThingSpeak(char* Key) : thingSpeakKey(Key) { + thingSpeakUrl = "https://api.thingspeak.com/update"; + thingSpeakRead = "https://api.thingspeak.com/channels/"; + urlBuffer[0] = 0; + fieldBuffer[0] = 0; +} + float ThingSpeak::pull(long int readKey, int field) { + sprintf(urlBuffer, "%s%d/fields/%d/last", thingSpeakRead, readKey, field ); + pc.printf("URL Buffer request: %s ", urlBuffer); + res = http.get(urlBuffer, &resp); + if (res == HTTP_OK){ + pc.printf(" Result :\"%s\"\r\n,", resp.gets(), res); + float resp2 = atoi(resp.gets()); + return resp2; + } + else { + pc.printf(" Error %d\r\n", res); + return false; + } +} +void ThingSpeak::connect() { + pc.printf("Setting up Ethernet...\r\n"); + EthernetErr ethErr = eth.setup(); + if(ethErr){ + pc.printf("Error %d in ethernet setup.\r\n", ethErr); + connect(); + } + pc.printf("Ethernet setup OK\r\n"); +} +/* +void ThingSpeak::getIP() { + +} +*/ +void ThingSpeak::setField(float field, int i) { + char fieldi; + char fieldShortBuff[8]; + sprintf(fieldShortBuff, "%f", field); + sprintf(&fieldi, "%i", i); + strncat(fieldBuffer, "&field", 6 ); + strncat(fieldBuffer, &fieldi , 1); + strncat(fieldBuffer, "=", 1); + strncat(fieldBuffer, fieldShortBuff , 6); +} + +void ThingSpeak::putUp() { + sprintf(urlBuffer, "%s?key=%s%s", thingSpeakUrl, thingSpeakKey, fieldBuffer); + pc.printf("URL Buffer request: %s ", urlBuffer); + res = http.get(urlBuffer, &resp); + if (res == HTTP_OK) + pc.printf(" Result :\"%s\"\r\n", resp.gets()); + else + pc.printf(" Error %d\r\n", res); + fieldBuffer[0] = 0; +} \ No newline at end of file