So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By surya arief
#67937 I want, if wifi connect to esp, then led is on,
if not connect, then led is off
but this below code not work, and ssid cannot scaned.
iam use NodeMcu V3.

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;

const int led = 7; //pin on GPIO13 connected to LED

void setup(){
pinMode(led, OUTPUT);
digitalWrite(led, LOW); //turn off led

// set Wifi SSID and password
WiFiMulti.addAP("mywifi", "12345678");
}
void loop(){

// scanning wifi with SSID and password above
if ((WiFiMulti.run() == WL_CONNECTED))
{ digitalWrite(ledA, HIGH); //turn on led }
else
{digitalWrite(ledA, LOW); //turn off led }}
User avatar
By atexit8
#68004 This code works for me.




Code: Select all#include <ESP8266WiFi.h>
 
const char* ssid = "ssid";
const char* password = "password";
 

void setup() {
  Serial.begin(9600);
  delay(10);
 
 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

   WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  //put your LED code here


}
 
void loop() {
 }
 

User avatar
By surya arief
#68013 Code above is esp8266 as client not accesspoint, if esp8266 connect to hotspot, then led is on.

is possible if i want esp8266 as accesspoint, not as client?
So if there is client connect to esp8266, then led is on.
User avatar
By schufti
#68022
const int led = 7; //pin on GPIO13 connected to LED


are you decided yet if the LED is on gpio 7 or 13 ?

If you intend to use nodemcu pin marked "D7" you should write
const int led = D7
or alternatively
const int led = 13

maybe that will make your code work ....