BPI-M2+ Weather station:Air pressure sensor, temperature sensor,light intensity sensor arduino lib

thank Water do this cool project :slight_smile:

BPI-M2+ Weather station_04_Air pressure sensor arduino lib

Air pressure sensor use IIC interface(A4->SDA,A5->SCL)

#include <bmp180.h>

bmp180 BMP180;

void setup() {
  Serial.begin(9600);
  BMP180.Calibration();
}

void loop() {
  float temperature = BMP180.GetTemperature(); //MUST be called first
  float pressure = BMP180.GetPressure();
  float atm = pressure / 101325; // "standard atmosphere"
  float altitude = BMP180.calcAltitude(pressure); //Uncompensated caculation - in Meters 

  Serial.print("Temperature: ");
  Serial.print(temperature, 2); //display 2 decimal places
  Serial.print("deg C ");

  Serial.print("Pressure: ");
  Serial.print(pressure, 0); //whole number only.
  Serial.print(" Pa ");

  Serial.print("Standard Atmosphere: ");
  Serial.print(atm, 4); //display 4 decimal places
  Serial.print(" ");

  Serial.print("Altitude: ");
  Serial.print(altitude, 2); //display 2 decimal places
  Serial.print(" M");

  Serial.println();//line break

  delay(1000); //wait a second and get values again.
}

bmp180 lib download link:

1 Like

BPI-M2+ Weather station_06_temperature sensor arduino lib (DS18b20)

#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  
  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  
  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();
 
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  } 

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
}

ds18b20 lib download link:

https://drive.google.com/file/d/0B4PAo2nW2KfnOENfYVAxSFdudGM/view?usp=sharing

BPI-M2+ Weather station_05_light intensity sensor arduino lib (BH1750)

light intensity sensor use IIC interface(A4->SDA,A5->SCL)

#include "bh1750.h"
bh1750 BH1750;
void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println( BH1750.lx() );
}

BH1750 lib download link:

https://drive.google.com/file/d/0B4PAo2nW2KfnRGxmcm05Yl9rTHc/view?usp=sharing

BPI-M2+ Weather station_07_CO2 sensor arduino lib(MH-Z14A/MH-Z19B)

CO2 sensor support read from UART and PWM at the same time:

PWM :

#include "MH_Z1X.h"
MH_Z1X co2;
#define pin 11

void setup() {
  Serial.begin(9600);      
}

void loop() {
  co2.read_PWM(pin);     // pin
  Serial.print("CO2=");
  Serial.print(co2.co2);
  Serial.println();
  delay(1000);
}

UART:

#include "MH_Z1X.h"
#ifdef MH_Z1X_USE_SOFTWARE_SERIAL
#include "SoftwareSerial.h"
SoftwareSerial mySerial(4, 5); // MEAG TX:10, RX:11
MH_Z1X co2(mySerial);
#else
MH_Z1X co2(Serial1);
#endif
void setup() {
  Serial.begin(9600);      
}

void loop() {
  co2.read();
  Serial.print("CO2=");
  Serial.print(co2.co2);
  Serial.println();
  delay(1000);
}

MH-Z14A / MH-Z19B lib download link:

https://drive.google.com/file/d/0B4PAo2nW2KfnRXlnbWp6bGwtdk0/view?usp=sharing

BPI-M2+ Weather station_08_PM2.5 sensor arduino lib(ZH03A)

PM2.5 sensor support read from UART and PWM

PMW:

#include "ZH03A.h"
ZH03A pm2_5;
#define pin 11

void setup() {
  Serial.begin(9600);  
}

void loop() {
  pm2_5.read_PWM(pin);
 
  Serial.print("pm2.5=");
  Serial.print(pm2_5.pm2d5);
  Serial.print("; AQI=");
  Serial.print(pm2_5.getAQI(pm2_5.pm2d5));
  Serial.println();
  delay(1000);
}

UART:

#include "ZH03A.h"
#ifdef ZH03A_USE_SOFTWARE_SERIAL
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11); // MEAG TX:10, RX:11
ZH03A pm2_5(mySerial);
#else
ZH03A pm2_5(Serial1);
#endif
void setup() {
  Serial.begin(9600);  
}

