Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
int state ;
// Credentials
String deviceId = "55a63b5b25f2bc11200055db"; // * set your device id (will be the MQTT client username)
String deviceSecret = "SdO/u3JoNNO/PeUqsAkWR5tdgfwzq/LK"; // * set your device secret (will be the MQTT client password)
String clientId = "iTemp2"; // * set a random string (max 23 chars, will be the MQTT client id)
// WiFi name & password
const char* ssid = "id-wifi";
const char* password = "password";
// Sketch logic
// See more: http://lelylan.github.io/types-dashboard-ng/#/
char* payloadOn = "{\"properties\":[{\"id\":\"55a63a0314415711bf000008\",\"value\":\"on\"}]}";
char* payloadOff = "{\"properties\":[{\"id\":\"55a63a0314415711bf000008\",\"value\":\"off\"}]}";
// Topics
String outTopic = "devices/" + deviceId + "/set"; // * MQTT channel where physical updates are published
String inTopic = "devices/" + deviceId + "/get"; // * MQTT channel where lelylan updates are received
// WiFi client
WiFiClient wifiClient;
// Pins
int outPin = 5; // speakerpin
int inPin = 4;
// Logic
int stateS = HIGH; // current state of the output pin
int reading; // current reading from the input pin
int previous = LOW; // previous reading from the input pin
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// Pin & type
#define PIN_SPEAKER 5
#define PIN_VCC 16
#define PIN_GND 13
#define DHTPIN 14
#define DHTTYPE DHT22
// Initialize DHT sensor
DHT *dht;
// MQTT server address
IPAddress server(178, 62, 108, 47);
// MQTT
PubSubClient client(server);
void call(const MQTT::Publish& pub) {
// Copy the payload content into a char*
char* json;
json = (char*) malloc(pub.payload_len() + 1);
memcpy(json, pub.payload(), pub.payload_len());
json[pub.payload_len()] = '\0';
// Update the physical status and confirm the executed update
//boolean state;
if (String(payloadOn) == String(json)) {
Serial.println("Buzzer on");
lelylanPublish("on");
stateS = HIGH;
} else {
Serial.println("Buzzer off");
lelylanPublish("off");
stateS = LOW;
}
digitalWrite(outPin, stateS);
free(json);
}
void setup() {
Serial.begin(115200);
delay(500);
// Set callback
client.set_callback(call);
pinMode(PIN_SPEAKER, OUTPUT);
pinMode(PIN_VCC, OUTPUT);
pinMode(PIN_GND, OUTPUT);
digitalWrite( PIN_VCC, HIGH );
digitalWrite( PIN_GND, LOW );
digitalWrite(PIN_SPEAKER,LOW);
dht = new DHT( DHTPIN, DHTTYPE, 15 );
dht->begin();
// Connect to WiFi
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin("ssid", "password");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// MQTT server connection
lelylanConnection();
pinMode(outPin, OUTPUT); // led pin setup
pinMode(inPin, INPUT);
}
void loop() {
// Keep connection open
lelylanConnection();
reading = digitalRead(inPin);
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (stateS == LOW) {
Serial.println("Buzzer on");
lelylanPublish("on");
stateS = HIGH;
} else {
Serial.println("Buzzer off");
lelylanPublish("off");
stateS = LOW;
}
time = millis();
}
// Reading temperature and humidity
int h = dht->readHumidity();
// Read temperature as Celsius
int t = dht->readTemperature();
if (t >= 29){
Serial.println("Buzzer on");
lelylanPublish("on");
stateS = HIGH;
state = 1 ;
digitalWrite(PIN_SPEAKER, HIGH);
}else{
Serial.println("Buzzer off");
lelylanPublish("off");
stateS = LOW;
state = 0;
digitalWrite(PIN_SPEAKER, LOW);
}
// Messages for MQTT
String temperature = "{\"properties\":[{\"id\":\"55a63b7d14415708fe00000b\",\"value\":\"" + String(t) + "\"}]}";
String humidity = "{\"properties\":[{\"id\":\"55a63b8b144157ec61000008\",\"value\":\"" + String(h) + "\"}]}";
String state1 = "{\"properties\":[{\"id\":\"55a63b9614415711bf00000c\",\"value\":\"" + String(state) + "\"}]}";
// Publish temperature
client.publish(outTopic, (char *) temperature.c_str());
delay(100);
// Publish humidity
client.publish(outTopic, (char *) humidity.c_str());
delay(10000);
// Publish state
client.publish(outTopic, (char *) state1.c_str());
delay(10000);
// effectively update the light status
digitalWrite(outPin, stateS);
previous = reading;
}
/* MQTT server connection */
void lelylanConnection() {
// add reconnection logics
if (!client.connected()) {
// connection to MQTT server
if (client.connect(MQTT::Connect(clientId)
.set_auth(deviceId, deviceSecret))) {
Serial.println("[PHYSICAL] Successfully connected with MQTT");
lelylanSubscribe(); // topic subscription
}
}
client.loop();
}
/* MQTT publish */
void lelylanPublish(char* value) {
if (value == "on")
client.publish(outTopic, payloadOn); // light on
else
client.publish(outTopic, payloadOff); // light off
}
/* MQTT subscribe */
void lelylanSubscribe() {
client.subscribe(inTopic);
}
/* Receive Lelylan message and confirm the physical change */
void callback(char* topic, byte* payload, unsigned int length) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.