-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm5stickc-restful-weather.ino
203 lines (168 loc) · 4.93 KB
/
m5stickc-restful-weather.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <WiFi.h>
#include <WebServer.h>
#include <M5StickC.h>
#include "DHT12.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>
#include "bmm150.h"
#include "bmm150_defs.h"
DHT12 dht12;
BMM150 bmm = BMM150();
bmm150_mag_data value_offset;
Adafruit_BMP280 bme;
float pressure = 0.0;
float headingDegrees = 0.0;
float tmp = 0.0;
float hum = 0.0;
/* Put your SSID & Password */
const char* ssid = "HackedNetwork";
const char* password = "12345678";
WebServer server(80);
void calibrate(uint32_t timeout)
{
int16_t value_x_min = 0;
int16_t value_x_max = 0;
int16_t value_y_min = 0;
int16_t value_y_max = 0;
int16_t value_z_min = 0;
int16_t value_z_max = 0;
uint32_t timeStart = 0;
bmm.read_mag_data();
value_x_min = bmm.raw_mag_data.raw_datax;
value_x_max = bmm.raw_mag_data.raw_datax;
value_y_min = bmm.raw_mag_data.raw_datay;
value_y_max = bmm.raw_mag_data.raw_datay;
value_z_min = bmm.raw_mag_data.raw_dataz;
value_z_max = bmm.raw_mag_data.raw_dataz;
delay(100);
timeStart = millis();
while((millis() - timeStart) < timeout)
{
bmm.read_mag_data();
/* Update x-Axis max/min value */
if(value_x_min > bmm.raw_mag_data.raw_datax)
{
value_x_min = bmm.raw_mag_data.raw_datax;
// Serial.print("Update value_x_min: ");
// Serial.println(value_x_min);
}
else if(value_x_max < bmm.raw_mag_data.raw_datax)
{
value_x_max = bmm.raw_mag_data.raw_datax;
// Serial.print("update value_x_max: ");
// Serial.println(value_x_max);
}
/* Update y-Axis max/min value */
if(value_y_min > bmm.raw_mag_data.raw_datay)
{
value_y_min = bmm.raw_mag_data.raw_datay;
// Serial.print("Update value_y_min: ");
// Serial.println(value_y_min);
}
else if(value_y_max < bmm.raw_mag_data.raw_datay)
{
value_y_max = bmm.raw_mag_data.raw_datay;
// Serial.print("update value_y_max: ");
// Serial.println(value_y_max);
}
/* Update z-Axis max/min value */
if(value_z_min > bmm.raw_mag_data.raw_dataz)
{
value_z_min = bmm.raw_mag_data.raw_dataz;
// Serial.print("Update value_z_min: ");
// Serial.println(value_z_min);
}
else if(value_z_max < bmm.raw_mag_data.raw_dataz)
{
value_z_max = bmm.raw_mag_data.raw_dataz;
// Serial.print("update value_z_max: ");
// Serial.println(value_z_max);
}
Serial.print(".");
delay(1);
}
value_offset.x = value_x_min + (value_x_max - value_x_min)/2;
value_offset.y = value_y_min + (value_y_max - value_y_min)/2;
value_offset.z = value_z_min + (value_z_max - value_z_min)/2;
}
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
M5.begin();
Wire.begin(0,26);
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 0, 2);
M5.Lcd.printf("RESTful weather", headingDegrees);
WiFi.begin(ssid, password);
// Setting the hostname
WiFi.setHostname("weather-stick");
Serial.print("Start WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("Connecting ..");
}
M5.Lcd.setCursor(0, 20, 2);
M5.Lcd.print("IP: ");
M5.Lcd.println(WiFi.localIP());
pinMode(M5_BUTTON_HOME, INPUT);
if(bmm.initialize() == BMM150_E_ID_NOT_CONFORM) {
Serial.println("Chip ID can not read!");
while(1);
} else {
Serial.println("Initialize done!");
}
if (!bme.begin(0x76)){
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
calibrate(10);
Serial.print("\n\rCalibrate done..");
delay(100);
server.on("/", handle_JsonResponse);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
Serial.print("Connected to the WiFi network. IP: ");
Serial.println(WiFi.localIP());
}
void handle_JsonResponse(){
String response;
response += "{ \"weather\": { \"tempreture\": ";
response += String(dht12.readTemperature(), 6);
response += ", \"humidity\": ";
response += String(dht12.readHumidity(), 6);
response += ", \"pressure\": ";
response += String(bme.readPressure(), 6);
response += " } }";
Serial.println(response);
server.send(200, "application/json", response);
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
uint8_t setup_flag = 1;
void loop() {
server.handleClient();
// put your main code here, to run repeatedly:
float tmp = dht12.readTemperature();
float hum = dht12.readHumidity();
M5.Lcd.setCursor(0, 40, 2);
M5.Lcd.printf("Temp: %2.1f Humi: %2.0f%%", tmp, hum);
float pressure = bme.readPressure();
M5.Lcd.setCursor(0, 60, 2);
M5.Lcd.printf("Pressure: %2.1f", pressure);
Serial.print("Pressure: ");
Serial.println(pressure);
delay(100);
if(!setup_flag){
setup_flag = 1;
calibrate(10);
Serial.print("\n\rCalibrate done..");
}
if(digitalRead(M5_BUTTON_HOME) == LOW){
setup_flag = 0;
while(digitalRead(M5_BUTTON_HOME) == LOW);
}
}