#1 Re: Програмування Arduino » Esp32+Arduino: Перезагрузка по watchdog при использовани BLE и FastLED » 2018-02-01 14:12:10

Путем проб и ошибок обнаружил, что подвисание из за
bool SaveSettings(char * key, uint8_t value){
  Preferences prefs;
  bool res = prefs.begin("settings", false); if(!res) return false;
  prefs.putUChar(key, value);
  return true;
}

#3 Програмування Arduino » Esp32+Arduino: Перезагрузка по watchdog при использовани BLE и FastLED » 2018-02-01 10:38:04

serovsergey79
відповідей: 5

Делаю некий управляемый светильник на WS2812
Использую библиотеку FastLED, управление пока по BLE.
После приёма характеристики и записи в лену происходит перезагрузка по WATCHDOG (rsn 0x7)
Что не так?

#include "FastLED.h"
#include <WiFi.h>
#include "DHT.h"
#include <Preferences.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include "FastLED.h"

#define M_OFF               00000
#define M_WHITE             00001
#define M_RGB               00011
#define M_WHITE_AUTO        00101
#define M_CLOCK             01000
#define M_WHITE_CLOCK       01001
#define M_WHITE_AUTO_CLOCK  01101
#define M_RAINBOW           19

#define DEF_MODE  M_WHITE
#define DEF_BRIGHTNESS  255

#define NUM_LEDS 60
#define LEDS_PIN 12
CRGB leds[NUM_LEDS];

uint8_t Mode = DEF_MODE;
uint8_t Brightness = DEF_BRIGHTNESS;

#define SERVICE_UUID          "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHAR_MODE_UUID        "beb54801-36e1-4688-b7f5-ea07361b26a8"
#define CHAR_BRIGHTNESS_UUID  "beb54802-36e1-4688-b7f5-ea07361b26a8"

bool deviceConnected = false;
BLECharacteristic *pChar_mode, *pChar_brightness;

bool ReadSettings(){
  Preferences prefs;
  bool res = prefs.begin("settings", false); if(!res) return false;
  Mode = prefs.getUChar("mode", DEF_MODE);
  Brightness = prefs.getUChar("brightness", DEF_BRIGHTNESS);
}

bool SaveSettings(char * key, uint8_t value){
  Preferences prefs;
  bool res = prefs.begin("settings", false); if(!res) return false;
  prefs.putUChar(key, value);
  return true;
}

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class ModeCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pChar_mode) {
      std::string s = pChar_mode->getValue();
      String ss = (String)s.c_str();
      Mode = ss.toInt();
      SaveSettings("mode", Mode);
      Serial.print("Mode: ");
      Serial.println(Mode);
      ModeChanged();
    }
};

class BrightnessCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pChar_brightness) {
      std::string s = pChar_brightness->getValue();
      String ss = (String)s.c_str();
      Brightness = ss.toInt();
      SaveSettings("brightness", Brightness);
      Serial.print("Brightness: ");
      Serial.println(Brightness);
      ModeChanged();
    }
};

void setup() {
  Serial.begin(115200);
  BLEDevice::init("MyESP32");
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create a BLE Characteristic
  pChar_mode = pService->createCharacteristic(
                      CHAR_MODE_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  
                    );
  pChar_mode->setCallbacks(new ModeCallbacks());
  // Create a BLE Descriptor
  pChar_mode->addDescriptor(new BLE2902());

  // Create a BLE Characteristic
  pChar_brightness = pService->createCharacteristic(
                      CHAR_BRIGHTNESS_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  
                    );
  pChar_brightness->setCallbacks(new BrightnessCallbacks());
  // Create a BLE Descriptor
  pChar_brightness->addDescriptor(new BLE2902());

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

  // Start advertising
  pServer->getAdvertising()->start();
  
  ReadSettings();
  pChar_mode->setValue(&Mode, 1);
  pChar_brightness->setValue(&Brightness, 1);
  Serial.println("Startup.");
  Serial.print("Mode: "); Serial.println(Mode);
  Serial.print("Brightness: "); Serial.println(Brightness);

  FastLED.addLeds<NEOPIXEL, 12>(leds, 60);
}

static void ModeChanged(){
  switch(Mode){
    case M_WHITE:
        FastLED.showColor(CHSV(0, 0, Brightness));
      break;
    case M_RAINBOW:
      fill_rainbow(leds, NUM_LEDS, 0, 256/NUM_LEDS);
      FastLED.show();
      break;
    default: FastLED.clear(true);
  }
}

void loop() {

}

Підвал форуму