Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- November 25, 2019
- nodemcu_experiment_005
- NodeMCU stand-alone hot spot/web server to control I/O pins
- Code copied from:
- https://lastminuteengineers.com/creating-esp8266-web-server-arduino-ide/
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- Notes:
- 1. This sketch is for a NodeMCU board.
- 2. It allows you to control two of the I/O pins, using the web browser on a mobile phone.
- 3. The NodeMCU board acts as a stand-alone hot spot, and serves a web page with buttons on it that you view in your phone's browser.
- The page text should change when you change the button states.
- 4. Once the sketch is loaded on the NodeMCU and the NodeMCU is running, this is how you connect to it:
- A. You open your phone's wifi control panel, and you should see the name that is contained in the variable 'ssid' below.
- B. When you try to connect, this sketch asks for a password. That is shown below in the variable 'password'.
- C. Once connected, you enter the address { 192.168.1.1 } into your phone's browser address bar and hit [go/enter] in the browser. Then you should see the web page.
- 5. You can store the NodeMCU wifi info in your phone's wifi settings list, so that you don't need to re-enter it again.
- 6. You can store the home page address (192.168.1.1) as a bookmark in your phone's browser, so that you don't need to re-enter it again.
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- */
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- /// Put your SSID & Password (this is the hotspot name and password that you want the NodeMCU to have)
- const char* ssid = "NodeMCU"; // Enter SSID here
- const char* password = "12345678"; //Enter Password here
- /// Put IP Address details
- IPAddress local_ip(192, 168, 1, 1);
- IPAddress gateway(192, 168, 1, 1);
- IPAddress subnet(255, 255, 255, 0);
- ESP8266WebServer server(80);
- uint8_t LED1pin = D7;
- bool LED1status = LOW;
- uint8_t LED2pin = D6;
- bool LED2status = LOW;
- void setup() {
- Serial.begin(115200);
- pinMode(LED1pin, OUTPUT);
- pinMode(LED2pin, OUTPUT);
- WiFi.softAP(ssid, password);
- WiFi.softAPConfig(local_ip, gateway, subnet);
- delay(100);
- server.on("/", handle_OnConnect);
- server.on("/led1on", handle_led1on);
- server.on("/led1off", handle_led1off);
- server.on("/led2on", handle_led2on);
- server.on("/led2off", handle_led2off);
- server.onNotFound(handle_NotFound);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop() {
- server.handleClient();
- if (LED1status)
- {
- digitalWrite(LED1pin, HIGH);
- }
- else
- {
- digitalWrite(LED1pin, LOW);
- }
- if (LED2status)
- {
- digitalWrite(LED2pin, HIGH);
- }
- else
- {
- digitalWrite(LED2pin, LOW);
- }
- }
- void handle_OnConnect() {
- LED1status = LOW;
- LED2status = LOW;
- Serial.println("GPIO7 Status: OFF | GPIO6 Status: OFF");
- server.send(200, "text/html", SendHTML(LED1status, LED2status));
- }
- void handle_led1on() {
- LED1status = HIGH;
- Serial.println("GPIO7 Status: ON");
- server.send(200, "text/html", SendHTML(true, LED2status));
- }
- void handle_led1off() {
- LED1status = LOW;
- Serial.println("GPIO7 Status: OFF");
- server.send(200, "text/html", SendHTML(false, LED2status));
- }
- void handle_led2on() {
- LED2status = HIGH;
- Serial.println("GPIO6 Status: ON");
- server.send(200, "text/html", SendHTML(LED1status, true));
- }
- void handle_led2off() {
- LED2status = LOW;
- Serial.println("GPIO6 Status: OFF");
- server.send(200, "text/html", SendHTML(LED1status, false));
- }
- void handle_NotFound() {
- server.send(404, "text/plain", "Not found");
- }
- String SendHTML(uint8_t led1stat, uint8_t led2stat) {
- String ptr = "<!DOCTYPE html> <html>\n";
- ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
- ptr += "<title>LED Control</title>\n";
- ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
- ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
- ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
- ptr += ".button-on {background-color: #1abc9c;}\n";
- ptr += ".button-on:active {background-color: #16a085;}\n";
- ptr += ".button-off {background-color: #34495e;}\n";
- ptr += ".button-off:active {background-color: #2c3e50;}\n";
- ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
- ptr += "</style>\n";
- ptr += "</head>\n";
- ptr += "<body>\n";
- ptr += "<h1>ESP8266 Web Server</h1>\n";
- ptr += "<h3>Using Access Point(AP) Mode</h3>\n";
- if (led1stat)
- {
- ptr += "<p>LED1 Status: ON</p><a class=\"button button-off\" href=\"/led1off\">OFF</a>\n";
- }
- else
- {
- ptr += "<p>LED1 Status: OFF</p><a class=\"button button-on\" href=\"/led1on\">ON</a>\n";
- }
- if (led2stat)
- {
- ptr += "<p>LED2 Status: ON</p><a class=\"button button-off\" href=\"/led2off\">OFF</a>\n";
- }
- else
- {
- ptr += "<p>LED2 Status: OFF</p><a class=\"button button-on\" href=\"/led2on\">ON</a>\n";
- }
- ptr += "</body>\n";
- ptr += "</html>\n";
- return ptr;
- }
Add Comment
Please, Sign In to add comment