xniinja:
So, I'm assuming when you run this you print out a lot more information than just the JSON. Correct?
Could you run this code for me and post everything that is being printed out to the serial console? I'm afraid there might be some parsing that must be done before we get to parsing the JSON.
Thanks
I've been doing a bit more work to the code and now have the following:
#include <ArduinoJson.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gw[] = {192,168,1,254};
byte server[] = { 51, 255, 42, 54 }; // Google
byte subnet[] = { 255, 255, 255, 0 };
// 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;
char json[] = "{\"date\":\"04/12/2016\",\"request_time\":1480852521}";
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
jsoncheck();
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, gw, gw, subnet);
}
// 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 /time.php");
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();
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
void jsoncheck(){
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
const char* date = root["date"];
long request_time = root["request_time"];
Serial.print("Date:");
Serial.println(date);
Serial.print("Request_Time:");
Serial.print(request_time);
}
This actually seems to be working now. However it still won't decode the JSON from my webserver. I believe it is because the formatting is different. Up the top of this code I have declared a variable "char json" which has the format recommended by the library but the code I am being sent is in a different format. Is there any way of easily converting it from the format sent by the php to something the library can recognise?
Thanks
The serial monitor displays:
Date:04/12/2016
Request_Time:1480852521