From 1e7950b6713fa2cf0bb843c04860565efcf1ad4c Mon Sep 17 00:00:00 2001 From: cheetah Date: Mon, 1 May 2023 00:51:33 +0200 Subject: [PATCH] first commit --- lib/BLECast | 1 + lib/README | 46 ++++++++++++ platformio.ini | 20 +++++ src/main.cpp | 200 +++++++++++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 5 files changed, 278 insertions(+) create mode 160000 lib/BLECast create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/lib/BLECast b/lib/BLECast new file mode 160000 index 0000000..1a0e347 --- /dev/null +++ b/lib/BLECast @@ -0,0 +1 @@ +Subproject commit 1a0e347eb0534a2b1a1970110cdba4d3126db764 diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..68c4a85 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,20 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:seeed_xiao_esp32c3] +platform = espressif32 +board = seeed_xiao_esp32c3 +framework = arduino +monitor_speed = 115200 +monitor_port = /dev/ttyACM0 +upload_port = /dev/ttyACM0 +lib_deps = + paulstoffregen/OneWire@^2.3.7 + milesburton/DallasTemperature@^3.11.0 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..59e1328 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,200 @@ +#include + +#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ +#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */ + + +void print_wakeup_reason() { + esp_sleep_wakeup_cause_t wakeup_reason; + + wakeup_reason = esp_sleep_get_wakeup_cause(); + + switch(wakeup_reason) + { + case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break; + case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; + case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break; + case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break; + case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break; + case ESP_SLEEP_WAKEUP_GPIO : Serial.println("Wakeup caused by GPIO0"); break; + + default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break; + } +} +void deepsleep_fix_check() { + esp_sleep_wakeup_cause_t wakeup_reason; + + wakeup_reason = esp_sleep_get_wakeup_cause(); + + switch(wakeup_reason) + { + case ESP_SLEEP_WAKEUP_GPIO : + + esp_cpu_reset(0); + esp_cpu_reset(1); + break; + + default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break; + } +} + + + +#include +#include +OneWire oneWire(D0); +DallasTemperature sensors(&oneWire); +uint8_t sensorCount = 0; +DeviceAddress devices[20]; +int32_t rawTemperatures[20]; + +#include +#include +#include +#include + +#include +BLECast bleCast("cat"); + +void tempSetup() { + sensors.begin(); + sensors.setResolution(12); +} +void tempLoop() { + uint8_t scanOffset = 0; + do { + byte i; + if (!oneWire.search(devices[scanOffset])) { + oneWire.reset_search(); + delay(250); + break; + } + + Serial.print("ScanOffset="); Serial.print(scanOffset); + Serial.print(" ROM ="); + for (i = 0; i < 8; i++) Serial.print(devices[scanOffset][i], HEX); + Serial.println(); + scanOffset++; + } while (true); + delay(250); + + Serial.print("Requesting temperatures..."); + sensors.requestTemperatures(); + Serial.println("DONE"); + // + for (uint8_t addressIndex = 0; addressIndex < scanOffset; addressIndex++) { + sensors.setResolution(devices[addressIndex], 12); + Serial.print("ScanOffset="); Serial.print(addressIndex); + Serial.print("="); + for (uint8_t i = 0; i < 8; i++) Serial.print(devices[addressIndex][i], HEX); + Serial.print("="); + int32_t temp = sensors.getTemp(devices[addressIndex]); + Serial.print(temp); + Serial.print("="); + Serial.println(sensors.rawToCelsius(temp)); + rawTemperatures[addressIndex] = temp; + } + sensorCount = scanOffset; +} + +uint16_t batteryMillivolts = 0; +uint8_t batteryLevel = 0; +void battSetup(){ + pinMode(A1, INPUT); +} +void battLoop() { + uint32_t Vbatt = 0; + for (int i = 0; i < 16; i++) Vbatt = Vbatt + analogReadMilliVolts(A1); // denoise ADC + Serial.println(Vbatt / 16, 3); + float Vbattf = 1.455 * Vbatt / 16; /// 1000.0; + Serial.println(Vbattf, 3); + batteryMillivolts = (uint16_t)Vbattf; + delay(1000); +} + +void setup() { + Serial.begin(115200); + delay(1000); + + deepsleep_fix_check(); + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + + tempSetup(); + battSetup(); + + bleCast.begin(); +} + +struct AdvDataTemps { + uint16_t vBatt; + uint16_t addr1; + int16_t temp1; + uint16_t addr2; + int16_t temp2; + uint16_t addr3; + int16_t temp3; + uint16_t addr4; + int16_t temp4; + uint16_t addr5; + int16_t temp5; +} advdat; +uint16_t getSensorAddrShort(uint8_t index) { + uint16_t res = 0x0000; + res = devices[index][6] << 8; + res = res | devices[index][7]; + return res; +} +uint8_t sensorOffset = 0; +uint8_t loopCounter = 0; +void loop() { + if (loopCounter++ >= 3){ + Serial.println("going to sleep now"); + delay(1000); + + bleCast.end(); + oneWire.reset(); + + esp_deep_sleep_start(); + + return; + } + + tempLoop(); + battLoop(); + + advdat.vBatt = batteryMillivolts; + + advdat.addr1 = getSensorAddrShort(sensorOffset); + advdat.temp1 = (int16_t)rawTemperatures[sensorOffset]; + + sensorOffset++; + if (sensorOffset >= sensorCount) sensorOffset = 0; + advdat.addr2 = getSensorAddrShort(sensorOffset); + advdat.temp2 = (int16_t)rawTemperatures[sensorOffset]; + + sensorOffset++; + if (sensorOffset >= sensorCount) sensorOffset = 0; + advdat.addr3 = getSensorAddrShort(sensorOffset); + advdat.temp3 = (int16_t)rawTemperatures[sensorOffset]; + + sensorOffset++; + if (sensorOffset >= sensorCount) sensorOffset = 0; + advdat.addr4 = getSensorAddrShort(sensorOffset); + advdat.temp4 = (int16_t)rawTemperatures[sensorOffset]; + + sensorOffset++; + if (sensorOffset >= sensorCount) sensorOffset = 0; + advdat.addr5 = getSensorAddrShort(sensorOffset); + advdat.temp5 = (int16_t)rawTemperatures[sensorOffset]; + + sensorOffset++; + if (sensorOffset >= sensorCount) sensorOffset = 0; + + Serial.print("ManufData="); + for (uint8_t i = 0; i < sizeof(advdat); i++) Serial.print(((char*)&advdat)[i], HEX); + Serial.println(); + + bleCast.setManufacturerData((char*)&advdat, sizeof(advdat)); + + delay (1000); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html