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());
}
}
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.
I have an ESP32 which should receive data over BLE and then send the data over WiFi to a webserver. If I code both tasks separately in Arduino, then everything works but as soon as I merge both tasks, sending over WiFi breaks down.
From what I understand, BLE and WiFi are sharing the same radio on the ESP32, thus the tasks need to be done alternately to avoid interferences. I've tried to implement this by adding delays between the two tasks but was unsuccessful.
This is the code I have so far:
#include <HTTPClient.h>
#include <BLEDevice.h>
#include <BLEScan.h>
const char* ssid = "xx";
const char* password = "xx";
int scanTime = 2; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(1);
Serial.setDebugOutput(0); //turn off debut output
WiFi.begin(ssid, password);
int retrycon = 50;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if (--retrycon == 0)
{
Serial.println("RESTART");
ESP.restart();
}
Serial.print(".");
}
Serial.print("WiFi connected with IP: ");
Serial.println(WiFi.localIP());
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
}
void loop()
{
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
pBLEScan->clearResults();
delay(3000);
int tryconnect = 20;
while (--tryconnect != 0) {
if (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("...");
} else {
break;
}
}
if (WiFi.status() != WL_CONNECTED) {
WiFi.reconnect();
Serial.println("reconnect");
}
else {
Serial.println("connected to WiFi");
HTTPClient http;
http.begin("http://httpbin.org/ip");
int httpCode = http.GET();
if (httpCode > 0) {
Serial.print("HTTP code ");
Serial.println(httpCode);
} else {
Serial.println("Error on HTTP request");
}
http.end();
delay(10000);
}
}
Can anybody tell me how to implement the two tasks (receiving over BLE, sending over WiFi) to avoid interferences?
I use commands like this to turn off BLE and WiFi:
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
btStop();
adc_power_off();
//esp_wifi_stop(); // Doesn't work for me!
esp_bt_controller_disable();
In my code I BLE advertize/scan, then do the stuff above, then connect to WiFi. No problem. Oh and remember to BLEDevice::deinit when you finished with BLE, otherwise you can't get it to fit in a 4Mb ESP32. I do BLE, WiFi, HTTPS/SSL, OTA and use the SPIFFS to store data, all on a standard 4Mb ESP32 (ESP-WROOM-32) without PSRAM.
All above is in a function to lower the power usage, so I call it all every time I change 'mode'. In deep sleep I only use 5uA.
Also:
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);
... don't work for me, so I commented them out, and scan for 3 sec.
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...
I'm trying to connect my Wemos D1 R2 to the wifi in my house. It connects and shows only the MAC address of the board but no IP is displayed on the list of the connected device on the router. The serial monitor also doesn't print anything but the dots.
I expected the serial monitor to print my IP address and the Wemos to stay connected to the internet
#include <ESP8266WiFi.h>
const char *ssid = "SSID";
const char *password = "Pass";
void setup() {
Serial.begin(115200);
delay(3000);
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.println(password);
// ---------------This was the magic WiFi reconnect fix for me
WiFi.persistent(false);
WiFi.mode(WIFI_OFF); // this is a temporary line, to be removed after SDK
// update to 1.5.4
WiFi.mode(WIFI_STA);
// ---------------END - WiFi reconnect fix
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
void loop() {
}
Today I got my NodeMCU and I instantly started with coding. I wanted to connect to my WiFi and to my MQTT server.
I used the PubSub example for this.
In the serial monitor I get the message that I connected successfully with the WiFi, but I get the IP 172.20.10.6. However we have a 192.168... network.
Then when I try to reach my MQTT server it doesn't find it. When I try to give the NodeMCU a static IP it also says connected successfully and shows up the static IP I gave it, but I still can't connect to my MQTT server.
I can't ping the NodeMCU and don't find it in my Smartphone app "Fing".
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "myssid";
const char* password = "mypw";
const char* mqtt_server = "192.168.42.131";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
// Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW);
// Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH);
// Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("mathistest");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
}
}
You are most likely having a DHCP failure.
A 172.20.x.x address is a non-routable IP address (see: https://www.lifewire.com/what-is-a-private-ip-address-2625970) and the DHCP code is (likely) using that address when the address assignment fails.
Stepping back, DHCP is most likely failing because you are failing to connect to the Wifi network with the correct SSID and password.