ENC28J60 GET data for reading in json

Hello,
I have problem with my ethernet modul ENC28J60 (Ali … only 5V). Over modul UIPEthernet.h is failing to display data from the Internet. I always get only 0 or 1.

If I use the EtherCard.h library, therr.browseUrl will print the data without any problem.

Unfortunately, I do not want to list the data, but use the <ArduinoJson.h> JSON library, which I want to keep working with.

How can I get the JSON data from ther.browseUrl to, for example, use JsonObject & root = jsonBuffer.parseObject (client);

Thank you very much for the help. I have been playing with it for two days and no result brings it :frowning:

(deleted)

Try one of the tutorials:

https://randomnerdtutorials.com/decoding-and-encoding-json-with-arduino-or-esp8266/

https://gist.github.com/ericoporto/fb9c975a290a822edca9

#include <UIPEthernet.h>

byte mac = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
IPAddress ip(192,168,0,110);
EthernetClient client;

char server = “napajedlaci.cz”; // IP Adres (or name) of server to dump data to
int interval = 5000; // Wait between dumps

void setup() {

Serial.begin(115200);
Ethernet.begin(mac, ip);

Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
}

void loop() {

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("-> Připojeno k Napajedlaci.cz");
// Make a HTTP request:
client.println(“Host: www.napajedlaci.cz”);
client.println(“GET /webkamera/linux/arduino_wifi.php HTTP/1.1”);
client.println(“Connection: close”);
client.println();
Serial.println(client);

client.stop();

}
else {
// you didn’t get a connection to the server:
Serial.println(“Nepřipojeno k internetu”);
}

delay(interval);
}

So you want to intepret json data which looks like this and you are using the json library ?

{"time":"13:17","teplota_radovany":"5.8 C","teplota_nabrezi":"5.4 C","teplota_sehradice":"4.2 C","teplota_brezolupy":"5.8 C","teplota_radovany_min":"4.1 C","teplota_nabrezi_min":"3.6 C","teplota_sehradice_min":"2.6 C","teplota_brezolupy_min":"3.9 C","teplota_radovany_max":"5.8 C","teplota_nabrezi_max":"5.7 C","teplota_sehradice_max":"4.2 C","teplota_brezolupy_max":"5.8 C","vitr_radovany":"0.0 m\/s","vitr_nabrezi":"1.0 m\/s","vitr_sehradice":"0.9 m\/s","vitr_brezolupy":"0.0 m\/s","vitr_radovany_max":"3.3 m\/s","vitr_nabrezi_max":"12.2 m\/s","vitr_sehradice_max":"5.0 m\/s","vitr_brezolupy_max":"2.9 m\/s","srazky_radovany":"0.00 mm\/h","srazky_nabrezi":"","srazky_sehradice":"0.00 mm\/h","srazky_brezolupy":"0.00 mm\/h","srazky_radovany_max":"0.00 mm","srazky_nabrezi_max":"","srazky_sehradice_max":"3.50 mm","srazky_brezolupy_max":"0.00 mm","srazky_radovany_mesic":"34.50 mm","srazky_nabrezi_mesic":"","srazky_sehradice_mesic":"81.40 mm","srazky_brezolupy_mesic":"35.90 mm","kamera_radovany_teplota":"15.3","kamera_balkon_teplota":"12.2","kamera_sehradice_teplota":"4.2 C","kamera_radovany_stav":"OK","kamera_balkon_stav":"OK","kamera_strecha_stav":"OK","kamera_sehradice_stav":"OK"}

That is quite big (approx 1.2k Bytes). The code you have shown appears to have no attempt to use the json library.

Processing JSON is not here because it would not work. The data to be downloaded from the URL does not download anything. Ask Serial.println (client); I always get only the number 0 or 1. It should list JSON data itself.

cevepe: Processing JSON is not here because it would not work. The data to be downloaded from the URL does not download anything. Ask Serial.println (client); I always get only the number 0 or 1. It should list JSON data itself.

client is an object handle so trying to print it out won't help you.

Doesn't the example you are following show the use of client.read() to extract data from the server ?

Post a link to the example or tutorial you have found.

This is my completed code with JSON. But json data is [Faild JSON!]

#include <ArduinoJson.h>
#include <UIPEthernet.h> // bez lomítka na začátku UIPE

byte mac = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
IPAddress ip(192,168,0,110);
EthernetClient client;

char server = “napajedlaci.cz”; // IP Adres (or name) of server to dump data to
int interval = 5000; // Wait between dumps

void setup() {

Serial.begin(115200);
Ethernet.begin(mac, ip);

Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
}

void loop() {

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("-> Připojeno k Napajedlaci.cz");
// Make a HTTP request:
client.println(“Host: www.napajedlaci.cz”);
client.println(“GET /webkamera/linux/arduino_wifi.php HTTP/1.1”);
client.println(“Connection: close”);
client.println();
Serial.println(client);

const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);

JsonObject& root = jsonBuffer.parseObject(client);
if (!root.success()) {
Serial.println(F(“Faild JSON!”));
}

Serial.println(root[“teplota_radovany”].as<char*>());

client.stop();

}
else {
// you didn’t get a connection to the server:
Serial.println(“Nepřipojeno k internetu”);
}

delay(interval);
}

At a guess, the json data being returned is too big for this:

const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;

As I said before, it is quite large.

Your options are to 1. print out the value of capacity to see what size it calculates. 2. do this: const size_t capacity = 1300 ; instead and see what happens (but depending on what Arduino you are using, it may be too big) 3. For testing, find a site with a json API which delivers a smaller data set. 4. find a streaming version/option of the json library which does not need to store the whole data in RAM.

