Unable to post multiple values to ThingSpeak? | ThingSpeak API | Forum

Avatar

Please consider registering
Guest

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 post multiple values to ThingSpeak?
No permission to create posts
June 24, 2018
6:21 am
Avatar
mnaveed1

Gold
Members
Forum Posts: 25
Member Since:
June 15, 2018
sp_UserOfflineSmall Offline

I am able to store and access data in internal memory of RTC ESP8266 thing dev board. Now I am trying to loop the data stored in internal memory and post data to ThingSpeak. However, I am only able to post the first value stored in RTC memory to ThingSpeak field 1,2 and 3. I am unable to post rest of the values to ThingSpeak. The code which access and post data to ThingSpeak is below. Can anyone help me figure out the problem.

void connects()
{

Serial.println("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

if (client.connect(server,80)) // "184.106.153.149" or
api.thingspeak.com
{

for(int i=1;i<6;i++){

system_rtc_mem_read(100+i, rtcCel1, sizeof(rtcCel1));
system_rtc_mem_read(110+i, rtcFah1, sizeof(rtcFah1));
system_rtc_mem_read(120+i, rtcHum1, sizeof(rtcHum1));

float cel=*rtcCel1;

float fah=*rtcFah1;

float hum=*rtcHum1;

String postStr = apiKey;
postStr +="&field1=";
postStr += String(cel);
postStr +="&field2=";
postStr += String(fah);
postStr +="&field3=";
postStr += String(hum);
postStr += "

";

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

Serial.print("Temperature: ");
Serial.print(*rtcCel1);
Serial.print(" *C ");
Serial.print(*rtcFah1);
Serial.print(" *F\t");
Serial.print("Humidity: ");
Serial.print(*rtcHum1);

Serial.println("%. Send to Thingspeak.");

// thingspeak needs minimum 15 sec delay between updates, i've
set it to 30 seconds

delay(15000);

}
}
client.stop();

// Serial.println("Waiting...");

// thingspeak needs minimum 15 sec delay between updates, i've
set it to 30 seconds
delay(30000);

}

June 25, 2018
9:52 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 332
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

There are at least two ways to achieve your goal.
When your device wakes, you can loop through all the stored values, making sure you delay between write commands so as not to exceed the posting rate for your account type. (15 seconds if you are a free user)
Use the parameter 'created_at" to specify the time the data was created. You ThingSpeak plots will show the data at this time. See the doc for the correct syntax.
https://www.mathworks.com/help/thingspeak/writedata.html

Alternatively, you can use the Bulk update feature, which is meant for exactly the situation you describe. The syntax is a bit more complicated, but there is example code here:
https://www.mathworks.com/help/thingspeak/continuously-collect-data-and-bulk-update-a-thingspeak-channel-using-an-arduino-mkr1000-board-or-an-esp8266-board.html

And here is the doc page for bulk update:
https://www.mathworks.com/help/thingspeak/bulkwritejsondata.html
In this example, you build the posting string first and supply the relative time for each post, and then post all the data simultaneously (you can also supply an exact timestamp.) Be careful with the size of the posting string you use however. Some devices have very small buffer sizes, and wont warn you when they run out.

July 3, 2018
9:40 am
Avatar
mnaveed1

Gold
Members
Forum Posts: 25
Member Since:
June 15, 2018
sp_UserOfflineSmall Offline

So I have modified my code to test bulk update. My code uploads successfully however, it doesn't post anything to ThingSpeak. Can you tell me what is wrong ?
I have included my write api key and channel id if you can also check it from your hardware. Thank You!!

#include // Including library for dht
#include //Uncomment this library to work with ESP8266
#include
char jsonBuffer[500] = "[";

String apiKey = "6OF022OLH2QM50TP"; // Enter your Write API key from ThingSpeak

const char *ssid = "Xperia Z_a769"; // replace with your wifi ssid and wpa2 key
const char *pass = "7c6761b56ed2";
const char* server = "api.thingspeak.com";

#define DHTPIN 14 //pin where the dht22 is connected

//There are different kind of sensors like DHT11,DHT12
// For this project I am using DHT22 temperature and humidity sensor. Also known as AM2302

DHT dht(DHTPIN, DHT22);

WiFiClient client;

void connects()
{
Serial.begin(9600);
delay(10);
dht.begin();

Serial.println("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

}

void readings()
{

// Reading temperature or humidity takes about 250 milliseconds!!
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

if (isnan(h) || isnan(t) || isnan(f))
{
Serial.println("Failed to read from DHT sensor!");
return;
}

/* "[{\"delta_t\":0,\"field1\":-70},{\"delta_t\":15,\"field1\":-66}]" */

// Format the jsonBuffer as noted above
strcat(jsonBuffer,"{\"delta_t\":4");
strcat(jsonBuffer,",");
strcat(jsonBuffer, "\"field1\":1.0");

strcat(jsonBuffer,"},]");

char data[500] = "{\"write_api_key\":\"6OF022OLH2QM50TP\",\"updates\":";
strcat(data,jsonBuffer);
strcat(data,"}");

String data_length = String(strlen(data)+1);

if (client.connect(server, 80)) {
client.println("POST /channels/522490/bulk_update.json HTTP/1.1"); //Replace YOUR-CHANNEL-ID with your ThingSpeak channel ID
client.println("Host: api.thingspeak.com");
client.println("User-Agent: mw.doc.bulk-update (Arduino ESP8266)");
client.println("Connection: close");
client.println("Content-Type: application/json");
client.println("Content-Length: "+data_length);
client.println();
client.println(data);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("data: ");
Serial.print(data);
Serial.println(". Send to Thingspeak.");

//}
client.stop();

Serial.println("Waiting...");

// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(15000);
}

}
////////////////////////

void setup() {
Serial.begin(9600);
Serial.setTimeout(2000);

// Wait for serial to initialize.
while(!Serial) { }

Serial.println("I'm awake.");
connects();
readings();

Serial.println("Going into deep sleep for 20 seconds");
ESP.deepSleep(20e6,WAKE_RFCAL); // 20e6 is 20 microseconds
}

void loop() {

}

July 3, 2018
4:17 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 332
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

What do you see on the serial monitor when you run this code? Does it connect successfully? Is there a response from the server?

July 4, 2018
3:57 am
Avatar
mnaveed1

Gold
Members
Forum Posts: 25
Member Since:
June 15, 2018
sp_UserOfflineSmall Offline

Yes, it does connect successfully. So I tried to print the response code and I get response code of zero. Also, in my previous post I forgot to include the libraries. But in my original code I do have the following libraries:

#include // Including library for dht
#include //Uncomment this library to work with ESP8266
#include

But still something is wrong. Following is my output:

WiFi connected
Temperature: 29.70 *C 85.46 *F Humidity: 54.20data: {"write_api_key":"6L4GICVTAF3GMAMG","updates":[{"delta_t":4,"field1":1.0},]}. Send to Thingspeak.
Waiting...
Response code:0
Going into deep sleep for 20 seconds

July 6, 2018
3:01 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 332
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I think the forum strips away library references. Probably you should take the #include off.

I am presently guessing the trailing comma is a problem.
I would try the post on POSTMAN.

This worked for me:

{
"write_api_key":"XXXXXXXXXXXXXXXX",
"updates":
[
{
"delta_t":4,
"field1":1.0
}
]
}

July 7, 2018
10:35 am
Avatar
mnaveed1

Gold
Members
Forum Posts: 25
Member Since:
June 15, 2018
sp_UserOfflineSmall Offline

It gives me this error when I use this :

{
"write_api_key":"XXXXXXXXXXXXXXXX",
"updates":
[
{
"delta_t":4,
"field1":1.0
}
]
}

thingspeak_deep_sleep_with_thing_dev_6:71: error: expected ';' before ']' token

exit status 1
expected ';' before ':' token

July 9, 2018
11:21 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 332
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Can you try the format you used to generate your post above, but just remove the comma?
The one where you have
"Temperature: 29.70 *C 85.46 *F Humidity: 54.20data: {"write_api_key":"6L4GICVTAF3GMAMG","updates":[{"delta_t":4,"field1":1.0},]}. Send to Thingspeak."
Try is this way.
Temperature: 29.70 *C 85.46 *F Humidity: 54.20data: {"write_api_key":"6L4GICVTAF3GMAMG","updates":[{"delta_t":4,"field1":1.0}]}. Send to Thingspeak.

July 10, 2018
4:01 am
Avatar
mnaveed1

Gold
Members
Forum Posts: 25
Member Since:
June 15, 2018
sp_UserOfflineSmall Offline

I tried removing the comma as suggested but still got the same result.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: James181
28 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:

James181, kimberlypg2, JamesAdorb, waynesmith, paulettebw60, ashleytb4

Administrators: Hans: 387, lee: 457