• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

esp32 reboot at wifi

Q&A Forum › Category: ESP32 › esp32 reboot at wifi
0 Vote Up Vote Down
peppeg85 asked 5 years ago

hello, i have an AT09 BLE connected to the uart, i use it to communicate to android phone, i’m simulating the discovering of wifi networks and the connection at the change of a characteristic, maybe following the code is better than explaining. in the code i set the wifi ssid static because i don’t have an app ready, but this is not important now. the problem is when i send the password of my wifi the esp32 goes in core 1 panic, where am i wrong? someone could help me? here is my code:
 

#include "WiFi.h"
#include 
#include "DHT.h"

#define DHTPIN 4 
#define DHTTYPE DHT22   
#define BLERX 26
#define BLETX 25

SoftwareSerial BLESerial(BLERX, BLETX); // RX, TX
DHT dht(DHTPIN, DHTTYPE);

int connected=0;
const char* ssid = "FRITZ!Box 7490";
char* password =  "";

void setup() {
  BLESerial.begin(9600);
  Serial.begin(115200);
  dht.begin();
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
  if(connected!=0){
    }else{
      scanNetwork();
    }
}
void scanNetwork(){
  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");
        scanNetwork();
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
          BLESerial.println(WiFi.SSID(i));
          Serial.println(WiFi.SSID(i));
        }
    }
    Serial.println("");
  }

void sendCommand(const char * command){
  Serial.print("Command send :");
  Serial.println(command);
  BLESerial.println(command);
  delay(100);
  
  char reply[100];
  int i = 0;
  while (BLESerial.available()) {
    reply[i] = BLESerial.read();
    i += 1;
  }
  //end the string
  reply[i] = '\0';
  Serial.print(reply);
  Serial.println("Reply successful");
}

void loop() {
  if(BLESerial.available()){
    Serial.println("ReadBLE");
    readBle();
  }
}

void readBle(){
  char BLEReadings[200];
  int i=0;
    while (BLESerial.available()) {
   
    BLEReadings[i] = BLESerial.read();
    Serial.println(BLEReadings[i]);
    i += 1;
  }
  //end the string
  BLEReadings[i] = '\0';
   Serial.println(BLEReadings);
 // password=BLEReadings;
  
  WiFi.begin(ssid, BLEReadings);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
 connected=1;
  Serial.println("Connected to the WiFi network");
 
  }

thank you in advance!

1 Answers
0 Vote Up Vote Down
peppeg85 answered 5 years ago

hello, i modified the sketch and “solved” with a delay of 500, the problem seems to be the while(bleseral.available), now changed with an if). i don’t like the delay that i’ve used, have you some suggestions?
 

#include 
#include 
#include "WiFi.h"
#include 
#include 

#define BLERX 26
#define BLETX 25

SoftwareSerial BLESerial(BLERX, BLETX); // RX, TX
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

String formattedDate;
String dayStamp;
String timeStamp;
int connected=0;
const char* ssid = "myssid_tosimulatethewifiscan";
char* password =  "";

void setup() {
  BLESerial.begin(9600);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
}
void scanNetwork(){
  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");
        scanNetwork();
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
          BLESerial.println(WiFi.SSID(i));
          Serial.println(WiFi.SSID(i));
        }
    }
    Serial.println("");
  }
void sendCommand(const char * command){
  Serial.print("Command send :");
  Serial.println(command);
  BLESerial.println(command);
  delay(100);
  
  char reply[100];
  int i = 0;
  while (BLESerial.available()) {
    reply[i] = BLESerial.read();
    i += 1;
  }
  //end the string
  reply[i] = '\0';
  Serial.print(reply);
  Serial.println("Reply successful");
}

void loop() {
  if(connected==0){
  if(BLESerial.available()){
    Serial.println("ReadBLE");
    char BLEReadings[200];
    int i=0;
    while (BLESerial.available()) {
      BLEReadings[i] = BLESerial.read();
      Serial.println(BLEReadings[i]);
      i += 1;
    }
  BLEReadings[i] = '\0';
  Serial.println(BLEReadings);
  if(strcmp(BLEReadings,"scan")==0){
    scanNetwork();
    }else if(strstr(BLEReadings, "xxx-") != NULL){
        removeSubstr (BLEReadings, "xxx-");
        WiFi.begin(ssid, BLEReadings);
        int maxRepeat=0;
        while (WiFi.status() != WL_CONNECTED && maxRepeat<20) {
          delay(500);
          Serial.println("Connecting to WiFi..");
          maxRepeat++;
        }
        if(WiFi.status() == WL_CONNECTED){
          connected=1;
          Serial.println("Connected to the WiFi network");
          Serial.println(WiFi.localIP());
          timeClient.begin();
          timeClient.setTimeOffset(3600);
          while(!timeClient.update()) {
            timeClient.forceUpdate();
          }
          formattedDate = timeClient.getEpochTime();
          Serial.println(formattedDate);
          time_t rawtime = atoi(formattedDate.c_str());
          struct tm ts;
          char buf[80];
          ts = *localtime(&rawtime);
          strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
          Serial.println(buf);
        }
    }
  }
}
  delay(500);                  // this is the delay i dont like
}

void readBle(){
  char BLEReadings[200];
  int i=0;
  while (BLESerial.available()) {
    BLEReadings[i] = BLESerial.read();
    i += 1;
  }
  //end the string
  BLEReadings[i] = '\0';
  Serial.println(BLEReadings);
 // password=BLEReadings;
  
  WiFi.begin(ssid, BLEReadings);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  connected=1;
  Serial.println("Connected to the WiFi network");
}

void removeSubstr (char *string, char *sub) {
    char *match = string;
    int len = strlen(sub);
    while ((match = strstr(match, sub))) {
        *match = '\0';
        strcat(string, match+len);
                match++;
    }
}

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.