Library for writing and reading on ThingSpeak with ethernet
ThingSpeak.cpp@0:a7bce9e88175, 2017-06-05 (annotated)
- Committer:
- mpuric
- Date:
- Mon Jun 05 08:17:19 2017 +0000
- Revision:
- 0:a7bce9e88175
- Child:
- 1:ea7f0ef29ef5
ThingSpeak converted to library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mpuric | 0:a7bce9e88175 | 1 | #include "mbed.h" |
mpuric | 0:a7bce9e88175 | 2 | #include "EthernetNetIf.h" |
mpuric | 0:a7bce9e88175 | 3 | #include "HTTPClient.h" |
mpuric | 0:a7bce9e88175 | 4 | #include "ThingSpeak.h" |
mpuric | 0:a7bce9e88175 | 5 | |
mpuric | 0:a7bce9e88175 | 6 | Serial pc(USBTX, USBRX); |
mpuric | 0:a7bce9e88175 | 7 | |
mpuric | 0:a7bce9e88175 | 8 | ThingSpeak::ThingSpeak(char* Key) : thingSpeakKey(Key) { |
mpuric | 0:a7bce9e88175 | 9 | thingSpeakUrl = "https://api.thingspeak.com/update"; |
mpuric | 0:a7bce9e88175 | 10 | thingSpeakRead = "https://api.thingspeak.com/channels/"; |
mpuric | 0:a7bce9e88175 | 11 | urlBuffer[0] = 0; |
mpuric | 0:a7bce9e88175 | 12 | fieldBuffer[0] = 0; |
mpuric | 0:a7bce9e88175 | 13 | } |
mpuric | 0:a7bce9e88175 | 14 | float ThingSpeak::pull(long int readKey, int field) { |
mpuric | 0:a7bce9e88175 | 15 | sprintf(urlBuffer, "%s%d/fields/%d/last", thingSpeakRead, readKey, field ); |
mpuric | 0:a7bce9e88175 | 16 | pc.printf("URL Buffer request: %s ", urlBuffer); |
mpuric | 0:a7bce9e88175 | 17 | res = http.get(urlBuffer, &resp); |
mpuric | 0:a7bce9e88175 | 18 | if (res == HTTP_OK){ |
mpuric | 0:a7bce9e88175 | 19 | pc.printf(" Result :\"%s\"\r\n,", resp.gets(), res); |
mpuric | 0:a7bce9e88175 | 20 | float resp2 = atoi(resp.gets()); |
mpuric | 0:a7bce9e88175 | 21 | return resp2; |
mpuric | 0:a7bce9e88175 | 22 | } |
mpuric | 0:a7bce9e88175 | 23 | else { |
mpuric | 0:a7bce9e88175 | 24 | pc.printf(" Error %d\r\n", res); |
mpuric | 0:a7bce9e88175 | 25 | return false; |
mpuric | 0:a7bce9e88175 | 26 | } |
mpuric | 0:a7bce9e88175 | 27 | } |
mpuric | 0:a7bce9e88175 | 28 | void ThingSpeak::connect() { |
mpuric | 0:a7bce9e88175 | 29 | pc.printf("Setting up Ethernet...\r\n"); |
mpuric | 0:a7bce9e88175 | 30 | EthernetErr ethErr = eth.setup(); |
mpuric | 0:a7bce9e88175 | 31 | if(ethErr){ |
mpuric | 0:a7bce9e88175 | 32 | pc.printf("Error %d in ethernet setup.\r\n", ethErr); |
mpuric | 0:a7bce9e88175 | 33 | connect(); |
mpuric | 0:a7bce9e88175 | 34 | } |
mpuric | 0:a7bce9e88175 | 35 | pc.printf("Ethernet setup OK\r\n"); |
mpuric | 0:a7bce9e88175 | 36 | } |
mpuric | 0:a7bce9e88175 | 37 | /* |
mpuric | 0:a7bce9e88175 | 38 | void ThingSpeak::getIP() { |
mpuric | 0:a7bce9e88175 | 39 | |
mpuric | 0:a7bce9e88175 | 40 | } |
mpuric | 0:a7bce9e88175 | 41 | */ |
mpuric | 0:a7bce9e88175 | 42 | void ThingSpeak::setField(float field, int i) { |
mpuric | 0:a7bce9e88175 | 43 | char fieldi; |
mpuric | 0:a7bce9e88175 | 44 | char fieldShortBuff[8]; |
mpuric | 0:a7bce9e88175 | 45 | sprintf(fieldShortBuff, "%f", field); |
mpuric | 0:a7bce9e88175 | 46 | sprintf(&fieldi, "%i", i); |
mpuric | 0:a7bce9e88175 | 47 | strncat(fieldBuffer, "&field", 6 ); |
mpuric | 0:a7bce9e88175 | 48 | strncat(fieldBuffer, &fieldi , 1); |
mpuric | 0:a7bce9e88175 | 49 | strncat(fieldBuffer, "=", 1); |
mpuric | 0:a7bce9e88175 | 50 | strncat(fieldBuffer, fieldShortBuff , 6); |
mpuric | 0:a7bce9e88175 | 51 | } |
mpuric | 0:a7bce9e88175 | 52 | |
mpuric | 0:a7bce9e88175 | 53 | void ThingSpeak::putUp() { |
mpuric | 0:a7bce9e88175 | 54 | sprintf(urlBuffer, "%s?key=%s%s", thingSpeakUrl, thingSpeakKey, fieldBuffer); |
mpuric | 0:a7bce9e88175 | 55 | pc.printf("URL Buffer request: %s ", urlBuffer); |
mpuric | 0:a7bce9e88175 | 56 | res = http.get(urlBuffer, &resp); |
mpuric | 0:a7bce9e88175 | 57 | if (res == HTTP_OK) |
mpuric | 0:a7bce9e88175 | 58 | pc.printf(" Result :\"%s\"\r\n", resp.gets()); |
mpuric | 0:a7bce9e88175 | 59 | else |
mpuric | 0:a7bce9e88175 | 60 | pc.printf(" Error %d\r\n", res); |
mpuric | 0:a7bce9e88175 | 61 | fieldBuffer[0] = 0; |
mpuric | 0:a7bce9e88175 | 62 | } |