Hello,
DNS lookup fails when using the Ethernet library and setting a static IP.
DNS lookup works when using DHCP.
DNS lookup request work on a PC on the same network as the Arduino.
Setting the DNS server IP does not work. When getting it, it returns "8.8.8.8".
Network sees a request to 8.8.8.8 even though it is not part of my code.
Thank you.
#include <SPI.h>
#include <PortentaEthernet.h>
#include <Ethernet.h>
byte mac[] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 }; // example
IPAddress ip(10, 72, 34, 68); // Static IP address
IPAddress gateway(10, 72, 34, 1);
IPAddress subnet(255, 255, 254, 0);
IPAddress dns(192, 100, 100, 1);
// initialize the EthernetClient library instance:
EthernetClient client;
char server[] = "some_server_address"; // DNS lookup not working... // "10.72.34.53"
// Server port
int serverPort = 5679;
unsigned long lastConnectionTimeMs = 0; // last time you connected to the server, in milliseconds
const unsigned long postingIntervalMs = 10 * 1000; // delay between updates, in milliseconds
void setup()
{
// Initialize serial communication
Serial.begin(9600);
while (!Serial)
; // Wait for the serial connection to be established
// Check if Ethernet hardware is present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Can't run without hardware.");
NVIC_SystemReset(); // Software reset
}
// Start Ethernet with static IP only
Serial.println("Initializing Ethernet with static IP");
Ethernet.begin(mac, ip, dns, gateway, subnet);
// give the Ethernet shield a second to initialize:
delay(1000);
// Check the link status
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
NVIC_SystemReset(); // Software reset
}
// Check if we have a valid IP address (i.e., connected to the network)
IPAddress assignedIP = Ethernet.localIP();
if (assignedIP == IPAddress(0, 0, 0, 0))
{
Serial.println("Failed to configure Ethernet using static IP");
NVIC_SystemReset(); // Software reset
}
else
{
Serial.print("Connected to the network with IP: ");
Serial.println(assignedIP);
}
}
void loop()
{
// if there's incoming data from the network connection,
// send it out the serial port for debugging purposes:
if (client.available())
{
char c = client.read();
Serial.write(c);
}
// If the posting interval has passed, send a new TCP message to server:
if (millis() - lastConnectionTimeMs > postingIntervalMs)
{
sendTcpMessageToServer();
}
}
void sendTcpMessageToServer()
{
Serial.println("sendTcpMessageToServer");
// close any previous connection before making a new request
client.stop();
// if there's a successful connection:
if (client.connect(server, serverPort))
{
Serial.println("connected");
// TODO: send message
client.write("TEST_MESSAGE");
}
else
{
// if the connection failed:
Serial.println("connection failed");
}
// note the time that the connection was made:
lastConnectionTimeMs = millis();
}