1 |
#include <ESP8266WiFi.h>
|
2 |
#include <PubSubClient.h>
|
3 |
|
4 |
// Pins Config
|
5 |
const int LightPin = D0;
|
6 |
const int PhotoPin = A0;
|
7 |
const int DefaultPin = D4;
|
8 |
|
9 |
// Wifi Config
|
10 |
const char* ssid = "SSID";
|
11 |
const char* password = "PASSWORD";
|
12 |
|
13 |
// MQTT Config
|
14 |
const char* mqtt_server = "mqtt.tingg.io";
|
15 |
const int port = 1883; // 8883 for mqtts connections.
|
16 |
|
17 |
// Thing Config
|
18 |
const char* thing_id = "###";
|
19 |
const char* thing_key = "###";
|
20 |
|
21 |
// Topics Config
|
22 |
const char* pubTopic = "<pub_resource>";
|
23 |
const char* subTopic = "<sub_resource>";
|
24 |
|
25 |
|
26 |
// Default Config
|
27 |
const char* username = "thing";
|
28 |
|
29 |
WiFiClient espClient;
|
30 |
PubSubClient client(espClient);
|
31 |
|
32 |
// Vars
|
33 |
int val;
|
34 |
int prevVal = 0;
|
35 |
char buf[12];
|
36 |
long lastUpdateMillis = 0;
|
37 |
|
38 |
|
39 |
// Function to validate connection with the board
|
40 |
void blinking_loop(){
|
41 |
while (ssid == "SSID") {
|
42 |
digitalWrite(DefaultPin,HIGH);
|
43 |
delay(500);
|
44 |
digitalWrite(DefaultPin,LOW);
|
45 |
delay(500);
|
46 |
Serial.println("Default light should be blinking. Please set up your Wifi configuration.");
|
47 |
}
|
48 |
}
|
49 |
|
50 |
void setup_wifi() {
|
51 |
delay(10);
|
52 |
Serial.print("Connecting to ");
|
53 |
Serial.println(ssid);
|
54 |
WiFi.mode(WIFI_STA);
|
55 |
|
56 |
WiFi.begin(ssid, password);
|
57 |
while (WiFi.status() != WL_CONNECTED) {
|
58 |
delay(500);
|
59 |
Serial.print(".");
|
60 |
}
|
61 |
|
62 |
randomSeed(micros());
|
63 |
|
64 |
Serial.println("");
|
65 |
Serial.print("Congratulations!! WiFi connected on IP address: ");
|
66 |
Serial.println(WiFi.localIP());
|
67 |
Serial.println("");
|
68 |
Serial.println("Now, follow up on the steps for the MQTT configuration. ");
|
69 |
}
|
70 |
|
71 |
void reconnect() {
|
72 |
while (!client.connected()) {
|
73 |
Serial.print("Attempting to connect MQTT...");
|
74 |
if (client.connect(thing_id, username, thing_key)) {
|
75 |
Serial.println("connected");
|
76 |
client.subscribe(subTopic);
|
77 |
} else {
|
78 |
Serial.print(" Still not connected..."); // Serial.print(client.state());
|
79 |
Serial.println(" trying again in 5 seconds"); // Wait 5 seconds before retrying
|
80 |
delay(5000);
|
81 |
}
|
82 |
}
|
83 |
}
|
84 |
|
85 |
String message(byte* payload, unsigned int length) {
|
86 |
payload[length] = '\0';
|
87 |
String s = String((char*)payload);
|
88 |
return s;
|
89 |
}
|
90 |
|
91 |
void callback(char* topic, byte* payload, unsigned int length) {
|
92 |
Serial.println(topic);
|
93 |
String msg = message(payload, length);
|
94 |
|
95 |
if ((msg == "ON") || (msg == "on") || (msg == "1")) {
|
96 |
Serial.println("Light turned On");
|
97 |
digitalWrite(LightPin,HIGH);
|
98 |
}
|
99 |
else if ((msg == "OFF") || (msg == "off") || (msg == "0")) {
|
100 |
Serial.println("Light turned Off");
|
101 |
digitalWrite(LightPin,LOW);
|
102 |
}
|
103 |
else {
|
104 |
Serial.println("Unknown command. Please try: ON, on, 1, OFF, off or 0.");
|
105 |
}
|
106 |
}
|
107 |
|
108 |
void setup() {
|
109 |
pinMode(LightPin,OUTPUT);
|
110 |
pinMode(PhotoPin,INPUT);
|
111 |
pinMode(DefaultPin,OUTPUT);
|
112 |
|
113 |
Serial.begin(115200);
|
114 |
blinking_loop();
|
115 |
setup_wifi();
|
116 |
client.setServer(mqtt_server, port);
|
117 |
client.setCallback(callback);
|
118 |
}
|
119 |
|
120 |
void loop() {
|
121 |
if (!client.connected()) {
|
122 |
reconnect();
|
123 |
}
|
124 |
client.loop();
|
125 |
|
126 |
val = analogRead(PhotoPin);
|
127 |
|
128 |
if (prevVal != val || millis() - lastUpdateMillis > 3000) {
|
129 |
lastUpdateMillis = millis();
|
130 |
prevVal = val;
|
131 |
client.publish(pubTopic,itoa(val, buf, 10));
|
132 |
Serial.print("Updating value to ");
|
133 |
Serial.println(val);
|
134 |
delay(250);
|
135 |
}
|
136 |
delay(50);
|
137 |
}
|