Ein Blog

Gesammelte Notizen

Weather Station with arduino

31 Januar, 2016 | Programming

Here is an example for a small weather station using an arduino uno. The arduino collects air pressure, humidity, temperature and light values and sends the collected data to a web service.

To collect the pressure, temperature and the humidity a BPM 280 is used. The light value is collected using a LDR and to send the data to the web service the cc3000 wlan shield is used.

The REST service is described here.

Parts:

or BMP280 Breakout von Watterott

The parts are connected as shown on this picture:

undefined

The BMP280 is connected using I2C (in the picture above a BMP085 pressure sensor is show but the BMP 280 has the same pin layout).

To read the values from the sensors this sketch is used:

 /*************************************************** 
  This is a sketch to use the CC3000 WiFi chip and a BME280 sensor
  
  Copyright 2016 Jens Reese

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 ****************************************************/

// Libraries
#include <Adafruit_CC3000.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_SleepyDog.h>
#include <Wire.h>
#include <SPI.h>
#include "utility/debug.h"


// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
#define LDR_PIN               A0                   

// WLAN parameters
#define WLAN_SSID       "<your ssid>"        // cannot be longer than 32 characters!
#define WLAN_PASS       "<your wlan password>"
#define WLAN_SECURITY   WLAN_SEC_WPA2

// Create instance of the BME280 sensor.
Adafruit_BME280 bme;
//  Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2); // you can change this clock speed

// Wlan client
Adafruit_CC3000_Client client;

void setup(void)
{  
  // Initialize
  Serial.begin(115200);

  if (!bme.begin()) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }
  
  Serial.println(F("Initializing WiFi chip..."));
  if (!cc3000.begin())
  {
    Serial.println(F("Couldn't initialize Wifi chip! Check your wiring?"));
    while(1);
  }
  
  if (!cc3000.deleteProfiles()) {
    Serial.println(F("Failed!"));
    while(1);
  }
  
  // Connect to WiFi network
  Serial.println(F("Connecting to WiFi network ..."));
  cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
  Serial.println(F("done!"));

  /* Wait for DHCP to complete */
  Serial.println(F("Request DHCP"));
  
  int count = 0;
  while (!cc3000.checkDHCP() && count < 20)
  {
    delay(1000); 
    count++;
  } 
}

void loop(void)
{  
  Serial.print(F("Free RAM: "));
  String ram = String(getFreeRam(), DEC);
  Serial.println(ram);
  
  // Start watchdog 
  Watchdog.enable();
  
  // Get IP
  uint32_t ip = 0;
  Serial.print(F("bananapi -> "));
  while  (ip  ==  0)  {
    if  (!  cc3000.getHostByName("bananapi", &ip))  {
      Serial.println(F("Couldn't resolve!"));
      while(1){}
    }
    delay(500);
  }  
  cc3000.printIPdotsRev(ip);
  Serial.println(F(""));

  Watchdog.reset();
  
  // Get data
  int ldrVal = analogRead(LDR_PIN);
  float temp = bme.readTemperature();
  float hum = bme.readHumidity();
  float pres = bme.readPressure() / 100.0F; 
  String temperature  = "undef";
  String humidity     = "undef";
  String pressure     = "undef";
  String light        = "undef";
  temperature = String(temp);
  humidity = String(hum);
  pressure = String(pres);
  light = String(ldrVal);
   
  Watchdog.reset();
  
  // Check connection to WiFi
  if(!cc3000.checkConnected()){
    while(1){}
  }
  
  Watchdog.reset();
  
  // Send request
  client = cc3000.connectTCP(ip, 8080);
  if (client.connected()) {
    String request = "GET /data/arduino1?v1="+temperature+"&v2="+humidity+"&v3="+pressure+"&v4="+light+" HTTP/1.0\r\nConnection: close\r\n\r\n";
    Serial.print("Sending request: ");
    Serial.println(request);
    
    send_request(request);
    
    Watchdog.reset();

    client.close();

    Watchdog.reset();
  } else {
    Serial.println(F("Connection failed"));    
    return;
  }  
  
  Watchdog.disable();
  
  // Wait 5min until next update
  int minutes = 30;
  wait(minutes);
  
}

/*******************************************************************************
 * send_request
 ********************************************************************************/
bool send_request (String request) {
  // Transform to char
  char requestBuf[request.length()+1];
  request.toCharArray(requestBuf,request.length()); 
  // Send request
  if (client.connected()) {
    client.fastrprintln(requestBuf); 
  } 
  else {
    Serial.println(F("Connection failed"));
    free(requestBuf);    
    return false;
  }
  free(requestBuf);
  return true;  
}


/*******************************************************************************
 * Wait for a given time using the watchdog
 ********************************************************************************/
void wait(int minutes) {
  int total = minutes *6;
  for (int i = total; i>0; i--) {
    Watchdog.sleep(5000);
  }
}

Get it here