Hello,
I’m having difficulty communicating with the chip via web server with Atmega 328pb. Server code below.
#define __AVR_ATmega328PB__
#include "Ethernet.h"
#include "C:/Users/user/source/Workspaces/directory/Directory/Libraries/SPI_Atmega_328PB/SPI/src/SPI.h"
//#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
IPAddress ip(192, 168, 50, 252 );
// the router's gateway address:
//IPAddress gateway(192, 168, 50, 1);
// the subnet:
//IPAddress subnet( 255, 255, 0, 0 );
EthernetServer server(80);
String readString;
void setup()
{
Serial.begin(9600);
while(!Serial) {};
// initialize the ethernet device
Ethernet.init(PIN_SPI_SS);
Ethernet.begin(mac, ip);
//Ethernet.setLocalIP(ip);
Serial.println(Ethernet.localIP());
// 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.");
}
// start listening for clients
server.begin();
Serial.println("Server is at: ");
Serial.print(Ethernet.localIP());
}
void loop()
{
// Create a client connection
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an HTTP request ends with a blank line
bool currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
Serial.print(c);
}
//if HTTP request has ended
if (c == '\n' && currentLineIsBlank) {
/*
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<TITLE>hello world</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1></H1>");
client.println("<hr />");
client.println("<br />");
client.println("<H2></H2>");
client.println("<br />");
client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
client.println("<a href=\"/?button1off\"\">Turn Off LED</a><br />");
client.println("<br />");
client.println("<br />");
//client.println("<a href=\"/?button2on\"\">Rotate Left</a>");
//client.println("<a href=\"/?button2off\"\">Rotate Right</a><br />");
client.println("<p></p>");
client.println("<br />");
client.println("</BODY>");
client.println("</html>");
break;
*/
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") > 0)
{
}
if (readString.indexOf("?button1off") > 0)
{
}
if (readString.indexOf("?button2on") > 0)
{
}
if (readString.indexOf("?button2off") > 0)
{
}
//Clear readString for next read
readString = "";
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}