· 6 years ago · Apr 26, 2020, 03:10 PM
1#include <Wire.h>
2#include <ESP8266WiFi.h>
3#include <Adafruit_Sensor.h>
4#include <Adafruit_BME280.h>
5
6Adafruit_BME280 bme; // I2C
7
8float h, t, p, pin, dp;
9char temperatureString[6];
10char dpString[6];
11char humidityString[6];
12char pressureString[7];
13char pressureInchString[6];
14unsigned long delayTime;
15
16// replace with you First Channel ThingSpeak Write API key
17String apiKey = "api key";
18// replace with your local network SSID
19const char* ssid = "Naziv mreze";
20// replace with your local network password
21const char* password = "lozinka";
22const char* server = "api.thingspeak.com";
23
24WiFiClient client;
25
26// only runs once on boot
27void setup() {
28 // Initializing serial port for debugging purposes
29 Serial.begin(115200);
30 delay(10);
31 Wire.begin(D2, D1); // Make sure you have D3(SDA) & D4 (SCL) hooked up to the BME280 SENSOR
32 Wire.setClock(100000);
33 // Connecting to WiFi network
34 Serial.println();
35 Serial.print("Connecting to ");
36 Serial.println(ssid);
37
38 WiFi.begin(ssid, password);
39
40 while (WiFi.status() != WL_CONNECTED) {
41 delay(500);
42 Serial.print(".");
43 }
44 Serial.println("");
45 Serial.println("WiFi connected");
46
47 // Printing the ESP IP address
48 Serial.println(WiFi.localIP());
49
50 if (!bme.begin()) {
51 Serial.println("Could not find a valid BME280 sensor, check wiring!");
52 while (1);
53 }
54}
55
56void loop() {
57 h = bme.readHumidity();
58 t = bme.readTemperature();//temperature in Celcius
59 //t = t*1.8+32.0; //uncomment if you want to get temperature in Fahrenheit
60 dp = t-0.36*(100.0-h);
61
62 p = bme.readPressure()/100.0F;
63 pin = 0.02953*p;
64 dtostrf(t, 5, 1, temperatureString);
65 dtostrf(h, 5, 1, humidityString);
66 dtostrf(p, 6, 1, pressureString);
67 dtostrf(pin, 5, 2, pressureInchString);
68 dtostrf(dp, 5, 1, dpString);
69 delay(10000);
70
71 Serial.print("Temperature = ");
72 Serial.println(temperatureString);
73 Serial.print("Humidity = ");
74 Serial.println(humidityString);
75 Serial.print("Pressure = ");
76 Serial.println(pressureString);
77 Serial.print("Pressure Inch = ");
78 Serial.println(pressureInchString);
79 Serial.print("Dew Point = ");
80 Serial.println(dpString);
81
82 if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
83 {
84 String postStr = apiKey;
85 postStr +="&field1=";
86 postStr += String(temperatureString);
87 postStr +="&field2=";
88 postStr += String(humidityString);
89 postStr +="&field3=";
90 postStr += String(pressureString);
91 //postStr += String(pressureInchString);//uncomment if you need pressure in inch
92 postStr += "\r\n\r\n";
93
94 client.print("POST /update HTTP/1.1\n");
95 client.print("Host: api.thingspeak.com\n");
96 client.print("Connection: close\n");
97 client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
98 client.print("Content-Type: application/x-www-form-urlencoded\n");
99 client.print("Content-Length: ");
100 client.print(postStr.length());
101 client.print("\n\n");
102 client.print(postStr);
103 }
104 client.stop();
105 //every 30 seconds
106 delay(30000);
107}