-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGasSensor.ino
69 lines (54 loc) · 1.54 KB
/
GasSensor.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
/*
GasSensor.ino - Example for Library SmartDevices.
For device: MQ-9 (gas sensor).
Created by Marcelo A. and Caio Souza, November 2, 2021.
Released into the public domain.
*/
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "ESP8266HTTPClient.h"
#include "SmartDevices.h"
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 size;
// Set up the client objet
WiFiClient client;
HTTPClient http;
SmartDevices smartDevices(server_url, client, http);
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);
Serial.print("Gas voltage: " );
Serial.println(sensor_volt);
}
void setup() {
delay(3000);
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
smartDevices.initConnection(devicesIds, 1);
delay(1000);
}
void loop() {
readGasStatus();
String measures[] = {String(gasStatus)};
size = sizeof(measures) / sizeof(String);
smartDevices.sendMeasures(devicesIds[0], measures, size);
delay(1000);
}