Hello
I am trying to get my Arduino Uno Wifi rev.2 to connect to mosquitto running on my computer on another LAN.
I set up port forwarding on that LAN and made sure the port was open, and also have a dynamic DNS on NoIP set up to point to the router.
But I cannot get my arduino to connect to the server. It connects to wifi fine, and I can ping my router, but I can't ping any websites, even if I use their IP addresses.
Maybe the PubSubClient library interferes with the ping?
I also made sure the server was actually running
Code (ssid, password, and ip addresses changed):
#include <SPI.h>
#include <WiFiNINA.h>
#include <PubSubClient.h>
#include <Bounce2.h>
#include <EEPROM.h>
char ssid[] = "ABCDEFG";
char pass[] = "**********";
char server[] = "domain-pointing-to-router.ddns.net";
int status = WL_IDLE_STATUS;
IPAddress serverIP;
void callback(char* topic, byte* payload, unsigned int length) {
// Print the topic
Serial.println(F("Received message"));
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
void dnsResolve() {
byte counter = 0;
WiFi.setDNS(IPAddress(8, 8, 8, 8));
Serial.println("Resolving dns");
while (true) {
if (counter == 5) {
Serial.println("Defaulting to last IP address used");
for (byte i = 0; i < 4; i++) {
serverIP[i] = EEPROM.read(i);
}
Serial.println(serverIP);
break;
}
int err = WiFi.hostByName(server, serverIP);
if(err == 1){
Serial.print("Ip address: ");
Serial.println(serverIP);
for (int i = 0; i < 4; i++) {
if (EEPROM.read(i) != serverIP[i]) {
EEPROM.write(i, serverIP[i]);
}
}
break;
} else {
Serial.print("Error code: ");
Serial.println(err);
Serial.println(F("Retrying"));
WiFi.setDNS(IPAddress(8, 8, 4, 4));
counter++;
}
}
}
WiFiClient wifiClient;
PubSubClient client(wifiClient);
unsigned long lastMillis;
unsigned long pingMillis;
void wifiConnect() {
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.config(IPAddress(192, 168, 0, 198));
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println(F("Connected"));
digitalWrite(LED_BUILTIN, HIGH);
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println();
Serial.println("Client");
wifiConnect();
for (int i = 0; i < 4; i++) {
Serial.print("Pinging router: ");
int pingResult = WiFi.ping(IPAddress(192,168,0,1));
if (pingResult >= 0) {
Serial.print("SUCCESS! RTT = ");
Serial.print(pingResult);
Serial.println(" ms");
} else {
Serial.print("FAILED! Error code: ");
Serial.println(pingResult);
switch(pingResult) {
case WL_PING_DEST_UNREACHABLE:
Serial.println("Dest unreachable");
break;
case WL_PING_TIMEOUT:
Serial.println("Timeout");
break;
case WL_PING_UNKNOWN_HOST:
Serial.println("Unknown host");
break;
case WL_PING_ERROR:
Serial.println("Unknown Error");
break;
}
}
delay(5000);
}
for (int i = 0; i < 4; i++) {
Serial.print("Pinging google: ");
int pingResult = WiFi.ping("www.google.com");
if (pingResult >= 0) {
Serial.print("SUCCESS! RTT = ");
Serial.print(pingResult);
Serial.println(" ms");
} else {
Serial.print("FAILED! Error code: ");
Serial.println(pingResult);
switch(pingResult) {
case WL_PING_DEST_UNREACHABLE:
Serial.println("Dest unreachable");
break;
case WL_PING_TIMEOUT:
Serial.println("Timeout");
break;
case WL_PING_UNKNOWN_HOST:
Serial.println("Unknown host");
break;
case WL_PING_ERROR:
Serial.println("Unknown Error");
break;
}
}
delay(5000);
}
dnsResolve();
client.setServer(serverIP, 1883);
Serial.println("Attempting to connect to server");
if (client.connect("arduino-test-1")) {
Serial.println(F("Connection successful"));
client.setCallback(callback);
} else {
Serial.println(F("Connection failed"));
while(1);
}
}
void loop() {
client.loop();
client.subscribe("test");
if (client.publish("test", "Hello World")) {
Serial.println(F("Successfully published"));
} else {
Serial.println(F("Failed to send message :("));
}
delay(4000);
}
Output:
15:52:29.408 -> Client
15:52:30.151 -> Attempting to connect to Network named: ABCDEFG
15:52:45.256 -> Attempting to connect to Network named: ABCDEFG
15:53:00.250 -> Connected
15:53:00.250 -> SSID: ABCDEFG
15:53:00.250 -> IP Address: 192.168.0.198
15:53:00.297 -> Pinging router: SUCCESS! RTT = 10 ms
15:53:05.231 -> Pinging router: SUCCESS! RTT = 0 ms
15:53:10.265 -> Pinging router: SUCCESS! RTT = 10 ms
15:53:15.240 -> Pinging router: SUCCESS! RTT = 10 ms
15:53:20.220 -> Pinging google: FAILED! Error code: -3
15:53:20.266 -> Unknown host
15:53:25.195 -> Pinging google: FAILED! Error code: -3
15:53:25.234 -> Unknown host
15:53:30.225 -> Pinging google: FAILED! Error code: -3
15:53:30.225 -> Unknown host
15:53:35.209 -> Pinging google: FAILED! Error code: -3
15:53:35.209 -> Unknown host
15:53:40.180 -> Resolving dns
15:53:46.789 -> Error code: 0
15:53:46.789 -> Retrying
15:53:53.785 -> Error code: 0
15:53:53.785 -> Retrying
15:54:00.776 -> Error code: 0
15:54:00.776 -> Retrying
15:54:07.777 -> Error code: 0
15:54:07.777 -> Retrying
15:54:14.751 -> Error code: 0
15:54:14.805 -> Retrying
15:54:14.805 -> Defaulting to last IP address used
15:54:14.823 -> 123.456.7.890
15:54:14.851 -> Attempting to connect to server
15:54:43.026 -> Connection failed