Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By dgncsk
#68666 I am interested in personal weather stations. I built simple one by utilizing wemos d1 mini board with external antenna on it. I am using sparkfun BME280 sensor to read temperature, pressure and humidity. I also have a 0.96" oled screen to see readings. I managed to upload my instantaneous reading to thingspeak.com but i am confused when try to figure the WU wiki page. Could you please tell me the very simple data upload sketch to WU( I want to add that part to my code) ?

here is my working code to gather data and upload over wifi to thingspeak account.

Code: Select all#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.
User avatar
By dgncsk
#68712 here is my code. this is working partially. it uploads data to thingspeak account and to weather underground by using http protocol but.. it uploads only once. it seems i need to close connection and open again to WU but my code does not work. i included wifi connection section inside loop nothing changes.

here is the code
Code: Select all#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <stdint.h>
#include "SparkFunBME280.h"

#include "Wire.h"
#include "SPI.h"

float tempf;
float humidity;
float baromin;






//Global sensor object
BME280 mySensorA;


String apiKey = "xxxxxxxxxxxxxx";                                     //fill in the api key from thingspeak
const char* ssid = "TTNET_ZyXEL_AFR3";                                  //fill in your wifi name
const char* password = "xxxxxxxxxxxxx";                              //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();
//vvvvvvvvvvvvvvvvvvvvvvvvvvv




tempf = mySensorA.readTempF();
humidity = mySensorA.readFloatHumidity();
baromin = mySensorA.readFloatPressure();

//vvvvvvvvvvvvvvvvvvvvvvvvvvvvv



}

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);


//WU

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http;  //Declare an object of class HTTPClient
 
    http.begin("https://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?action=updateraw&ID=IANKARA23&PASSWORD=xxxxxxxx&tempf=70&baromin=29.1&humidity=90&softwaretype=vws%20versionxx&action=updateraw&realtime=1&rtfreq=2.5");  //Specify request destination
    int httpCode = http.GET();                                                                  //Send the request
 
    if (httpCode > 0) { //Check the returning code
 
      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);                     //Print the response payload
 
    }
   
    http.end();   //Close connection

  }
  }
  client.stop();

 
  // thingspeak needs minimum 15 sec delay between updates
   delay(20000);
}

User avatar
By dgncsk
#68951 Thanks for reply. Since i adapt the code i am not sure what may cause this upload problem. I need to correct this sketch to loop as it can do for thingspeak. How to make WU part loop continuously?

I forgot to add i declared some variables and they represent actısl readings from sensor not like constants on previous sketch.