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!
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++;
}
}