Part 1 WiFi
This is the first of a 2 part series on how to program WiFi enabled devices without the use of a USB cable.
If you are like me, your project may end up in the attic, in the basement, behind the stove or in any other awkward place. Gallons of coffee later you were sure your code was perfect, a work of art. Well, reality came back to bite you and, yes, there is a bug. Lugging a portable computer to your project board and finding a long enough USB cable to reach the PCB is not that big a pain as the one in your back after moving the heavy furniture or equipment that was in the way. The point is, WiFi enable devices can be remotely programmed over the air (OTA). Since many WiFi enabled boards are now low cost, why use anything else.
WiFi Credentials
The easy way is to hard code the WiFi SSID and password. It is also the least convenient in the long run.
If your WiFi credential ever change (to lock out hackers) your in trouble.
The best way is for your device to boot up trying non-volatile memory saved credentials. If it cannot connect after 30 seconds, it turns itself into a hotspot access point with a credential configuration web page.
Connect to this WiFi using your smartphone or tablet and navigate to the page. In my case I use 192.168.4.1
Here is some of the code:
Read EEprom:
/********************************************
* Read eeprom to retrieve saved ssid and pass
*/
for (int i = 0; i < 32; ++i)
{
esid += char(EEPROM.read(i));
}
//esid=”test”; // Used for testing to force bad ssid and psw forcing AccessPoint mode
Serial.println();
Serial.print(“SSID:”);
Serial.println(esid);
for (int i = 32; i < 96; ++i)
{
epass += char(EEPROM.read(i));
}
Serial.print(“PASSWORD:”);
Serial.println(epass);
Try to connect:
/******************************************************
*
* Try to connect to local WiFi with saved ssid psw
*
***************************************************/
WiFi.begin(esid.c_str(), epass.c_str());
WiFi.mode(WIFI_STA);
WiFi.macAddress(mac1); // returns mac address
char temp = 10; // seconds tying to connect
// wait up to 10 sec for connection
while ((WiFi.status() != WL_CONNECTED) && (temp > 0) )
{
Serial.print(“nc”); // no connection
delay(1000);
temp–;
}
// after wait if still not connected launch web for new credentials
if (WiFi.status() != WL_CONNECTED)
{
Serial.println(“Connection Status Negative”);
Serial.println(“Turning On the HotSpot”);
setupAP();// Setup HotSpot to get new SSID and PSW
AccesPointMode = 1;
digitalWrite(GPIO_LED, LOW);
}
else // here wifi connect successful
{
Serial.println(“”);
Serial.print(“Connected to “);
Serial.print(esid);
Serial.println(” Successfully”);
AccesPointMode = 0;
digitalWrite(GPIO_LED, HIGH);
The setupAP() function handles the hotspot and web page to setup the credentials with the correct SSID and password. It includes changing from client to access point, generating a web page, saving input text SSID and PSW to eeprom and rebooting.
The full code can be found in the Seenov Github repository: https://github.com/Seenov/Raspberry_Pi_ADC_HAT
Once it has rebooted, it will execute the code you painstakingly developed while keep an ear open for OTA instructions. Part 2 will explore how to incorporate OTA programming into your code and how to use it.