Escolar Documentos
Profissional Documentos
Cultura Documentos
logged and receive the entry-id back, but nothing gets written.
The code is
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Connection Timeout...Stopping");
client.stop();
}
}
Serial.println("Receiving Thingspeak response");
while(client.available()) {
body = client.readStringUntil('
');
Serial.print(".");
}
Serial.print(body);
}
client.stop(); // Whether connection was successful or not
}
Any idea what can be rong ? Nothing is shown in the dashboard, and the json export
is a proof that the packet was sent and received, but it says nothing else than
"field1":null
Avatar
cstapels
Moderator
Forum Posts: 197
sp_UserOfflineSmall Offline
2
March 13, 2018 - 9:12 am
sp_Permalink
sp_Print
The format of your HTTP POST command seems to be fine, you might want to remove the
(nothans) and change it to (tochicnet), but that wont change how anything works.
Here are some troubleshooting steps:
Can you write to the channel using a browser GET command? Here is the format, make
sure you use the channel write API key.
https://api.thingspeak.com/update.json?api_key=<your_write_api_key>&field1=123
Can you output the variable you call field1 to the serial monitor just before you
append it to the body to make sure something is there? I see there is already a
print command for the body variable. What do you see in the serial monitor after
Serial.println(body); ?
Avatar
tochinet
Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
3
March 13, 2018 - 2:19 pm
sp_Permalink
sp_Print
Can you write to the channel using a browser GET command? Here is the format, make
sure you use the channel write API key.
https://api.thingspeak.com/update.json?api_key=<your_write_api_key>&field1=123
Yes, it works.
Can you output the variable you call field1 to the serial monitor just before you
append it to the body to make sure something is there? I see there is already a
print command for the body variable. What do you see in the serial monitor after
Serial.println(body); ?
You seem to be doing everything correct. If your channel is public, can you share
the channel number? Otherwise can you show what the last update looks like? You
can use this in your browser:
https://api.thingspeak.com/channels/<your_channelID>/feeds/last.json
Avatar
tochinet
Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
5
March 16, 2018 - 9:52 am
sp_Permalink
sp_Print
{"created_at":"2018-03-
13T19:17:04+01:00","entry_id":262,"field1":"123","field2":null,"field3":null}
I see that the entry_id or nulls have no quotes but the field1 value has.
Avatar
cstapels
Moderator
Forum Posts: 197
sp_UserOfflineSmall Offline
6
March 16, 2018 - 11:01 am
sp_Permalink
sp_Print
ThingSpeak can understand decimal values. If you want you can convince yourself
with this command in your browser
https://api.thingspeak.com/update.json?api_key=<your_write_api_key>&field1=12.3
The network strength example in the documentation does not send a line feed at the
end of the post. I see some comments in your code that might indicate you already
tried this.
Also,the string for body may not be printing to the client as expected.
Instead of client.print(body);
client.print("field1=1.23");
And then if that works, you can try it in two parts:
client.print("field1=");
client.print(string(field1));
Avatar
tochinet
Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
7
March 18, 2018 - 12:06 pm
sp_Permalink
sp_Print
Thanks for the proposal, but it won't work, length is only a method of string. It
says undefined.
I tried many other variations in no particular order, and when I focused on the
content type
client.println("Content-Type: application/x-www-form-urlencoded");
Connected to Thingspeak
field1=123.23&field2=6.0Receiving Thingspeak response
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
ETag: "e78f0xxxxxxxxxxxxxxxxxx0203"
Cache-Control: max-age=0, private, must-revalidate
Set-Cookie: request_method=POST; path=/
X-Request-Id: c4cf.....................4d
X-Runtime: 0.067653
X-Powered-By: Phusion Passenger 4.0.57
Date: Sun, 18 Mar 2018 16:04:46 GMT
Server: nginx/1.9.3 + Phusion Passenger 4.0.57
102
{"channel_id":160089,"field1":"123.23","field2":"6.0","field3":null,"field4":null,"
field5":null,"field6":null,"field7":null,"field8":null,"created_at":"2018-03-
18T16:04:46+00:00","entry_id":357,"status":null,"latitude":null,"longitude":null,"e
levation":null}
0
Any reason why there is a number before and after the JSON in the body ?
Avatar
cstapels
Moderator
Forum Posts: 197
sp_UserOfflineSmall Offline
8
March 18, 2018 - 2:13 pm
sp_Permalink
sp_Print
client.print("field1=1.23");
I'm glad you got it working. Can you show the line(s) where you 'put the values in
the URL'?
I will look into the numbers you are getting before the JSON response, I do not see
those with an update from POSTMAN. I can try it with an ESP32 later. These are the
other headers I see:
access-control-allow-headers ?origin, content-type, X-Requested-With
access-control-allow-methods ?GET, POST, PUT, OPTIONS, DELETE, PATCH
access-control-allow-origin ?*
access-control-max-age ?1800
cache-control ?max-age=0, private, must-revalidate
connection ?close
content-type ?application/json; charset=utf-8
date ?Sun, 18 Mar 2018 17:57:51 GMT
etag ?"25ddfff65cd7e90df008cb339d7a0d11"
server ?nginx/1.9.3 + Phusion Passenger 4.0.57
status ?200 OK
transfer-encoding ?chunked
x-frame-options ?ALLOWALL
x-powered-by ?Phusion Passenger 4.0.57
x-request-id ?bbefaa13-c868-4a87-9870-813d94d89ae7
x-runtime ?0.037343
{"channel_id":nnnnnn,"field1":"2","field2":null,"field3":null,"field4":null,"field5
":null,"field6":null,"field7":null,"field8":null,"created_at":"2018-03-
18T17:57:51+00:00","entry_id":8,"status":null,"latitude":null,"longitude":null,"ele
vation":null}
Avatar
tochinet
Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
9
March 19, 2018 - 8:05 am
sp_Permalink
sp_Print
Thanks for your great and fast support. The code as I changed it to post was now as
below.
I'm still considering moving back the "body" part to the request body, it's
cleaner.
I may be mistaken, but at least for the first number, it couldn't be due to some
other Serial.print etc because it's part of the same loop. However, it could be
some bogus content-length. For the second ("0"), there could always be another
unspotted Serial.print somewhere.
");
Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
10
March 19, 2018 - 8:09 am
sp_Permalink
sp_Print
Error in the post above, the <backslash>-r and <backslash>-n were not automatically
escaped. Trying to repeat here
Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
11
March 19, 2018 - 8:14 am
sp_Permalink
sp_Print
Can't apparently edit the postings, I obviously meant I'm wondering if the
"<backslach>-r <backslash>-n" vs "<backslash>-n" vs "println" (instead of print)
would have an impact.
Avatar
cstapels
Moderator
Forum Posts: 197
sp_UserOfflineSmall Offline
12
March 19, 2018 - 4:35 pm
sp_Permalink
sp_Print
The code below works to post values. I think the difference is that you need to
convert the output of body.length() to a string. You can also include the API key
in the body, and that saves one extra header and some lines of code.
I have also observed the extra characters before and after the output for a device
(esp8266), we are looking into that, and the ability to edit posts here.
Avatar
cstapels
Moderator
Forum Posts: 197
sp_UserOfflineSmall Offline
13
March 19, 2018 - 5:20 pm
sp_Permalink
sp_Print
The numbers before and after the response are the size of the remaining chunks in
hex. The response includes the header:
In your case, there were 258 bytes in the response, or 102 in hex. After the data,
there were 0 bytes remaining. This may be a device or library dependent issue,
POSTMAN does not generally show the bytes in the response. I did see it on an
ESP8266 with <ESP8266WiFi.h> library.