I reduced the JSON data and now have only 30B over new URL http://www.napajedlaci.cz/webkamera/linux/arduino_lan.php

{"time":"14:41","t_r":"5.5"}

But the problem still persists. :confused: capacity is 114.

Faild JSON!

I tried to list the customer yet, but unfortunately nothing was written.

String line = client.readStringUntil('\r'); Serial.println("data:"); Serial.print(line);

OK. If you can't even receive the JSON as text, you have another problem unrelated to the use of the JSON library. Show the complete code that you used to (including the lines below) which you attempted to use to print out the text of the JSON file:

String line = client.readStringUntil('\r');
Serial.println("data:");
Serial.print(line);

I have Arduino Nano clon with connection:
CS - D10
SI - D11
SO - D12
SCK - D13
RST - RST
VSC - 5V
GND - GND

This is my all code:

#include <ArduinoJson.h>
#include <UIPEthernet.h> // bez lomítka na začátku UIPE

byte mac = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
IPAddress ip(192,168,0,110);
EthernetClient client;

char server = “napajedlaci.cz”; // IP Adres (or name) of server to dump data to
int interval = 5000; // Wait between dumps

void setup() {

Serial.begin(115200);
Ethernet.begin(mac, ip);
}

void loop() {

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("-> Pripojeno");
// Make a HTTP request:
client.println(F(“GET /webkamera/linux/arduino_lan.php HTTP/1.0”));
client.println(F(“Host: www.napajedlaci.cz”));
client.println(F(“Connection: close”));

String line = client.readStringUntil(’\r’);
Serial.println(“data:”);
Serial.print(line);

const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);

JsonObject& root = jsonBuffer.parseObject(client);
if (!root.success()) {
Serial.println(F(“Faild JSON!”));
Serial.println(capacity);
}

Serial.println(root[“t_r”].as<char*>());

client.stop();

}
else {
// you didn’t get a connection to the server:
Serial.println(“Error - nepripojeno”);
}

delay(interval);
}

OK. The first thing to do is get a minimal sketch working which is capable of showing just the JSON text without attempting to interpret it with the JSON library.

I suggest the following until something works:

  1. Add client.println() ; after this statement: client.println(F("Connection: close"));

  2. Remove all the JSON library code

  3. Find a simpler basic web client sketch which works and adapt it to your environment e.g. https://codebender.cc/example/Ethernet/WebClient#WebClient.ino (but may be there is a better one)

Once you get something working, then add back the JSON code and try again.

<Ethernet.h> I tried, but I did not break it.

Failed to configure Ethernet using DHCP
connecting…
connection failed

disconnecting.

I guess you have checked that the IP address is valid in your network: IPAddress ip(192,168,0,110); The failure to use DHCP is not critical if the IP address is OK but you should still try to find out why DHCP fails.

Maybe you have to keep some of the code from your own sketch such as the ethernet library include, the IP address, mac address, the server name, the URL etc. etc.

Clearly there is something basically wrong if you are not able to even get the text of that JSON file. The format of the HTTP GET is critical as is the order activities in building up a connection to the web server. Compare what you are doing with some examples.

You can google for "Arduino web client ethernet" to find example sketches which you can use as a basis.

You can try accessing other web sites as well with your sketch, but ensure these use http:// and not https://

Once you get so far, then look at interpreting the JSON data.

Problem is modul ENC28J60. What I read, it only works with the library <EtherCard.h>

Everything I’ve tried and <EtherCard.h> really works as the only one.

[webClient]
IP: 192.168.0.103
GW: 192.168.0.1
DNS: 8.8.8.8
SRV: 78.24.9.95

<<< REQ HTTP/1.1 200 OK
Date: Sat, 29 Dec 2018 15:42:08 GMT
Server: Apache
Set-Cookie: PHPSESSID=2c320a7bd71e16ecd1aaeaf2dc2d2994; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: domacnost=1; expires=Sun, 29-Dec-2019 15:42:08 GMT; path=/; domain=.napajedlaci.cz
Content-Length: 28
Connection: close
Content-Type: application/json

{“time”:“16:42”,“t_r”:“5.2”}

#include <EtherCard.h>

static byte mymac = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
static uint32_t timer;

const char website PROGMEM = “www.napajedlaci.cz”;

//called when the client request is complete
static void my_callback (byte status, word off, word len) {
//Serial.println(">>>");
Ethernet::buffer[off+len] = 0;
char* fullResponse;
fullResponse = (char*) Ethernet::buffer + off;
Serial.println(fullResponse);
//Serial.println("…");
}

void setup () {
Serial.begin(57600);
Serial.println(F("\n[webClient]"));

if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F(“Failed to access Ethernet controller”));
if (!ether.dhcpSetup())
Serial.println(F(“DHCP failed”));

ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);

if (!ether.dnsLookup(website))
Serial.println(“DNS failed”);

ether.printIp("SRV: ", ether.hisip);
}

void loop () {
ether.packetLoop(ether.packetReceive());

if (millis() > timer) {
timer = millis() + 5000;
Serial.println();
Serial.print("<<< REQ “);
ether.browseUrl(PSTR(”/webkamera/linux/"), “arduino_lan.php”, website, my_callback);
ether.persistTcpConnection(true);
}
}

How do I get only data without a header?

That looks OK. In the call back routine you can use strstr() to get a pointer to the start of your json string (or maybe the phrase which precedes it (application/json) or maybe a marker which you insert before the json string since you appear to have control over the format of the json.