I was so happy to have finaly a fonctionnal sketch for my esp8266 gateway whith the DHT22 that i post directly my code.
I hope that it will help some people.
based on: https://github.com/RobTillaart/DHTNew/blob/master/examples/dhtnew_endless/dhtnew_endless.ino
Seawolf
@Seawolf
Best posts made by Seawolf
Latest posts made by Seawolf
-
RE: 💬 Relay
-
RE: 💬 Relay
// Enable debug prints to serial monitor
#define MY_DEBUG// Enables and select radio type (if attached)
#define MY_RADIO_RF24
//#define MY_RADIO_RFM69
//#define MY_RADIO_RFM95#define MY_GATEWAY_ESP8266
#define MY_WIFI_SSID "SSID"
#define MY_WIFI_PASSWORD "PSW"// Enable UDP communication
//#define MY_USE_UDP // If using UDP you need to set MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS below// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
#define MY_HOSTNAME "ESP8266_GW"// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
//#define MY_IP_ADDRESS 192,168,0,0// If using static ip you can define Gateway and Subnet address as well
//#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
//#define MY_IP_SUBNET_ADDRESS 255,255,255,0// The port to keep open on node server mode
#define MY_PORT 5003// How many clients should be able to connect to this gateway (default 1)
#define MY_GATEWAY_MAX_CLIENTS 2// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
//#define MY_CONTROLLER_URL_ADDRESS "my.controller.org"// Enable inclusion mode
//#define MY_INCLUSION_MODE_FEATURE// Enable Inclusion mode button on gateway
//#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
//#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
//#define MY_INCLUSION_MODE_BUTTON_PIN D1// Set blinking period
//#define MY_DEFAULT_LED_BLINK_PERIOD 300// Flash leds on rx/tx/err
// Led pins used if blinking feature is enabled above
//#define MY_DEFAULT_ERR_LED_PIN 16 // Error led pin
//#define MY_DEFAULT_RX_LED_PIN 16 // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN 16 // the PCB, on board LED
#define LWIP_OPEN_SRC whatever
#define TCP_MSS whatever
#define LWIP_IPV6 whatever
#define LWIP_FEATURES whatever#include <MySensors.h>
#include <SPI.h>
//DHT with autodetection#include <dhtnew.h>
DHTNEW mySensor(5); // ESP 16 UNO 6
uint32_t count = 0;
uint32_t start, stop;uint32_t errors[10] = { 0,0, 0,0, 0,0, 0,0, 0,0 };
//Domoticz
#define CHILD_ID_HUM 2
#define CHILD_ID_TEMP 3
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);void presentation()
{
// Send the sketch version information to the gateway
sendSketchInfo("TemperatureAndHumidity", "1.1");// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID_HUM, S_HUM);
present(CHILD_ID_TEMP, S_TEMP);}
void setup()
{
Serial.begin(115200);
Serial.println("DHT_endless.ino");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHTNEW_LIB_VERSION);
Serial.println();
}void loop()
{
count++;
// show counters for OK and errors.
if (count % 50 == 0)
{
Serial.println();
Serial.println("OK \tCRC \tTOA \tTOB \tTOC \tTOD \tSNR \tBS \tUNK");
for (uint8_t i = 0; i < 9; i++)
{
Serial.print(errors[i]);
Serial.print('\t');
}
Serial.println();
Serial.println();
}if (count % 10 == 0)
{
Serial.println();
Serial.println("TIM\tCNT\tSTAT\tHUMI\tTEMP\tTIME\tTYPE");
}
Serial.print(millis());
Serial.print("\t");
Serial.print(count);
Serial.print("\t");start = micros();
int chk = mySensor.read();
stop = micros();switch (chk)
{
case DHTLIB_OK:
Serial.print("OK,\t");
errors[0]++;
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print("CRC,\t");
errors[1]++;
break;
case DHTLIB_ERROR_TIMEOUT_A:
Serial.print("TOA,\t");
errors[2]++;
break;
case DHTLIB_ERROR_TIMEOUT_B:
Serial.print("TOB,\t");
errors[3]++;
break;
case DHTLIB_ERROR_TIMEOUT_C:
Serial.print("TOC,\t");
errors[4]++;
break;
case DHTLIB_ERROR_TIMEOUT_D:
Serial.print("TOD,\t");
errors[5]++;
break;
case DHTLIB_ERROR_SENSOR_NOT_READY:
Serial.print("SNR,\t");
errors[6]++;
break;
case DHTLIB_ERROR_BIT_SHIFT:
Serial.print("BS,\t");
errors[7]++;
break;
default:
Serial.print("U");
Serial.print(chk);
Serial.print(",\t");
errors[8]++;
break;
}
// DISPLAY DATA
#ifdef MY_DEBUG
Serial.print(mySensor.getHumidity(), 1);
Serial.print(",\t");
Serial.print(mySensor.getTemperature(), 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.print(",\t");
Serial.println(mySensor.getType());
#endif//send to gateway
float humidity = mySensor.getHumidity();
send(msgHum.set(humidity, 1));
float temperature = mySensor.getTemperature();
send(msgTemp.set(temperature, 1));delay(3000);
}// -- END OF FILE --