# Wifi access point using NodeMCU and ESP8266.

**URL:** <https://forum.arduino.cc/t/wifi-access-point-using-nodemcu-and-esp8266/695746>\
**Category:** Programming\
**Created:** [February 12, 2021, 2:40am UTC](https://forum.arduino.cc/t/wifi-access-point-using-nodemcu-and-esp8266/695746 "2021-02-12T02:40:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![terryn](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@terryn](https://forum.arduino.cc/u/terryn)\
**Post date:** [February 12, 2021, 2:40am UTC](https://forum.arduino.cc/t/wifi-access-point-using-nodemcu-and-esp8266/695746/1 "2021-02-12T02:40:47Z")

</div>

Good Day All,

I was able to get the following sketch to install into this NodeMCU that I am using to on a train set I am 3D printing for my grandson.

I can connect my android phone to it using WIFI but when I use a web browser and type in the ip address of 192.168.4.1 a message comes up saying the server cannot display the page.

I have never owned a Arduino I am just trying to follow directions to get this working for my grandson.

Please review the sketch and let me know what the problem is.

Thanks

Terry N  
\*

- Wifi accesspoitn using NodeMCU and ESP8266.
- 
- This software is provided free and you may use, change and distribute as you like.
- 

\*/

#include \<ESP8266WiFi.h\>  
#include \<WiFiClient.h\>  
#include \<ESP8266WebServer.h\>  
#include "TimerObject.h"

#define forward 0  
#define backward 1  
/\* Set these to your desired credentials. \*/  
const char \*ssid = "OSRHector";  
const char \*password = "osrailway";

bool highBeamActive = false;  
bool motionActive = false;  
bool motionDirection = forward;  
int target\_speed;  
int actual\_speed;  
int acceleration\_step = 5;

int LED\_BL2 = 15; // Red light on side 2 GPIO15  
int LED\_BL1 = 13; // Red light on side 1 GPIO13  
int LED\_HB2 = 14; // High beam side 2 GPIO12  
int LED\_HB1 = 12; // High beam side 1 GPIO14

int LED\_HDL = 2; //PWM for headlights controlled by motor board

int motor\_AIN1 = 5; //GPIO5  
int motor\_AIN2 = 4; //GPIO4  
int motor\_PWM = 0; //GPIO0

TimerObject \*timer1 = new TimerObject(10);

ESP8266WebServer server(80);

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\*

- Server IP address is: [http://192.168.4.1](http://192.168.4.1)
- 
- When you have connected your device to the wifi network, open the browser and enter the IP address above.
- 

_/  
void handleRoot() {  
/_\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- This is the web page sent to the connected device. There are lots of memory available so this page can be made much more complex.
- It uses Ajax to send commands which means the page doesn't need to be reloaded each time a button is pressed. In this way,
- the page can be made much more graphically complex without suffering from long reloading delays.  
\*/  
String html =" \<style type="text/css"\> body { background: #595B82; } #header { height: 30px; margin-left: 2px; margin-right: 2px; background: #d5dfe2; border-top: 1px solid #FFF; border-left: 1px solid #FFF; border-right: 1px solid #333; border-bottom: 1px solid #333; border-radius: 5px; font-family: "arial-verdana", arial; padding-top: 10px; padding-left: 20px; } #speed\_setting { float: right; margin-right: 10px; } #control { height: 200px; margin-top: 4px; margin-left: 2px; margin-right: 2px; background: #d5dfe2; border-top: 1px solid #FFF; border-left: 1px solid #FFF; border-right: 1px solid #333; border-bottom: 1px solid #333; border-radius: 5px; font-family: "arial-verdana", arial; } .button { border-top: 1px solid #FFF; border-left: 1px solid #FFF; border-right: 1px solid #333; border-bottom: 1px solid #333; border-radius: 5px; margin: 5px; text-align: center; float: left; padding-top: 20px; height: 50px; background: #FFF} .long { width: 30%; } .short { width: 100px; } #message\_box { float: left; margin-bottom: 0px; width: 100%; } #speed\_slider {width: 300px;} \<script type="text/javascript"\> function updateSpeed(speed) { sendData("updateSpeed?speed="+speed); } function runForward(){ var speed = document.getElementById("speed\_slider").value; sendData("run?dir=forward&speed="+speed); } function runBackward(){ var speed = document.getElementById("speed\_slider").value; sendData("run?dir=backward&speed="+speed); } function sendData(uri){ var messageDiv = document.getElementById("message\_box"); messageDiv.innerHTML = "192.168.4.1/"+uri; var xhr = new XMLHttpRequest(); xhr.open('GET', '[http://192.168.4.1/'+uri](http://192.168.4.1/'+uri)); xhr.withCredentials = true; xhr.setRequestHeader('Content-Type', 'text/plain'); xhr.send(); } \<div id="header"\> OS-Railway Wifi Hectorrail 141 \<div id="speed\_setting"\> Speed \<input id="speed\_slider" type="range" min="0" max="1023" step="10" value="512" onchange="updateSpeed(this.value)"\> \<div id="control"\> \<div id="button\_backward" class="button long" onclick="runBackward()"\> Run backward \<div id="button\_stop" class="button long" onclick="updateSpeed(0)"\> Stop \<div id="button\_forward" class="button long" onclick="runForward()"\> Run forward \<div id="button\_light\_off" class="button long" onclick="sendData('lightoff')"\> Headlight off \<div id="button\_light\_on" class="button long" onclick="sendData('lighton')"\> Headlight on \<div id="button\_emergency" class="button long" onclick="sendData('stop')"\> Emergency stop \<div id="message\_box"\> ";  
server.send(200, "text/html", html);  
}

void motionControl(){  
if(motionActive){  
if(actual\_speed \< target\_speed){  
actual\_speed = actual\_speed + acceleration\_step;  
} else if(actual\_speed \> target\_speed){  
actual\_speed = actual\_speed - acceleration\_step;  
}  
if(actual\_speed \> 1023){  
actual\_speed = 1023;  
}  
if(actual\_speed \< 0){  
actual\_speed = 0;  
}  
analogWrite(motor\_PWM, actual\_speed);  
} else {  
analogWrite(motor\_PWM, 0);  
}  
}

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- Motor operation  
\*/

void motorOperation(){

motionActive = true;  
String move\_dir = server.arg("dir");  
String motion\_speed = server.arg("speed");  
target\_speed = motion\_speed.toInt();

//Execute change of direction, but only if actual\_speed is below 100, otherwise set target speed to 0 and initiate a slowdown.  
if(move\_dir == "backward"){  
if(motionDirection != backward && actual\_speed \> 100){  
target\_speed = 0;  
} else {  
motionDirection = backward;  
}  
} else {  
if(motionDirection != forward && actual\_speed \> 100){  
target\_speed = 0;  
} else {  
motionDirection = forward;  
}  
}

digitalWrite(motor\_AIN1, motionDirection);  
digitalWrite(motor\_AIN2, !motionDirection);  
digitalWrite(LED\_BL2, motionDirection);  
digitalWrite(LED\_BL1, !motionDirection);

if(highBeamActive){  
digitalWrite(LED\_HB2, motionDirection);  
digitalWrite(LED\_HB1, !motionDirection);  
} else {  
digitalWrite(LED\_HB2, 0);  
digitalWrite(LED\_HB1, 0);  
}

}

void updateSpeed(){  
String motion\_speed = server.arg("speed");  
target\_speed = motion\_speed.toInt();  
}

void switchLightOn(){  
highBeamActive = true;  
digitalWrite(LED\_HB2, motionDirection);  
digitalWrite(LED\_HB1, !motionDirection);  
}

void switchLightOff(){  
highBeamActive = false;  
digitalWrite(LED\_HB2, 0);  
digitalWrite(LED\_HB1, 0);  
}

void motionStop(){  
motionActive = false;  
analogWrite(motor\_PWM, 0);  
}

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- SETUP  
\*/

void setup() {  
delay(1000);

pinMode(LED\_BL2, OUTPUT);  
pinMode(LED\_BL1, OUTPUT);  
pinMode(LED\_HB2, OUTPUT);  
pinMode(LED\_HB1, OUTPUT);

pinMode(LED\_HDL, OUTPUT);

pinMode(motor\_AIN1, OUTPUT);  
pinMode(motor\_AIN2, OUTPUT);  
pinMode(motor\_PWM, OUTPUT);

digitalWrite(LED\_BL2, 0);  
digitalWrite(LED\_BL1, 0);  
digitalWrite(LED\_HB2, 0);  
digitalWrite(LED\_HB1, 0);

digitalWrite(motor\_AIN1, 0);  
digitalWrite(motor\_AIN2, 1);

analogWrite(motor\_PWM, 0);  
analogWrite(LED\_HDL, 1023);

timer1-\>setOnTimer(&motionControl);  
timer1-\>Start();

WiFi.softAP(ssid, password);

IPAddress myIP = WiFi.softAPIP();

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

- Here you find the functions that are called when the browser sends commands.  
\*/  
server.on("/", handleRoot);  
server.on("/run", motorOperation);  
server.on("/stop", motionStop);  
server.on("/lighton", switchLightOn);  
server.on("/lightoff", switchLightOff);  
server.on("/updateSpeed", updateSpeed);  
server.begin();  
}

void loop() {  
server.handleClient();  
timer1-\>Update();  
}

---

<div class="post-metadata">

**Author:** ![SteveMann](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@SteveMann](https://forum.arduino.cc/u/SteveMann)\
**Post date:** [February 12, 2021, 4:22am UTC](https://forum.arduino.cc/t/wifi-access-point-using-nodemcu-and-esp8266/695746/2 "2021-02-12T04:22:59Z")

</div>

Please edit your post and use code tags.  
If you post your code as described in the _how to use this forum_ sticky, more forum members will read it.

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [February 12, 2021, 8:41am UTC](https://forum.arduino.cc/t/wifi-access-point-using-nodemcu-and-esp8266/695746/3 "2021-02-12T08:41:19Z")

</div>

When i compile, upload, connect and type 192.168.4.1 i get send to the root of the page. There is nothing wrong with the sketch (except maybe for the first '/' that starts the comments, but that may have dropped out with the copy), but maybe you should try turning 'off' your data 4g or 5g connection so your phone will only be looking for a page on wifi and not on the internet. And please as in reply#1

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 12, 2021, 8:42am UTC](https://forum.arduino.cc/t/wifi-access-point-using-nodemcu-and-esp8266/695746/4 "2021-06-12T08:42:07Z")

</div>

Closed on June 12, 2021, 8:42am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
