-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp8266_bmp180_server.ino
86 lines (74 loc) · 2 KB
/
esp8266_bmp180_server.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
//================================================================
// esp8266_bmp180_server
//
// An ESP8266 with BMP180 attached to I2C to serve up temperature
// and pressure.
//
// * pin 4 = SDA
// * pin 5 = SCL
//
// Carter Nelson
// 2016-03-15
//================================================================
#include <ESP8266WiFi.h>
#include <Adafruit_BMP085.h>
#include "network_config.h" // your network config here
WiFiServer server(MY_PORT);
IPAddress IP_ADDRESS(MY_IP_ADDRESS);
IPAddress IP_GATEWAY(MY_IP_GATEWAY);
IPAddress IP_SUBNET(MY_IP_SUBNET);
Adafruit_BMP085 bmp;
void setup() {
Serial.begin(115200);
Serial.println("ESP8266 BMP180 Server");
// Connect to WiFi network
Serial.print("Connecting to ");
Serial.println(MY_SSID);
WiFi.config(IP_ADDRESS, IP_GATEWAY, IP_SUBNET);
WiFi.begin(MY_SSID, MY_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected. [ip addr ");
Serial.print(WiFi.localIP());
Serial.println("]");
server.begin();
Serial.println("Server started.");
// Initialize BMP180 sensor
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP180 sensor, check wiring!");
while (1) {}
}
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("New client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Process the request
req.toUpperCase();
String resp;
if (req.indexOf("GCT") != -1)
resp = String(32.0+1.8*bmp.readTemperature(), 2);
else if (req.indexOf("GCP") != -1)
resp = String(0.000145038*bmp.readPressure(), 2);
else
resp = "-999";
// Send the response to the client
Serial.println(resp);
client.print(resp);
delay(1);
Serial.println("Client disonnected");
}