-
Notifications
You must be signed in to change notification settings - Fork 40
/
Window_Static_CONFIGURE_JSON.ino
83 lines (73 loc) · 1.98 KB
/
Window_Static_CONFIGURE_JSON.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
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
ADC_MODE(ADC_VCC);
//USER CONFIGURED SECTION START//
const char* ssid = "YOUR_WIRELESS_SSID";
const char* password = "YOUR_WIRELESS_SSID";
const char* mqtt_server = "YOUR_MQTT_SERVER_ADDRESS";
const int mqtt_port = 1883;
const char *mqtt_user = "YOUR_MQTT_USERNAME";
const char *mqtt_pass = "YOUR_MQTT_PASSWORD";
const char *mqtt_client_name = "PICK_UNIQUE_WINDOW_NAME"; // Client connections cant have the same connection name
const char *mqtt_topic = "PICK_UNIQUE_WINDOW_TOPIC";
IPAddress ip(192, 168, 86, 48);
IPAddress gateway(192, 168, 86, 160);
IPAddress subnet(255, 255, 255, 0);
//USER CONFIGURED SECTION END//
WiFiClient espClient;
PubSubClient client(espClient);
// Variables
bool boot = true;
char openMessage[50];
char closedMessage[50];
//Functions
void setup_wifi()
{
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(50);
}
}
void reconnect()
{
while (!client.connected())
{
int battery_Voltage = ESP.getVcc() + 600;
String temp_str = String(battery_Voltage);
String mqttString1 = "{\"State\":\"closed\", \"voltage\":\"" + temp_str + "\"}";
mqttString1.toCharArray(closedMessage, mqttString1.length() + 1);
String mqttString2 = "{\"State\":\"open\", \"voltage\":\"" + temp_str + "\"}";
mqttString2.toCharArray(openMessage, mqttString2.length() + 1);
if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, mqtt_topic, 0, 1, closedMessage))
{
if(boot == true)
{
client.publish(mqtt_topic, openMessage);
boot = false;
}
}
else
{
ESP.restart();
}
}
}
void setup()
{
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void loop()
{
if (boot == true)
{
reconnect();
}
else
{
ESP.deepSleep(0);
}
}