Arduino-esp32: Reboot with server BLE and WIFI

Created on 27 Oct 2018  Â·  10Comments  Â·  Source: espressif/arduino-esp32

hi, i try to utilize BLE and Wifi server, but ig i start BLE server i have a loop of reset withi this error.

in serial monitor i have only
rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1496
load:0x40078000,len:8596
load:0x40080400,len:6980
entry 0x400806f4
ets Jun 8 2016 00:22:57

Help me

stale

Most helpful comment

I don't know if this was the OP's issue, but the symptoms above closely align with my issues so I figured I'd post here. My code would run fine with WiFi or BLE commented out, but when I had both I would get the same output described above.

My problem was that I didn't completley configure partitions correctly. I updated my boards.txt to properly account for my program size, but was still pointing to the default partition table (as opposed to the new one I created). This basically meant that my program overran the partition (or that it was only partially written, not sure how that is handled by the uploader). In any case, it basically meant part of my code wasn't accessible, so when it started up, it immediately crashed (which really confused me for a while, because it wasn't clearly crashing when it hit any particular line).

To @gigirizzi's question, yes you can. You need to change 2 files:

  1. esp32/tools/partitions/.csv - This defines the actual partituions. You may want to look at huge_app.csv to see if it meets your needs, or copy one of the existing files and modify it
  2. esp32/boards.txt - This is where the IDE will pick up the partition file, and determine the max program/data sizes (upload.maxium_size and upload.maximum_data_size). It is also where you specify which partition to use (build.partitions)

A few things to note:

  1. Be sure to edit the correct entry in the boards.txt. Many different boards are in there, each with different settings. I'm using the Adafruit Feather, so I went down to edit the featheresp32 entries.
  2. The partition table uses sizes and offests in hex, while the boards.txt uses decimal values. I generally get my layout correct in the partition csv, then convert to decimal and paste it into the boards.txt

All 10 comments

i have enable all but i have the same result. In srial monitorl i see a loop of rst:0x3 (SW_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1496
load:0x40078000,len:8596
load:0x40080400,len:6980
entry 0x400806f4
ets Jun 8 2016 00:22:57

I think than my sket not start; if i comment intible or all call of Wifi library , i havne't problem but if i use all together i have reboot loop on start

Then it has nothing to do with ble and wifi. If there is nothing more in logs, then your code is causing reboot even before wifi or ble starts.

i attach pmy code: PS i rean douring my compilation there are many arning: 'LOG_TAG' defined but not used [-Wunused-variable].

PS. i have a borad jzk esp32s what kind of board i can select?
now i try if i sue nodemcu32s work if i use ep32 dev module thi sketch go in loop reboot.

define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE

// Default Arduino includes

include

include

include

include

// Includes for JSON object handling
// Requires ArduinoJson library
// https://arduinojson.org
// https://github.com/bblanchon/ArduinoJson
//#include

// Includes for BLE

include

include

include

include

include

define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE

include "esp_log.h"

#include "esp32-hal-log.h"

include

int loc_HIGH = HIGH;
int loc_LOW = LOW;
/*
char lettura[BUFSIZE];
char master[BUFSIZE];
char slave[BUFSIZE];
*/
const int RELAY_PIN_1 = 12;
const int RELAY_PIN_2=14;

/** Build time */
const char compileDate[] = __DATE__ " " __TIME__;

/* Unique device name */
char apName[] = "ESP32-xxxxxxxxxxxx";
/
* Selected network
true = use primary network
false = use secondary network
/
bool usePrimAP = true;
/
* Flag if stored AP credentials are available /
bool hasCredentials = false;
/
* Connection status /
volatile bool isConnected = false;
/
* Connection change status /
bool connStatusChanged = false;
bool ElaborazioneIncorso=false;
/
*

  • Create unique device name from MAC address
    **/
    void createName() {
    uint8_t baseMac[6];
    // Get MAC address for WiFi station
    esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
    // Write unique name into apName
    sprintf(apName, "ESP32-%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
    }

// List of Service and Characteristic UUIDs
//#define SERVICE_UUID "0000aaaa-ead2-11e7-80c1-9a214cf093ae"

define WIFI_UUID "00005555-ead2-11e7-80c1-9a214cf093ae"

define SERVICE_UUID "0000ffe0-0000-1000-8000-00805f9b34fb" // UART service UUID

define CHARACTERISTIC_UUID_RX "0000ffe0-0000-1000-8000-00805f9b34fb"

define CHARACTERISTIC_UUID_TX "0000ffe0-0000-1000-8000-00805f9b34fb"

/* SSIDs of local WiFi networks */
String ssidPrim;
String ssidSec;
/
* Password for local WiFi network */
String pwPrim;
String pwSec;

/* Characteristic for digital output */
BLECharacteristic *pCharacteristicWiFi;
/
* BLE Advertiser /
BLEAdvertising
pAdvertising;
/* BLE Service */
BLEService *pService;
/
* BLE Server */
BLEServer *pServer;

/**

  • MyServerCallbacks
  • Callbacks for client connection and disconnection
    /
    class MyServerCallbacks: public BLEServerCallbacks {
    // TODO this doesn't take into account several clients being connected
    void onConnect(BLEServer
    pServer) {
    Serial.println("BLE client connected");
    };

void onDisconnect(BLEServer* pServer) {
Serial.println("BLE client disconnected");
pAdvertising->start();
}
};

/**

  • MyCallbackHandler
  • Callbacks for BLE client read/write requests
    */
    class MyCallbackHandler: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
    std::string value = pCharacteristic->getValue();
    if (value.length() == 0) {
    return;
    }
    Serial.println("Received over BLE: " + String((char *)&value[0]));
// Decode data
int keyIndex = 0;
for (int index = 0; index < value.length(); index ++) {
  value[index] = (char) value[index] ^ (char) apName[keyIndex];
  keyIndex++;
  if (keyIndex >= strlen(apName)) keyIndex = 0;
}

};

void onRead(BLECharacteristic *pCharacteristic) {
Serial.println("BLE onRead request");
String wifiCredentials;

}
};

/**

  • initBLE
  • Initialize BLE service and characteristic
  • Start BLE server and service advertising
    */
    void initBLE() {
    // Initialize BLE and set output power
    BLEDevice::init(apName);
    BLEDevice::setPower(ESP_PWR_LVL_P7);

// Create BLE Server
pServer = BLEDevice::createServer();

// Set server callbacks
pServer->setCallbacks(new MyServerCallbacks());

// Create BLE Service
pService = pServer->createService(BLEUUID(SERVICE_UUID),20);

// Create BLE Characteristic for WiFi settings
pCharacteristicWiFi = pService->createCharacteristic(
BLEUUID(WIFI_UUID),
// WIFI_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
| BLECharacteristic::PROPERTY_WRITE_NR| BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristicWiFi->setCallbacks(new MyCallbackHandler());

// Start the service
pService->start();

// Start advertising
pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}

/** Callback for receiving IP address from AP */
void gotIP(system_event_id_t event) {
isConnected = true;
connStatusChanged = true;
}

// Callback for connection loss
void lostCon(system_event_id_t event) {
isConnected = false;
connStatusChanged = true;
}

/**
scanWiFi
Scans for available networks
and decides if a switch between
allowed networks makes sense

@return bool
True if at least one allowed network was found
/
bool scanWiFi() {
/
* RSSI for primary network /
int8_t rssiPrim=-1;
/
* RSSI for secondary network /
int8_t rssiSec=-1;
/
* Result of this function */
bool result = false;

Serial.println("Start scanning for networks");

WiFi.disconnect(true);
WiFi.enableSTA(true);
WiFi.mode(WIFI_STA);

// Scan for AP
int apNum = WiFi.scanNetworks(false,true,false,1000);
if (apNum == 0) {
Serial.println("Found no networks?????");
return false;
}

byte foundAP = 0;
bool foundPrim = false;

for (int index=0; index String ssid = WiFi.SSID(index);
Serial.println("Found AP: " + ssid + " RSSI: " + WiFi.RSSI(index));
if (!strcmp((const char) &ssid[0], (const char) &ssidPrim[0])) {
Serial.println("Found primary AP");
foundAP++;
foundPrim = true;
rssiPrim = WiFi.RSSI(index);
}
if (!strcmp((const char) &ssid[0], (const char) &ssidSec[0])) {
Serial.println("Found secondary AP");
foundAP++;
rssiSec = WiFi.RSSI(index);
}
}

switch (foundAP) {
case 0:
result = false;
break;
case 1:
if (foundPrim) {
usePrimAP = true;
} else {
usePrimAP = false;
}
result = true;
break;
default:
Serial.printf("RSSI Prim: %d Sec: %dn", rssiPrim, rssiSec);
if (rssiPrim > rssiSec) {
usePrimAP = true; // RSSI of primary network is better
} else {
usePrimAP = false; // RSSI of secondary network is better
}
result = true;
break;
}
return result;
}

/**

  • Start connection to AP
    */
    void connectWiFi() {
    // Setup callback function for successful connection
    WiFi.onEvent(gotIP, SYSTEM_EVENT_STA_GOT_IP);
    // Setup callback function for lost connection
    WiFi.onEvent(lostCon, SYSTEM_EVENT_STA_DISCONNECTED);

WiFi.disconnect(true);
WiFi.enableSTA(true);
WiFi.mode(WIFI_STA);

Serial.println();
Serial.print("Start connection to ");
if (usePrimAP) {
Serial.println(ssidPrim);
WiFi.begin(ssidPrim.c_str(), pwPrim.c_str());
} else {
Serial.println(ssidSec);
WiFi.begin(ssidSec.c_str(), pwSec.c_str());
}
}

void setup() {
// Create unique device name
//createName();

// Initialize Serial port
Serial.begin(115200);

esp_log_level_set("*", ESP_LOG_VERBOSE);

log_v("Verbose");
log_d("Debug");
log_i("Info");
log_w("Warning");
log_e("Error");
// Send some device info
Serial.print("Build: ");
Serial.println(compileDate);
//connectWiFi();

String invert="";
Preferences preferences;
preferences.begin("Generale", false);
invert=preferences.getString("reverse","0");

preferences.end();

Serial.println("invert "+ invert);
if (invert[0] == '1') {
loc_HIGH = LOW;
loc_LOW = HIGH;
}

pinMode(RELAY_PIN_1, OUTPUT);
pinMode(RELAY_PIN_2, OUTPUT);
digitalWrite(RELAY_PIN_1, loc_LOW);
digitalWrite(RELAY_PIN_2, loc_LOW);

//TODO Preferences preferences;
preferences.begin("WiFiCred", false);
bool hasPref = preferences.getBool("valid", false);
if (hasPref) {
ssidPrim = preferences.getString("ssidPrim","");
ssidSec = preferences.getString("ssidSec","");
pwPrim = preferences.getString("pwPrim","");
pwSec = preferences.getString("pwSec","");

if (ssidPrim.equals("") 
    || pwPrim.equals("")
    || ssidSec.equals("")
    || pwPrim.equals("")) {
  Serial.println("Found preferences but credentials are invalid");
} else {
  Serial.println("Read from preferences:");
  Serial.println("primary SSID: "+ssidPrim+" password: "+pwPrim);
  Serial.println("secondary SSID: "+ssidSec+" password: "+pwSec);
  hasCredentials = true;
}

} else {
Serial.println("Could not find preferences, need send data over BLE");
}
preferences.end();

// Start BLE server
initBLE();
/*
if (hasCredentials) {
// Check for available AP's
if (!scanWiFi()) {
Serial.println("Could not find any AP");
} else {
// If AP was found, start connection
connectWiFi();
}
}*/
}

void loop() {
if (connStatusChanged) {
if (isConnected) {
Serial.print("Connected to AP: ");
Serial.print(WiFi.SSID());
Serial.print(" with IP: ");
Serial.print(WiFi.localIP());
Serial.print(" RSSI: ");
// Serial.println(WiFi.RSSI());
delay(5000);
//
delay(5000);
} else {
if (hasCredentials) {
Serial.println("Lost WiFi connection");
// Received WiFi credentials

  if (!scanWiFi()) { // Check for available AP's
      Serial.println("Could not find any AP");
    } else { // If AP was found, start connection
      connectWiFi();
    }

  } 
}
connStatusChanged = false;

}
}

#define SERVICE_UUID "0000ffe0-0000-1000-8000-00805f9b34fb" // UART service UUID
#define CHARACTERISTIC_UUID_RX "0000ffe0-0000-1000-8000-00805f9b34fb"
#define CHARACTERISTIC_UUID_TX "0000ffe0-0000-1000-8000-00805f9b34fb"

Each UUID must unique. Try

#define SERVICE_UUID "0000ffe0-0000-1000-8000-00805f9b34fb" // UART service UUID
#define CHARACTERISTIC_UUID_RX "0000ffe1-0000-1000-8000-00805f9b34fb"
#define CHARACTERISTIC_UUID_TX "0000ffe2-0000-1000-8000-00805f9b34fb"

The binary size of the firmware might exceed the size of the app partition. BT increases the firmware by at least 600K. make sure it fits.

Can i make one partition with one app?

Inviato da iPhone

Il giorno 4 nov 2018, alle ore 14:17, Clemens Kirchgatterer notifications@github.com ha scritto:

The binary size of the firmware might exceed the size of the app partition. BT increases the firmware by at least 600K. make sure it fits.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

I don't know if this was the OP's issue, but the symptoms above closely align with my issues so I figured I'd post here. My code would run fine with WiFi or BLE commented out, but when I had both I would get the same output described above.

My problem was that I didn't completley configure partitions correctly. I updated my boards.txt to properly account for my program size, but was still pointing to the default partition table (as opposed to the new one I created). This basically meant that my program overran the partition (or that it was only partially written, not sure how that is handled by the uploader). In any case, it basically meant part of my code wasn't accessible, so when it started up, it immediately crashed (which really confused me for a while, because it wasn't clearly crashing when it hit any particular line).

To @gigirizzi's question, yes you can. You need to change 2 files:

  1. esp32/tools/partitions/.csv - This defines the actual partituions. You may want to look at huge_app.csv to see if it meets your needs, or copy one of the existing files and modify it
  2. esp32/boards.txt - This is where the IDE will pick up the partition file, and determine the max program/data sizes (upload.maxium_size and upload.maximum_data_size). It is also where you specify which partition to use (build.partitions)

A few things to note:

  1. Be sure to edit the correct entry in the boards.txt. Many different boards are in there, each with different settings. I'm using the Adafruit Feather, so I went down to edit the featheresp32 entries.
  2. The partition table uses sizes and offests in hex, while the boards.txt uses decimal values. I generally get my layout correct in the partition csv, then convert to decimal and paste it into the boards.txt

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This stale issue has been automatically closed. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings