forked from SamZorSec/Open-Home-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha_mqtt_sensor_photocell.ino
158 lines (130 loc) · 4.36 KB
/
ha_mqtt_sensor_photocell.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
MQTT Sensor - Brightness (photocell) for Home-Assistant - NodeMCU (ESP8266)
https://home-assistant.io/components/sensor.mqtt/
Libraries :
- ESP8266 core for Arduino : https://github.com/esp8266/Arduino
- PubSubClient : https://github.com/knolleary/pubsubclient
- ArduinoJson : https://github.com/bblanchon/ArduinoJson
Sources :
- File > Examples > ES8266WiFi > WiFiClient
- File > Examples > PubSubClient > mqtt_auth
- File > Examples > PubSubClient > mqtt_esp8266
- File > Examples > ArduinoJson > JsonGeneratorExample
Schematic :
- https://github.com/mertenats/open-home-automation/blob/master/ha_mqtt_sensor_photocell/Schematic.png
- Photocell leg 1 - VCC
- Photocell leg 2 - A0 - Resistor 10K Ohms - GND
- D0/GPIO16 - RST (wake-up purpose)
Configuration (HA) :
sensor 1:
platform: mqtt
state_topic: 'office/sensor1'
name: 'Brightness'
unit_of_measurement: '%'
value_template: '{{ value_json.brightness }}'
Samuel M. - v1.1 - 08.2016
If you like this example, please add a star! Thank you!
https://github.com/mertenats/open-home-automation
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#define MQTT_VERSION MQTT_VERSION_3_1_1
// Wifi: SSID and password
const PROGMEM char* WIFI_SSID = "[Redacted]";
const PROGMEM char* WIFI_PASSWORD = "[Redacted]";
// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "office_brightness";
const PROGMEM char* MQTT_SERVER_IP = "[Redacted]";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "[Redacted]";
const PROGMEM char* MQTT_PASSWORD = "[Redacted]";
// MQTT: topic
const PROGMEM char* MQTT_SENSOR_TOPIC = "office/sensor1";
// sleeping time
const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 600; // 10 minutes x 60 seconds
// Photocell: A0
const PROGMEM uint8_t PHOTOCELL_PIN = 0;
WiFiClient wifiClient;
PubSubClient client(wifiClient);
// function called to publish the temperature and the humidity
void publishData(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();
root["brightness"] = (String)brightness;
root.prettyPrintTo(Serial);
Serial.println("");
/*
{
"brightness": "75"
}
*/
char data[200];
root.printTo(data, root.measureLength() + 1);
client.publish(MQTT_SENSOR_TOPIC, data);
}
// 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);
// 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();
// read the value of the photocell
uint16_t photocell = analogRead(PHOTOCELL_PIN);
if (photocell < 0 || photocell > 1024) {
Serial.println("ERROR: Failed to read from the photocell!");
return;
} else {
publishData(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
}