void loop() {
  pm2_5.read();
  Serial.print("pm1.0=");
  Serial.print(pm2_5.pm1);
  Serial.print("; pm2.5=");
  Serial.print(pm2_5.pm2d5);
  Serial.print("; pm10=");
  Serial.print(pm2_5.pm10);
  Serial.print("; AQI=");
  Serial.print(pm2_5.getAQI(pm2_5.pm2d5));
  Serial.println();
  delay(1000);
}

PM2.5 sensor lib(ZH03A)

https://drive.google.com/file/d/0B4PAo2nW2KfnWkdWTHB6d2JrbTA/view?usp=sharing

BPI-M2+ Weather station_01_read dht11 data:

dht11 DHT11;

#define DHT11PIN 2

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
}

void loop()
{
  int chk = DHT11.read(DHT11PIN);

  //Serial.print("Read sensor: ");
  switch (chk)
  {
    case DHTLIB_OK:
      //Serial.println("OK");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.println("Checksum error");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.println("Time out error");
      break;
    default:
      Serial.print("Unknown error ");
      Serial.println(chk);
      break;
  }

  Serial.print(" Humidity (%): ");
  Serial.print((float)DHT11.humidity, 2);

  Serial.print(" Temperature (oC): ");
  Serial.print((float)DHT11.temperature, 2);

  Serial.println("");
  delay(2000);
}

DHT11 lib download link:

https://drive.google.com/file/d/0B4PAo2nW2KfnNy1rT2tOeGhobVk/view?usp=sharing

PM2.5 sensor (HLPM025K3)lib:

UART port read data:

#include "HLPM025K3.h"
#ifdef HLPM025K3_USE_SOFTWARE_SERIAL
SoftwareSerial mySerial(2, 3); // MEAG TX:10, RX:11
HLPM025K3 pm2_5(mySerial);
#else
HLPM025K3 pm2_5(Serial2);
#endif
void setup() {
  Serial.begin(9600);  
}

void loop() {
  pm2_5.read();

  Serial.print("pm2.5=");
  Serial.print(pm2_5.pm25);
  Serial.print("; pm10=");
  Serial.print(pm2_5.pm10);
  Serial.print("; AQI=");
  Serial.print(pm2_5.getAQI(pm2_5.pm25));
  Serial.println();
  delay(1000);
}

PM2.5 sensor (HLPM025K3)lib download link:

https://drive.google.com/file/d/0B4PAo2nW2KfnMjI2NG43TnRzTFE/view?usp=sharing

BPI-M2+ Weather station_10 PM2.5 sensor (PMS5003[G5])

Read data form UART port:

#include "PMS5XXX.h"
#ifdef PMS5XXX_USE_SOFTWARE_SERIAL
SoftwareSerial mySerial(6, 7); // MEAG TX:10, RX:11
PMS5XXX pm2_5(mySerial);
#else
PMS5XXX pm2_5(Serial2);
#endif
void setup() {
  Serial.begin(9600);  
}

void loop() {
  pm2_5.read();
  Serial.print("pm1.0=");
  Serial.print(pm2_5.pm1);
  Serial.print("; pm2.5=");
  Serial.print(pm2_5.pm2d5);
  Serial.print("; pm10=");
  Serial.print(pm2_5.pm10);
  Serial.print("; AQI=");
  Serial.print(pm2_5.getAQI(pm2_5.pm2d5));
  Serial.println();
  delay(1000);
}

PM2.5 sensor (PMS5003[G5]) lib download:

https://drive.google.com/file/d/0B4PAo2nW2KfnRTJkZEY3VG5DeHc/view?usp=sharing

Unbelievable. The picture above shows sensor connected to Arduino and BPi M2+ connected through USB to Arduino. So all your code examples don’t work on BPi M2+ anyway.

And regarding 1-Wire and DS18b20 sensors. Please keep in mind that for M2+ you use other’s work (loboris) and that here 1-Wire

  • works out of the box
  • is accessible through sysfs just when the correct module is loaded

So there’s absolutely no need to post crappy code examples. Better teach your users how to easily access sensors. And you should better look into how easy it is there to acticate 1-Wire modules for your ‘great’ BPi M3 too (currently you do not activate 1-Wire there for whatever reasons)

How much time took it to produce this absolutely useless collection of code examples? Why don’t you do real work? Improve support and software quality instead?

BPI-M2+ Weather station_11 create web server(nginx+php)

