Nodemcu how to not hard code ssid for wifi


(Nick) #1

I am trying to program a nodemcu but the code i have requires me to hard code in the ssid and password. Does anyone know how to edit the code so that it does not require hard code. so that i can take this to different networks and somehow using an app or something can select the network to connect to and then enter the password?

If someone has code to help me with this I would greatly appreciate it. Here is the code I have thru the ssid and password

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Servo.h>

#define WLAN_SSID “ssid” //change to your Wifi SSID
#define WLAN_PASS “password” //change to your Wifi Password

MDNSResponder mdns;
ESP8266WebServer server(80);

int servoPin = 0; //Servo on GPIO0 or NODEMCU pin D3
const int buttonPin = 12; //manual button on GPIO12 or NODEMCU pin D6
int buttonState = 0;
int direction = 0;


(Andreas A.) #2

Use the WiFiManager package.


(Nick) #5

so I am using the nodemcu esp8266 and am using the code below but i can not get it to show up as a network what am I doing wrong?

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <DNSServer.h>            //Local DNS Server used for redirecting all requests to the configuration portal
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Servo.h>


MDNSResponder mdns;
ESP8266WebServer server(80);

Servo myservo;
int pos = 0;
int state = 0;
int prevstate = 0;
int dirc = 0;
int servoPin = D3; //CHANGE TO WHATEVER PIN YOUR USING

String webPage = "";

void setup(void) {
  webPage += "<h1>ESP8266 Web Server</h1><p>Blinds <a href=\"open\"><button>OPEN</button></a>&nbsp;<a href=\"close\"><button>CLOSE</button></a></p>";

  delay(1000);
  Serial.begin(9600);
  delay(500);
  Serial.println("Blind Startup Sequence");
  delay(500);
  Serial.println();
  Serial.print("Connecting to ");
 WiFiManager wifiManager;
 wifiManager.autoConnect("blackjack");

  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  if (mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("MDNS responder started");
  }

  server.on("/", []() {
    server.send(200, "text/html", webPage);
  });
  server.on("/open", []() {
    server.send(200, "text/html", webPage);
    Serial.println("HTTP OPEN COMMAND RECEIVED");
    dirc = 0;  // direction for servo to run
    state = 2; // sets current state
  });
  server.on("/close", []() {
    server.send(200, "text/html", webPage);
    Serial.println("HTTP CLOSE COMMAND RECEIVED");
    dirc = 180; // direction for servo to run
    state = 1;  // sets current state
  });
  server.begin();
  Serial.println("HTTP server started");
}

void servo_move() {
  Serial.println("State Change. Rotating Servo");
  if ( dirc == 180) {
    myservo.attach(servoPin);              // energize servo
    delay(50);
    for (pos = 0; pos <= 90; pos += 1) {   // goes from 0 degrees to 90 degrees in steps of 1 degree CHANGE 90 TO MATCH ANGLE OF TILT DESIRED
      myservo.write(pos);                  // tell servo to go to position in variable 'pos'
      delay(30);                           // waits 30ms between each degree to slow rotation speed
    }
    delay(50);
    myservo.detach();                      // movement finished so detach servo to conserve power
  }
  else if (dirc == 0) {
    myservo.attach(servoPin);              // energize servo
    delay(50);
    for (pos = 90; pos >= 0; pos -= 1) {   // goes from 90 degrees to 0 degrees in steps of 1 degree CHANGE 90 TO MIRROR ANGLE OF TILT DESIRED ABOVE
      myservo.write(pos);                  // tell servo to go to position in variable 'pos'
      delay(30);                           // waits 30ms between each degree to slow rotation speed
    }
    delay(50);
    myservo.detach();                      // movement finished so detach servo to conserve power
  }

  Serial.println("Returning to main loop");
  return;
}

void loop(void) {
  if (state != prevstate) {
    Serial.println("State change!");
    servo_move();
  }
  prevstate = state;
  server.handleClient();
}

(Andreas A.) #6

At first glance I don’t see anything wrong with this. Did you try running one of the WifiManager examples to see whether they work as expected? If so, there is something different between your code and the examples…


(Nick) #7

I had first ran static ssid and password in my code and that worked.
then i changed the code for the wifimanager per your suggestion but I have never been able to get the nodemcu to show with the wifimanager, do i need to somehow clear the nodemcu?


(Andreas A.) #8

The chip remembers the last network it connected to - if you successfully connected before, it most likely will reconnect even after changing the code. Try putting

ESP.eraseConfig();

at the beginning of your setup(). Not sure whether you need to reboot after that.


(Nick) #9

That worked to fix the saved login. and it started the AP function, then from my phone i connected to it no problem and then selected my ssid from the list and password. after I did that

how do i then access the code i have on my chip to open and close my servo?

the serial monitor is showing this below but the 192.168.1.133 does not load do i need something in my code somewhere to refrerence a new ip for the controls like 192.168.5.1 or something?

*WM: Sent config page
*WM: Request redirected to captive portal
*WM: Handle root
*WM: Request redirected to captive portal
*WM: Handle root
*WM: Request redirected to captive portal
*WM: Handle root
*WM: WiFi save
*WM: Sent wifi save page
*WM: Connecting to new AP
*WM: Connecting as wifi client…
*WM: Connection result:
*WM: 3

WiFi connected
IP address:
192.168.1.133
MDNS responder started
HTTP server started