Skip to content

Instantly share code, notes, and snippets.

@blackfyre
Created June 23, 2017 04:59
Show Gist options
  • Save blackfyre/a88b8ed36375b7773aca9a45bf07ef1e to your computer and use it in GitHub Desktop.
Save blackfyre/a88b8ed36375b7773aca9a45bf07ef1e to your computer and use it in GitHub Desktop.
Desk Sensor
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <ArduinoJson.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const char* WIFI_SSID = "____________";
const char* WIFI_PASSWORD = "____________";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "desk_sensor";
const PROGMEM char* MQTT_SERVER_IP = "____________";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "____________";
const PROGMEM char* MQTT_PASSWORD = "____________";
// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "ha/sensorpacks/office/desk";
// sleeping time
const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 600; // 10 minutes x 60 seconds
// DHT - D1/GPIO5
#define DHTPIN D1
#define DHTTYPE DHT11
// Photocell: A0
const PROGMEM uint8_t PHOTOCELL_PIN = 0;
DHT dht(DHTPIN, DHTTYPE);
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the temperature and the humidity
void publishData(float p_temperature, float p_humidity, int p_analogRead) {
// convert 0-1024 into a percentage
uint8_t brightness = map(p_analogRead, 0, 1024, 0, 100);
// create a JSON object
// doc : https://github.com/bblanchon/ArduinoJson/wiki/API%20Reference
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
// INFO: the data must be converted into a string; a problem occurs when using floats...
root["temperature"] = (String)p_temperature;
root["humidity"] = (String)p_humidity;
root["brightness"] = (String)brightness;
root.prettyPrintTo(Serial);
Serial.println("");
/*
{
"temperature": "23.20" ,
"humidity": "43.70"
}
*/
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data, true);
}
// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("INFO: Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("INFO: connected");
} else {
Serial.print("ERROR: failed, rc=");
Serial.print(client.state());
Serial.println("DEBUG: try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// init the serial
Serial.begin(115200);
dht.begin();
// init the WiFi connection
Serial.println();
Serial.println();
Serial.print("INFO: Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("INFO: WiFi connected");
Serial.println("INFO: IP address: ");
Serial.println(WiFi.localIP());
// init the MQTT connection
client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// read the value of the photocell
uint16_t photocell = analogRead(PHOTOCELL_PIN);
if (isnan(h) || isnan(t)) {
Serial.println("ERROR: Failed to read from DHT sensor!");
return;
}
if (photocell < 0 || photocell > 1024) {
Serial.println("ERROR: Failed to read from the photocell!");
return;
}
publishData(t, h, photocell);
Serial.println("INFO: Closing the MQTT connection");
client.disconnect();
Serial.println("INFO: Closing the Wifi connection");
WiFi.disconnect();
ESP.deepSleep(SLEEPING_TIME_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
delay(500); // wait for deep sleep to happen
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment