1
How do I interpret information from a Webservice who was called by the Arduino?
Webservice returns a JSON and I need to interpret it. An example of the answer can be seen below:
[{"valor":"10"}]
The code below, obtained from that website, shows the call and the interpretation, but is read one byte at a time and, therefore, I have doubts of how could do this for the return in JSON.
The part that reads one byte at a time can be seen below:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
Complete code:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
https://www.arduino.cc/en/Tutorial/WebClient
– William Monteiro
Related: http://answall.com/questions/101273/como-fazer-uma-requisi%C3%A7%C3%A3o-http-get-para-um-web-service-com-o-Arduino
– cantoni
I don’t quite understand what Arduino has to do with the webservice, if in fact who connects in the webservice is
c
and it seems that it has nothing to do with the "Serial" part. I could explain, maybe it helps to understand your doubt.– Guilherme Nascimento
@Guilhermenascimento, in the context of this question, who connects in Webservice is the Arduino. In the above code, the instructions that use the Serial are serving as a kind of logger to know what is happening in the program. Anyway, it connects to Webservice using Ethernet and uses Arduino’s Serial output to track that connection.
– cantoni
I believe that this question was closed erroneously. I do not know if before or after the author’s edition. The question is not so different from this one: http://answall.com/questions/101273/como-fazer-uma-requisi%C3%A7%C3%A3o-http-get-to-a-web-service-com-o-Arduino
– cantoni
@Cantoni understand that closing is almost never mistaken, if the question was closed is because the author has not made it clear, at the time the author improve the question can be reopened, note that this has been discussed several times, I recommend you to read this discussion of the goal: http://meta.pt.stackoverflow.com/a/2676/3635 The author did not make it clear in several ways, if you understood the doubt you can edit the question and improve it, so it automatically enters the voting process to reopen, will be 5 votes needed to reopen - I hope you understand ;)
– Guilherme Nascimento
@Cantoni actually is this question is duplicate the other, only there it seems clearer. If I had known the other would have voted as duplicate
– Guilherme Nascimento
@Guilhermenascimento, did not know that after an issue the question automatically enters the line to reopen. Good to know. Thanks for the clarification.
– cantoni
It is not the same thing. Note that this question here wishes to interpret the JSON response of a Webservice call. The other question quoted by me just wishes to make the call.
– cantoni
Okay, but here’s the question very confused and as I said if you know how to improve, then improve, this is the main foundation of Stackoverflow http://meta.pt.stackoverflow.com/q/2212/3635 - But edit without defacing and only if you’re sure @Cantoni.
– Guilherme Nascimento
@Guilhermenascimento, edited question. Thank you.
– cantoni
@Cantoni For nothing, just recommend that always encourage the author to edit by himself first, because sometimes (in other future questions) the author may want something else. ;)
– Guilherme Nascimento