This is an example of how to use the RF24Ethernet class to connect out to a web server and retrieve data via HTTP. It also demonstrates how to read from the HTTP header to read the Content-Length before reading the data.
#include <SPI.h>
#include <RF24.h>
#include <RF24Network.h>
#include <RF24Mesh.h>
RF24 radio(7, 8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);
void setup() {
Serial.begin(115200);
Serial.print("Start Mesh:");
IPAddress myIP(10, 10, 2, 4);
if (mesh.begin()) {
Serial.println(" OK");
} else {
Serial.println(" Failed");
}
IPAddress gwIP(10, 10, 2, 2);
}
uint32_t counter = 0;
uint32_t reqTimer = 0;
uint32_t mesh_timer = 0;
bool gotHeader = 0;
long contentLength = 0;
void loop() {
if (millis() - mesh_timer > 30000) {
mesh_timer = millis();
if (!mesh.checkConnection()) {
if (mesh.renewAddress() == MESH_DEFAULT_ADDRESS) {
mesh.begin();
}
}
}
size_t size;
if ((size = client.available()) > 0) {
if (!gotHeader) {
if (client.find("Content-Length: ")) {
contentLength = client.parseInt();
client.find("\r\n\r\n");
counter = 0;
gotHeader = true;
}
} else {
if (client.available() > 0) {
char c = client.read();
Serial.print(c);
counter++;
}
if (counter == contentLength) {
gotHeader = false;
Serial.print("Content length ");
Serial.println(contentLength);
Serial.print("Received length ");
Serial.println(counter);
}
}
}
if (!client.connected()) {
Serial.println();
Serial.println(F("Disconnect. Waiting for disconnect timeout"));
client.stop();
reqTimer = millis();
while (millis() - reqTimer < 5000 && !client.available()) {
}
connect();
}
}
void connect() {
Serial.println(F("connecting"));
IPAddress ascii(208, 86, 224, 90);
if (client.connect(ascii, 80)) {
Serial.println(F("connected"));
client.println("GET http://artscene.textfiles.com/asciiart/texthistory.txt HTTP/1.1");
client.println("Host: 208.86.224.90");
client.println("Connection: close");
client.println();
} else {
Serial.println(F("connection failed"));
}
}
RF24EthernetClass RF24Ethernet