Data to Thingspeak Over Ethernet Port

Dependencies:   mbed HTTPClient mbed-rtos NTPClient EthernetInterface

Committer:
dewantkatare
Date:
Sat Apr 13 19:46:56 2019 +0000
Revision:
5:b8a76bfc45c9
Parent:
4:21c1357bb880
Data to Thingspeak over ethernet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rbagwe 0:0666873f31ca 1 #include "mbed.h"
rbagwe 0:0666873f31ca 2 #include "EthernetInterface.h"
dewantkatare 4:21c1357bb880 3 #include "HTTPClient.h"
rbagwe 0:0666873f31ca 4 #include <stdio.h>
rbagwe 0:0666873f31ca 5
rbagwe 0:0666873f31ca 6
dewantkatare 4:21c1357bb880 7 char buffer[256];
dewantkatare 4:21c1357bb880 8 char resp[1024];
dewantkatare 4:21c1357bb880 9 char val[4][16];
dewantkatare 4:21c1357bb880 10 float f_out = 23.0;
dewantkatare 4:21c1357bb880 11 int ret = 0;
rbagwe 0:0666873f31ca 12
rbagwe 0:0666873f31ca 13 //Ethernet Interface
rbagwe 0:0666873f31ca 14 EthernetInterface eth;
rbagwe 0:0666873f31ca 15
rbagwe 0:0666873f31ca 16 //HTTP Client for interfacing to web services
rbagwe 0:0666873f31ca 17 HTTPClient http;
rbagwe 0:0666873f31ca 18
rbagwe 0:0666873f31ca 19 //serial over USB for debug and Info
rbagwe 0:0666873f31ca 20 Serial pc(USBTX, USBRX);
rbagwe 0:0666873f31ca 21
rbagwe 0:0666873f31ca 22 //http client init
rbagwe 0:0666873f31ca 23 HTTPMap map;
rbagwe 0:0666873f31ca 24 HTTPText inText(resp, 1024);
rbagwe 0:0666873f31ca 25
rbagwe 0:0666873f31ca 26
rbagwe 0:0666873f31ca 27 int main(){
rbagwe 0:0666873f31ca 28
rbagwe 0:0666873f31ca 29 //ethernet init
rbagwe 0:0666873f31ca 30 eth.init(); //Use DHCP
rbagwe 0:0666873f31ca 31 ret= eth.connect();
rbagwe 0:0666873f31ca 32
rbagwe 0:0666873f31ca 33 if (!ret){
dewantkatare 4:21c1357bb880 34 pc.printf("\r\nConnected, IP: %s, MASK: %s, GW: %s",
rbagwe 0:0666873f31ca 35 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
rbagwe 0:0666873f31ca 36 } else {
dewantkatare 4:21c1357bb880 37 pc.printf("\r\nError eth.connect() - ret = %d", ret);
rbagwe 0:0666873f31ca 38 }
dewantkatare 4:21c1357bb880 39
rbagwe 0:0666873f31ca 40 while(1){
dewantkatare 4:21c1357bb880 41 pc.printf("\r\nWriting to thingspeak");
dewantkatare 4:21c1357bb880 42 map.put("api_key","EEMB3URN9LDSPRIM"); /* The thingspeak API KEY*/
dewantkatare 4:21c1357bb880 43 sprintf(val[0],"%4.1f",f_out);
dewantkatare 4:21c1357bb880 44 map.put("field1", val[0]);
dewantkatare 4:21c1357bb880 45 map.put("field2", val[0]);
dewantkatare 4:21c1357bb880 46 pc.printf("\r\nPosting Data...");
dewantkatare 4:21c1357bb880 47 ret = http.post("https://api.thingspeak.com/update", map, &inText); //writing data to Thingspeak
dewantkatare 4:21c1357bb880 48 if (!ret){
dewantkatare 4:21c1357bb880 49 pc.printf("\r\nPOST successfully - read %d characters", strlen(resp));
dewantkatare 4:21c1357bb880 50 pc.printf("\r\nResult: %s\n", resp);
dewantkatare 4:21c1357bb880 51 }else{
dewantkatare 4:21c1357bb880 52 pc.printf("\r\nError Connecting to ethernet port - ret = %d - HTTP return code = %d", ret, http.getHTTPResponseCode());
rbagwe 0:0666873f31ca 53 }
rbagwe 0:0666873f31ca 54 }
rbagwe 0:0666873f31ca 55 }