daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest Dec 23rd, 2017 302 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Created by edwin martin on the 03/09/2017 last modified 21/12/2017
  2. // All right reserved, copyright 23/12/2017
  3. // This code is not to be used commercially with out permission.
  4. // contact husqvarna1990@yahoo.com
  5.  
  6.  
  7. #include <WiFiEsp.h>
  8. #include "SoftwareSerial.h"
  9.  
  10. SoftwareSerial Serial1(6, 7); // RX, TX
  11.  
  12.  
  13. char ssid[] = "ESP8266_AP";      // your Access point  SSID (name)
  14. char pass[] = "Password1";       // your Access point password
  15. char Bssid[60];
  16. char Bpass[60];
  17. int status = WL_IDLE_STATUS;     // the Wifi radio's status
  18. int reqCount = 0;                // number of requests received
  19. byte buffer[100];
  20. String output = "";
  21. String output2 = "";
  22. boolean loops = true;
  23. float temp, hum;
  24. int fan1, fan2;
  25. WiFiEspServer server(80);
  26. void setup()
  27. {
  28.   Serial.begin(115200);   // initialize serial for debugging
  29.   Serial1.begin(9600);    // initialize serial for ESP module
  30.   WiFi.init(&Serial1);    // initialize ESP module
  31.   // check for the presence of the shield
  32.   if (WiFi.status() == WL_NO_SHIELD) {
  33.     Serial.println(F("WiFi shield not present"));
  34.     while (true); // don't continue
  35.   }
  36.   Serial.print(F("Attempting to start AP "));
  37.   Serial.println(ssid);
  38.   IPAddress localIp(192, 168, 4, 1); // uncomment these two lines if you want to set the IP address of the AP
  39.   WiFi.configAP(localIp);
  40.   status = WiFi.beginAP(ssid);// start access point
  41.   Serial.println(F("Access point started"));
  42.   printWifiStatus();
  43.   // start the web server on port 80
  44.   server.begin();
  45.   Serial.println(F("Server started"));
  46.   while (loops) {
  47.     WiFiEspClient client = server.available();  // listen for incoming clients
  48.     if (client) {
  49.       int ctr = 0;
  50.       buffer[100] = "";
  51.       //Serial.println("Client connected");
  52.       // an http request ends with a blank line
  53.       boolean currentLineIsBlank = true;
  54.       while (client.connected()) {
  55.         while (client.available()) {
  56.           char c = client.read();
  57.           // if you've gotten to the end of the line (received a newline
  58.           // character) and the line is blank, the http request has ended,
  59.           // so you can send a reply
  60.           if (c == 'n' && currentLineIsBlank) {
  61.             // Here is where the POST data is.
  62.             Serial.println(F("Post data: "));
  63.             while (client.available())
  64.             {
  65.               buffer[ctr] = client.read();
  66.               ctr++;
  67.             }
  68.  
  69.             int datactr = 0;
  70.             int andctr = 0;
  71.  
  72.             for (int x = 0; x < ctr; x++) {
  73.  
  74.               if (buffer[x] == '=') {
  75.                 if (datactr < 1) {
  76.                   Serial.print(F("User: "));
  77.                 }
  78.                 datactr++;
  79.                 x++;
  80.               }
  81.               if (buffer[x] == '&') {
  82.                 andctr++;
  83.                 x++;
  84.                 Serial.print(F("nPassword: "));
  85.               }
  86.               if (datactr == 1 && andctr == 0) {
  87.                 Serial.write(buffer[x]);
  88.                 output += char(buffer[x]);
  89.               }
  90.               if (datactr == 2 && andctr == 1) {
  91.                 Serial.write(buffer[x]);
  92.                 output2 += char(buffer[x]);
  93.               }
  94.             }
  95.             output.toCharArray(Bssid, 50);
  96.             Serial.print(F("nSSID: "));
  97.             Serial.println(Bssid);
  98.             output2.toCharArray(Bpass, 50);
  99.             Serial.print(F("nPASS: "));
  100.             Serial.println(Bpass);
  101.             Serial.println();
  102.             //Serial.println("Sending response");
  103.             // send a standard http response header
  104.             client.println(F("HTTP/1.0 200 OK"));
  105.             client.println(F("Content-Type: text/html"));
  106.             client.println(F("Connection: keep-alive"));
  107.             client.println();
  108.             //form added to send data from browser and view received data in serial monitor
  109.             client.println();
  110.             client.println(F("<!DOCTYPE html>"));
  111.             client.println(F("<html lang="en"><head><style>body{background-color:#eef;}"));
  112.             client.println(F("h1{text-align:center;font-family:sans-serif;font-size:72px;}"));
  113.             client.println(F("p{text-align:center;font-family:sans-serif;font-size:22px;padding-top:12px; margin-bottom:0px;}"));
  114.             client.println(F("#dl{width:500px;height:260px;margin:70px auto;background-color:#d0d0e1;display: table;}"));
  115.             client.println(F("</style></head><body><h1>Configure WiFi</h1>"));
  116.             client.println(F("<div id="dl"><p>Please enter your wifi SSID and Password</p>"));
  117.             client.println(F("<form action="" method="POST"><p>"));
  118.             client.println(F("<label for="username">SSID:</label>"));
  119.             client.println(F("<input type="text" name="uname"><br><br>"));
  120.             client.println(F("<label for="password">Password:</label>"));
  121.             client.println(F("<input type="password" name="pwd"><br><br>"));
  122.             client.println(F("<input type="submit" value="Submit"></p></form>"));
  123.             if (output != "") {
  124.               client.println(F("<p>SSID and password changed.</p>"));
  125.               loops = false;
  126.             }
  127.             client.println(F("</div></body>"));
  128.             client.println(F("</html>"));
  129.             client.println();
  130.             client.stop();
  131.           }
  132.           else if (c == 'n') {
  133.             // you're starting a new line
  134.             currentLineIsBlank = true;
  135.           }
  136.           else if (c != 'r') {
  137.             // you've gotten a character on the current line
  138.             currentLineIsBlank = false;
  139.           }
  140.         }
  141.       }
  142.       Serial.println(F("Disconnected"));
  143.       if (!loops){
  144.      int status = WL_IDLE_STATUS;
  145.      network();
  146.       }
  147.     }
  148.   }
  149. }
  150.  
  151. void network() {
  152.   Serial1.println(F("AT+RST"));
  153.   delay(5000);
  154.   WiFi.init(&Serial1);    // initialize ESP module
  155.   // check for the presence of the shield
  156.   if (WiFi.status() == WL_NO_SHIELD) {
  157.     Serial.println(F("WiFi shield not present"));
  158.     while (true); // don't continue
  159.   }
  160.   // attempt to connect to WiFi network
  161.   while ( status != WL_CONNECTED) {
  162.     Serial.print(F("Attempting: "));
  163.     Serial.println(Bssid);
  164.     // Connect to WPA/WPA2 network
  165.     status = WiFi.begin(Bssid, Bpass);
  166.   }
  167.   Serial.println(F("You're connected"));
  168.   printWifiStatus();
  169.   server.begin();
  170. }
  171.  
  172. void loop()
  173. {
  174.   temp = random(1, 30);
  175.   hum = random(30, 99);
  176.   fan1 = random(10, 99);
  177.   fan2 = random(10, 99);
  178.   WiFiEspClient client = server.available();
  179.   if (client) {
  180.     Serial.println(F("New client"));
  181.     boolean currentLineIsBlank = true;
  182.     while (client.connected()) {
  183.       if (client.available()) {
  184.         char c = client.read();
  185.         Serial.write(c);
  186.         if (c == 'n' && currentLineIsBlank) {
  187.           Serial.println(F("Sending response"));
  188.           client.print(F(
  189.             "HTTP/1.1 200 OKrn"
  190.             "Content-Type: text/htmlrn"
  191.             "Connection: closern"
  192.             "Refresh: 120rn"
  193.             "rn"));
  194.           client.print(F("<!DOCTYPE HTML>rn"));
  195.           client.print(F("<html><head>rn"));
  196.           client.print(F("<link rel='stylesheet' href='http://www.southamptonstudentrooms.com/ESP.css'>"));
  197.           client.print(F("</head><body><h1>Aerium Botanix V1.0</h1><div id="di">"));
  198.           client.print(F("<p>These values will refresh every two minutes</p>"));
  199.           client.print(F("<div class="div1"><h2>Temp<br>R/H<br>Fan 1<br>Fan 2</h2></div>"));
  200.           client.print("<div class="div2"><h2>"+String(temp)+"&#x2103<br>"+String(hum)+"%<br>"+String(fan1)+"%<br>"+String(fan2)+"%</h2>");
  201.           client.print(F("</div></div></body>"));
  202.           client.print(F("</html>rn"));
  203.           break;
  204.         }
  205.         if (c == 'n') {
  206.           // you're starting a new line
  207.           currentLineIsBlank = true;
  208.         }
  209.         else if (c != 'r') {
  210.           // you've gotten a character on the current line
  211.           currentLineIsBlank = false;
  212.         }
  213.       }
  214.     }
  215.     delay(10);
  216.     client.stop();
  217.     Serial.println(F("Client disconnected"));
  218.   }
  219. }
  220.  
  221. void printWifiStatus()
  222. {
  223.   // print your WiFi shield's IP address
  224.   IPAddress ip = WiFi.localIP();
  225.   Serial.print(F("IP Address: "));
  226.   Serial.println(ip);
  227.   Serial.println();
  228. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top