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

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





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

Register Lost password?
sp_Feed sp_TopicIcon
unable to send data to Thingspeak cloud API
Avatar
hanytalaat
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
January 12, 2018 - 7:14 pm
sp_Permalink sp_Print

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()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}

}
//================================================================================ setup

void setup() {
DEBUG=true; // enable debug serial
Serial.begin(9600);

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
//showResponse(1000);

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

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

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 += "184.106.153.149"; // api.thingspeak.com
cmd +="api.thingspeak.com";
cmd += "\",80";
espSerial.println(cmd);
showResponse(1000);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
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());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);

delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// 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
Serial.println(w);
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;
Serial.println(distance);
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,
delay(16000);
}

Avatar
Piez

Silver
Forum Posts: 15
sp_UserOfflineSmall Offline
2
January 13, 2018 - 6:30 pm
sp_Permalink sp_Print sp_EditHistory

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
//showResponse(1000);

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

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

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: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

epicpaymentsny

DanielBobby

mercedeszw18

ujuxoketate

olihfez

winnieeq69

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee