Library for writing and reading on ThingSpeak with ethernet
Fork of ThingSpeakEthernet by
ThingSpeak.cpp@1:ea7f0ef29ef5, 2017-06-08 (annotated)
- Committer:
- mpuric
- Date:
- Thu Jun 08 18:28:20 2017 +0000
- Revision:
- 1:ea7f0ef29ef5
- Parent:
- 0:a7bce9e88175
Added comments.; ;
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 | 1:ea7f0ef29ef5 | 14 | |
mpuric | 0:a7bce9e88175 | 15 | float ThingSpeak::pull(long int readKey, int field) { |
mpuric | 0:a7bce9e88175 | 16 | sprintf(urlBuffer, "%s%d/fields/%d/last", thingSpeakRead, readKey, field ); |
mpuric | 0:a7bce9e88175 | 17 | pc.printf("URL Buffer request: %s ", urlBuffer); |
mpuric | 0:a7bce9e88175 | 18 | res = http.get(urlBuffer, &resp); |
mpuric | 0:a7bce9e88175 | 19 | if (res == HTTP_OK){ |
mpuric | 0:a7bce9e88175 | 20 | pc.printf(" Result :\"%s\"\r\n,", resp.gets(), res); |
mpuric | 0:a7bce9e88175 | 21 | float resp2 = atoi(resp.gets()); |
mpuric | 0:a7bce9e88175 | 22 | return resp2; |
mpuric | 0:a7bce9e88175 | 23 | } |
mpuric | 0:a7bce9e88175 | 24 | else { |
mpuric | 0:a7bce9e88175 | 25 | pc.printf(" Error %d\r\n", res); |
mpuric | 0:a7bce9e88175 | 26 | return false; |
mpuric | 0:a7bce9e88175 | 27 | } |
mpuric | 0:a7bce9e88175 | 28 | } |
mpuric | 1:ea7f0ef29ef5 | 29 | |
mpuric | 0:a7bce9e88175 | 30 | void ThingSpeak::connect() { |
mpuric | 0:a7bce9e88175 | 31 | pc.printf("Setting up Ethernet...\r\n"); |
mpuric | 0:a7bce9e88175 | 32 | EthernetErr ethErr = eth.setup(); |
mpuric | 0:a7bce9e88175 | 33 | if(ethErr){ |
mpuric | 0:a7bce9e88175 | 34 | pc.printf("Error %d in ethernet setup.\r\n", ethErr); |
mpuric | 0:a7bce9e88175 | 35 | connect(); |
mpuric | 0:a7bce9e88175 | 36 | } |
mpuric | 0:a7bce9e88175 | 37 | pc.printf("Ethernet setup OK\r\n"); |
mpuric | 0:a7bce9e88175 | 38 | } |
mpuric | 1:ea7f0ef29ef5 | 39 | |
mpuric | 0:a7bce9e88175 | 40 | void ThingSpeak::setField(float field, int i) { |
mpuric | 0:a7bce9e88175 | 41 | char fieldi; |
mpuric | 0:a7bce9e88175 | 42 | char fieldShortBuff[8]; |
mpuric | 0:a7bce9e88175 | 43 | sprintf(fieldShortBuff, "%f", field); |
mpuric | 0:a7bce9e88175 | 44 | sprintf(&fieldi, "%i", i); |
mpuric | 0:a7bce9e88175 | 45 | strncat(fieldBuffer, "&field", 6 ); |
mpuric | 0:a7bce9e88175 | 46 | strncat(fieldBuffer, &fieldi , 1); |
mpuric | 0:a7bce9e88175 | 47 | strncat(fieldBuffer, "=", 1); |
mpuric | 0:a7bce9e88175 | 48 | strncat(fieldBuffer, fieldShortBuff , 6); |
mpuric | 0:a7bce9e88175 | 49 | } |
mpuric | 0:a7bce9e88175 | 50 | |
mpuric | 0:a7bce9e88175 | 51 | void ThingSpeak::putUp() { |
mpuric | 0:a7bce9e88175 | 52 | sprintf(urlBuffer, "%s?key=%s%s", thingSpeakUrl, thingSpeakKey, fieldBuffer); |
mpuric | 0:a7bce9e88175 | 53 | pc.printf("URL Buffer request: %s ", urlBuffer); |
mpuric | 0:a7bce9e88175 | 54 | res = http.get(urlBuffer, &resp); |
mpuric | 0:a7bce9e88175 | 55 | if (res == HTTP_OK) |
mpuric | 0:a7bce9e88175 | 56 | pc.printf(" Result :\"%s\"\r\n", resp.gets()); |
mpuric | 0:a7bce9e88175 | 57 | else |
mpuric | 0:a7bce9e88175 | 58 | pc.printf(" Error %d\r\n", res); |
mpuric | 0:a7bce9e88175 | 59 | fieldBuffer[0] = 0; |
mpuric | 0:a7bce9e88175 | 60 | } |