-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSensor.ino
125 lines (97 loc) · 2.77 KB
/
MultiSensor.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
/*
MultiSensor.ino - Example for Library SmartDevices.
For devices: DHT11 (temperature and humidity sensor), HC-SR501 PIR (presence sensor) and MQ-9 (gas sensor).
Created by Marcelo A. and Caio Souza, November 2, 2021.
Released into the public domain.
*/
#include "DHT.h"
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "ESP8266HTTPClient.h"
#include "SmartDevices.h"
#define DHTPIN D1 // what digital pin the DHT11 is conected to
#define DHTTYPE DHT11 // setting the type as DHT11
DHT dht(DHTPIN, DHTTYPE);
int h = 0;
float t = 0;
#define PIRPIN D2
boolean presence;
float sensor_volt;
int sensorValue;
bool gasStatus;
const char* ssid = ""; //Enter your WIFI ssid
const char* password = ""; //Enter your WIFI password
String server_url = "";// Enter the API endpoint without '/' at the end
String devicesIds[] = {"id1", "id2"}; //Enter your device Id
int dRead;
int size;
// Set up the client objet
WiFiClient client;
HTTPClient http;
SmartDevices smartDevices(server_url, client, http);
void readDht11(){
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Humidity = ");
Serial.print(h);
Serial.print("% ");
Serial.print("Temperature = ");
Serial.print(t);
Serial.println("C ");
}
void readPresence(){
dRead = digitalRead(PIRPIN);
if(dRead == HIGH){
presence = true;
}else {
presence = false;
}
Serial.print("Presence: " );
Serial.println(presence);
}
void readGasStatus(){
sensorValue = analogRead(A0);
sensor_volt = ((float)sensorValue / 1024) * 5.0;
if(sensor_volt > 1){
gasStatus = true;
}else {
gasStatus = false;
}
Serial.print("Gas Status: " );
Serial.println(gasStatus);
}
void setup() {
delay(3000);
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
smartDevices.initConnection(devicesIds, 1);
pinMode(PIRPIN, INPUT);
delay(1000);
}
void loop() {
readDht11();
String measures[] = {String(h),String(t)};
size = sizeof(measures) / sizeof(String);
smartDevices.sendMeasures(devicesIds[0], measures, size);
delay(500);
readPresence();
String measures2[] = {String(presence)};
size = sizeof(measures2) / sizeof(String);
smartDevices.sendMeasures(devicesIds[1], measures2, size);
delay(500);
readGasStatus();
String measures3[] = {String(gasStatus)};
size = sizeof(measures3) / sizeof(String);
smartDevices.sendMeasures(devicesIds[2], measures3, size);
delay(5000);
}