-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino code
116 lines (92 loc) · 2.63 KB
/
Arduino code
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
#include <SoftwareSerial.h>
//Setting Up port, Wifi Id,Pass, server address etc.
#define RX 2
#define TX 3
String AP = "invalid"; // AP NAME
String PASS = "029144653Ss@$#"; // AP PASSWORD
String API = "WEO2LZHKNHV1ABR9"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field2";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
float valSensor = 1;
int measurePin = A0;
int ledPower = 12;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
SoftwareSerial esp8266(RX, TX);
#Begin the Wifi connectivity
void setup() {
Serial.begin(9600);
pinMode(ledPower, OUTPUT);
esp8266.begin(115200);
sendCommand("AT", 5, "OK");
sendCommand("AT+CWMODE=1", 5, "OK");
sendCommand("AT+CWJAP=\"" + AP + "\",\"" + PASS + "\"", 20, "OK");
}
# Send the data into the server
void loop() {
valSensor = getSensorData();
String getData = "GET /update?api_key=" + API + "&" + field + "=" + String(valSensor);
sendCommand("AT+CIPMUX=1", 5, "OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
esp8266.println(getData); delay(1500); countTrueCommand++;
sendCommand("AT+CIPCLOSE=0", 5, "OK");
delay(60000);
}
#get the sensor data
float getSensorData() {
digitalWrite(ledPower, LOW); // power on the LED
delayMicroseconds(samplingTime);
voMeasured = analogRead(measurePin); // read the dust value
delayMicroseconds(deltaTime);
digitalWrite(ledPower, HIGH); // turn the LED off
delayMicroseconds(sleepTime);
calcVoltage = voMeasured * (5.0 / 1024);
dustDensity = 0.17 * calcVoltage - 0.1;
dustDensity *= 1000;
Serial.print("Raw Signal Value (0-1023): ");
Serial.print(voMeasured);
Serial.print(" - Voltage: ");
Serial.print(calcVoltage);
Serial.print(" - Dust Density: ");
Serial.println(dustDensity);
return dustDensity;
}
#check whatif the data was send successfully or not
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while (countTimeCommand < (maxTime * 1))
{
esp8266.println(command);//at+cipsend
if (esp8266.find(readReplay)) //ok
{
found = true;
break;
}
countTimeCommand++;
}
if (found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if (found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}