Address of tempsensors is published to MQTT

This commit is contained in:
Ollo 2021-04-07 20:27:42 +02:00
parent dc5dc27ba5
commit 3932e82593
4 changed files with 62 additions and 19 deletions

View File

@ -82,5 +82,6 @@ DS18B20 one wire temp sensor
* Nicht mehrere Messungen vom Temp-Sensor nur warten, bis wir einen Wert bekommen * Nicht mehrere Messungen vom Temp-Sensor nur warten, bis wir einen Wert bekommen
* Partitionslayout * Partitionslayout
* OW-search address in topic nutzen * OW-search address in topic nutzen
* Wifi bei timout deaktivieren (damit wir entweder wlan spielen oder fallback: nur pflanzen giessen)

View File

@ -46,8 +46,8 @@ int readTemp();
******************************************************************************/ ******************************************************************************/
//only relevant if mode2 did start pumping before //only relevant if mode2 did start pumping before
RTC_DATA_ATTR int lastPumpRunning = 0; /**< store last successfully waterd plant */ RTC_DATA_ATTR int lastPumpRunning = 0; /**< store last successfully waterd plant */
RTC_DATA_ATTR long lastWaterValue = 0; /**< to calculate the used water per plant */ RTC_DATA_ATTR long lastWaterValue = 0; /**< to calculate the used water per plant */
RTC_DATA_ATTR int gBootCount = 0; RTC_DATA_ATTR int gBootCount = 0;
@ -192,12 +192,6 @@ void mode2MQTT()
sensorLipo.setProperty("CCA").send(String(battery.getCCA())); sensorLipo.setProperty("CCA").send(String(battery.getCCA()));
sensorSolar.setProperty("volt").send(String(mSolarVoltage)); sensorSolar.setProperty("volt").send(String(mSolarVoltage));
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(mTempLipo));
Serial << "Lipo Temperatur " << mTempLipo << " °C " << endl;
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(mTempWater));
Serial << "Water Temperatur " << mTempWater << " °C " << endl;
sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(mChipTemp)); sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(mChipTemp));
Serial << "Chip Temperatur " << mChipTemp << " °C " << endl; Serial << "Chip Temperatur " << mChipTemp << " °C " << endl;
@ -261,9 +255,12 @@ void readDistance()
sei(); sei();
int mmDis = duration * 0.3432 / 2; int mmDis = duration * 0.3432 / 2;
if(mmDis > MAX_TANK_DEPTH){ if (mmDis > MAX_TANK_DEPTH)
{
waterRawSensor.add(0); waterRawSensor.add(0);
} else { }
else
{
waterRawSensor.add(mmDis); waterRawSensor.add(mmDis);
} }
} }
@ -276,13 +273,13 @@ void readDistance()
void readSensors() void readSensors()
{ {
Serial << "Read Sensors" << endl; Serial << "Read Sensors" << endl;
/* activate all sensors */ /* activate all sensors */
digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH); digitalWrite(OUTPUT_ENABLE_SENSOR, HIGH);
/* wait before reading something */ /* wait before reading something */
delay(20); delay(20);
int timeoutTemp = millis() + TEMPERATUR_TIMEOUT; int timeoutTemp = millis() + TEMPERATUR_TIMEOUT;
int sensorCount = 0; uint8_t sensorCount = 0U;
/* Required to read the temperature at least once */ /* Required to read the temperature at least once */
while ((sensorCount == 0 || !battery.isFound()) && millis() < timeoutTemp) while ((sensorCount == 0 || !battery.isFound()) && millis() < timeoutTemp)
@ -300,10 +297,33 @@ void readSensors()
sensors.requestTemperatures(); sensors.requestTemperatures();
} }
for (int i = 0; i < sensorCount; i++) { for (uint8_t i = 0; i < sensorCount; i++)
float temp = sensors.getTempCByIndex(i); {
DeviceAddress ds18b20Address;
sensors.getAddress(ds18b20Address, i);
float temp = sensors.getTempC(ds18b20Address);
Serial << "OneWire sensor " << i << " has value " << temp << endl; Serial << "OneWire sensor " << i << " has value " << temp << endl;
/** FIXME: handle via address */ char buf[sizeof(DeviceAddress) * 2];
snprintf(buf, sizeof(buf), "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X",
ds18b20Address[0],
ds18b20Address[1],
ds18b20Address[2],
ds18b20Address[3],
ds18b20Address[4],
ds18b20Address[5],
ds18b20Address[6],
ds18b20Address[7]);
if (String(lipoSensorAddr.get()).compareTo(String(buf))) {
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(temp));
Serial << "Lipo Temperatur " << temp << " °C " << endl;
} else if (String(waterSensorAddr.get()).compareTo(String(buf))) {
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(temp));
Serial << "Water Temperatur " << temp << " °C " << endl;
}
/* Always send the sensor address with the temperatur value */
sensorTemp.setProperty(String(buf)).send(String(temp));
Serial << "Temperatur " << String(buf) << " : " << temp << " °C " << endl;
} }
// Update battery chip data // Update battery chip data
@ -455,8 +475,8 @@ void systemInit()
// Set default values // Set default values
//in seconds //in seconds
deepSleepTime.setDefaultValue(600).setValidator([] (long candidate) { deepSleepTime.setDefaultValue(600).setValidator([](long candidate) {
return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */ ); return (candidate > 0) && (candidate < (60 * 60 * 2) /** 2h max sleep */);
}); });
deepSleepNightTime.setDefaultValue(600); deepSleepNightTime.setDefaultValue(600);
wateringDeepSleep.setDefaultValue(5); wateringDeepSleep.setDefaultValue(5);

View File

@ -15,3 +15,5 @@ framework = arduino
build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
lib_deps = OneWire lib_deps = OneWire
DallasTemperature DallasTemperature
upload_port = /dev/ttyUSB1

View File

@ -110,9 +110,29 @@ void setup() {
Serial.print("Bat I"); Serial.print("Bat I");
Serial.print("\t"); Serial.print("\t");
Serial.println("Temp/10"); Serial.println("Temp/10");
} }
void loop() { void loop() {
static int loop=1;
DeviceAddress t;
for(int i=0; i < sizeof(t); i++) {
t[i] = loop + i*2;
}
char buf[sizeof(DeviceAddress)*2];
snprintf(buf, sizeof(buf), "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X", t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
/*
%X -> linksbündige hexzahl
%2X -> 2Stellige hexzahl ... dynamisch erweitert
%0.2X -> 2stellige hexzahl mit führerder "0"
*/
printf("Print: %s\n", buf);
loop++;
delay(500);
return;
whatever = !whatever; whatever = !whatever;
digitalWrite(OUTPUT_PUMP, HIGH); digitalWrite(OUTPUT_PUMP, HIGH);
delay(500); delay(500);