we need finish create nginx+php server

install nginx:


1 install nginx

sudo apt-get install nginx
  1. Modify Profile

    sudo nano /etc/nginx/sites-available/default

change web root directory

chane root to as below

#root /var/www/html;

root /home/pi/www;
  1. create a test page

    sudo nano /home/pi/www/index.html

edit index.html add below:

<h1>hello world!</h1>

save

4.reboot nginx

sudo nginx -s stop
sudo nginx

5 test open browser and imput : 127.0.0.1

install PHP:


  1. download php

    wget –c http://cn2.php.net/distributions/php-5.4.45.tar.bz2

  2. unzip

    tar –jxvf php-5.4.45.tar.bz2

  3. Generate configuration files

    cd php-5.4.45 ./configure --enable-fpm --with-mysql

erroe:

error:xml2-config not found. Please check your libxml2 installation

Execute the commands below:

sudo apt-get install libxml2-dev

rebuild makefile

../configure--enable-fpm --with-mysql
  1. compile

    ./make

  2. when compile finish , do install

    sudo makeinstall

  3. Create a configuration file, and copy it to the correct location.

    cp php.ini-development/usr/local/php/php.ini cp /usr/local/etc/php-fpm.conf.default/usr/local/etc/php-fpm.conf cp sapi/fpm/php-fpm /usr/local/bin

  4. set php.ini cgi.fix_pathinfo to 0 。

open php.ini:

sudo nano /usr/local/php/php.ini

find cgi.fix_pathinfo= and change it to 0

cgi.fix_pathinfo=0
  1. before you start php server ,need to editphp-fpm.conf file ,check php-fpm module use www-data user and www-data user group to run it,

    sudo nano /usr/local/etc/php-fpm.conf

change as below:

; Unix user/group of processes
; Note: The user is mandatory. If the groupis not set, the default user's group
;      will be used.
user = www-data
group = www-data
  1. start php-fpm server :

    ./usr/local/bin/php-fpm

  2. add php start when boot OS

edit rc.local

sudo nano /etc/rc.local

at exit 0 add below

sudo php-fpm &

change nginx configuration

  1. change main page and php configuration

    sudo nano/etc/nginx/sites-available/default

find index line ,add index.php, as below:

index index.php index.html index.htm

find php define line, change it as below:

location ~* .php$ {

fastcgi_index index.php;

fastcgi_pass 127.0.0.1:9000;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

}

  1. reboot nginx

    sudo nginx -s stop sudo nginx

  2. create a test page:

    sudo nano /home/pi/www/index.php

input

<? phpinfo(); ?>

save

  1. open brower ,and input :127.0.0.1

so all is working fine.

BPI-M2+ Weather station_12 create yourself website (nginx+php):

at Weather station_11,we have create web server ,so we can add ourself mainpage on it(mainpgae main function is :data receive ,save and show ) 1.unzip web file , and place it to web root dir. 2.edit config.php to change location of data.(Also can not change, directory must exist, and have read and write permissions);

$data_path = "/home/pi/data/";

3.enter the IP address of the BPI in the browser, check whether can normal visit

web source code download link:

BPI-M2+ Weather station_13_ The data uploading (arduino):

Used here hardware is more, also can upload only part of the sensor data

1.arduino nano (mega2560, uno all support); 2.ds18b20; 3.dht11; 4.bmp180; 5.bh1750; 6.esp8266 (3~5 is option Modules)

Arduino through ESP8266 sensor uploading data to BPI, and ESP8266 need setting wifi user name and password ,let it auto login when boot OS .

pm2.5 and co2 you can add by youself ,please refer to the previous code

// ESP8266
#include <ESP8266.h>
#ifdef ESP8266_USE_SOFTWARE_SERIAL
SoftwareSerial mySerial_8266(2, 3); // MEAG TX:10, RX:11
ESP8266 wifi(mySerial_8266, 9600);
#else
ESP8266 wifi(Serial1, 9600);
#endif
#define HOST_NAME   "192.168.1.2"   //bpi ip
#define HOST_PORT   (80)
String MAC = "";
String IP = "";

#include "bmp180.h"
#include "dht11.h"
#include "bh1750.h"
#include <OneWire.h>

