XIAOMI LYWSD03MMC - odczyt przez ESP32

lukasz06
Posts: 2543
Joined: Sun Jul 17, 2022 6:53 pm
Has thanked: 46 times
Been thanked: 21 times

Post

Byłoby zajefajnie😉
kurus
Posts: 1312
Joined: Wed Jan 15, 2020 8:17 pm

Post

Jeśli można by ilość czujników i ich adresy konfigurować z poziomu www to już by była petarda na maxa :D
t0mek wrote: Thu Jan 25, 2024 8:19 pm
andras wrote: Thu Jan 25, 2024 7:58 pm
t0mek wrote: Thu Jan 25, 2024 6:37 pm

Źle parametry masz. Partycja zła. Zobacz wcześniej w temacie
Jaka partycja ma być bo już różne ustawiałem i wtedy wywala błędy
Minimal SPIFFS
Dokładnie jak w załączniku, zwróć uwagę że w zakładce cpu jest wifi/bt dla esp32, dla twojego esp32c3 niema tam bt wiec nie wiem czy to obsługuje bluetooth
You do not have the required permissions to view the files attached to this post.
User avatar
veeroos
Posts: 1081
Joined: Sun Mar 20, 2022 9:30 am
Location: Głogów
Has thanked: 1 time
Been thanked: 9 times

Post

kurus wrote: Fri Jan 26, 2024 7:24 am Jeśli można by ilość czujników i ich adresy konfigurować z poziomu www to już by była petarda na maxa :D
Mam na to już pomysł, najszybciej zrobić przez Custom Parameter. Jak ktoś będzie miał chwilę, to można pokombinować, ja słabo stoję z czasem teraz.
Zamel Mew-01, Zamel PEW-01, Zamel SBW-01, Zamel THW-01, Zamel SRW-01, Zamel mSLW-02, Auraton BOX + Indor Sensor, Airly Gate By Veeroos, Zigbee to Supla Gateway + sensors, Zamel SGW-01 and more

https://github.com/v33r005
andras
Posts: 636
Joined: Fri Dec 30, 2016 5:51 pm
Has thanked: 2 times
Been thanked: 4 times

Post

Ma BT , wgrałem ponownie z partycją Minimal SPIFFS i teraz jest ok
kurus
Posts: 1312
Joined: Wed Jan 15, 2020 8:17 pm

Post

U was też takie szpilki do zera występują?
You do not have the required permissions to view the files attached to this post.
t0mek
Posts: 92
Joined: Sat Dec 02, 2017 11:24 am
Location: Legnica

Post

kurus wrote: Fri Jan 26, 2024 10:34 pm U was też takie szpilki do zera występują?
Właśnie
Przy starcie i po resecie zanim złapie odczyt wyświetla "0" w parametrach, których nie odczytał. Tylko pierwszy Mac jest ok (są "--"). Wydaje mi się że pętla zerowanie nie działa właściwie, ale nie wiem jak to zmienić :?
Pozdrawiam
Tomek
marcinl
Posts: 18
Joined: Mon Apr 24, 2023 6:20 pm

Post

Przy kilku podłączonych termometrach do jednego ESP32 można im nadać jakieś nazwy?
t0mek
Posts: 92
Joined: Sat Dec 02, 2017 11:24 am
Location: Legnica

Post

marcinl wrote: Sat Jan 27, 2024 1:20 pm Przy kilku podłączonych termometrach do jednego ESP32 można im nadać jakieś nazwy?
Normalnie ne w cloud wpisujesz co chcesz
Pozdrawiam
Tomek
Adamus10
Posts: 31
Joined: Sat Oct 28, 2023 8:05 pm
Been thanked: 6 times

Post

kurus wrote: Fri Jan 26, 2024 10:34 pm U was też takie szpilki do zera występują?
Poprawka powyższego + parę modyfikacji, ale nie zmieniających schematu działania

Code: Select all

