commit 279541f3c3049c30cbc678cec9ca8d2a384be0e9 Author: Knut Ahlers Date: Fri May 29 02:52:18 2020 +0200 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..1d3415e --- /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:heltec_wifi_lora_32_V2] +platform = espressif32 +board = heltec_wifi_lora_32_V2 +framework = arduino + +lib_deps = + Heltec ESP32 Dev-Boards@1.0.9 + TTN_esp32@0.1.0 + +lib_ldf_mode = deep diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b9d2ade --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,102 @@ +#include +#include "heltec.h" +#include "config.h" +#include "main.h" + +TTN_esp32 ttn; + +void loop() { + // Read voltage and add as uint8 to transmission + float voltage = readVoltage(); + uint8_t payload = (uint8_t)(int)(voltage * 10); + + if (ENABLE_SEND) { + sendVoltage(payload); + } else { + // Sending is disabled, display value and give a moment to read + Heltec.display->drawString(0, 22, "Current Voltage: " + String((float)payload / 10) + "V"); + Heltec.display->display(); + delay(1000); + } + + if (voltage >= BATTERY_LOAD_VOLTAGE || DISABLE_SLEEP) { + // We're getting power from the charger, no need to deep sleep. + // Just wait for a moment to do the next measurement which will + // allow us to receive a measurement shortly after car shuts down. + delay(CHARGE_SLEEP_MS); + } else { + // We're running on battery and should preserve as much power + // as possible, so we power down and ask in a few minutes again. + esp_sleep_enable_timer_wakeup(BATTERY_SLEEP_US); + esp_deep_sleep_start(); + } +} + +float readVoltage() { + Serial.println("Reading voltage..."); + + int rawSum = 0; + for (int i = 0; i < MEAS_COUNT; i++) { + rawSum += analogRead(PIN_VOLTAGE_READ); + delay(MEAS_WAIT_MS); + } + + float raw = rawSum / float(MEAS_COUNT); + float vout = raw * 3.3 / 4094.0; + float value = vout / (7500.0 / (30000.0 + 7500.0)) + CORRECT_DIFF; + + Serial.printf("Voltage read: %.1f %.2f\r\n", raw, value); + return value; +} + +void sendVoltage(uint8_t payload) { + // Make sure any pending transactions are handled first + ttn.waitForPendingTransactions(); + + Heltec.display->clear(); + // Transmit voltage and display it until deep-sleep + if (ttn.sendBytes(&payload, sizeof(payload))) { + Heltec.display->drawString(0, 22, "Current Voltage: " + String((float)payload / 10) + "V"); + } + Heltec.display->display(); + + // Make sure our transactions is handled before going to sleep + ttn.waitForPendingTransactions(); +} + +void setup() { + Heltec.begin(true /* DisplayEnable */, false /* LoRaEnable */, true /* SerialEnable */, false /* PABOOST */, BAND /* long BAND */); + + analogReadResolution(12); + + // Initialize display for usage + Heltec.display->init(); + Heltec.display->flipScreenVertically(); + Heltec.display->setFont(ArialMT_Plain_10); + Heltec.display->clear(); + + // Join TTN and reset counters + Serial.println("Starting TTN"); + ttn.begin(); + ttn.join(devEui, appEui, appKey); + + // Wait for join to be completed + Heltec.display->clear(); + Heltec.display->drawString(0, 0, "Joining TTN:"); + Heltec.display->display(); + int x = 60; + while (!ttn.isJoined()) { + Heltec.display->drawString(x, 0, "."); + Heltec.display->display(); + delay(250); + x++; + } + Heltec.display->clear(); + Heltec.display->drawString(0, 0, "TTN Connection online..."); + Heltec.display->display(); + + // Add connection info + Serial.println("Setup done"); + Heltec.display->clear(); + Heltec.display->drawString(0, 0, "TTN Connection online..."); +} diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..2128bc7 --- /dev/null +++ b/src/main.h @@ -0,0 +1,7 @@ +#ifndef MAIN_H_INCLUDED +#define MAIN_H_INCLUDED + +float readVoltage(); +void sendVoltage(uint8_t payload); + +#endif