-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalThingspeakSensor.ino
117 lines (96 loc) · 2.88 KB
/
finalThingspeakSensor.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
// id: 2297602, key = 5ACP1C2BJ1UNG72L
//Library
#include <WiFi.h>
#include "ThingSpeak.h"
//Nie amek dari secrete.h
#define SECRET_SSID "XXXXXX"
#define SECRET_PASS "passsword"
#define SECRET_CH_ID ID 000000000
#define SECRET_WRITE_APIKEY "APITHINGSPEAK"
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
String myStatus = "";
//==================
//Definition
const int lightSensor = 34;
const int irSensor = 35;
const int soundSensor = 12;
const int gasSensor = 4;
// Define the range of raw sensor values
const int rawMin = 55;
const int rawMax = 445;
// Define the corresponding CO2 range
const long co2Min = 1000;
const long co2Max = 400;
//variable
int lightValue;
int irStatus;
int soundValue;
int rawValue;
void setup() {
Serial.begin(9600);
pinMode(lightSensor,INPUT);
pinMode(irSensor, INPUT);
pinMode(soundSensor, INPUT);
pinMode(gasSensor, INPUT);
//=========
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
//AnalogSensor
lightValue = analogRead(lightSensor);
Serial.print("Light Value: ");
Serial.print(lightValue);
//IR Sensor
irStatus = digitalRead(irSensor);
Serial.print(" IR Status:");
Serial.print(irStatus);
//Sound Level
soundValue = analogRead(soundSensor);
Serial.print(" Sound Value:");
Serial.print(soundValue);
//MQ Sensor
rawValue = analogRead(gasSensor);
long co2Value = mapCO2Value(rawValue);
Serial.print(" Gas Value:");
Serial.println(co2Value);
delay(500);
//====== WiFi & ThingSpeak=======
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// set the fields with the values
ThingSpeak.setField(1, lightValue); //sensorValue
ThingSpeak.setField(2, co2Value); //sensorValue
ThingSpeak.setField(3, soundValue); //sensorValue
ThingSpeak.setField(4, irStatus); //sensorValue
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
delay(15000);
//========Thingspeak=====//
}
// Function to map the CO2 value based on the global range variables
long mapCO2Value(long x) {
return map(x, rawMin, rawMax, co2Min, co2Max);
}