/*
  Copyright (C) AC SOFTWARE SP. Z O.O.

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

//Parametry
#define STATUS_LED_GPIO 2
#define BUTTON_CFG_RELAY_GPIO 0
#define SCAN_TIME  2 // seconds
#define SCAN_BT_TIME  120 // seconds
const int liczbaElementow = 17; //Liczba czujników
const char* xiaomiBleDeviceMacs[] = {  //Lista czujników
  "A4:C1:38:00:00:00",
  "A4:C1:38:00:00:00",
};

//Ładowanie bibiotek
#include <sstream>
#include <NimBLEDevice.h>
#include <NimBLEScan.h>
#include <NimBLEAdvertisedDevice.h>
#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/control/button.h>
#include <supla/control/action_trigger.h>
#include <supla/device/status_led.h>
#include <supla/storage/littlefs_config.h>
#include <supla/network/esp_web_server.h>
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/device/supla_ca_cert.h>
#include <supla/sensor/therm_hygro_meter.h>
#include <supla/sensor/thermometer.h>

//Deklaracja zmiennych
byte od[liczbaElementow];
byte bat[liczbaElementow];
int wilg[liczbaElementow];
float temp[liczbaElementow];

unsigned long czas_pomiedzy_skanowaniem;
unsigned long wifimilis;
unsigned long lastReadTime = 0;
boolean METRIC = true; //Set true for metric system; false for imperial
String text[12];
String zmienna;
String MAC_ADR;
byte BLE_DEVICE;
bool wyswietl = 0;
byte current_humidity;
byte current_batt;
//word val;
float current_temperature;

//#include <supla/storage/eeprom.h>
//Supla::Eeprom eeprom;

Supla::ESPWifi wifi;
Supla::LittleFsConfig configSupla;
Supla::Device::StatusLed statusLed(STATUS_LED_GPIO, true); // inverted state
Supla::EspWebServer suplaServer;
//wifi.setSSLEnabled(false);
Supla::Html::DeviceInfo htmlDeviceInfo(&SuplaDevice);
Supla::Html::WifiParameters htmlWifi;
Supla::Html::ProtocolParameters htmlProto;
Supla::Html::StatusLedParameters htmlStatusLed;
NimBLEScan *pBLEScan;
/*
void IRAM_ATTR resetModule(){
    ets_printf("reboot\n");
    esp_restart();
}
*/

//Zamiana małych liter na duże dla HEX
String Zamien_litery(const String& input) {
  String wynik = input;
  for (char& c : wynik) {
    if (c >= 'a' && c <= 'f') {
      c = toupper(c);
    }
  }
  return wynik;
}

class MyAdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks {
    void onResult(NimBLEAdvertisedDevice* advertisedDevice)
    {
        if (advertisedDevice->haveName() && advertisedDevice->haveServiceData()) {

            int serviceDataCount = advertisedDevice->getServiceDataCount();
            std::string strServiceData = advertisedDevice->getServiceData(0);
            uint8_t cServiceData[100];
            char charServiceData[100];

            strServiceData.copy((char *)cServiceData, strServiceData.length(), 0);

            for (int i=0;i<strServiceData.length();i++) {
                sprintf(&charServiceData[i*2], "%02x", cServiceData[i]);
            }

            std::stringstream ss;
            ss << "fe95" << charServiceData;


          for (int i = 0; i < 12; i++) {
            //text[i] = Zamien_litery(String(charServiceData[i * 2], 2));
            text[i] = Zamien_litery(String(charServiceData[i * 2]) + String(charServiceData[i * 2 + 1]));  
            }

            //Serial.println();

        
            Konwersja();


            
        }
    }
};

class Czujnik : public Supla::Sensor::ThermHygroMeter {
 public:
  explicit Czujnik(int sensorNumber)
      : sensorNumber(sensorNumber)//,
        //temperature(-275),
        //humidity(-1)
  {

  }

  double getTemp() {
    temperature = temp[sensorNumber];
    return temperature;
  }

  double getHumi() {
    humidity = wilg[sensorNumber];
    return humidity;
  }

 private:
  void iterateAlways() {
    if (millis() - lastReadTime > 10000) {
      lastReadTime = millis();
      channel.setNewValue(getTemp(), getHumi());
      channel.setBatteryLevel(bat[sensorNumber]);
    }
  }

  void onInit() {
    channel.setNewValue(temp[sensorNumber], wilg[sensorNumber]);
    channel.setBatteryLevel(bat[sensorNumber]);
  }

 protected:
  const int sensorNumber;
  float temperature;
  int humidity;
};

Czujnik* czujniki[liczbaElementow]; 


void setup() {

  Serial.begin(115200);
  initBluetooth();

  // Channels configuration
  auto buttonCfgRelay = new Supla::Control::Button(BUTTON_CFG_RELAY_GPIO, true, true);
  buttonCfgRelay->configureAsConfigButton(&SuplaDevice);

 for (int i = 0; i < liczbaElementow; ++i) {
    od[i]=0;
    bat[i]=0;
    wilg[i]=-1;
    temp[i]=-275;
    czujniki[i] = new Czujnik(i);  
    czujniki[i]->getChannel()->setFlag(SUPLA_CHANNELSTATE_FIELD_BATTERYPOWERED);
    czujniki[i]->getChannel()->setFlag(SUPLA_CHANNELSTATE_FIELD_BATTERYLEVEL);
  }
 
  // configure defualt Supla CA certificate
  SuplaDevice.setSuplaCACert(suplaCACert);
  SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);
  SuplaDevice.setName("Supla Xiaomi");
  //SuplaDevice.addFlags(SUPLA_DEVICE_FLAG_SLEEP_MODE_ENABLED);

  SuplaDevice.begin();
  NimBLEDevice::setPower(ESP_PWR_LVL_P9); 
}

