Unable to write data by bulk update *csv method [ESP8266, Arduino framework]
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I have a problem to write a bulk-update using csv format. I have some experience with thingspeak using JSON update method, but I need to change it because *csv format would be more RAM-saving method in case of bulk update (less characters = less memory needed).
I was trying to change my code written for JSON method to csv according to instructions: https://www.mathworks.com/help/thingspeak/bulkwritecsvdata.html but my example-string csv_feed is not accepted by Thingspeak server. I don't have ideas where is the problem, I tried to comment "//" some non-necessery lines, because in API's example, those lines doesn't appears in code.
Thingspeak response code is always the same: 401, meaning "authorization required". But I wrote in code "write_api_key" properly I think (I'm sure that API key and channel number is the same as it is in channel settings on Thingspeak page). The key is authentic, I don't have important data on this channel and I'll change the key after getting rid problems with that code. Without success, I've been changing csv_feed content with diffrent time format (I'd like to use EPOCH time), diffrent number of commas representing field values and I'm still stuck with that code. Could anybody show my mistake? Thank you in advance, Jakub
char csv_feed[] = "write_api_key%3DxxxxxxxxxxxxxxxxC%26time_format%3Dabsolute%26updates%3D1662883022%2C20%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C%2C";
String csv_lenght = String(strlen(csv_feed)+1);
if(client.connect(server, 80)){
client.println("POST /channels/647137/bulk_update.csv HTTP/1.1");
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/x-www-form-urlencoded");
client.println("time_format: absolute");
//client.println("write_api_key: xxxxxxxxxxxxxxxx");
client.println("Content-Length: "+csv_lenght);
client.println();
client.println(csv_feed);
0 Kommentare
Antworten (2)
Christopher Stapels
am 12 Sep. 2022
The arduino client should take care of url encoding your data. I would use the raw text in the value of char csv_feed
Perhaps try to use the example from the doc directly: (with your API_Key of course)
char csv_feed="write_api_key=XXXXXXXXXXXXXXXX&time_format=absolute&updates=2018-06-14T12:12:22-0500,1,,3,0.4,1.5,1.6,,1.8,40.0,5.4,0,wet|2018-01-30T10:26:23-0500,1.2,2.3,3,4,5,6,7,8,42.0,0.5,50,falling"
You should leave these two lines in
//client.println("User-Agent: mw.doc.bulk-update (Arduino ESP8266)");
//client.println("Connection: close");
but leave this one out, since you have already incuded the api key in the csv_feed string.
//client.println("write_api_key: xxxxxxxxxxxxxxxx");
For troubleshooting, I would reccomend using POSTMAN to get the syntax correct.
I edited out your api key before I saw your explanation. Its still best not to share these.
2 Kommentare
Christopher Stapels
am 13 Sep. 2022
Its definitely not the port.
I might have some old code for an ESP8266, I do remember having to be careful with the url encoding, i forget the details. You might have to encode the commas but not the equals signs.
Christopher Stapels
am 13 Sep. 2022
I havent tested this recently, but I found it in an old folder and I expect that it works. I had to escape some values. Please let me know if it works for you.
//Prepare the data
for (int loop_value=0;loop_value<DATA_POINTS;loop_value++){
DataBuffer[loop_value]=random(300);
}
PostData = "{\"write_api_key\":\"";
PostData+=String(WriteAPIKey);
PostData+="time_format=relative";
PostData+="\",\"updates\":[";
for (int index=0;index<DATA_POINTS;index++)
{
PostData+="{\"delta_t\":\"" + String(index*DATA_POINTS+value);
if (index<(DATA_POINTS-1)) //add comma except last time
{
PostData+=",";
}
}
PostData+="]}";
Serial.println("POST "+ String(link_address) +" HTTP/1.1");
Serial.println(PostData);
if (client.connected()){
Serial.println("Connected");
//build HTTP request
client.println("POST "+ String(link_address) +" HTTP/1.1");
client.println("Host: " + String(host));
client.println("Connection: close");
client.println( Content-Type: application/x-www-form-urlencoded);
client.println("Content-Length: " + String(PostData.length()));
client.println();
client.println(PostData);
}
else{
Serial.println("Failed to post");
}
Communitys
Weitere Antworten in ThingSpeak Community
Siehe auch
Kategorien
Mehr zu Read Data from Channel finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!