OneWire  ds(5);     // ds18b20 pin D5
bh1750 BH1750;
bmp180 BMP180;
dht11 dht11_0;

unsigned long t_loop;
boolean b_con = false;

#define DHT11PIN_0 4

int e_18b20 = 0;
float getTemp_ds18b20() {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    if (e_18b20 < 10) {
      e_18b20++;
      return getTemp_ds18b20();
    }
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
    if (e_18b20 < 10) {
      e_18b20++;
      return getTemp_ds18b20();
    }
  }

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      type_s = 1;
      break;
    case 0x28:
      type_s = 0;
      break;
    case 0x22:
      type_s = 0;
      break;
    default:
      if (e_18b20 < 10) {
        e_18b20++;
        return getTemp_ds18b20();
      }
  }

  e_18b20 = 0;

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);      

  delay(100);     

  present = ds.reset();
  ds.select(addr);
  ds.write(0xBE);         

  for ( i = 0; i < 9; i++) {          
    data[i] = ds.read();

  }
  OneWire::crc8(data, 8);

  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; 
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);

    if (cfg == 0x00) raw = raw & ~7;  
    else if (cfg == 0x20) raw = raw & ~3; 
    else if (cfg == 0x40) raw = raw & ~1; 
  }
  celsius = (float)raw / 16.0;
  return celsius;
}

void setup() {
  Serial.begin(9600);
  BMP180.Calibration();
  
  t_loop = millis();
}

void loop() {

  float temperature = BMP180.GetTemperature();
  float pressure = BMP180.GetPressure();

  dht11_0.read(DHT11PIN_0);
  float dht_h0 = (float)dht11_0.humidity;
  float dht_t0 = (float)dht11_0.temperature;

  float lx = BH1750.lx();
  lx = abs(lx);

  float t_ds18b20 = getTemp_ds18b20();
  t_ds18b20 = long(t_ds18b20 * 10) / 10.0;
  float t_s = ( millis() - t_loop) / 1000;

  Serial.print("t0:");
  Serial.print(t_ds18b20, 1);
  Serial.print("; ");

  Serial.print("h1:");
  Serial.print(dht_h0, 0);
  Serial.print("; ");

  Serial.print("t1:");
  Serial.print(dht_t0, 0);
  Serial.print("; ");

  Serial.print("t3:");
  Serial.print(temperature, 1);
  Serial.print("; ");

  Serial.print("p:");
  Serial.print(pressure, 0);
  Serial.print("; ");

  Serial.print("l:");
  Serial.print( lx );
  Serial.print("; ");

  Serial.print("delay:");
  Serial.print( t_s );
  Serial.println();
  
  if ( t_s > 120) {
#ifdef ESP8266_USE_SOFTWARE_SERIAL
    mySerial_8266.listen();
#endif
    b_con = wifi.createTCP(HOST_NAME, HOST_PORT);
    Serial.print("con:");
    Serial.print(B(b_con));
    if (!b_con) {
      wifi.restart();
      Serial.println();
      t_loop = millis();
      return;
    }

    postData("ds18b20",         t_ds18b20     );
    postData("dht11_temp",      dht_t0        );
    postData("dht11_humidity",  dht_h0        );
    postData("bmp180_p",        pressure      );
    postData("bmp180_temp",     temperature   );
    postData("bh1750",          lx            );
    postData("dht11_temp2",     dht_t0        );
    postData("dht11_humidity2", dht_h0        );
    
    b_con = ! wifi.releaseTCP();
    Serial.print("; close:");
    Serial.println(B(!b_con));
    t_loop = millis();
  }

  delay(5000);
}

String B(boolean b) {
  if (b) return "true";
  else return "false";
}


void postData(String key, float val) {
  if (b_con) {

    String s = "POST /weather/main.php?cmd=setdata&key="
               + key + "&val=" + String(val) + " HTTP/1.1\r\n"
               + "Host: " + String(HOST_NAME) + "\r\n"
               + "Content-Length: 0\r\n"
               + "\r\n"
               + "\r\n";

    char c[s.length()];
    s.toCharArray(c, s.length());

    char *_p = c;

    wifi.send((const uint8_t*)_p, strlen(_p));

    delay(1000);

  } else {
    Serial.print("create tcp err\r\n");
  }

}

weather_Water.ino download link:

ESP8266 lib download link:

1 Like