first commit

This commit is contained in:
2023-06-26 22:46:45 +02:00
commit 2072c89796
2 changed files with 141 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
.vscode
+140
View File
@@ -0,0 +1,140 @@
// #define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_RF24
// #define MY_RF24_PA_LEVEL RF24_PA_MIN
// #define MY_RF24_PA_LEVEL RF24_PA_LOW
#define MY_RF24_PA_LEVEL RF24_PA_HIGH
//#define MY_RF24_PA_LEVEL RF24_PA_MAX
#define MY_RF24_CHANNEL 14
// #define MY_SIGNING_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
// #define MY_SECURITY_SIMPLE_PASSWD "C@KptBi1L@5op?8!"
#include <SPI.h>
#include <MySensors.h>
// https://github.com/enjoyneering/AHTxx/blob/main/examples/AHT10_Serial/AHT10_Serial.ino
#include <Wire.h>
#include <AHTxx.h>
static const uint64_t UPDATE_INTERVAL = 900000; // (15min * 60sec = 900 seconds * 1000 milliseconds)
#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_BAT 2
#define CHILD_ID_BATV 3
bool metric = true;
float ahtTemp; //to store T/RH result
float ahtHumi;
int oldBatteryPcnt = 0;
#define FULL_BATTERY 3 // 3V for 2xAA alkaline. Adjust if you use a different battery setup.
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgBat(CHILD_ID_BAT, V_VAR1);
MyMessage msgBatPrefix(CHILD_ID_BAT, V_UNIT_PREFIX); // Custom unit message.
MyMessage msgBatV(CHILD_ID_BATV, V_VAR2);
MyMessage msgBatVPrefix(CHILD_ID_BATV, V_UNIT_PREFIX); // Custom unit message.
// AHT10
// #define AHT1X_POWER_ON_DELAY 100 //wait for AHT1x to initialize after power-on, in milliseconds
// #define AHTXX_I2C_SPEED_HZ 400000
AHTxx aht10(0x38, AHT1x_SENSOR); //sensor address, sensor type
// AHTxx aht10(0x00, 0x38);
void presentation()
{
Serial.println("- - - - - presentation - - - - -");
// Send the sketch version information to the gateway
sendSketchInfo("aht10", "0.4.2");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_TEMP, S_TEMP, "Temperature");
present(CHILD_ID_HUM, S_HUM, "Humidity");
present(CHILD_ID_BAT, S_CUSTOM, "Battery Percent");
present(CHILD_ID_BATV, S_CUSTOM, "Battery Voltage");
metric = getControllerConfig().isMetric;
}
void setup()
{
#ifdef MY_DEBUG
Serial.println("- - - - - setup - - - - -");
// Serial.println(AHT1x_SENSOR);
// aht10.setType(AHT1x_SENSOR);
#endif
while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2);
{
Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
delay(5000);
}
#ifdef MY_DEBUG
Serial.println(F("AHT10 OK"));
#endif
}
void loop()
{
#ifdef MY_DEBUG
Serial.println("- - - - - loop - - - - -");
Serial.println("getting humidity & temperature");
#endif
ahtTemp = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
#ifdef MY_DEBUG
Serial.print(F("Temperature: "));
Serial.print(ahtTemp);
Serial.println(F(" +-0.3C"));
#endif
// static MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
send(msgTemp.set(ahtTemp, 1));
delay(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
ahtHumi = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
#ifdef MY_DEBUG
Serial.print(F("Humidity: "));
Serial.print(ahtHumi);
Serial.println(F(" +-2%"));
#endif
// static MyMessage msgHum(CHILD_ID_HUM, V_HUM);
send(msgHum.set(ahtHumi, 1));
delay(2000);
// get the battery Voltage
long batteryMillivolts = hwCPUVoltage();
float batteryVolts = batteryMillivolts / 1000.0;
int batteryPcnt = batteryMillivolts / FULL_BATTERY / 1000.0 * 100 + 0.5;
#ifdef MY_DEBUG
Serial.print("Battery voltage: ");
Serial.print(batteryVolts);
Serial.println("V");
Serial.print("Battery percent: ");
Serial.print(batteryPcnt);
Serial.println(" %");
#endif
if (oldBatteryPcnt != batteryPcnt) {
sendBatteryLevel(batteryPcnt);
oldBatteryPcnt = batteryPcnt;
send(msgBatPrefix.set("%")); // Set custom unit.
send(msgBat.set(batteryPcnt, 1));
send(msgBatVPrefix.set("V")); // Set custom unit.
send(msgBatV.set(batteryVolts, 1));
}
// Sleep for a while to save energy
sleep(UPDATE_INTERVAL);
}