the following has a function byte getPage(int thisPort, const char* serverName, const char *page) which executes a HTTP GET and searches the response for a JSON string using strstr()
/* HTTP GET method from http://playground.arduino.cc/Code/WebClient */
// test with http://httpbin.org/ip which responds with the originator IP address
// modifications to original
// 7/10/2017 update IP address and web page information
// 8/10/2017 replaced sprintf() with strcat() calls to build client data
#define SPRINTF // comment out to use strcat()
#include <SPI.h>
#include <Ethernet.h>
// this must be unique
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// change to your network settings
IPAddress ip(192,168,1,177);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
// change to your server
IPAddress server(192,185,16,85);
// change as required
char serverName[] = "httpbin.org"; // domain name of server
char pageToGet[]= "/ip"; // page to get
int serverPort = 80; // server port
EthernetClient client;
// set this to the number of milliseconds delay between GET requests
#define delayMillis 30000UL
unsigned long thisMillis = 0;
unsigned long lastMillis = delayMillis;
void setup() {
Serial.begin(115200);
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
// Start ethernet
Serial.println(F("Starting ethernet..."));
//Ethernet.begin(mac, ip, gateway, gateway, subnet);
// If using dhcp, comment out the line above
// and uncomment the next 2 lines plus the Ethernet.maintain call in loop
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.print(F("IP "));
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready"));
}
void loop()
{
Ethernet.maintain(); // If using dhcp to get an IP, uncomment line
// send data every delayMillis
thisMillis = millis();
if(thisMillis - lastMillis > delayMillis)
{
// GET the page
if(!getPage(serverPort, serverName, pageToGet)) Serial.print(F("Fail "));
else { Serial.print(F("Pass ")); lastMillis=millis(); }
}
}
char inChar;
char data[100]={0}; // << add these
int index=0;
// HTTP GET from page on server serverName using thisPort
byte getPage(int thisPort, const char* serverName, const char *page)
{
int inChar;
char outBuf[128]="GET ";
Serial.print(F("\n\nconnecting to "));
Serial.println(serverName);
// connect to server
if(client.connect(serverName, thisPort) == 1)
{
Serial.println(F("connected"));
#ifdef SPRINTF
// form HTTP GET and send it - replace sprint() with strcat()
sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
Serial.println(outBuf);
// form server and send it
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
#else
client.println(strcat(strcat(outBuf, page), " HTTP/1.1"));
Serial.println(outBuf);
outBuf[0]=0;
client.println(strcat(strcat(outBuf, "Host: "), serverName));
#endif
Serial.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("failed"));
return 0;
}
// connectLoop controls the hardware fail timeout
int connectLoop = 0;
while(client.connected())
{
while(client.available())
{
// copy a line of characters into array data
inChar = client.read();
if(inChar < ' ') // if end of line search for "origin": "xxx.xxx.xxx.xxx"
{
if(strstr(data, "\"origin") > 0)
{ Serial.print("\n found > "); Serial.println(data); }
//Serial.print(" index = "); Serial.println(index);
index=0;
}
else data[index++]=inChar; // add character to buffer
data[index]=0; // add NULL to array
Serial.write(inChar); // and display character
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
}
Serial.println();
Serial.println(F("disconnecting."));
// close client end
client.stop();
return 1;
}
a run gives the origin IP address
Starting ethernet...
IP 192.168.1.177
Ready
connecting to httpbin.org
connected
GET /ip HTTP/1.1
Host: httpbin.org
HTTP/1.1 200 OK
Connection: close
Server: gunicorn/19.8.1
Date: Mon, 28 May 2018 17:51:48 GMT
Content-Type: application/json
Content-Length: 29
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Via: 1.1 vegur
{"origin":"xxx.xxx.xxx.xxx"}
found > {"origin":"xxx.xxx.xxx.xxx"}