unable to send data to Thingspeak cloud API | ThingSpeak API | Forum


Please consider registering

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —

— Match —

— Forum Options —

Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
unable to send data to Thingspeak cloud API
No permission to create posts
January 12, 2018
7:14 pm
New Member
Forum Posts: 1
Member Since:
January 12, 2018
sp_UserOfflineSmall Offline

Hi All ,  i'm writing arduino Code using Arduino Uno and ESP01 Wifi module  to sense some data and send it to Thingspeak API but i always got error and TCP connection close . here is my code and response 

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3

#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
#include <SoftwareSerial.h> // Code to use SoftwareSerial
SoftwareSerial espSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
String apiKey = "JWB4K7P94Y5BI0RR"; // replace with your channel's thingspeak WRITE API key
//String ssid="NTG-Egypt"; // NTG Wifi network SSID
//String password ="P@ssw0rd@Ntg-eg"; // NTG Wifi network password
//String ssid="HanyHotspot"; // Mobile Wifi network SSID
//String password ="fd8dbe30049d"; // Mobile Wifi network password
String ssid="TP-LINK_9299B0"; //Home Wifi network SSID
String password ="hggihglsjuhk1"; // Home Wifi network password
boolean DEBUG=true;
double v_amps;
double v_watt;
double v_tanklevel;

//======================================================================== showResponce
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
if (DEBUG) Serial.print(c);

//================================================================================ setup

void setup() {
DEBUG=true; // enable debug serial

espSerial.begin(9600); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again

// espSerial.println("AT+RST"); // Enable this line to reset the module;
// showResponse(1000);

//espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps

//espSerial.println("AT+CWMODE=1"); // set esp8266 as client

//espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password

if (DEBUG) Serial.println("Setup completed");

emon1.current(2, 60); // Current: input pin, calibration.

boolean thingSpeakWrite(double amps, double watt , double tanklevel){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
// cmd += ""; // api.thingspeak.com
cmd +="api.thingspeak.com";
cmd += "\",80";
if (DEBUG) Serial.println(cmd);
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;

String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;

getStr +="&field1=";
getStr += String(amps);
getStr +="&field2=";
getStr += String(watt);
getStr +="&field3=";
getStr += String(tanklevel);
// ...
getStr += "


// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
if (DEBUG) Serial.println(cmd);

if (DEBUG) Serial.print(getStr);
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
return true;
double calcIrms(){
double Irms = emon1.calcIrms(1480); // Calculate Irms only

// Serial.print(Irms*220.0); // Apparent power
// Serial.print(" ");
Serial.println(Irms); // Irms

return Irms;

double calcPower(double i){
double w = (i*220.0); // Apparent power
return w;

double measureLevel(double dim1 , double dim2 , String shape,double totalvolume){

unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
double distance = (uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
double levelvol = distance *dim1*dim2;
return levelvol;

void loop()
// setup();
v_amps = calcIrms();
v_watt = calcPower(v_amps);
v_tanklevel = measureLevel(1,1,"cube",100);
// // Read sensor values
thingSpeakWrite(v_amps,v_watt,v_tanklevel); // Write values to thingspeak
// // thingspeak needs 15 sec delay between updates,

January 13, 2018
6:30 pm

Forum Posts: 15
Member Since:
January 1, 2018
sp_UserOfflineSmall Offline

You need to enter your WiFi information and UN Comment these lines.....

This will (or should) work IF you are using DHCP on your router (most people are).  I do NOT use DHCP and assign ALL my network devices a static IP address.

// espSerial.println("AT+RST"); // Enable this line to reset the module;
// showResponse(1000);

//espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps

//espSerial.println("AT+CWMODE=1"); // set esp8266 as client

//espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
17 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

josephrj11, dennis55, jillhp4, tabathawg69, MarinaMa, Susandom

Administrators: Hans: 387, lee: 457