-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino Code.ino
More file actions
121 lines (102 loc) · 2.65 KB
/
Arduino Code.ino
File metadata and controls
121 lines (102 loc) · 2.65 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
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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTPIN 13
#define DHTTYPE DHT11
//Enter the credentials of your WiFi
const char* ssid = "";
const char* password = "";
//Enter the address for the MQTT Server
//You can create a local server by using Mosquitto
const char* mqtt_server = "";
WiFiClient esp8266device;
PubSubClient client(esp8266device);
DHT dht(DHTPIN, DHTTYPE);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
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.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
//When new data is received from MQTT
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messagein;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messagein += (char)message[i];
}
if(topic=="fan/status"){
if (messagein == "ON"){
Serial.println("\nFan ON");
digitalWrite(4, LOW);
}
if(messagein == "OFF"){
Serial.println("\nFan OFF");
digitalWrite(4, HIGH);
}
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("device1")) {
Serial.println("connected");
client.subscribe("fan/status");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
dht.begin();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
randomSeed(analogRead(0));
pinMode(4 , OUTPUT);
digitalWrite(4,HIGH);
}
void loop() {
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("device1");
// int temp=random(0, 100);
char temp [8];
char hum[8];
delay(2000);
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% Temperature: ");
Serial.print(t);
Serial.print("°C\n ");
dtostrf(t,6,2,temp);
dtostrf(h , 6 , 2 ,hum);
client.publish("room/temp",temp);
client.publish("room/hum" ,hum );
}