Compare commits

..

6 Commits

  1. 18
      include/MessenSensoren.h
  2. 29
      lib/Sensoren/BH1750.cpp
  3. 14
      lib/Sensoren/BH1750.h
  4. 115
      lib/Sensoren/bme280.cpp
  5. 74
      lib/Sensoren/bme280.h
  6. 119
      lib/Sensoren/myI2C.cpp
  7. 20
      lib/Sensoren/myI2C.h
  8. 140
      src/MessenSensoren.cpp
  9. 38
      src/main.cpp

18
include/MessenSensoren.h

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
#ifndef h_messenSensoren
#define h_messenSensoren
void init_Messen(void);
void MessenSensoren(void);
// nicht schoen aber funktioniert
#include "hseSensorProtocol.h"
extern HseSP hse;
#endif

29
lib/Sensoren/BH1750.cpp

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
#include <Arduino.h>
#include "myi2c.h"
#include "bh1750.h"
//-------------------------------------------------
// Power Up and start Messung
void BH1750_Init(void)
{
// BH1750 = HY30 Licht Sensor
Set_I2C_Adresse(BH1750_ADR); // GY-30 = 0x23
I2C_write8A( 1); // Power On
I2C_write8A( 0x20); // Start 1 lux Wandlung 120ms
delay(180);
}
//-------------------------------------------------
// Raed an Power Down
unsigned int BH1750_ReadLux( void )
{
unsigned int uiLux;
uiLux = I2C_read16A()*10L/12;
I2C_write8A( 0); // Power Down
return( uiLux);
}
//------------------------------------------------

14
lib/Sensoren/BH1750.h

@ -0,0 +1,14 @@ @@ -0,0 +1,14 @@
#ifndef _BH1750_H_
#define _BH1750_H_
#include <Arduino.h>
#define BH1750_ADR 0x23
void BH1750_Init(void);
unsigned int BH1750_ReadLux( void );
#endif

115
lib/Sensoren/bme280.cpp

