My company is working on a project where several Photon are installed in the street. We would like to connect to the Internet via a 3/4G router. First I connected the router via a fixed wired WAN connection. My simple slightly adapted Blink code works fine. When I discconect the router from the wired connection and enable the 3G connection I can no longer use WiFi.off() as the Photon keeps complaining about unable to resolve device.spark.io:
0000069004 [system] ERROR: Cloud: unable to resolve IP for device.spark.io
When I connect my own laptop to the same router I can resolve device.spark.io. So this seems to be a timing issue.
Here is my code:
// ------------
// Blink an LED
// ------------
/*-------------
We've heavily commented this code for you. If you're a pro, feel free to ignore it.
Comments start with two slashes or are blocked off by a slash and a star.
You can read them, but your device can't.
It's like a secret message just for you.
Every program based on Wiring (programming language used by Arduino, and Particle devices) has two essential parts:
setup - runs once at the beginning of your program
loop - runs continuously over and over
You'll see how we use these in a second.
This program will blink an led on and off every second.
It blinks the D7 LED on your Particle device. If you have an LED wired to D0, it will blink that LED as well.
-------------*/
// First, we're going to make some variables.
// This is our "shorthand" that we'll use throughout the program:
int led1 = D0; // Instead of writing D0 over and over again, we'll write led1
// You'll need to wire an LED to this one to see it blink.
int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
// This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.
// Having declared these variables, let's move on to the setup function.
// The setup function is a standard part of any microcontroller program.
// It runs only once when the device boots up or is reset.
// added proper logger here, such that we see much more details
SerialLogHandler logHandler(LOG_LEVEL_TRACE);
int looped = 0;
void setup() {
// some time to get our serial line running and connected to a terminal
delay(5000);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Log.trace(WiFi.SSID());
delay(5000);
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.dnsServerIP());
}
// Next we have the loop function, the other essential part of a microcontroller program.
// This routine gets repeated over and over, as quickly as possible and as many times as possible, after the setup function is called.
// Note: Code that blocks for too long (like more than 5 seconds), can make weird things happen (like dropping the network connection). The built-in delay function shown below safely interleaves required background activity, so arbitrarily long delays can safely be done if you need them.
void loop() {
// To blink the LED, first we'll turn it on...
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
// We'll leave it on for 1 second...
delay(10000);
Log.trace("Trying particle connect");
Particle.connect();
// Then we'll turn it off...
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(200);
Log.trace("Connected to WLAN: %s", WiFi.SSID());
Log.trace("Publish loop count");
Particle.publish("lwup", String(looped));
// Wait 2 seconds...
delay(3000);
looped++;
Log.trace("Wifi off");
WiFi.off();
// And repeat!
}
And here the log from the serial output:
cat /tmp/3g.log
0000023607 [app] TRACE: Trying particle connect
0000023807 [app] TRACE: Connected to WLAN: SensorHub_AFORT1
0000023807 [app] TRACE: Publish loop count
0000026808 [app] TRACE: Wifi off
0000026808 [system] INFO: Cloud: disconnecting
0000026808 [system] INFO: Cloud: disconnected
0000036811 [app] TRACE: Trying particle connect
0000036813 [hal.wlan] INFO: Using external antenna
0000036825 [system] INFO: ARM_WLAN_WD 1
0000037672 [system] INFO: ARM_WLAN_WD 2
0000037672 [hal.wlan] INFO: Bringing WiFi interface up with DHCP
0000037694 [system] INFO: CLR_WLAN_WD 1, DHCP success
0000037695 [system] INFO: Cloud: connecting
0000037695 [system] INFO: Read Server Address = type:1,domain:device.spark.io
0000052699 [system] ERROR: Cloud: unable to resolve IP for device.spark.io
0000052699 [system] WARN: Cloud socket connection failed: -1
0000052999 [system] WARN: Internet available, Cloud not reachable!
0000053000 [system] WARN: Handling cloud error: 3
0000053000 [app] TRACE: Connected to WLAN: SensorHub_AFORT1
0000053000 [app] TRACE: Publish loop count
0000054001 [system] INFO: Cloud: connecting
0000054001 [system] INFO: Read Server Address = type:1,domain:device.spark.io
0000069004 [system] ERROR: Cloud: unable to resolve IP for device.spark.io
0000069004 [system] WARN: Cloud socket connection failed: -1
0000069004 [system] WARN: Resetting WLAN due to 2 failed connect attempts
0000069146 [system] WARN: Internet available, Cloud not reachable!
0000069147 [system] WARN: Handling cloud error: 3
0000069147 [app] TRACE: Wifi off
0000079150 [app] TRACE: Trying particle connect
0000079350 [app] TRACE: Connected to WLAN:
0000079351 [app] TRACE: Publish loop count
0000082351 [app] TRACE: Wifi off
0000092351 [app] TRACE: Trying particle connect
0000092551 [app] TRACE: Connected to WLAN:
0000092551 [app] TRACE: Publish loop count
0000095552 [app] TRACE: Wifi off
0000105552 [app] TRACE: Trying particle connect
0000105752 [app] TRACE: Connected to WLAN:
0000105752 [app] TRACE: Publish loop count
0000108752 [app] TRACE: Wifi off
0000118753 [app] TRACE: Trying particle connect
0000118953 [app] TRACE: Connected to WLAN:
0000118953 [app] TRACE: Publish loop count