Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WebServer.h>
  3. #include <ESP8266mDNS.h>
  4.  
  5. // Configure the framework
  6. #include "bconf/MCU_ESP8266.h" // Load the code directly on the ESP8266
  7. #include "conf/Gateway.h"
  8. #include "conf/IPBroadcast.h"
  9. #include "Souliss.h"
  10.  
  11. // Define the WiFi name and password
  12. #define WIFICONF_INSKETCH
  13.  
  14. // This identify the number of the LED logic
  15. #define MYLEDLOGIC 0
  16. #define LEDPWM 2 //THIS TYPICAL USES TWO SLOTS, SO THE NEXT FREE SLOT IS 2.
  17. #define LEDPWM1 4
  18.  
  19. //PWM pin
  20. #define LEDPWMP 12
  21. #define LEDPWMP1 13
  22. #define BLINK 5
  23.  
  24. enum {
  25. APPLICATION_WEBSERVER = 0,
  26. ACCESS_POINT_WEBSERVER
  27. };
  28.  
  29. MDNSResponder mdns;
  30. ESP8266WebServer server(80);
  31. const char* ssid = "esp8266e"; // Use this as the ssid as well
  32. // as the mDNS name
  33. const char* passphrase = "esp8266e";
  34. String st;
  35. String content;
  36.  
  37. void setup() {
  38. Serial.begin(9600);
  39. WiFi.mode(WIFI_STA); // Assume we've already been configured
  40.  
  41. if (testWifi()) {
  42. setupApplication(); // WiFi established, setup application
  43.  
  44. Initialize();
  45.  
  46. // Connect to the WiFi network and get an address from DHCP
  47. GetIPAddress();
  48. SetAsGateway(myvNet_dhcp); // Set this node as gateway for SoulissApp
  49.  
  50. // This is the vNet address for this node, used to communicate with other
  51. // nodes in your Souliss network
  52. SetAddress(0xAB01, 0xFF00, 0x0000); //Temp & Hum and Main Antitheft
  53. SetAsPeerNode(0xAB02, 1); //Door/Windows Sensor connected to peer Node 2 - 0xDO02 using USART
  54. SetAsPeerNode(0xD002, 2); //RGB Led etc connected to peer Node 1 - 0xDO01 using USART
  55.  
  56. Set_DimmableLight(LEDPWM);
  57. Set_DimmableLight(LEDPWM1);
  58.  
  59.  
  60. } else {
  61. setupAccessPoint(); // No WiFi yet, enter configuration mode
  62. }
  63.  
  64. pinMode(BLINK, OUTPUT); // use pin 5 as Blink Led
  65. pinMode(LEDPWMP, OUTPUT);
  66. pinMode(LEDPWMP1, OUTPUT);
  67. }
  68.  
  69. void loop() {
  70. if (WiFi.status() == WL_CONNECTED) {
  71.  
  72. // Here we start to play
  73. EXECUTEFAST() {
  74. UPDATEFAST();
  75.  
  76. FAST_50ms(){
  77. if (WiFi.status() == WL_CONNECTED) {
  78. digitalWrite(BLINK, !digitalRead(BLINK));
  79.  
  80. Logic_DimmableLight(LEDPWM);
  81. analogWrite(LEDPWMP, mOutput(LEDPWM+1)*4);
  82.  
  83. Logic_DimmableLight(LEDPWM1);
  84. analogWrite(LEDPWMP1, mOutput(LEDPWM1+1)*4);
  85.  
  86. }
  87. }
  88.  
  89. // Here we handle here the communication with Android
  90. FAST_GatewayComms();
  91. }
  92.  
  93. EXECUTESLOW(){
  94. UPDATESLOW();
  95.  
  96. SLOW_10s() {
  97.  
  98. Timer_DimmableLight(LEDPWM);
  99. Timer_DimmableLight(LEDPWM1);
  100.  
  101. }
  102. }
  103.  
  104. } else {
  105. server.handleClient(); // In this example we're not doing too much
  106.  
  107.  
  108. }
  109.  
  110. }
  111. bool testWifi(void) {
  112. int c = 0;
  113. Serial.println("\nWaiting for Wifi to connect...");
  114. while ( c < 20 ) {
  115. if (WiFi.status() == WL_CONNECTED) {
  116. return true;
  117. }
  118. delay(500);
  119. Serial.print(WiFi.status());
  120. c++;
  121. }
  122. Serial.println("\nConnect timed out, opening AP");
  123. return false;
  124. }
  125.  
  126. void setupApplication() {
  127. if (mdns.begin(ssid, WiFi.localIP())) {
  128. Serial.println("\nMDNS responder started");
  129. }
  130. launchWeb(APPLICATION_WEBSERVER); // In this example just launch a
  131. // web server
  132. }
  133.  
  134. void setupAccessPoint(void) {
  135. WiFi.mode(WIFI_STA);
  136. WiFi.disconnect();
  137. delay(100);
  138. int n = WiFi.scanNetworks();
  139. Serial.println("scan done");
  140. if (n == 0)
  141. Serial.println("no networks found");
  142. else
  143. {
  144. Serial.print(n);
  145. Serial.println(" networks found");
  146. for (int i = 0; i < n; ++i)
  147. {
  148. // Print SSID and RSSI for each network found
  149. Serial.print(i + 1);
  150. Serial.print(": ");
  151. Serial.print(WiFi.SSID(i));
  152. Serial.print(" (");
  153. Serial.print(WiFi.RSSI(i));
  154. Serial.print(")");
  155. Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
  156. delay(10);
  157. }
  158. }
  159. Serial.println("");
  160. st = "<ol>";
  161. for (int i = 0; i < n; ++i)
  162. {
  163. // Print SSID and RSSI for each network found
  164. st += "<li>";
  165. st += WiFi.SSID(i);
  166. st += " (";
  167. st += WiFi.RSSI(i);
  168. st += ")";
  169. st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
  170. st += "</li>";
  171. }
  172. st += "</ol>";
  173. delay(100);
  174. WiFi.mode(WIFI_AP);
  175. WiFi.softAP(ssid, passphrase, 6);
  176. launchWeb(ACCESS_POINT_WEBSERVER);
  177. }
  178.  
  179. void launchWeb(int webservertype) {
  180. Serial.println("\nWiFi connected");
  181. Serial.print("Local IP: ");
  182. Serial.println(WiFi.localIP());
  183. Serial.print("SoftAP IP: ");
  184. Serial.println(WiFi.softAPIP());
  185. setupWebServerHandlers(webservertype);
  186. // Start the server
  187. server.begin();
  188. Serial.print("Server type ");
  189. Serial.print(webservertype);
  190. Serial.println(" started");
  191. // WiFi.printDiag(Serial);
  192. }
  193.  
  194. void setupWebServerHandlers(int webservertype)
  195. {
  196. if ( webservertype == ACCESS_POINT_WEBSERVER ) {
  197. server.on("/", handleDisplayAccessPoints);
  198. server.on("/setap", handleSetAccessPoint);
  199. server.onNotFound(handleNotFound);
  200. } else if (webservertype == APPLICATION_WEBSERVER) {
  201. server.on("/", handleRoot);
  202. server.on("/setap", handleAccessPointAlreadySet);
  203. server.onNotFound(handleNotFound);
  204. }
  205. }
  206.  
  207. void handleDisplayAccessPoints() {
  208. IPAddress ip = WiFi.softAPIP();
  209. String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
  210. uint8_t mac[6];
  211. WiFi.macAddress(mac);
  212. String macStr = macToStr(mac);
  213. content = "<!DOCTYPE HTML>\n<html>Hello from ";
  214. content += ssid;
  215. content += " at ";
  216. content += ipStr;
  217. content += " (";
  218. content += macStr;
  219. content += ")";
  220. content += "<p>";
  221. content += st;
  222. content += "<p><form method='get' action='setap'><label>SSID: </label>";
  223. content += "<input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
  224. content += "<p>We will attempt to connect to the selected AP and reset if successful.";
  225. content += "<p>Wait a bit and try connecting to http://";
  226. content += ssid;
  227. content += ".local";
  228. content += "</html>";
  229. server.send(200, "text/html", content);
  230. }
  231.  
  232. void handleSetAccessPoint() {
  233. int httpstatus = 200;
  234. String qsid = server.arg("ssid");
  235. String qpass = server.arg("pass");
  236. if (qsid.length() > 0 && qpass.length() > 0) {
  237. for (int i = 0; i < qsid.length(); i++)
  238. {
  239. // Deal with (potentially) plus-encoded ssid
  240. qsid[i] = (qsid[i] == '+' ? ' ' : qsid[i]);
  241. }
  242. for (int i = 0; i < qpass.length(); i++)
  243. {
  244. // Deal with (potentially) plus-encoded password
  245. qpass[i] = (qpass[i] == '+' ? ' ' : qpass[i]);
  246. }
  247. WiFi.mode(WIFI_AP_STA);
  248. WiFi.begin(qsid.c_str(), qpass.c_str());
  249. if (testWifi()) {
  250. Serial.println("\nGreat Success!");
  251. delay(3000);
  252. abort();
  253. }
  254. content = "<!DOCTYPE HTML>\n<html>";
  255. content += "Failed to connect to AP ";
  256. content += qsid;
  257. content += ", try again.</html>";
  258. } else {
  259. content = "<!DOCTYPE HTML><html>";
  260. content += "Error, no ssid or password set?</html>";
  261. Serial.println("Sending 404");
  262. httpstatus = 404;
  263. }
  264. server.send(httpstatus, "text/html", content);
  265. }
  266.  
  267. void handleRoot() {
  268. IPAddress ip = WiFi.localIP();
  269. String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
  270. uint8_t mac[6];
  271. WiFi.macAddress(mac);
  272. String macStr = macToStr(mac);
  273. content = "<!DOCTYPE HTML>\n<html>Hello from ";
  274. content += ssid;
  275. content += " at ";
  276. content += ipStr;
  277. content += " (";
  278. content += macStr;
  279. content += ")";
  280. content += "</html>";
  281. server.send(200, "text/html", content);
  282. }
  283.  
  284. void handleAccessPointAlreadySet() {
  285. content = "<!DOCTYPE HTML>\n<html>";
  286. content += "You already set up the access point and it is working if you got this far.";
  287. content += "</html>";
  288. server.send(200, "text/html", content);
  289. }
  290.  
  291. void handleNotFound() {
  292. content = "File Not Found\n\n";
  293. content += "URI: ";
  294. content += server.uri();
  295. content += "\nMethod: ";
  296. content += (server.method() == HTTP_GET) ? "GET" : "POST";
  297. content += "\nArguments: ";
  298. content += server.args();
  299. content += "\n";
  300. for (uint8_t i = 0; i < server.args(); i++) {
  301. content += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  302. }
  303. server.send(404, "text/plain", content);
  304. }
  305. String macToStr(const uint8_t* mac)
  306. {
  307. String result;
  308. for (int i = 0; i < 6; ++i) {
  309. result += String(mac[i], 16);
  310. if (i < 5)
  311. result += ':';
  312. }
  313. return result;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement