Showing posts with label thingspeak. Show all posts
Showing posts with label thingspeak. Show all posts

Saturday, December 2, 2017

BME280 and ESP8266

Latest environmental sensor from Bosh is the BME280 which can measure:

  • temperature
  • humidity
  • pressure

and can be found in mobile phones (Nexus 5). There are rumors  that the BMP280 is a BME280 which couldn't be calibrated for humidity, but I have no confirmation on this.

BME280

Can be used for:



  • Indoor navigation (based on changing the measured altitude - pressure)
  • Outdoor navigation
  • Weather forecast
  • Home application control
  • Context awareness ( change room detection)  
  • Internet of Things.


Temperature precision is ± 1 C degree in 0-60C range.




Can be found in multiple modules, from SPI connectivity to I2C.

Make sure that if you are using the I2C version  to change the I2C address to 0x76. ( Default value for I2C address in Adafruit's library is 0x77).


I2C version

The SPI version can be found around USD 5 here.




Code is similar with the BMP280 , just read the humidity


/******************************************************
 * Catalin Batrinu bcatalin@gmail.com 
 * Read temperature, humidity and pressure from BME280
 * and send it to thingspeaks.com
*******************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <ESP8266WiFi.h>


Adafruit_BME280 bme; // I2C
// replace with your channel’s thingspeak API key,
String apiKey = "YOUR-API-KEY";
const char* ssid = "YOUR-SSID";
const char* password = "YOUR-ROUTER-PASSWORD";
const char* server = "api.thingspeak.com";
WiFiClient client;


/**************************  
 *   S E T U P
 **************************/
void setup() {
  Serial.begin(115200);
  Serial.println(F("BMP280 test"));
  
  if (!bme.begin()) {  
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  WiFi.begin(ssid, password);
  
  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");  
}

  /**************************  
 *  L O O P
 **************************/
void loop() {

    if (client.connect(server,80))  // "184.106.153.149" or api.thingspeak.com
    {
        String postStr = apiKey;
        postStr +="&field1=";
        postStr += String(bme.readTemperature());
        postStr +="&field2=";
        postStr += String(bme.readHumidity());
        postStr +="&field3=";
        postStr += String(bme.readPressure() / 100.0F);
        postStr += "\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");
        client.print(postStr);    
    }
    client.stop(); 
    //every 20 sec   
    delay(20000);
}


If your readings are with almost 2 degrees more than the expected value is because the BME280 is to close to the ESP8266. Try to keep at least 10 cm between the BME280 and ESP8266 to eliminate the RF heating and heating produced by ESP8266. 

Also is possible that you run the BME280 in normal mode ( more samples per second) versus forced mode when you are reading the values exactly when you need them ( here the drift is around 0.6 degrees Celsius).

The complete datasheet can be found here.

Tuesday, March 10, 2015

Usefull tools #2

If you want a nice gauge to display some data you can use the google's one with the help from thingspeak.com.


Thingspeak.com Plugin

For this gauge we will need to read data from thingspeak.com and this require an API Read Key that can be generated from the API KEYS menu.



Now go to the Plugin menu from the Main Menu:



and click on "New Plugin"


and select the Google Gauge and "Create Plugin".

Now you can modify the Javascript with your channel id and the READ API Key:


  // set your channel id here
  var channel_id = 87654;
  // set your channel's read api key here if necessary
  var api_key = 'Q68B3YMORI9S49JJ';
  // maximum value for the gauge
  var max_gauge_value = 100;
  // name of the gauge

  var gauge_name = 'My_gauge_name';

To include the new created gauge in other page you can use the HTML TAG <IFRAME>.

<iframe width="330" height="360" style="border: 1px solid #cccccc;" src="http://api.thingspeak.com/plugins/2345" ></iframe>



Thursday, March 5, 2015

Usefull tools #1






Ok, we have the board, we have the environment but we need to see the values from the sensors as a nice graph. What we should do ?

For storing the values I recommend and I use thingspeak.com.

Make an account on thingspeak.com and create a new channel.



Complete the fields for the newly created channel, a channel name, a description, metadata,
tags, Make Public check box.



On this step important fields are ones labeled Field 1 to Field 8. You can use them later 
to insert values into the thingspeak database. So complete the labels that your values will have.




Save your channel so you can use it later. You can come back later and modify those values.



API KEY

In order to upload values to thingspeak.com from the ESP8266, match must be done between your account and your ESP8266. This is done by the API KEY which can be found in the API KEYS menu.

Keep this KEY secret and don't post it on the web as I've seen it in a lot of github code. Every one that know you API KEY can alter your values and you will bang your head on the tables to see why your temperature is 200 degrees.  



Don't bother to use the API KEY above, I'll delete the channel later.


Testing

It's time to test the channel and the api key.

For now just add the the link changing the API_KEY with your own value in a browser.

http://api.thingspeak.com/update?key=[API_KEY]&field1=22&field2=55

which in my case is 

http://api.thingspeak.com/update?key=UD05NUCXSTSJ39W&field1=22&field2=55

Don't panic if your browser will show you just a blank page with one value "1", is the id
for your first record inserted into the database.





If you are going back the the channel page, you will see the values for field1 (temperature) and field2(humidity). In this way you can update up to 8 values in one shot.

Temperature:



Humidity:



Note: Keep at least 15 seconds between two writes !