Skip to content

Instantly share code, notes, and snippets.

@simonjamain
Created May 16, 2019 17:14
Show Gist options
  • Save simonjamain/38f41c7a2382036090348753e80fd3b9 to your computer and use it in GitHub Desktop.
Save simonjamain/38f41c7a2382036090348753e80fd3b9 to your computer and use it in GitHub Desktop.
Code (platformio) capteur de température connecté wemos d1 mini
#include <Homie.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN D4
#define DHTTYPE DHT11 // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
const int TEMPERATURE_INTERVAL_MS = 1UL * 60UL * 1000UL;
unsigned long lastTemperatureSent = 0;
HomieNode temperatureNode("temperature", "temperature");
void setupHandler() {
temperatureNode.setProperty("unit").send("c");
}
void loopHandler() {
if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL_MS || lastTemperatureSent == 0) {
sensors_event_t event;
dht.temperature().getEvent(&event);
if (!isnan(event.temperature)) {
//float temperature = 22; // Fake temperature here, for the example
Homie.getLogger() << "Temperature: " << event.temperature << " °C" << endl;
temperatureNode.setProperty("degrees").send(String(event.temperature));
lastTemperatureSent = millis();
}
}
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
dht.begin();
Homie_setFirmware("awesome-temperature", "1.0.0");
Homie.setResetTrigger(D1, LOW, 2000);
Homie.setSetupFunction(setupHandler).setLoopFunction(loopHandler);
temperatureNode.advertise("unit");
temperatureNode.advertise("degrees");
Homie.setup();
}
void loop() {
Homie.loop();
}
; 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:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
lib_deps = Homie, DHT sensor library, Adafruit Unified Sensor
; Serial Monitor options
monitor_speed = 115200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment