Here is the code example I am using to setup my ATmega2560 as a client. I would like the arduino to receive a string from a server at 10.2.0.32 and hold it in the serial buffer.
Then I want to display it in the serial monitor, look for the certain characters and do something (turn on led), and display on LCD. I am hung up on the "GET" request format line of code.
By typing this address in my local browser >>>> http://10.2.0.33/mtlqueue/status
A script outputs a blank white webpage with the following string:
AGE 0UPDATELCDMSG$$MSG$$2:23:25PMEXIT
It's this string that reads a database on the server called "mtlqueue". If the database increases in size, the string will change some of its charactera, size and length. For example,
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
//setting up LCD
const int rs=12, en=11, d4=5, d5=4, d6=3, d7=2;
LiquidCrystal lcd (rs, en, d4, d5, d6, d7);
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xAE, 0xD9 }; //alternate mac {0x90,A2,DA,0F,AE,D9};
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(10,2,0,32); // numeric IP for your server (no DNS)
//char server[] = "http://10.2.0.32/mtlqueue/status"; (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10,2,0,25); //default(192.168.0.177)
IPAddress myDns(10,2,0,1); //default (192.168.0.1)
// 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;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
lcd.begin (16,2);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
lcd.print("Initalize ethernet");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
lcd.print("cat5 not connected");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
lcd.print(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to "); //connecting to server database
Serial.print(server);
Serial.println("...");
lcd.print(server); //lcd displays the server
// if you get a connection, report back via serial:
if (client.connect(server, 80)) { //server=HOST_NAME,HTTP_PORT
Serial.print("connected to "); //connected to 10.2.0.32
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("GET / /status HTTP/1.1"); //example>> client.println("GET /search?q=mtlqueue/status HTTP/1.1");
client.println("http://10.2.0.32/mtlqueue/status"); //example>> client.println("Host:www.google.com");
lcd.clear();
lcd.setCursor(0,0); //display on top row
lcd.print("Get request");
lcd.setCursor(0,1); //display on botton row
lcd.print("retrieving data");
client.println("Connection: close"); //Connection: close
client.println(); //end HTTTP header
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
beginMicros = micros();
}
while (client.connected()) {
if (client.available()) {
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("disconnecting");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}