Skip to main content

You are here

EtherCard Read Json from Website

2 posts / 0 new
Last post
EtherCard Read Json from Website

Hello everybody,

I have a problem regarding receiving a json string properly from a website. At the moment I receive the whole data due to ether.persistTcpConnection(true); but I have difficulties how to build a single json string in the end. For example sometimes I receive the whole Header again or substrings are repeated. Could somebody give me any advice? Thank you so much!

#include "EtherCard.h" #include "Adafruit_Thermal.h" #include "SoftwareSerial.h" #include "bitmaps.h" #include "avr/pgmspace.h" #include "aJSON.h" const int printer_RX_Pin = 5, // Printer connection: green wire printer_TX_Pin = 4; // Printer connection: yellow wire // ethernet interface mac address, must be unique on the LAN static byte mac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; byte Ethernet::buffer[400]; char website[] PROGMEM = ""; byte state = 0; // called when the client request is complete static void browseUrlCallback1 (byte status, word off, word len) { Serial.print("<<<<<<<<<<<<>>>>>>>>>>>>>>"); char *pos;// used for buffer searching Ethernet::buffer[off+len] = 0; pos=(char *)(Ethernet::buffer+off); // set the byte after the end of the buffer to zero to act as an end marker (also handy for displaying the buffer as a string) Serial.println(pos); } void connectWWW() { Serial.println("Initializing Ethernet..."); for( int i=0; i<6; i++ ) { Serial.print( mac[i], HEX ); Serial.print( i < 5 ? ":" : "" ); } Serial.println(); if (ether.begin(sizeof Ethernet::buffer, mac,10) == 0){ Serial.println( "Failed to access Ethernet controller"); return; } Serial.println("Requesting IP Address..."); if (!ether.dhcpSetup()){ Serial.println("DHCP failed"); return; } // Get IP Address details ether.printIp("IP: ", ether.myip); ether.printIp("GW: ", ether.gwip); ether.printIp("DNS: ", ether.dnsip); Serial.println("Requesting HOST DNS..."); if (!ether.dnsLookup(website)){ Serial.println("HOST DNS failed..."); return; } ether.printIp("HOST DNS success: ", ether.hisip); } void setup () { Serial.begin(9600); connectWWW(); } void loop(){ word len = ether.packetReceive(); word pos = ether.packetLoop(len); if (state != 1) { state = 1; ether.persistTcpConnection(true); ether.browseUrl(PSTR("/apis/printer.php?"), "t=news", website, browseUrlCallback1); } }

I also attached the json file and the serialoutput.


json and serial output.txt3 KB

Has nobody any idea why its behaving like this?

Premium Drupal Themes by Adaptivethemes