Cannot connect to ThingSpeak | 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
Cannot connect to ThingSpeak
No permission to create posts
May 8, 2017
2:40 pm
New Member
Forum Posts: 2
Member Since:
April 27, 2017
sp_UserOfflineSmall Offline

I'm unable to connect and post data to my ThingSpeak channel.  Data being sent from ESP8266-01S module, using WiFi to access internet.  I can send data to Google Sheets, but not to ThingSpeak.  Something wrong in my connect code.  API is the WRITE API.  Code follows:



Measures the flow rate of water flowing through a 3/4-inch PVC pipe using a DIGITEN Hall effect sensor and displaying results on using an ESP8266-01S
Sensor date wire connected to Uno pin D2
The sensor output is in the form of the duration in milliseconds of a square wave pulse and the relationship to the water flowrate is:

F = 4.8 * Q ( in liters per minute)

ThingSpeak ( ) is an analytic IoT platform service that allows you to aggregate, visualize and
analyze live data streams in the cloud.

Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.

#include <ESP8266WiFi.h>

String apiKey = "0ZUS9CDG08ZU6VJ2";

#include <ThingSpeak.h>

// WiFi SSD and Password

// char ssid[] = "tracy"
// char password[] = "Mange161"
// char server[] = ""

const char* ssid = "tracy";
const char* password = "Mange161";

const char* server = "";

//Initialize the client library
WiFiClient client;

#include <SPI.h>

void setup() {

pinMode(2, INPUT);



// init done


Serial.print("Connecting to ");

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connected");



void loop() {

const unsigned long duration = pulseIn(2, HIGH);
//Pulse duration in microseconds

float dursec = duration / 1000000.0;
//Pulse duration in seconds

float pulsefreq = 10.0;

if ( duration != 0 )
pulsefreq = 1.0 / dursec;
pulsefreq = 0.0;

//Pulse frequency in Hz

float flolit = (pulsefreq / 4.8);
float flolitr = 60.0 * flolit;
//Flow rate in Liters/hour

float flogal = flolitr * .26417;
//Flow rate in Gallons/hour
float flocuft = flolitr * .035315;
//Flow rate in cu ft/hour

if (client.connect(server, 80)) { // "" or
String postStr = apiKey;
postStr += "&field1=";
postStr += String(flogal);
postStr += "


client.print("POST /update HTTP/1.1
client.print("Connection: close
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "
client.print("Content-Type: application/x-www-form-urlencoded
client.print("Content-Length: ");



// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.

//ThingSpeak.writeField( 255517, 1, flogal, "0ZUS9CDG08ZU6VJ2")


// ThingSpeak will only accept updates every 15 seconds.




May 9, 2017
2:23 pm

Forum Posts: 228
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

You might consider trying your HTTP request in POSTMAN first, just to get the syntax correct.  I think the issue is in this section:

if (client.connect(server, 80)) { // "" or
String postStr = apiKey;
postStr += "&field1=";
postStr += String(flogal);

your "postStr" should start with a string that says "api_key=" and then append the api_key. You want postStr="api_key=XXXXXXXXXXXXXXXX&field1="

For some hints, you might look at Continuously Collect Data and Bulk-Update a ThingSpeak Channel Using an Arduino MKR1000 Board or an ESP8266 Board, but keep in mind this syntax is for a bulk update which slightly different.

You also have the line "client.print("X-THINGSPEAKAPIKEY: " + apiKey + " 

You can safely keep this line, but you do not need to have the api key in both the header section and the body.  

You could also look at the "WriteVoltage" example that comes with the ThingSpeak Communication Library for Arduino.  It looks like you are combining methods from the thingspeak library with separate wifi client methods.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
22 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:

BarryMaync, ThomasRougs, DonaldGen, prince, JrGordon, Elamsweems

Administrators: Hans: 387, lee: 457