Uploading sensor data (voltage divider, MAX4172, INA219) over Ethernet to Thing Speak service. Uses old mbed revision that is compatible with NetServices library. I2C communication is made with I2CR library.

Dependencies:   C12832 I2CR INA219 NetServices mbed

Fork of NetServices_HelloWorld by Segundo Equipo

ThingSpeak.cpp

Committer:
tbjazic
Date:
2015-11-29
Revision:
8:9b35ac104ab7
Parent:
7:1da0a084cd69

File content as of revision 8:9b35ac104ab7:

#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";
    urlBuffer[0] = 0;
    fieldBuffer[0] = 0;
}

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 , 4);    
}

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;         
}