//pętla główna
void loop() {
  if ((millis() - czas_pomiedzy_skanowaniem) > (SCAN_BT_TIME * 1000)) {
    Serial.println(F("skanuje bluetooth "));
    czas_pomiedzy_skanowaniem = millis();
    NimBLEScan* pBLEScan = NimBLEDevice::getScan(); //create new scan
    pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
    pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
    NimBLEScanResults foundDevices = pBLEScan->start(SCAN_TIME);
    byte count = foundDevices.getCount();
    delay(100);
    for (int i = 0; i < liczbaElementow; i++) {
      od[i] = od[i] + 1;
    }
  }

  SuplaDevice.iterate();

  if (WiFi.status() != WL_CONNECTED) {    
    if (millis() > wifimilis)  {               
      WiFi.begin(); 
      wifimilis = (millis() + 30000) ;
    }
  }
  if (millis() > 86400000){
    ESP.restart();
  }
}

void initBluetooth()
{
    NimBLEDevice::init("");
    pBLEScan = NimBLEDevice::getScan(); //create new scan
    pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
    pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
    pBLEScan->setInterval(0x50);
    pBLEScan->setWindow(0x30);
}


//Odczyt i zapis 
void Konwersja()
{
  int number;
  String hexstring;
  int t1;
  int t2;


  hexstring = "#" + text[6];
  number = (int) strtol( &hexstring[1], NULL, 16);
  t1 = number & 0xFF;                        

  hexstring = "#" + text[7];
  number = (int) strtol( &hexstring[1], NULL, 16);
  t2 = number & 0xFF;

  current_temperature = ( t1 == 255 ? -1 : t1 ) * 256;
  current_temperature = current_temperature + t2;
  current_temperature = current_temperature / 10;
  // Konwersja z Faranheitów na Celsjusze
                                      
  hexstring = "#" + text[8];
  number = (int) strtol( &hexstring[1], NULL, 16);
  current_humidity = number & 0xFF;

  hexstring = "#" + text[9];
  number = (int) strtol( &hexstring[1], NULL, 16);
  current_batt = number & 0xFF;
  
  MAC_ADR = "";
  for (int i = 0; i < 6; i++) {
    MAC_ADR += text[i] + (i < 5 ? ":" : "");
  }

  BLE_DEVICE = -1;
for (int i = 0; i < liczbaElementow; i++) {
  if (MAC_ADR == xiaomiBleDeviceMacs[i]) {
    BLE_DEVICE = i ;
    wyswietl = 1;
    Wyswietl();
    break;  // Przerwij pętlę po znalezieniu pasującego adresu MAC
  }
}
}

//zerowanie wartości czujnika o id
void zerowanie(int index) {
  if (od[index] > 5) {
    temp[index] = -275;
    wilg[index] = -1;
    bat[index] = 0;
    od[index] = 0;
  }
}

// Wyświetlanie wartości na monitorze portu szeregowego
void Wyswietl(){
  if(wyswietl == 1){
    Serial.print(F("Urządzenie:"));
    Serial.print(BLE_DEVICE);
    Serial.print(F(" MAC: ")); 
    Serial.print(MAC_ADR);
                                   
    Serial.print(F(" Temp: "));
    Serial.print(current_temperature,1);
    Serial.print(F("stC. Wilgotnosc: "));
    Serial.print(current_humidity);
    Serial.print(F("%. Bateria: "));
    Serial.print(current_batt);
    Serial.println(F("%."));   
  
      temp[BLE_DEVICE] = current_temperature;
      wilg[BLE_DEVICE] = current_humidity;
      bat[BLE_DEVICE] = current_batt;
      od[BLE_DEVICE] = 0;
    

     for (int i = 0; i < liczbaElementow; i++) {
    zerowanie(i);
  }
    wyswietl = 0; 
  }
}
t0mek
Posts: 92
Joined: Sat Dec 02, 2017 11:24 am
Location: Legnica

Post

Adamus10 wrote: Sat Jan 27, 2024 7:34 pm Poprawka powyższego + parę modyfikacji, ale nie zmieniających schematu działania
Wjeżdża na testy. Dzięki :)
Pozdrawiam
Tomek

Return to “Zagadnienia ogólne”