-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlame_Sensor_ESP.ino
More file actions
94 lines (76 loc) · 2.6 KB
/
Copy pathFlame_Sensor_ESP.ino
File metadata and controls
94 lines (76 loc) · 2.6 KB
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
/*****************************************************************************************************************************
********************************** Author : Ehab Magdy Abdullah *************************************
********************************** Linkedin: https://www.linkedin.com/in/ehabmagdyy/ *************************************
********************************** Youtube : https://www.youtube.com/@EhabMagdyy *************************************
******************************************************************************************************************************/
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <PubSubClient.h>
/* Analog pin connected to the AO of Flame sensor */
#define FLAME_AN_PIN 35 /* Note: Do not select any ADC2 pin, beacuse it doesn't work when using WIFI */
#define ssid "2001" /* Your Wifi SSID */
#define password "19821968" /* Your Wifi Password */
#define mqtt_server "test.mosquitto.org" /* Mosquitto Server URL */
WiFiClient espClient;
PubSubClient client(espClient);
uint16_t analog_flame_value = 0;
uint16_t flame_percentage = 0;
void setup_wifi()
{
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect()
{
while(!client.connected())
{
Serial.println("Attempting MQTT connection...");
if(client.connect("EhabESPClient"))
{
Serial.println("Connected");
}
else
{
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop()
{
if (!client.connected()){ reconnect(); }
client.loop();
// Read value of Flame Sensor and map it
analog_flame_value = analogRead(FLAME_AN_PIN);
flame_percentage = map(analog_flame_value, 0, 4095, 100, 0);
String payload = String(flame_percentage);
// Send data to Node-RED
client.publish("Ehab/Flame", payload.c_str());
Serial.print("Flame: ");
Serial.print(flame_percentage);
Serial.println("%");
delay(1000);
}