Skip to content

Commit

Permalink
added BME280 sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
cctweaker committed Jun 18, 2020
1 parent 14e6441 commit 9d31d6e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 50 deletions.
32 changes: 32 additions & 0 deletions ESPsensor/ESP-Now.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
void init_espnow()
{
if (esp_now_init() != 0)
goto_sleep();

esp_now_set_kok(kok, 16);

esp_now_set_self_role(ESP_NOW_ROLE_COMBO);

esp_now_add_peer(gmac, ESP_NOW_ROLE_COMBO, WIFI_CHANNEL, key, 16);
esp_now_set_peer_key(gmac, key, 16);

esp_now_register_send_cb(txcb);
esp_now_register_recv_cb(rxcb);
}

void send_data()
{
uint8_t byteArray[sizeof(tx)];
memcpy(byteArray, &tx, sizeof(tx));
esp_now_send(gmac, byteArray, sizeof(tx));

SENT = true;
}

void txcb(uint8_t *mac, uint8_t sendStatus)
{
if (sendStatus == 0)
ACK = true;
}

void rxcb(uint8_t *senderMac, uint8_t *incomingData, uint8_t len) {}
60 changes: 11 additions & 49 deletions ESPsensor/ESPsensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ extern "C"

#include <Wire.h>
#include <SI7021.h>
#include <BME280_t.h>

SI7021 sensor;
BME280<> BMESensor;

void initVariant()
{
Expand All @@ -23,26 +25,15 @@ void initVariant()

void setup()
{
if (sensor.begin(2, 0))
// if (sensor.begin(0, 2))
SI = true;

if (esp_now_init() != 0)
goto_sleep();

esp_now_set_kok(kok, 16);

esp_now_set_self_role(ESP_NOW_ROLE_COMBO);

esp_now_add_peer(gmac, ESP_NOW_ROLE_COMBO, WIFI_CHANNEL, key, 16);
esp_now_set_peer_key(gmac, key, 16);

esp_now_register_send_cb(txcb);
esp_now_register_recv_cb(rxcb);
init_espnow();
init_sensors();
}

void loop()
{
if (!PREP)
prepare_data();

if (!SENT)
send_data();

Expand All @@ -53,41 +44,12 @@ void loop()
goto_sleep();
}

void send_data()
{
char tx[128];

float tmp = 0;
float hum = 0;
float vin = 0;

if (SI)
{
si7021_env data = sensor.getHumidityAndTemperature();

tmp = (float)data.celsiusHundredths / 100;
hum = (float)data.humidityBasisPoints / 100;
}
vin = (float)ESP.getVcc() / FACTOR;

sprintf(tx, "{\"t\":\"%s\",\"n\":\"%s\",\"ID\":\"%x\",\"v\":%d,\"tmp\":%.2f,\"hum\":%.2f,\"vin\":%.2f}", TIP, NAME, ESP.getChipId(), VERSION, tmp, hum, vin);

uint8_t byteArray[sizeof(tx)];
memcpy(byteArray, &tx, sizeof(tx));
esp_now_send(gmac, byteArray, sizeof(tx));

SENT = true;
}

void txcb(uint8_t *mac, uint8_t sendStatus)
void goto_sleep()
{
if (sendStatus == 0)
ACK = true;
ESP.deepSleepInstant(SLEEP, RF_NO_CAL);
}

void rxcb(uint8_t *senderMac, uint8_t *incomingData, uint8_t len) {}

void goto_sleep()
void goto_sleep_short()
{
ESP.deepSleepInstant(SLEEP, RF_NO_CAL);
ESP.deepSleepInstant(60 * 1000 * 1000, RF_NO_CAL);
}
17 changes: 17 additions & 0 deletions ESPsensor/example_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ uint8_t key[16] = {0xFE, 0xED, 0xFE, 0xED, 0xFE, 0xED, 0xFE, 0xED, 0xFE, 0xED, 0
// sleep time (message rate)
/////////////////////////////////////////////////
#define SLEEP 10 * 60 * 1000 * 1000 // 1 minute
// min sec msec usec
/////////////////////////////////////////////////

/////////////////////////////////////////////////
Expand All @@ -101,7 +102,23 @@ uint8_t key[16] = {0xFE, 0xED, 0xFE, 0xED, 0xFE, 0xED, 0xFE, 0xED, 0xFE, 0xED, 0
/////////////////////////////////////////////////
// flags
/////////////////////////////////////////////////
bool PREP = false; // prepare data flag
bool SENT = false; // data sent flag
bool ACK = false; // data sent ok flag
bool SI = false; // SI7021 present
bool BME = false; // BME280 present
bool DS = false; // DS18B20 present
/////////////////////////////////////////////////

char tx[128];

/////////////////////////////////////////////////
// BME280
/////////////////////////////////////////////////
// input your correct sensor altitude in meters
#define MYALTITUDE 100 // altitude in meters
// this value is needed for correct normalized
// pressure value (USA & Canada)
//
// OpenWeatherMap reports normalized pres. for Europe
/////////////////////////////////////////////////
52 changes: 52 additions & 0 deletions ESPsensor/sensors.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
void init_sensors()
{
if (sensor.begin(2, 0))
{
SI = true;
return;
}

if (BMESensor.begin())
{
BME = true;
return;
}

// add DS18B20 init here

if (!SI && !BME && !DS)
goto_sleep();
}

void prepare_data()
{

float vin = (float)ESP.getVcc() / FACTOR;

if (SI)
{
si7021_env data = sensor.getHumidityAndTemperature();

float temp = data.celsiusHundredths / 100.0F;
float hum = data.humidityBasisPoints / 100.0F;
float dew = 243.04 * (log(hum / 100.0) + ((17.625 * temp) / (243.04 + temp))) / (17.625 - log(hum / 100.0) - ((17.625 * temp) / (243.04 + temp)));

sprintf(tx, "{\"t\":\"%s\",\"n\":\"%s\",\"ID\":\"%x\",\"v\":%d,\"tmp\":%.2f,\"hum\":%.2f,\"dew\":%.2f,\"vin\":%.2f}", TIP, NAME, ESP.getChipId(), VERSION, temp, hum, dew, vin);
}

if (BME)
{
BMESensor.refresh();
// on the first reading, only after power down, BME280 reports erroneous values.
// will need to discard the first reading in next version

float temp = BMESensor.temperature;
float hum = BMESensor.humidity;
float dew = 243.04 * (log(hum / 100.0) + ((17.625 * temp) / (243.04 + temp))) / (17.625 - log(hum / 100.0) - ((17.625 * temp) / (243.04 + temp)));
float relativepressure = BMESensor.seaLevelForAltitude(MYALTITUDE) / 100.0F;

sprintf(tx, "{\"t\":\"%s\",\"n\":\"%s\",\"ID\":\"%x\",\"v\":%d,\"tmp\":%.2f,\"hum\":%.2f,\"dew\":%.2f,\"prs\":%.2f,\"prn\":%.2f,\"vin\":%.2f}", TIP, NAME, ESP.getChipId(), VERSION, temp, hum, dew, BMESensor.pressure / 100.0F, relativepressure, vin);
}

PREP = true;
}
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ board_build.f_cpu = 80000000L
monitor_speed = 115200
upload_speed = 921600
upload_resetmethod = nodemcu
lib_deps = 536 ;Si7021@06fadfb576
lib_deps = 536 ; Si7021@06fadfb576
1554 ; BME280_Light@600667f3a6


build_flags = -D VERSION=1
Expand Down

0 comments on commit 9d31d6e

Please sign in to comment.