Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Created by edwin martin on the 03/09/2017 last modified 21/12/2017
- // All right reserved, copyright 23/12/2017
- // This code is not to be used commercially with out permission.
- // contact husqvarna1990@yahoo.com
- #include <WiFiEsp.h>
- #include "SoftwareSerial.h"
- SoftwareSerial Serial1(6, 7); // RX, TX
- char ssid[] = "ESP8266_AP"; // your Access point SSID (name)
- char pass[] = "Password1"; // your Access point password
- char Bssid[60];
- char Bpass[60];
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- int reqCount = 0; // number of requests received
- byte buffer[100];
- String output = "";
- String output2 = "";
- boolean loops = true;
- float temp, hum;
- int fan1, fan2;
- WiFiEspServer server(80);
- void setup()
- {
- Serial.begin(115200); // initialize serial for debugging
- Serial1.begin(9600); // initialize serial for ESP module
- WiFi.init(&Serial1); // initialize ESP module
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println(F("WiFi shield not present"));
- while (true); // don't continue
- }
- Serial.print(F("Attempting to start AP "));
- Serial.println(ssid);
- IPAddress localIp(192, 168, 4, 1); // uncomment these two lines if you want to set the IP address of the AP
- WiFi.configAP(localIp);
- status = WiFi.beginAP(ssid);// start access point
- Serial.println(F("Access point started"));
- printWifiStatus();
- // start the web server on port 80
- server.begin();
- Serial.println(F("Server started"));
- while (loops) {
- WiFiEspClient client = server.available(); // listen for incoming clients
- if (client) {
- int ctr = 0;
- buffer[100] = "";
- //Serial.println("Client connected");
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- while (client.available()) {
- char c = client.read();
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == 'n' && currentLineIsBlank) {
- // Here is where the POST data is.
- Serial.println(F("Post data: "));
- while (client.available())
- {
- buffer[ctr] = client.read();
- ctr++;
- }
- int datactr = 0;
- int andctr = 0;
- for (int x = 0; x < ctr; x++) {
- if (buffer[x] == '=') {
- if (datactr < 1) {
- Serial.print(F("User: "));
- }
- datactr++;
- x++;
- }
- if (buffer[x] == '&') {
- andctr++;
- x++;
- Serial.print(F("nPassword: "));
- }
- if (datactr == 1 && andctr == 0) {
- Serial.write(buffer[x]);
- output += char(buffer[x]);
- }
- if (datactr == 2 && andctr == 1) {
- Serial.write(buffer[x]);
- output2 += char(buffer[x]);
- }
- }
- output.toCharArray(Bssid, 50);
- Serial.print(F("nSSID: "));
- Serial.println(Bssid);
- output2.toCharArray(Bpass, 50);
- Serial.print(F("nPASS: "));
- Serial.println(Bpass);
- Serial.println();
- //Serial.println("Sending response");
- // send a standard http response header
- client.println(F("HTTP/1.0 200 OK"));
- client.println(F("Content-Type: text/html"));
- client.println(F("Connection: keep-alive"));
- client.println();
- //form added to send data from browser and view received data in serial monitor
- client.println();
- client.println(F("<!DOCTYPE html>"));
- client.println(F("<html lang="en"><head><style>body{background-color:#eef;}"));
- client.println(F("h1{text-align:center;font-family:sans-serif;font-size:72px;}"));
- client.println(F("p{text-align:center;font-family:sans-serif;font-size:22px;padding-top:12px; margin-bottom:0px;}"));
- client.println(F("#dl{width:500px;height:260px;margin:70px auto;background-color:#d0d0e1;display: table;}"));
- client.println(F("</style></head><body><h1>Configure WiFi</h1>"));
- client.println(F("<div id="dl"><p>Please enter your wifi SSID and Password</p>"));
- client.println(F("<form action="" method="POST"><p>"));
- client.println(F("<label for="username">SSID:</label>"));
- client.println(F("<input type="text" name="uname"><br><br>"));
- client.println(F("<label for="password">Password:</label>"));
- client.println(F("<input type="password" name="pwd"><br><br>"));
- client.println(F("<input type="submit" value="Submit"></p></form>"));
- if (output != "") {
- client.println(F("<p>SSID and password changed.</p>"));
- loops = false;
- }
- client.println(F("</div></body>"));
- client.println(F("</html>"));
- client.println();
- client.stop();
- }
- else if (c == 'n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != 'r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- Serial.println(F("Disconnected"));
- if (!loops){
- int status = WL_IDLE_STATUS;
- network();
- }
- }
- }
- }
- void network() {
- Serial1.println(F("AT+RST"));
- delay(5000);
- WiFi.init(&Serial1); // initialize ESP module
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println(F("WiFi shield not present"));
- while (true); // don't continue
- }
- // attempt to connect to WiFi network
- while ( status != WL_CONNECTED) {
- Serial.print(F("Attempting: "));
- Serial.println(Bssid);
- // Connect to WPA/WPA2 network
- status = WiFi.begin(Bssid, Bpass);
- }
- Serial.println(F("You're connected"));
- printWifiStatus();
- server.begin();
- }
- void loop()
- {
- temp = random(1, 30);
- hum = random(30, 99);
- fan1 = random(10, 99);
- fan2 = random(10, 99);
- WiFiEspClient client = server.available();
- if (client) {
- Serial.println(F("New client"));
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- Serial.write(c);
- if (c == 'n' && currentLineIsBlank) {
- Serial.println(F("Sending response"));
- client.print(F(
- "HTTP/1.1 200 OKrn"
- "Content-Type: text/htmlrn"
- "Connection: closern"
- "Refresh: 120rn"
- "rn"));
- client.print(F("<!DOCTYPE HTML>rn"));
- client.print(F("<html><head>rn"));
- client.print(F("<link rel='stylesheet' href='http://www.southamptonstudentrooms.com/ESP.css'>"));
- client.print(F("</head><body><h1>Aerium Botanix V1.0</h1><div id="di">"));
- client.print(F("<p>These values will refresh every two minutes</p>"));
- client.print(F("<div class="div1"><h2>Temp<br>R/H<br>Fan 1<br>Fan 2</h2></div>"));
- client.print("<div class="div2"><h2>"+String(temp)+"℃<br>"+String(hum)+"%<br>"+String(fan1)+"%<br>"+String(fan2)+"%</h2>");
- client.print(F("</div></div></body>"));
- client.print(F("</html>rn"));
- break;
- }
- if (c == 'n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != 'r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- delay(10);
- client.stop();
- Serial.println(F("Client disconnected"));
- }
- }
- void printWifiStatus()
- {
- // print your WiFi shield's IP address
- IPAddress ip = WiFi.localIP();
- Serial.print(F("IP Address: "));
- Serial.println(ip);
- Serial.println();
- }
Add Comment
Please, Sign In to add comment