AVNET's iotconnect.io cloud platform is an online service that you can use to send data to, and then show it on a dashboard. In this blog series I'm learning the Python SDK and integrating BLE devices. In this post: the mechanism to enter your credentials and configure WiFi without keyboard or wired network. The technique in this post can be used on other Linux devices (Pi, BB, ...) |
USB Memory Stick as WiFi configurator
The SmartEdge is a headless Linux device. How do you connect it to your WiFi router?
I've seen several options used by WiFi devices.
- The out-of-box example for the Avnet SmartEdge acts as a WiFi access point. You can configure it to become a client for your network via an App on your phone.
- For a Raspberry Pi out-of-box, you either hard wire it to the network and set WiFi over an SSH session, or you temporarily connect a keyboard and screen.
- Others have a WPS button,
- etc ..
The Avnet example for the SmartEdge uses a neat mechanism. A USB memory stick that you prepare with your connect info.
The box runs a Python script as a service that waits for you to plug in a USB.
If it detects a stick, the script mounts it, then it looks for a file named OnSemiConfig.txt.
It then takes the config found on the stick, configures the WiFi (and other things in the case of the example I'm reviewing).
Then it restarts the box.
if(os.path.exists('/dev/sda1') == True): os.system("mount /dev/sda1 /mnt") if(os.path.exists('/mnt/OnSemiConfig.txt') == True): # ... # Usb stick inserted with onsemi configuration file # start processing the configuration config = configparser.ConfigParser() config.read('/mnt/OnSemiConfig.txt') my_config_parser_dict = {s:dict(config.items(s)) for s in config.sections()} # ... f = open('/etc/wpa_supplicant/wpa_supplicant.default', 'r') os.system('rm /etc/wpa_supplicant/wpa_supplicant.conf') f1 = open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') f1.write(f.read()) f.close() f1.write('\n') f1.write("network={\n ssid=\"%s\"\n psk=\"%s\"\n}\n" % (my_config_parser_dict['onsemiconfiguration']['wifissid'] , my_config_parser_dict['onsemiconfiguration']['wifipsk'])) f1.close() # ... # wait for usb removal os.system('/opt/avnet-iot/iotservices/wifi_client') else: # ... while(os.path.exists('/dev/sda1') == True): time.sleep(2) os.system('reboot')
Here is the WiFi related content of the config file on the stick.
[onsemiconfiguration] wifissid=<your ssid> wifipsk=<your password>
Running as a service
There's a shell script in the IoTConnect folder /opt/avnet-iot/iotservices, named usbwatcher.
#!/bin/bash if [[ $EUID -ne 0 ]]; then echo "This script must be run as root" exit 1 fi # ... python -u /home/avnet/USBWatcher.py
And a service wrapper in /etc/systemd/system, named usbwatcher.service.
[Unit] Description=UsbWatcher OnSemiConfiguration StartLimitIntervalSec=0 [Service] ExecStart=/bin/bash /opt/avnet-iot/iotservices/usbwatcher Restart=always RestartSec=10 [Install] WantedBy=default.target
This takes care that you can start and stop the service, enable it at startup, ...
Here is a good explanation of this. And here is one that digs deeper in the subject.