I can’t speak for QNEthernet but NativeEthernet doesn’t have MDNS lookup so .local addresses won’t work.
Try setting LWIP_DNS_SUPPORT_MDNS_QUERIES to 1 in lwipopts.h (line 148). I think I'll add this to the next release.
Side point: the "(const char *)" casts aren't needed.
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
*/
#include <SPI.h>
#include <QNEthernet.h>
using namespace qindesign::network;// ADD for QNEthernet
// Enter a MAC address and IP address for your controller below.
byte mac[] = { 0x08, 0x97, 0x98, 0x6C, 0x4E, 0x8C };
byte ip[] = { 169, 254, 240, 149 }; // ip in lan
byte gateway[] = { 0, 0, 0, 0 }; // internet access via router
byte subnet[] = { 255, 255, 0, 0 }; //subnet mask
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
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);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
stdPrint = &Serial; // ADD for QNEthernet
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin();
Ethernet.waitForLocalIP(10000);// ADD for QNEthernet
// 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 the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
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;
}
}
}
// give the web browser time to receive the data
delay(2);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
A little help please. Can anyone tell me why my code is returning 0.0.0.0 for my server address? I've tried NativeEthernet and Shawn's QNEthernet and both give me the same result. If I pass it my mac and IP then I do get my server ip to be the one I passed but every browser I use tells me connection refused....
Can I suggest that you post your code between code tags using the # button.Code:/* Web Server A simple web server that shows the value of the analog input pins. using an Arduino Wiznet Ethernet shield. Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Analog inputs attached to pins A0 through A5 (optional) created 18 Dec 2009 by David A. Mellis modified 9 Apr 2012 by Tom Igoe modified 02 Sept 2015 by Arturo Guadalupi */ #include <SPI.h> #include <QNEthernet.h> using namespace qindesign::network;// ADD for QNEthernet // Enter a MAC address and IP address for your controller below. byte mac[] = { 0x08, 0x97, 0x98, 0x6C, 0x4E, 0x8C }; byte ip[] = { 169, 254, 240, 149 }; // ip in lan byte gateway[] = { 0, 0, 0, 0 }; // internet access via router byte subnet[] = { 255, 255, 0, 0 }; //subnet mask // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); 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); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } stdPrint = &Serial; // ADD for QNEthernet Serial.println("Ethernet WebServer Example"); // start the Ethernet connection and the server: Ethernet.begin(); Ethernet.waitForLocalIP(10000);// ADD for QNEthernet // 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 the server server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header 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; } } } // give the web browser time to receive the data delay(2); // close the connection: client.stop(); Serial.println("client disconnected"); } }
That way the code is much more readable and easier to understand as I am sure
you will agree from the example above.
Assuming you are using T4.1, have you tried running any of shawn's QNEthernet examples? That would be a good starting point to be sure that your ethernet hardware/software is okay and the basics work, and then move on from there.
I've tried many examples, I thought a simple webserver would be the easiest to test. If you have a suggestion on a better example to start with please let me know. I just assumed a web server was the "hello world" level for ethernet coding.
Yes I have a Teensy 4.1 and the Ethernet kit from JPRC. I've run plenty of other code on the T4.1 (SPI, I2C, UART,etc.) so I know the board is good. I've also run Angry IPScanner and pinged the T4.1 with the Ethernet kit connected. That's were I got the MAC and IP address. Running ipconfig through the command line gives me the IP of 196.254.240.149 and the subnet of 255.255.0.0.
It doesn't give me a gateway though...is that why I'm getting an IP address of 0.0.0.0 when I run Ethernet.localIP()?
Call flush() to send any buffered data immediately. For reference, see the “Write immediacy” section in the Readme.
There’s no equivalent for setSocketSize() or setStackHeap(). Those are specific to how NativeEthernet operates. Try flush() first at points where you need to be sure data is sent. If you really need to, you can increase the stack’s heap by changing MEM_SIZE in lwipopts.h, but I don’t feel that will be necessary here.