ThingsPeak + ESP8266 + ds18b20 + BMP180 weather station
Good day everyday. Today we continue to add sensors to ESP8266 today it is BMP180 barometric pressure sensor and temperature.
Open the Arduino IDE create a sketch with the following code. Choose the type of board it ESP8266 and port to which it is attached.
/* This sketch sends data via HTTP GET requests to data.sparkfun.com service. You need to get streamId and privateKey at data.sparkfun.com and paste them below. Or just customize this script to talk to other HTTP servers. */ #include <ESP8266WiFi.h> #include <OneWire.h> #include <DallasTemperature.h> #include <stdlib_noniso.h> #include <Adafruit_BMP085.h> #define ONE_WIRE_BUS 2 // DS18B20 pin OneWire oneWire(ONE_WIRE_BUS); DallasTemperature DS18B20(&oneWire); #define I2C_SCL 12 // Barometric Pressure Sensor (BMP085) #define I2C_SDA 13 // Barometric Pressure Sensor (BMP085) Adafruit_BMP085 bmp; const char* ssid = "KonstRenome"; const char* password = ""; const char* host = "api.thingspeak.com"; const char* APIkey = "UQKZPLM0AC7N08H2"; float oldTemp; bool bmp085_present=true; float dst,bt,bp,ba; char dstmp[20],btmp[20],bprs[20],balt[20]; void setup() { Serial.begin(115200); delay(10); Wire.begin(I2C_SDA, I2C_SCL); delay(10); if (!bmp.begin()) { Serial.println("No BMP085 sensor detected!"); bmp085_present=false; } // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { delay(30000); DS18B20.requestTemperatures(); dst = DS18B20.getTempCByIndex(0); Serial.print("Temperature: "); Serial.println(dst); dtostrf(dst, 8, 2, dstmp); if(bmp085_present) { bt = bmp.readTemperature();//(bmp.readTemperature() * 9/5) + 32; Serial.print("Temperature bmp: "); Serial.println(bt); dtostrf(bt, 8, 2, btmp); } if(bmp085_present) { bp = bmp.readPressure()/133.3224;// / 3386; Serial.print("Pressure: "); Serial.println(bp); dtostrf(bp, 8, 2, bprs); } if(bmp085_present) { Serial.print("Real altitude = "); Serial.print(bmp.readAltitude(101500)); Serial.println(" meters"); ba=bmp.readAltitude(101500); dtostrf(ba, 8, 2, balt); } Serial.print("connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // We now create a URI for the request String url = "/update?key="; url += APIkey; url += "&field1="; url += dstmp;//DS Temp; url += "&field2="; url += btmp;//BM Temp; url += "&field3="; url += bprs;//BM Press; url += "&field4="; url += balt;//BM Alt; Serial.print("Requesting URL: "); Serial.println(url); // This will send the request to the server client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); // Read all the lines of the reply from server and print them to Serial while (client.available()) { String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); }
If you have compiling problems caused some libraries are not installed, attach it through the menu Sketch> Connect library.
This code every 30 seconds will send to thingspeak temperature of ds18b20 in Field 1, the temperature of BMP in field 2, BMP pressure in the field 3, altitude BMP in field4.
That’s all thank you for your attention.
No Comments