-
Notifications
You must be signed in to change notification settings - Fork 24
/
sds011_ds18b20_display_thingspeak.ino
134 lines (116 loc) · 3.51 KB
/
sds011_ds18b20_display_thingspeak.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
126
127
128
129
130
131
132
133
134
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
SSD1306 display(0x3c, D3, D7); // SDA, SCL
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
SoftwareSerial swSer(D2, D1, false, 256);
const char* host = "api.thingspeak.com";
String url = "/update?api_key=YQFVMWXGIAHSG846"; // Your Own Key here
const int httpPort = 80;
const char* ssid = "kipfa-class1_2.4G";
const char* password = "classroom1";
String working(int pm25, int pm10, float temp) {
return(String("field1=")+String(pm25) +"&field2="+String(pm10) +"&field3="+ String(temp));
}
void delivering(String payload) {
WiFiClient client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpPort)) {
Serial.print("connection failed: ");
Serial.println(payload);
return;
}
String getheader = "GET "+ String(url) +"&"+ String(payload) +" HTTP/1.1";
client.println(getheader);
client.println("User-Agent: ESP8266 Kyuho Kim");
client.println("Host: " + String(host));
client.println("Connection: close");
client.println();
Serial.println(getheader);
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
Serial.println("Done cycle.");
}
void connect_ap() {
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\n Got WiFi, IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
swSer.begin(9600);
connect_ap();
Serial.println("\nSoftware serial test started");
display.init();
sensors.begin();
}
int stat = 1;
int cnt = 0;
char buf[10];
int readytosend = 0;
void loop() {
while (swSer.available() > 0) {
char c;
c = swSer.read();
//Serial.print("stat="+ String(stat) +", "+ "cnt="+ String(cnt) +" ");
//Serial.print(c, HEX);
//Serial.println(" ");
switch (stat) {
case 1:
if (c == 0xAA) stat = 2;
break;
case 2:
if (c == 0xC0) stat =3;
else stat = 1;
break;
case 3:
buf[cnt++] = c;
if (cnt == 7) stat = 4;
break;
case 4:
if (c == 0xAB) {
//check checusum
stat = 1;
}
else {
Serial.println("Eh? wrong tailer");
}
cnt = 0;
int pm25 = buf[0] + 256*buf[1];
int pm10 = buf[2] + 256*buf[3];
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Serial.println(String(readytosend) +" "+String(pm25) +","+ String(pm10)+ ", "+ String(temp) +" ");
display.clear();
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, "Dust Montor V1.2");
display.flipScreenVertically();
display.setFont(ArialMT_Plain_16);
display.drawString(0, 16, "PM2.5: "+ String(pm25));
display.drawString(0, 32, "PM10: "+ String(pm10));
display.drawString(0, 48, "Temp: "+ String(temp));
display.display();
if (readytosend++ > 15) {
delivering(working(pm25, pm10, temp));
readytosend = 0;
}
break;
}
}
}