diff --git a/examples/WifiConfig/WifiConfig.ino b/examples/WifiConfig/WifiConfig.ino new file mode 100644 index 0000000..6c4b559 --- /dev/null +++ b/examples/WifiConfig/WifiConfig.ino @@ -0,0 +1,55 @@ +/* + WiFi Status + + This sketch runs a script called "pretty-wifi-info.lua" + installed on your Yún in folder /usr/bin. + It prints information about the status of your wifi connection. + + It uses Serial to print, so you need to connect your Yún to your + computer using a USB cable and select the appropriate port from + the Port menu + + created 18 June 2013 + By Federico Fissore + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/YunWiFiStatus + + */ + +#include +#include "credential.h" + +void setup() { + SerialUSB.begin(9600); // initialize serial communication + while (!SerialUSB); // do nothing until the serial monitor is opened + + SerialUSB.println("Starting bridge...\n"); + pinMode(13, OUTPUT); + digitalWrite(13, LOW); + Bridge.begin(); // make contact with the linux processor + digitalWrite(13, HIGH); // Led on pin 13 turns on when the bridge is ready + delay(2000); // wait 2 seconds + Serial.println("attempting to connect"); + Bridge.wifiConfig(yunName, yunPsw, wifissid, wifipsw, wifiAPname, countryCode); + Serial.println("..."); + +} + +void loop() { + Process wifiCheck; // initialize a new process + + wifiCheck.runShellCommand("/usr/bin/pretty-wifi-info.lua"); // command you want to run + + // while there's any characters coming back from the + // process, print them to the serial monitor: + while (wifiCheck.available() > 0) { + char c = wifiCheck.read(); + SerialUSB.print(c); + } + + SerialUSB.println(); + + delay(5000); +} diff --git a/examples/WifiConfig/credential.h b/examples/WifiConfig/credential.h new file mode 100644 index 0000000..a233f12 --- /dev/null +++ b/examples/WifiConfig/credential.h @@ -0,0 +1,6 @@ +const String yunName="yourYunName"; +const String yunPsw="yourYunPsw"; +const String wifissid="yourWifiSSID"; +const String wifipsw="yourWifiPSW"; +const String wifiAPname="YUNAccessPointName"; //this is the name of the access point you want the yun to create if somethings goes wrong with the connection to the wifi +const String countryCode="yourcountrycode"; // for example IT DE US diff --git a/src/Bridge.cpp b/src/Bridge.cpp index 02be380..d9f52a1 100644 --- a/src/Bridge.cpp +++ b/src/Bridge.cpp @@ -279,3 +279,31 @@ SerialBridgeClass Bridge(Serial1); SerialBridgeClass Bridge(Serial); #endif +void BridgeClass::wifiConfig(String yunName, String yunPsw, String wifissid, String wifipsw, String wifiAPname, String countryCode){ + Process p; + + p.runShellCommand("blink-start 100"); //start the blue blink + + p.runShellCommand("hostname " + yunName); //change the current hostname + p.runShellCommand("uci set system.@system[0].hostname='" + yunName + "'"); //change teh hostname in uci + + p.runShellCommand("uci set arduino.@arduino[0].access_point_wifi_name='" + wifiAPname + "'"); + + //this block resets the wifi psw + p.runShellCommand("uci set wireless.@wifi-iface[0].encryption='psk2'"); + p.runShellCommand("uci set wireless.@wifi-iface[0].mode='sta'"); + p.runShellCommand("uci set wireless.@wifi-iface[0].ssid='" + wifissid + "'"); + p.runShellCommand("uci set wireless.@wifi-iface[0].key='" + wifipsw + "'"); + p.runShellCommand("uci set wireless.radio0.channel='auto'"); + p.runShellCommand("uci set wireless.radio0.country='" + countryCode + "'"); + p.runShellCommand("uci delete network.lan.ipaddr"); + p.runShellCommand("uci delete network.lan.netmask"); + p.runShellCommand("uci set network.lan.proto='dhcp'"); + + p.runShellCommand("echo -e \"" + yunPsw + "\n" + yunPsw + "\" | passwd root"); //change the passwors + p.runShellCommand("uci commit"); //save the mods done via UCI + p.runShellCommand("blink-stop"); //start the blue blink + + p.runShellCommand("wifi "); + +} diff --git a/src/Bridge.h b/src/Bridge.h index 63361f1..29080f4 100644 --- a/src/Bridge.h +++ b/src/Bridge.h @@ -72,6 +72,8 @@ class BridgeClass { static const uint16_t TRANSFER_TIMEOUT = 0xFFFF; + void wifiConfig(String yunName, String yunPsw, String wifissid, String wifipsw, String wifiAPname, String countryCode); + private: uint8_t index; int timedRead(unsigned int timeout);