Quantcast
Channel: banana pi single board computer open source project official forum BPI team - Latest posts
Viewing all articles
Browse latest Browse all 44598

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

$
0
0

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:

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

ESP8266 lib download link:

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


Viewing all articles
Browse latest Browse all 44598

Trending Articles