Hello Friends, hope you all are fine and having fun. In today tutorial i am going to elaborate How to Automatically Connect with Wifi SSID using Arduino YUN. If you recall one of my previous tutorials named Getting started with Arduino YUN , in which i gave a brief introduction about Arduino YUN, its working and features. In that tutorial, I have explained How to connect Arduino YUN with Wifi manually. A little problem encounters while connecting Arduino manually to available wifi networks that if wifi connection drops then, then Arduino will also disconnect automatically and if wifi connection is energized again, it will still remain disconnected unless you reconnect it by yourself. This thing has very serious drawbacks in industrial projects, where data is continuously uploaded through multiple servers and if at any stage connection drops and Arduino stops working then, this thing leads to drastic outcomes.
So there is serious need to design an algorithm in which Arduino automatically connects to the available wifi connections and should enables the server to upload data through it. This whole process is a bit lengthy and much complicated. So, i will elaborate all this in the coming tutorials. In today's tutorial i am going to restrict myself only How to get Available wifi SSID using Arduino YUN. First of all lets recall the basics of Arduino YUN. Arduino YUN has 2 micro processors embedded on the same board. First is the Arduino microprocessor and the second is Atheros micro processor. Atheros supports Linux server (commonly used in Apple computers). With both these on-board micro processors, we can do anything we want to do. The beauty of Arduino board is that it has built in wifi, Ethernet port, USB host and SD card slot. We can also upload data into Arduino through wifi without physically connecting it with computer. That's why Arduino boards are the most widely used micro processors now a days, and are able to handle multitasking industrial projects. Above was a little introduction about Arduino YUN and now lets get started with our today's tutorial.
Automatically Connect with Wifi SSID using Arduino YUN
- First of all open the Arduino softeare. On toolbar click on the icon named "Tools" and a new window will open. Then go to the option "Board" and from the next opened window select the board "Arduino YUN".
- Remember you must download the Arduino software version 1.5.5 + instead of 1.0.3 because Arduino sketches will be only compiled in 1.5.5 version which is specifically designed for Arduino YUN.
- After selecting the board, you will load the given below code into Arduino Board.
#include <Process.h> #include <FileIO.h> String ESSID = "TEP"; String Pass = "Pakistan"; String Encrypt = "psk2"; void setup() { Serial.begin(9600); delay(5000); FileSystem.begin(); delay(5000); Bridge.begin(); delay(5000); } void loop() { // put your main code here, to run repeatedly: WifiAuthentication(); } void WifiAuthentication() { uploadScript(); delay(1000); runScript(); delay(20000); } void uploadScript() { File script = FileSystem.open("/tmp/setupwifi.sh", FILE_WRITE); script.print("#!/bin/sh\n"); script.print("/sbin/uci set network.lan=interface\n"); script.print("/sbin/uci set network.lan.proto=dhcp\n"); script.print("/sbin/uci delete network.lan.ipaddr\n"); script.print("/sbin/uci delete network.lan.netmask\n"); script.print("/sbin/uci set wireless.@wifi-iface[0].mode=sta\n"); script.print("/sbin/uci set wireless.@wifi-iface[0].ssid=" + ESSID + "\n"); script.print("/sbin/uci set wireless.@wifi-iface[0].encryption=" + Encrypt + "\n"); script.print("/sbin/uci set wireless.@wifi-iface[0].key=" + Pass + "\n"); script.print("/sbin/uci commit wireless; /sbin/wifi\n"); script.print("/etc/init.d/network restart\n"); script.close(); Process chmod; chmod.begin("chmod"); chmod.addParameter("755"); chmod.addParameter("/tmp/setupwifi.sh"); chmod.run(); } void runScript() { Process myscript; myscript.begin("/tmp/setupwifi.sh"); myscript.run(); Serial.println("Connected"); }
- The above given code is a bit complicated and it consists of many steps. Now i am going to show all the steps through a block diagram and then i will try to explain every step one by one.
- Using this code, the Arduino YUN board will automatically connected to the available SSID which is in our case is TEP. So you can give any other SSID there and it will connect to that one.
- In the above block diagram, you can see that first of all Initialize Serial Port. When you will connect Arduino YUN with your computer through cable or as i described earlier that Arduino YUN also have built-in wifi so you can also connect Arduino YUN with computer through wifi.
- Then you will load the code to Initialize Serial Port, which is the first step and also shown in the above block diagram.
- In this project we have kept Baud rate of Arduino YUN 9600.
- The next step in the block diagram is to 'Initialize File System' . In this step we will load 'File System' into our code as viewed in the code image given above.
- Now in the next step we have to initialize the Arduino YUN Bridge. The block diagram representing the internal structure of Arduino YUN is shown in the image below:
- As i stated earlier in the beginning of the tutorial that Arduino YUN has 2 on-board micro processors and Arduino YUN is the intermediate source who performs communication between both micro processors.
- The Bridge library facilitates communication between the two processors, giving Arduino sketches the ability to run shell scripts, communicate with network interfaces, and receive information from the AR 9331 processor.
- The USB Host is connected to the ATmega 32u4, while the all external interferences(like Wifi, Ethernet, SD card) are connected to Linux micro processor.
- The beauty of this board is that Bridge libraries also enables the Arduino micr processor ATmega to communicate with the other interferences, which are also connected with Linux microprocessor.
- When bridge has been activated then, Arduino YUN enables its wifi and search for the nearby available wifi connections.
- When it will get some available wifi connections in its surrounding then, from its algorithm, Arduino YUN will get the SSID from that available wifi connections.
- The next thing which we have implemented in the code is to get SSID String. It is a built in function and also available in Arduino libraries that if it gets any available wifi connection near it, then it automatically gets strings from those connections.
- When Arduino has searched for all the available wifi connections near it and after getting the SSID of available wifi connections, it sends all these SSID to serial port of Arduino YUN board.
- After that Arduino YUN will automatically connect to that wifi connection whose SSID matches with the given SSID.
- Now the end step is very important and it distinguishes Arduino YUN from all other Arduino boards, which is, After sending data to serial port it again starts the loop and got o step #4 and it again starts to search for available wifi networks.
- This phenomenon can also be verified from the above shown block diagram.
- At any stage, if wifi connection drops then the loop will again start and will search for available wifi connections, get their SSID and it will send these SSID to serial port and it will rehabilitate the connection within no time and no problem will occur at any stage of data execution.