I have an issue with WiFi.begin() in esp8266-12F.
I'm going to connect the ESP8266 with the specific Access Point in the loop() not in the setup().
I want if a specific AP is available, ESP8266 would connect to it.
In the below code, I supposed to connect to the "abc" AP and turns on an LED and if there is no connection, it turns the LED off, but WiFi.begin("abc", "123456789"); is not working.
What I have to do in this case?
setup(){
}
loop(){
if (WiFi.status() != WL_CONNECTED){
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin("abc", "123456789");
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
}
No point in adding WiFi-disconnect() if you're not connected to any AP at the moment. Just connect to the AP on the setup and leave on the loop() the if (WiFi.status() != WL_CONNECTED). The ESP reconnects itself to the AP when available.
setup(){
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setAutoConnect(true);
Serial.print("Connecting to ");
Serial.print(ssid);
int attempt = 0;
while(WiFi.status() != WL_CONNECTED && attempt<150){ //Connecting to Wi-Fi
delay(100);
Serial.print(".");
attempt++;
}
if(WiFi.status() == WL_CONNECTED){
Serial.println("");
Serial.println("WiFi Connected!");
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
}
if(attempt == 150){
Serial.println("Failed to connect to WiFi...");
}
}
loop(){
if(WiFi.status() != WL_CONNECTED){
digitalWrite(5,HIGH);
}else{
digitalWrite(5,LOW);
}
}
But for the love of good code otimization use a flag to prevent the digitalWrite to happen hundreds of times per second
I would use the standard code for building a WiFi connection in the setup() and just set the led as HIGH/LOW in the loop() according to WiFi.status(). Reconnect should be handled automatically...
Related
I am trying to connect my ESP32 to my Wifi Router using Arduino IDE but it is not connecting & giving a connection failed or disconnected status. I also confirmed it is scanning all the available Wifi Networks but not connecting to my router. I even tried with another ESP32 board but the problem is still there.
I tried this code below. This code would scan/give the available Wifi networks and it did. Also, I was expecting this code to run smoothly but my ESP32 won't connect to my Wifi router.
#include<WiFi.h>
const char *ssid = "my_SSID";
const char *password = "my_Password";
void setup()
{
Serial.begin(115200);
delay(2000);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");}
// Connect to my network.
WiFi.begin(ssid,password);
// Check Status of your WiFi Connection
int x = WiFi.status(); // If x=3 (Connected to Network) & If x=6 (Disconnected from Network)
Serial.print("WiFi Connection Status is ");
Serial.println(x);
while(WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("WiFi Connection Failed...");
WiFi.disconnect();
WiFi.reconnect(); }
//Print local IP address and start web server
Serial.println("\nConnecting");
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("ESP32 IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {}
1st image shows the output of my serial monitor. 2nd inamge shows the return value for WiFi.status function
Try this code:
#include<WiFi.h>
const char *ssid = "YourSSID";
const char *password = "YourPassword";
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
initWiFi();
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
}
void loop() {
// put your main code here, to run repeatedly:
}
I was just having this same issue. I found that, for me, the issue was not in the ESP32. It was the WiFi Router. I had the security on the router set to 'WEP' in the router. When I changed the security to 'WPA2-PSK' the ESP32 device connected right away.
few day I'm trying to connect SIM800L module and WeMos D1 R32 board. But any response from SIM module (SIM module is connected to GSM network, LED blink slow. But any response to commands Also I triedy SIM900). WIFI works fine.
Main task why I decided to use Wemosd D1 R32 and SIM module is connection to server by wifi check for (new) data and (if any new data occured) send message to phone. Maybe exist another better solution how handle this process. If someone has an better idea, let me know.
Ther is a part of code :
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
delay(1000);
while (!Serial);
tNow = millis(); //Set timer for connection
WiFi.begin(ssid, passphrase);
// Wait up to 30 seconds to connect.
while ((WiFi.status() != WL_CONNECTED) && (millis() - tNow < 30000))
{
delay(250);
Serial.print(".");
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("");
Serial.print("IP address: "), Serial.println(WiFi.localIP());
}
Serial.println("Check for signal...");
Serial2.println("AT");
delay(2000);
Serial2.println("AT+CGATT?");
delay(2000);
}
void loop() {
//If some data income read => available while data downloading
while (client.available()) {
char c = client.read();
Serial.write(c);
}
while (Serial2.available()){
Serial.write(Serial2.read());
}
}
I a beginner to IoT. I want to glow LED connected to arduino for this I have made connections as described below, also see the image of breadboard attached here.
LED Connections
Connected
Arduino GND to one leg of LED and 3.3V to another leg of LED.
My Arduino program is-
void setup() {
Serial.begin(9600);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.set("LED_STATUS",0);
}
int n = 0;
void loop() {
// get value
n = Firebase.getInt("LED_STATIS");
if (n == 1) {
Serial.print("LED IS ON");
digitalWrite(D1,HIGH);
return;
}else{
Serial.print("LED IS OFF");
digitalWrite(D1,LOW);
return;
}
delay(1000);
}
Serial monitor shows LED is ON as shown in screenshot.
Serial Monitor
i think you should declare pinMode in setup, and you put LED in D0
#define LED D0 // Led in NodeMCU at pin GPIO16 (D0).
void setup() {
pinMode(LED, OUTPUT); // LED pin as output.
}
void loop() {
if (n == 1) {
Serial.print("LED IS ON");
digitalWrite(LED ,HIGH);
return;
}else{
Serial.print("LED IS OFF");
digitalWrite(LED ,LOW);
return;
}
}
note that it has built-in LED, but its active low, it should glow when logic is 0
and you should using pull-up resistor, because you can't depend current supply from GPIO pin since its only provide 12mA
Hello has somebody managed a stable wifi connection that handles all kind of issues like power loss, router reboot, wifi loss and manage the machine to reconnect properly? This code wont work in a stable way:
void setup() {
wifiConnect();
}
void loop(){
wifiConnect();
}
void wifiConnect(){
if(WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// attempt to connect to Wifi network:
while(WiFi.status() != WL_CONNECTED) {
// Connect to WPA/WPA2 network.
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}
This is very stable and easy on a raspberry, because the pi handles it but on esp32 the sample codes do not handle at all the issues that can arise. And if you have issues you cannot debug, very frustrating.
Here's what's working perfectly for me (router reboot, channel changing, WiFi loss etc.):
void setup()
{
WiFi.begin(ssid, password);
}
void loop()
{
wifiConnect();
delay(10000);
}
void wifiConnect()
{
if (WiFi.status() != WL_CONNECTED) {
WiFi.disconnect(true);
delay(1000);
WiFi.begin(ssid, password);
return;
}
// other stuff
}
I'm just trying to get myself here "http://192.168.1.103:30000/?k=23&v=capture" when an if condition meet its requirement.
#include <ESP8266WiFi.h>
// I purposely don't include the ssid and ssid1 here
WiFiServer server(80);
void setup() {
pinMode(1, INPUT);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
String link = "http://192.168.1.103:30000/?k=23&v=capture";
WiFiClient client = server.available();
if (!client) {
return;
}
int var = digitalRead(1);
if (var == HIGH) {
client.print(link);
}
Let's say:
I already run Chrome.
How can that link above be called without even typing it on Chrome? I want to connect to it automatically.
Any method you could teach? I got the feeling this code itself is wrong.
Thanks.
-- EDIT --
NEW CODE FOR UNO
//language c++
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // Scanning address
LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
Servo Servo1;
int servopin = 9;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
// initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // finish with backlight on
lcd.setCursor(3, 0); //Start at character 4 on line 0
lcd.print("WAITING...");
pinMode(12, OUTPUT); // pin LaserLight
pinMode(11, INPUT); // pin LaserDetector
pinMode(10, INPUT); // pin PIR
pinMode(9, OUTPUT); // pin Servo
pinMode(8, OUTPUT); // MCU PIN GPIO2
Servo1.attach(servopin);
}
void loop() {
digitalWrite(12, HIGH);
boolean inputlaser = digitalRead(11);
boolean inputpir = digitalRead(10);
Serial.println(inputlaser);
Serial.println(inputpir);
if (inputlaser < 1) {
digitalWrite(8, HIGH);
lcd.setCursor(0, 0);
lcd.print("camera on");
lcd.setCursor(0, 1);
lcd.print("robber!");
delay(5000);
Servo1.write(180);
} else if (inputpir > 0) {
Servo1.write(180);
lcd.setCursor(0, 0);
lcd.print("robber inside!");
lcd.setCursor(0, 1);
lcd.print("HELP ROBBER!");
delay(500);
} else {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("standby...");
delay(500);
}
}
NEW CODE FOR MCU
#include <ESP8266WiFi.h>
char server[] = "192.168.1.103";
WiFiClient client;
void setup() {
pinMode(4, INPUT);
digitalWrite(4, LOW);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
boolean var = digitalRead(4);
if (var == HIGH) {
client.connect(server, 30000);
Serial.println("connected");
// Make your API request:
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.println(digitalRead(4));
}
First understand a fact that you doesn't need google chrome for requesting a website.
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
What you do in the above line is that you request for /?k=23&v=capture addressed content in the ip address 192.168.1.103, Actully this is what you do when you use a google chrome. On PC you require a google chrome (or any other browser) for web site request because its difficult to request using commands each time (Think of requesting for a single page using a hell of http commands instead of using chrome, Ohh that's mess). So understand chrome isn't needed to access a site.