here is my working code to gather data and upload over wifi to thingspeak account.
#include <ESP8266WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include "SparkFunBME280.h"
#include "Wire.h"
#include "SPI.h"
//Global sensor object
BME280 mySensorA;
String apiKey = "thingspeakapikey"; //fill in the api key fromthingspeak
const char* ssid = "wireless id"; //fill in your wifi name
const char* password = "password"; //fill in your wifi password
const char* server = "api.thingspeak.com";
WiFiClient client;
#define OLED_RESET LED_BUILTIN //4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup()
{
//***Set up sensor 'A'******************************//
//commInterface can be I2C_MODE or SPI_MODE
mySensorA.settings.commInterface = I2C_MODE;
mySensorA.settings.I2CAddress = 0x76;
mySensorA.settings.runMode = 3; // 3, Normal mode
mySensorA.settings.tStandby = 0; // 0, 0.5ms
mySensorA.settings.filter = 0; // 0, filter off
//tempOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.tempOverSample = 1;
//pressOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.pressOverSample = 1;
//humidOverSample can be:
// 0, skipped
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively
mySensorA.settings.humidOverSample = 1;
//***Initialize the things**************************//
Serial.begin(57600);
Serial.print("Program Started\n");
Serial.println("Starting BME280s... result of .begin():");
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
//Calling .begin() causes the settings to be loaded
Serial.print("Sensor A: 0x");
Serial.println(mySensorA.begin(), HEX);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
Serial.println("WiFi connected");
Serial.println();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clear the buffer.
display.clearDisplay();
display.display();
}
void loop()
{
//Start with temperature, as that data is needed for accurate compensation.
//Reading the temperature updates the compensators of the other functions
//in the background.
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempC(), 2);
Serial.print(", ");
Serial.println(" degrees C");
Serial.print("Temperature: ");
Serial.print(mySensorA.readTempF(), 2);
Serial.print(", ");
Serial.println(" degrees F");
Serial.print("Pressure: ");
Serial.print(mySensorA.readFloatPressure(), 2);
Serial.print(", ");
Serial.println(" Pa");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeMeters(), 2);
Serial.print(", ");
Serial.println("m");
Serial.print("Altitude: ");
Serial.print(mySensorA.readFloatAltitudeFeet(), 2);
Serial.print(", ");
Serial.println("ft");
Serial.print("%RH: ");
Serial.print(mySensorA.readFloatHumidity(), 2);
Serial.print(", ");
Serial.println(" %");
Serial.println();
display.setTextSize(2);
display.setTextColor(WHITE);
// Clear the buffer.
display.clearDisplay();
display.setCursor(0, 0);
display.print("T: ");
display.print(mySensorA.readTempC());
display.print(" C");
display.print("P: ");
display.print((mySensorA.readFloatPressure()) / 100);
display.print(" mb");
display.display();
//--------------------------thingspeak-------------------------
if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr += "&field1=";
postStr += String(mySensorA.readTempC());
postStr += "&field2=";
postStr += String(mySensorA.readFloatPressure() / 100);
postStr += "&field3=";
postStr += String(mySensorA.readFloatHumidity());
postStr += "&field4=";
postStr += String();
postStr += "&field5=";
postStr += String();
postStr += "&field6=";
postStr += String();
postStr += "&field7=";
postStr += String();
postStr += "&field8=";
postStr += String();
postStr += "\r\n\r\n\r\n\r\n\r\n\r\n\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(postStr.length());
client.print("\n\n\n\n\n\n\n\n");
client.print(postStr);
}
client.stop();
// thingspeak needs minimum 15 sec delay between updates
delay(20000);
}
as you see, for example;
"mySensorA.readTempC" is the reading for temperature. how to upload this to WU?
thank you in advance.