#include<ESP8266WiFi.h> //include ESP8266 library
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // I2C
const char* ssid = "SSID"; // internet wifi name
const char* pass = "PASSWORD"; // wifi password
const char* server = "api.thingspeak.com"; // server name
String apikey = "WVGCEG6BBM1XTPHD"; // Api key from website
WiFiClient client; // create a client Class
void setup() {
Serial.begin(115200); // Initialize the Serial monitor at 9600 bps
unsigned status;
status = bme.begin(0x76, &Wire);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
while (1) delay(10);
}
Serial.println("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, pass); // start wifi with ssid and passoword
if (WiFi.status() != WL_CONNECTED) // waiting untill the net connected
{
Serial.print(".");
delay(100);
}
Serial.println();
Serial.print("WiFi is coonected");
Serial.println(WiFi.localIP());
}
void loop() {
float temp = bme.readTemperature(); // read temperature and store in float data type "temp variable"
float humid = bme.readHumidity(); // read humidity and store in float data type "humid" variable
float Press = bme.readPressure() / 100.0F;
Serial.print("Humidity: "); // print
Serial.print(humid);
Serial.print(" %, Temperature: ");
Serial.print(temp);
Serial.print("*C, Biometric Pressure: ");
Serial.print(Press);
Serial.println(" hPa");
if (client.connect(server, 80))
{
String postreq = apikey;
postreq += "&field1=";
postreq += String(temp);
postreq += "&field2=";
postreq += String(humid);
postreq += "&field3=";
postreq += String(Press);
postreq += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("HOST: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apikey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postreq.length());
client.print("\n\n");
client.print(postreq);
}
client.stop();
delay(1000);
}