@ -0,0 +1,115 @@ @@ -0,0 +1,115 @@
#include "Arduino.h"
#include <Wire.h>
#include "myI2c.h"
#include "BME280.h"
bme280_calib_data _bme280_calib;
int32_t t_fine;
//-------------------------------------------------
void bme280_readCoefficients(void)
{
_bme280_calib.dig_T1 = I2C_read16_LE(BME280_REGISTER_DIG_T1);
_bme280_calib.dig_T2 = I2C_readS16_LE(BME280_REGISTER_DIG_T2);
_bme280_calib.dig_T3 = I2C_readS16_LE(BME280_REGISTER_DIG_T3);
_bme280_calib.dig_P1 = I2C_read16_LE(BME280_REGISTER_DIG_P1);
_bme280_calib.dig_P2 = I2C_readS16_LE(BME280_REGISTER_DIG_P2);
_bme280_calib.dig_P3 = I2C_readS16_LE(BME280_REGISTER_DIG_P3);
_bme280_calib.dig_P4 = I2C_readS16_LE(BME280_REGISTER_DIG_P4);
_bme280_calib.dig_P5 = I2C_readS16_LE(BME280_REGISTER_DIG_P5);
_bme280_calib.dig_P6 = I2C_readS16_LE(BME280_REGISTER_DIG_P6);
_bme280_calib.dig_P7 = I2C_readS16_LE(BME280_REGISTER_DIG_P7);
_bme280_calib.dig_P8 = I2C_readS16_LE(BME280_REGISTER_DIG_P8);
_bme280_calib.dig_P9 = I2C_readS16_LE(BME280_REGISTER_DIG_P9);
_bme280_calib.dig_H1 = I2C_read8(BME280_REGISTER_DIG_H1);
_bme280_calib.dig_H2 = I2C_readS16_LE(BME280_REGISTER_DIG_H2);
_bme280_calib.dig_H3 = I2C_read8(BME280_REGISTER_DIG_H3);
_bme280_calib.dig_H4 = (I2C_read8(BME280_REGISTER_DIG_H4) << 4) | (I2C_read8(BME280_REGISTER_DIG_H4+1) & 0xF);
_bme280_calib.dig_H5 = (I2C_read8(BME280_REGISTER_DIG_H5+1) << 4) | (I2C_read8(BME280_REGISTER_DIG_H5) >> 4);
_bme280_calib.dig_H6 = (int8_t)I2C_read8(BME280_REGISTER_DIG_H6);
}
//-------------------------------------------------------------
float bme280_readTemperature(void)
{
int32_t var1, var2;
int32_t adc_T = I2C_Read24(BME280_REGISTER_TEMPDATA);
adc_T >>= 4;
var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) *
((int32_t)_bme280_calib.dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1)) *
((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1))) >> 12) *
((int32_t)_bme280_calib.dig_T3)) >> 14;
t_fine = var1 + var2;
float T = (t_fine * 5 + 128) >> 8;
return T/100;
}
//---------------------------------------------------
float bme280_readPressure(void)
{
int64_t var1, var2, p;
// Must be done first to get the t_fine variable set up
bme280_readTemperature();
int32_t adc_P = I2C_Read24(BME280_REGISTER_PRESSUREDATA);
adc_P >>= 4;
var1 = ((int64_t)t_fine) - 128000;
var2 = var1 * var1 * (int64_t)_bme280_calib.dig_P6;
var2 = var2 + ((var1*(int64_t)_bme280_calib.dig_P5)<<17);
var2 = var2 + (((int64_t)_bme280_calib.dig_P4)<<35);
var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3)>>8) +
((var1 * (int64_t)_bme280_calib.dig_P2)<<12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bme280_calib.dig_P1)>>33;
if (var1 == 0) {
return 0; // avoid exception caused by division by zero
}
p = 1048576 - adc_P;
p = (((p<<31) - var2)*3125) / var1;
var1 = (((int64_t)_bme280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
var2 = (((int64_t)_bme280_calib.dig_P8) * p) >> 19;
p = ((p + var1 + var2) >> 8) + (((int64_t)_bme280_calib.dig_P7)<<4);
return (float)p/256;
}
//----------------------------------------------------------
float bme280_readHumidity(void)
{
bme280_readTemperature(); // must be done first to get t_fine
int32_t adc_H = I2C_read16(BME280_REGISTER_HUMIDDATA);
int32_t v_x1_u32r;
v_x1_u32r = (t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bme280_calib.dig_H4) << 20) -
(((int32_t)_bme280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
(((((((v_x1_u32r * ((int32_t)_bme280_calib.dig_H6)) >> 10) *
(((v_x1_u32r * ((int32_t)_bme280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
((int32_t)2097152)) * ((int32_t)_bme280_calib.dig_H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
((int32_t)_bme280_calib.dig_H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
float h = (v_x1_u32r>>12);
return h / 1024.0;
}

74
lib/Sensoren/bme280.h

@ -0,0 +1,74 @@ @@ -0,0 +1,74 @@
#define BME280_ADR 0x76
enum
{
BME280_REGISTER_DIG_T1 = 0x88,
BME280_REGISTER_DIG_T2 = 0x8A,
BME280_REGISTER_DIG_T3 = 0x8C,
BME280_REGISTER_DIG_P1 = 0x8E,
BME280_REGISTER_DIG_P2 = 0x90,
BME280_REGISTER_DIG_P3 = 0x92,
BME280_REGISTER_DIG_P4 = 0x94,
BME280_REGISTER_DIG_P5 = 0x96,
BME280_REGISTER_DIG_P6 = 0x98,
BME280_REGISTER_DIG_P7 = 0x9A,
BME280_REGISTER_DIG_P8 = 0x9C,
BME280_REGISTER_DIG_P9 = 0x9E,
BME280_REGISTER_DIG_H1 = 0xA1,
BME280_REGISTER_DIG_H2 = 0xE1,
BME280_REGISTER_DIG_H3 = 0xE3,
BME280_REGISTER_DIG_H4 = 0xE4,
BME280_REGISTER_DIG_H5 = 0xE5,
BME280_REGISTER_DIG_H6 = 0xE7,
BME280_REGISTER_CHIPID = 0xD0,
BME280_REGISTER_VERSION = 0xD1,
BME280_REGISTER_SOFTRESET = 0xE0,
BME280_REGISTER_CAL26 = 0xE1, // R calibration stored in 0xE1-0xF0
BME280_REGISTER_CONTROLHUMID = 0xF2,
BME280_REGISTER_CONTROL = 0xF4,
BME280_REGISTER_CONFIG = 0xF5,
BME280_REGISTER_PRESSUREDATA = 0xF7,
BME280_REGISTER_TEMPDATA = 0xFA,
BME280_REGISTER_HUMIDDATA = 0xFD,
};
// CALIBRATION DATA
typedef struct
{
uint16_t dig_T1;
int16_t dig_T2;
int16_t dig_T3;
uint16_t dig_P1;
int16_t dig_P2;
int16_t dig_P3;
int16_t dig_P4;
int16_t dig_P5;
int16_t dig_P6;
int16_t dig_P7;
int16_t dig_P8;
int16_t dig_P9;
uint8_t dig_H1;
int16_t dig_H2;
uint8_t dig_H3;
int16_t dig_H4;
int16_t dig_H5;
int8_t dig_H6;
} bme280_calib_data;
void bme280_readCoefficients(void);
float bme280_readTemperature(void);
float bme280_readPressure(void);
float bme280_readHumidity(void);

119
lib/Sensoren/myI2C.cpp

@ -0,0 +1,119 @@ @@ -0,0 +1,119 @@
#include "Arduino.h"
#include <Wire.h>
uint8_t _i2caddr;
//-----------------------------------
void Set_I2C_Adresse(uint8_t I2C_Adr)
{
_i2caddr = I2C_Adr;
//Wire.setClock(10000);
}
//--------------------------------------------------
uint8_t I2C_read8(byte reg)
{
uint8_t value;
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2caddr, (byte)1);
value = Wire.read();
return value;
}
//----------------------------------------------
void I2C_write8(byte reg, byte value)
{
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.write((uint8_t)reg);
Wire.write((uint8_t)value);
Wire.endTransmission();
}
//---------------------------------------------
// 8bit ohne Register schreiben
void I2C_write8A(byte value)
{
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.write((uint8_t)value);
Wire.endTransmission();
}
//---------------------------------------------
uint16_t I2C_read16(byte reg)
{
uint16_t value;
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2caddr, (byte)2);
value = (Wire.read() << 8) | Wire.read();
return value;
}
//------------------------------------------
// 16 bit lesen ohne Register
uint16_t I2C_read16A(void)
{
uint16_t value;
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.requestFrom((uint8_t)_i2caddr, (byte)2);
value = (Wire.read() << 8) | Wire.read();
return value;
}
//------------------------------------------
int16_t I2C_readS16(byte reg)
{
return (int16_t)I2C_read16(reg);
}
//---------------------------------------------
uint16_t I2C_read16_LE(byte reg)
{
uint16_t temp = I2C_read16(reg);
return (temp >> 8) | (temp << 8);
}
//------------------------------------------
int16_t I2C_readS16_LE(byte reg)
{
return (int16_t)I2C_read16_LE(reg);
}
//----------------------------------------------
uint32_t I2C_Read24(byte reg)
{
uint32_t value;
Wire.beginTransmission((uint8_t)_i2caddr);
Wire.write((uint8_t)reg);
Wire.endTransmission();
Wire.requestFrom((uint8_t)_i2caddr, (byte)3);
value = Wire.read();
value <<= 8;
value |= Wire.read();
value <<= 8;
value |= Wire.read();
return value;
}
//--------------------------
uint8_t I2C_Test(byte adr)
{
uint8_t value;
Wire.beginTransmission((uint8_t)adr);
Wire.write((uint8_t)0);
Wire.endTransmission();
Wire.requestFrom((uint8_t)adr, (byte)1);
value = Wire.read();
return value;
}
//----------------------------------------------

20
lib/Sensoren/myI2C.h

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
void Set_I2C_Adresse(uint8_t I2C_Adr);
uint8_t I2C_read8(byte reg);
void I2C_write8(byte reg, byte value);
uint16_t I2C_read16(byte reg);
int16_t I2C_readS16(byte reg);
int16_t I2C_readS16_LE(byte reg);
int16_t I2C_readS16_LE(byte reg);
uint16_t I2C_read16_LE(byte reg) ;
uint32_t I2C_Read24(byte reg);
void I2C_write8A(byte value);
uint16_t I2C_read16A(void);
uint8_t I2C_Test(byte adr);
extern uint8_t _i2caddr;

140
src/MessenSensoren.cpp

@ -0,0 +1,140 @@ @@ -0,0 +1,140 @@
// Sensoren lesen
#include <arduino.h>
#include "MessenSensoren.h"
#include <Wire.h>
#include "myi2c.h"
#include "bme280.h"
#include "bh1750.h"
#include "hseSensorProtocol.h"
#define I2C_POWER 5 // bei Feather M0
// hse-Protokoll Datensatz
HseSP hse(2, 60);
void init_Messen(void)
{
int i;
Serial.println("PowerOn I2C");
// I2C Power einschalten LoRa Transponder
pinMode(I2C_POWER,OUTPUT);
digitalWrite(I2C_POWER, HIGH);
delay(20);
Wire.begin();
// Hardware detection, liste was angeschlossen ist
Serial.println("I2C Hardware detection");
if ( I2C_Test(BME280_ADR) != 0xFF )
{
Set_I2C_Adresse(BME280_ADR);
i = I2C_read8(BME280_REGISTER_CHIPID);
Serial1.print(F("BMx280 Chip ID = 0x"));
Serial1.print(i,HEX);
if ( i== 0x58) Serial1.println(F(" = BMP280"));
if ( i== 0x60) Serial1.println(F(" = BME280"));
}
if ( I2C_Test(0x77) != 0xFF )
{
Serial.println("BMP180 gefunden");
}
if ( I2C_Test(0x38) != 0xFF )
{
Serial.println(F("AHT10 gefunden"));
}
// such BH1730
if ( I2C_Test(0x29) != 0xFF )
{
Serial.println("BH1730 gefunden");
}
// such BH1750
if ( I2C_Test(0x23) != 0xFF )
{
Serial.println("BH1750 gefunden");
BH1750_Init();
}
Serial.println("ende");
}
//--------------------------
void MessenSensoren(void)
{
int i,status, error;
float t,p,h;
long lVisLux;
Set_I2C_Adresse(BME280_ADR);
i = I2C_read8(BME280_REGISTER_CHIPID);
bme280_readCoefficients();
if ( i== 0x58)
{
I2C_write8(BME280_REGISTER_CONTROL, 0x3F);
delay(50); // gib ihm Zeit zum messen
}
if ( i== 0x60)
{
//Set before CONTROL_meas (DS 5.4.3)
I2C_write8(BME280_REGISTER_CONTROLHUMID, 0x05); //16x oversampling
I2C_write8(BME280_REGISTER_CONTROL, 0xB7); // 16x ovesampling, normal mode
delay(100); // gib ihm Zeit zum messen
}
// 35ms reicht beim BMP280
// 75ms warten bis BME280 den Druck gemessen hat
//----------------
t = bme280_readTemperature();
p = bme280_readPressure()/100.0;
h = bme280_readHumidity();
I2C_write8(BME280_REGISTER_CONTROL, 0x3C); // Sleep Mode
// such BH1750
if ( I2C_Test(0x23) != 0xFF )
{
// BH1750 = HY30 Licht Sensor
BH1750_Init();
lVisLux = BH1750_ReadLux();
}
if ( lVisLux > 65535) lVisLux = 654321;
Serial.print(F("Temp = "));
Serial.println(t);
Serial.print(F("Druck = "));
Serial.println(p);
Serial.print(F("Feuchte = "));
Serial.println(h);
Serial.print(F("Vis = "));
Serial.println(lVisLux);
hse.reset();
//Ein Klimasensor hinzufügen
HseSP::ClimateSensor_t cs;
cs.Temperature = t; //°C
cs.Humidity = h; //%
cs.Pressure = p; //hPa
cs.Illuminance = lVisLux; //lux
hse.addClimateSensor(&cs);
// hse.addVoltage(fUb/1000.0); // in Volt
// hse.addCounter(uiOnStd); // Zeit On
}

38
src/main.cpp

@ -38,7 +38,7 @@ @@ -38,7 +38,7 @@
#include <hal/hal.h>
#include <SPI.h>
#include "hseSensorProtocol.h"
#include "MessenSensoren.h"
//
// For normal use, we require that you edit the sketch to replace FILLMEIN
@ -75,8 +75,6 @@ static const u1_t PROGMEM APPKEY[16] = {0xca, 0x96, 0x9a, 0x15, 0x76, 0x5d, 0xaf @@ -75,8 +75,6 @@ static const u1_t PROGMEM APPKEY[16] = {0xca, 0x96, 0x9a, 0x15, 0x76, 0x5d, 0xaf
void os_getDevKey(u1_t *buf) { memcpy_P(buf, APPKEY, 16); }
// hse-Protokoll Datensatz
HseSP hse(2, 60);
static uint8_t mydata[] = "Hello, world!";
static osjob_t sendjob;
@ -144,17 +142,6 @@ void printHex2(unsigned v) { @@ -144,17 +142,6 @@ void printHex2(unsigned v) {
Serial.print(v, HEX);
}
void Messen()
{
hse.reset();
HseSP::ClimateSensor_t cs;
cs.Temperature = 22.2; //°C
cs.Humidity = 55.5; //%
cs.Pressure = 1000.1; //hPa
cs.Illuminance = 123;
hse.addClimateSensor(&cs);
}
void do_send(osjob_t *j) {
@ -163,7 +150,7 @@ void do_send(osjob_t *j) { @@ -163,7 +150,7 @@ void do_send(osjob_t *j) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
// Prepare upstream data transmission at the next possible time.
Messen();
MessenSensoren();
uint8_t* buffer = hse.getBuffer();
int size = hse.getSize();
@ -305,18 +292,23 @@ void onEvent(ev_t ev) { @@ -305,18 +292,23 @@ void onEvent(ev_t ev) {
void setup() {
delay(5000);
while (!Serial);
Serial.begin(9600);
Serial.println(F("Starting 1.0"));
Serial.begin(19200);
Serial.println(F("Starting 1.02"));
#ifdef __SAMD21G18A__
Serial.println(F("SAMD21G18A ARM Cortex-M0+ detected"));
#endif
#ifdef ARDUINO_SAMD_FEATHER_M0
Serial.println(F("defined for Feather M0 Board"));
#endif
init_Messen();
#ifdef CFG_eu868
Serial.println(F("Using EU868"));
#endif
#ifdef VCC_ENABLE
// For Pinoccio Scout boards
pinMode(VCC_ENABLE, OUTPUT);
digitalWrite(VCC_ENABLE, HIGH);
delay(1000);
#endif
// LMIC init
os_init();

Loading…
Cancel
Save