Danke schon mal für die Hilfe.
Thank you in advance for the help.
Ich bin jetzt so weit das alle Daten gesendet werden, bis auf die ds18b20. Ich habe die Verkabelung überprüft, kann jedoch keinen Fehler finden. Bin ich Blind? Sieht jemand eventuell einen Fehler in dem Sketch?
I am now so far that all data is sent, except for the ds18b20. I have checked the cabling but can not find a fault. Am I blind? Does anyone see an error in the sketch?
#include <MySensor.h>
//ds18b20 & DHT
#include <DHT.h>
#include <DallasTemperature.h>
#include <OneWire.h>
//
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
#define CHILD_ID_MOISTURE1 0
#define CHILD_ID_MOISTURE2 1
#define CHILD_ID_MOISTURE3 2
#define CHILD_ID_MOISTURE4 3
//#define CHILD_ID_BATTERY 1
#define SENSOR_ANALOG_PIN 0
#define SENSOR_ANALOG_PIN1 1
#define SENSOR_ANALOG_PIN2 2
#define SENSOR_ANALOG_PIN3 3
#define SENSOR_POWER_PIN 8
//#define SLEEP_TIME 600000 // Sleep time between reads (in milliseconds) (=10 MINUTES)
#define STABILIZATION_TIME 300 // Let the sensor stabilize before reading
//#define BATTERY_FULL 3700 // 3,700 millivolts
//#define BATTERY_ZERO 1700 // 1,700 millivolts
//ds18b20 & DHT
#define MAX_ATTACHED_DS18B20 6
#define ONE_WIRE_BUS 6
#define DHT1_DATA_PIN 7
#define CHILD_ID_DHT1HUM 6
#define CHILD_ID_DHT1TEMP 7
#define SENSOR_TEMP_OFFSET 0
// Sleep time between sensor updates (in milliseconds)
// Must be >1000ms for DHT22 and >2000ms for DHT11
static const uint64_t UPDATE_INTERVAL = 3000;
unsigned long SLEEP_TIME = 30000;
boolean receivedConfig = false;
boolean metric = true;
int numSensors=0;
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
MyMessage msgDallas(0,V_TEMP);
MyMessage msg6DHT1Temp(CHILD_ID_DHT1TEMP,V_TEMP);
MyMessage msg7DHT1Hum(CHILD_ID_DHT1HUM,V_HUM);
DHT dht;
//
MySensor gw;
MyMessage msg1(CHILD_ID_MOISTURE1, V_HUM);
MyMessage msg2(CHILD_ID_MOISTURE2, V_HUM);
MyMessage msg3(CHILD_ID_MOISTURE3, V_HUM);
MyMessage msg4(CHILD_ID_MOISTURE4, V_HUM);
//MyMessage voltage_msg(CHILD_ID_BATTERY, V_VOLTAGE);
int lastsoilValue1 = 0;
int lastsoilValue2 = 0;
int lastsoilValue3 = 0;
int lastsoilValue4 = 0;
//ds18b20 & DHT
void before()
{
// Startup up the OneWire library
sensors.begin();
}
//
void setup()
{
gw.begin();
gw.sendSketchInfo("Bodenfeuchte_Temperatur_Hum", "1.0");
//ds18b20 & DHT
// requestTemperatures() will not block current thread
sensors.setWaitForConversion(true);
// Fetch the number of attached temperature sensors
numSensors = sensors.getDeviceCount();
// Present all sensors to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++)
gw.present(i, S_TEMP);
gw.present(CHILD_ID_DHT1TEMP,S_TEMP);
gw.present(CHILD_ID_DHT1HUM,S_HUM);
metric = gw.getConfig().isMetric;
//
pinMode(SENSOR_POWER_PIN, OUTPUT);
gw.present(CHILD_ID_MOISTURE1, S_HUM);
gw.present(CHILD_ID_MOISTURE2, S_HUM);
gw.present(CHILD_ID_MOISTURE3, S_HUM);
gw.present(CHILD_ID_MOISTURE4, S_HUM);
delay(250);
//gw.present(CHILD_ID_BATTERY, S_CUSTOM);
}
void loop()
{
//ds18b20 & DHT
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// query conversion time and sleep until conversion completed
int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
// sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
gw.sleep(conversionTime);
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Send in the new temperature
gw.send(msgDallas.setSensor(i).set(temperature,1));
//#ifdef MY_DEBUG
Serial.print("Dallas");
Serial.print(i);
Serial.print(" :");
Serial.println(temperature);
//#endif
gw.wait(200);
// sleep(SLEEP_TIME);
}
dht.setup(DHT1_DATA_PIN); // set data pin of DHT1 sensor
if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
}
// Sleep for the time of the minimum sampling period to give the sensor time to power up
// (otherwise, timeout errors might occure for the first reading)
gw.sleep(dht.getMinimumSamplingPeriod());
// Force reading sensor, so it works also after sleep()
// dht.readSensor(false);
// Get temperature from DHT library
float dht1temperature = dht.getTemperature();
if (isnan(dht1temperature)) {
Serial.println("Failed reading temperature from DHT!");
} else
if (!metric) {
dht1temperature = dht.toFahrenheit(dht1temperature);
}
dht1temperature += SENSOR_TEMP_OFFSET;
gw.send(msg6DHT1Temp.set(dht1temperature, 1));
//#ifdef MY_DEBUG
Serial.print("DHT1TEMP: ");
Serial.println(dht1temperature);
//#endif
gw.sleep(UPDATE_INTERVAL);
// Get humidity from DHT library
float dht1humidity = dht.getHumidity();
if (isnan(dht1humidity)) {
Serial.println("Failed reading humidity from DHT");
}
else {
gw.send(msg7DHT1Hum.set(dht1humidity, 1));
//#ifdef MY_DEBUG
Serial.print("DHT1HUM: ");
Serial.println(dht1humidity);
//#endif
}
// Sleep for a while to save energy
gw.sleep(UPDATE_INTERVAL);
//gw.wait(300);
//DHT Ende
digitalWrite(SENSOR_POWER_PIN, HIGH); // Power on the sensors
//gw.wait(500);
gw.sleep(STABILIZATION_TIME); //stabilization before mesuring
int moistureLevel = (1023 - analogRead(SENSOR_ANALOG_PIN)) / 10.23; // read moisture of the first sensor
if (moistureLevel != lastsoilValue1) { //test if moisture is different than before
gw.send(msg1.set(moistureLevel)); //send to controller moisture level of sensor 1
lastsoilValue1 = moistureLevel;
gw.wait(200);
}
int moistureLevel1 = (1023 - analogRead(SENSOR_ANALOG_PIN1)) / 10.23; // read moisture of the second sensor
if (moistureLevel1 != lastsoilValue2) { //test if moisture is different than before
gw.send(msg2.set(moistureLevel1)); //send to controller moisture level of sensor 2
lastsoilValue2 = moistureLevel1;
gw.wait(200);
}
int moistureLevel2 = (1023 - analogRead(SENSOR_ANALOG_PIN2)) / 10.23; // read moisture of the third sensor
if (moistureLevel2 != lastsoilValue3) { //test if moisture is different than before
gw.send(msg3.set(moistureLevel2)); //send to controller moisture level of sensor 3
lastsoilValue3 = moistureLevel2;
gw.wait(200);
}
int moistureLevel3 = (1023 - analogRead(SENSOR_ANALOG_PIN3)) / 10.23; // read moisture of fourth sensor
if (moistureLevel3 != lastsoilValue4) { //test if moisture is different than before
gw.send(msg4.set(moistureLevel3)); //send to controller moisture level of sensor 4
lastsoilValue3 = moistureLevel3;
gw.wait(200);
}
//debugging comment it when dont use
Serial.print("first plant :");
Serial.println(moistureLevel);
Serial.print("second plant :");
Serial.println(moistureLevel1);
Serial.print("third plant :");
Serial.println(moistureLevel2);
Serial.print("fourth plant :");
Serial.println(moistureLevel3);
digitalWrite(SENSOR_POWER_PIN, LOW); //spower off sensors
//long voltage = readVcc();
//gw.send(voltage_msg.set(voltage / 1000.0, 3)); // redVcc returns millivolts and set wants volts and how many decimals (3 in our case)
//gw.sendBatteryLevel(round((voltage - BATTERY_ZERO) * 100.0 / (BATTERY_FULL - BATTERY_ZERO)));
gw.sleep(SLEEP_TIME);
}
/*long readVcc()
{
// From http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}*/```
send: 6-6-1-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
send: 6-6-1-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
send: 6-6-1-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:1
read: 0-1-6 s=255,c=3,t=15,pt=6,l=2,sg=0:0100
read: 0-1-6 s=255,c=3,t=6,pt=0,l=1,sg=0:M
sensor started, id=6, parent=1, distance=2
send: 6-6-1-0 s=255,c=3,t=11,pt=0,l=25,sg=0,st=ok:Bodenfeuchte_Temperatur_H
send: 6-6-1-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
send: 6-6-1-0 s=7,c=0,t=6,pt=0,l=0,sg=0,st=ok:
send: 6-6-1-0 s=6,c=0,t=7,pt=0,l=0,sg=0,st=ok:
send: 6-6-1-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=ok:
send: 6-6-1-0 s=1,c=0,t=7,pt=0,l=0,sg=0,st=ok:
send: 6-6-1-0 s=2,c=0,t=7,pt=0,l=0,sg=0,st=fail:
send: 6-6-1-0 s=3,c=0,t=7,pt=0,l=0,sg=0,st=ok:
send: 6-6-1-0 s=7,c=1,t=0,pt=7,l=5,sg=0,st=ok:18.3
DHT1TEMP: 18.30
send: 6-6-1-0 s=6,c=1,t=1,pt=7,l=5,sg=0,st=ok:25.0
DHT1HUM: 25.00
first plant :0
second plant :0
third plant :0
fourth plant :0