2018-07-29 14:25:04 +00:00
|
|
|
/*
|
2018-08-07 22:23:41 +00:00
|
|
|
* ESP32 Environmental Sensor Node with SI7021 (Temp + Hum)
|
|
|
|
* (to be build with custom compiled ESP-IDF)
|
2018-07-29 14:25:04 +00:00
|
|
|
*
|
|
|
|
* copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de | code@jannikbeyerstedt.de
|
|
|
|
* license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
#include "si7021.hpp"
|
|
|
|
#include <driver/adc.h>
|
2018-08-07 22:23:41 +00:00
|
|
|
#include <WiFi.h>
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
#define BOARD_HUZZAH 1 /* Adafruit Feather HUZZAH */
|
|
|
|
#define BOARD_LOLIN 2 /* LoLin NodeMCU */
|
|
|
|
#define BOARD_ESP07S 3 /* standalone ESP07S */
|
|
|
|
#define BOARD_ESP32 4 /* standalone ESP32 */
|
|
|
|
|
|
|
|
/* DEBUG SETTINGS */
|
2018-08-07 22:23:41 +00:00
|
|
|
#define TEST /* use test config and database */
|
|
|
|
#define PRINT_INFO /* enable Serial output */
|
|
|
|
// #define PRINT_DEBUG /* enable additional debug output */
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
/* CONFIGURATION OPTIONS */
|
|
|
|
#ifndef NODENAME
|
2018-08-24 09:23:04 +00:00
|
|
|
#define NODENAME "env-TODO" /* IMPORTANT: CHANGE FOR EACH DEVICE */
|
2018-07-29 14:25:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BOARD
|
2019-04-07 14:15:43 +00:00
|
|
|
#define BOARD 4 /* IMPORTANT: select one of the BOARD_* types */
|
2018-07-29 14:25:04 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-18 10:30:58 +00:00
|
|
|
#define DB_HOSTNAME "influx-iot.fra80"
|
2018-07-29 14:25:04 +00:00
|
|
|
#define DB_PASSWD "c2Vuc29yczpTZW5zb3JzLXcuaW5mbHV4QGhvbWU" // BasicAuth String
|
|
|
|
|
|
|
|
#if BOARD == BOARD_ESP32
|
2020-04-18 10:30:58 +00:00
|
|
|
#define BATT_FULL 3700 // 4.2V Battery
|
2018-10-03 19:28:26 +00:00
|
|
|
#define BATT_CUTOFF 2500 // 2.8V Battery
|
2018-07-29 14:25:04 +00:00
|
|
|
#else
|
|
|
|
#error "unsupported board chosen"
|
|
|
|
#endif
|
|
|
|
|
2018-10-03 19:28:26 +00:00
|
|
|
String loggerId = "esp32-"; // prefix for the last 3 octets of the MAC address
|
2018-08-24 09:23:04 +00:00
|
|
|
|
2018-07-29 14:25:04 +00:00
|
|
|
/* SETTINGS */
|
|
|
|
float tempOffset = +0.0;
|
|
|
|
float humiOffset = +0.0;
|
|
|
|
|
|
|
|
const char* host = DB_HOSTNAME;
|
|
|
|
const int httpPort = 8086;
|
|
|
|
|
|
|
|
const char* wlan_ssid = "WLAN-Bey-IoT";
|
|
|
|
const char* wlan_passwd = "IoT-Fra80";
|
|
|
|
|
|
|
|
#ifndef TEST
|
|
|
|
String loggerName = NODENAME;
|
|
|
|
const uint16_t sendInterval_sec = 1800; /* 1800 sec (30 min) */
|
|
|
|
const uint8_t sampleAvgNum = 6; /* 6 (every 5 minutes) */
|
|
|
|
const uint16_t sampleInterval_sec = sendInterval_sec / sampleAvgNum;
|
|
|
|
String serviceUri = "/write?db=sensors";
|
|
|
|
#else
|
|
|
|
String loggerName = "esp-test";
|
|
|
|
const uint16_t sendInterval_sec = 180; /* 180 sec (3 min) */
|
|
|
|
const uint8_t sampleAvgNum = 6; /* 3 (every 1 minute) */
|
|
|
|
const uint16_t sampleInterval_sec = sendInterval_sec / sampleAvgNum;
|
|
|
|
String serviceUri = "/write?db=test";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* GLOBAL VARIABLES */
|
2020-04-18 10:30:58 +00:00
|
|
|
const uint16_t wlanConnectCheckInterval = 250; // milli seconds: poll wifi.state after wifi.begin()
|
|
|
|
const uint16_t wlanConnectTimeout = 15000; // milli seconds
|
|
|
|
RTC_DATA_ATTR uint8_t wlanConnectFailCnt = 0;
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
Si7021 si7021 = Si7021();
|
|
|
|
float temp = 0.0;
|
|
|
|
float humi = 0.0;
|
|
|
|
|
|
|
|
uint16_t battLevel = 0;
|
|
|
|
float battPerc = 0.0;
|
|
|
|
|
2018-08-04 11:13:35 +00:00
|
|
|
RTC_DATA_ATTR uint8_t sampleCnt = 0; // for average value
|
2018-07-29 14:25:04 +00:00
|
|
|
RTC_DATA_ATTR float tempAccu = 0.0; // for average value
|
|
|
|
RTC_DATA_ATTR float humiAccu = 0.0; // for average value
|
|
|
|
|
2018-08-24 09:23:04 +00:00
|
|
|
uint8_t chipid[6] = {0}; // WiFi MAC address
|
|
|
|
|
2018-07-29 14:25:04 +00:00
|
|
|
#ifdef PRINT_INFO
|
|
|
|
#define INFO_PRINT(x) Serial.print(x)
|
|
|
|
#define INFO_PRINTLN(x) Serial.println(x)
|
|
|
|
#else
|
|
|
|
#define INFO_PRINT(x)
|
|
|
|
#define INFO_PRINTLN(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PRINT_INFO) && defined(PRINT_DEBUG)
|
|
|
|
#define DEBUG_PRINT(x) Serial.print(x)
|
|
|
|
#define DEBUG_PRINTLN(x) Serial.println(x)
|
|
|
|
#else
|
|
|
|
#define DEBUG_PRINT(x)
|
|
|
|
#define DEBUG_PRINTLN(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* GLOBAL FUNCTIONS */
|
|
|
|
bool wlanConnect(void) {
|
|
|
|
unsigned int cycles = 0;
|
|
|
|
|
|
|
|
INFO_PRINT("[INFO ] Connecting to WiFi");
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.begin(wlan_ssid, wlan_passwd);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
2018-08-04 11:13:35 +00:00
|
|
|
delay(wlanConnectCheckInterval);
|
2018-07-29 14:25:04 +00:00
|
|
|
INFO_PRINT(".");
|
|
|
|
cycles++;
|
2018-08-04 11:13:35 +00:00
|
|
|
if (cycles > wlanConnectTimeout / wlanConnectCheckInterval) {
|
2018-07-29 14:25:04 +00:00
|
|
|
INFO_PRINTLN(" FAILED");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INFO_PRINT(" ok, IP: ");
|
|
|
|
INFO_PRINTLN(WiFi.localIP());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void doDeepSleep(uint16_t durationSec) {
|
|
|
|
esp_sleep_enable_timer_wakeup(durationSec * 1000000);
|
|
|
|
// esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // TODO more fine grained settings
|
|
|
|
#ifdef PRINT_DEBUG
|
|
|
|
Serial.printf("[DEBUG] Going to deep sleep for %d seconds\n", durationSec);
|
|
|
|
#endif
|
|
|
|
#ifdef PRINT_INFO
|
|
|
|
delay(100); // time to send the messages via Serial
|
|
|
|
#endif
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void printWakeupReason() {
|
|
|
|
esp_sleep_wakeup_cause_t wakeup_reason;
|
|
|
|
wakeup_reason = esp_sleep_get_wakeup_cause();
|
|
|
|
switch (wakeup_reason) {
|
|
|
|
case 1:
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Wakeup caused by external signal using RTC_IO");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Wakeup caused by external signal using RTC_CNTL");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Wakeup caused by timer");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Wakeup caused by touchpad");
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Wakeup caused by ULP program");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Wakeup was not caused by deep sleep");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void app_main() {
|
2018-08-04 11:13:35 +00:00
|
|
|
// ESP32 Setup
|
2018-07-29 14:25:04 +00:00
|
|
|
initArduino();
|
|
|
|
pinMode(4, OUTPUT);
|
|
|
|
digitalWrite(4, HIGH);
|
|
|
|
|
2018-08-24 09:23:04 +00:00
|
|
|
// set logger node unique ID with MAC address
|
|
|
|
char macIdString[6] = {0};
|
|
|
|
esp_efuse_mac_get_default(chipid);
|
|
|
|
sprintf(macIdString, "%02x%02x%02x", chipid[3], chipid[4], chipid[5]);
|
|
|
|
loggerId += macIdString;
|
|
|
|
|
2018-07-29 14:25:04 +00:00
|
|
|
#ifdef PRINT_INFO
|
|
|
|
Serial.begin(115200);
|
|
|
|
delay(100);
|
|
|
|
Serial.println("");
|
|
|
|
|
2019-04-07 14:15:43 +00:00
|
|
|
Serial.print("[INFO ] Type ");
|
|
|
|
Serial.print(BOARD);
|
|
|
|
Serial.print(", Node: ");
|
2018-08-24 09:23:04 +00:00
|
|
|
Serial.print(loggerId);
|
2019-04-07 14:15:43 +00:00
|
|
|
Serial.print(" (");
|
2018-07-29 14:25:04 +00:00
|
|
|
Serial.print(loggerName);
|
2019-04-07 14:15:43 +00:00
|
|
|
Serial.print("), Build: ");
|
|
|
|
Serial.println(PROJ_VER);
|
2018-07-29 14:25:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef PRINT_DEBUG
|
|
|
|
printWakeupReason();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// setup the si7021 sensor
|
|
|
|
if (false == si7021.begin()) {
|
|
|
|
INFO_PRINTLN("[ERROR] SI7021 not set up properly");
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup the adc for VCC measurement
|
|
|
|
adc1_config_width(ADC_WIDTH_12Bit);
|
2018-07-31 08:32:37 +00:00
|
|
|
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_0db); // or ADC_ATTEN_2_5db
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
/* GET VALUE SAMPLES */
|
|
|
|
// measure environmental data from si7021
|
|
|
|
if (false == si7021.measure()) {
|
|
|
|
INFO_PRINT("[ERROR] SI7021 measurement failed");
|
|
|
|
}
|
|
|
|
humi = si7021.humidity + humiOffset;
|
|
|
|
temp = si7021.temperature + tempOffset;
|
|
|
|
|
|
|
|
// measure battery level
|
|
|
|
battLevel = adc1_get_raw(ADC1_CHANNEL_0); // ADC0 = GPIO36 = VP = pin 4 (after en)
|
|
|
|
battPerc = (0 == battLevel) ? 0 : 100.0 * (battLevel - BATT_CUTOFF) / (BATT_FULL - BATT_CUTOFF);
|
|
|
|
|
|
|
|
#ifdef PRINT_INFO
|
|
|
|
Serial.printf("[INFO ] > Humidity: %5.2f Temperature: %5.2f", humi, temp);
|
2018-08-04 11:13:35 +00:00
|
|
|
Serial.printf(" Battery Bat: %5.1f/100 (raw: %4d)\n", battPerc, battLevel);
|
2018-07-29 14:25:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ---------- MAIN LOGIC START ---------- */
|
|
|
|
// accumulate and average $sampleAvgNum samples before sending data
|
2018-08-04 11:13:35 +00:00
|
|
|
sampleCnt++;
|
|
|
|
tempAccu += temp;
|
|
|
|
humiAccu += humi;
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
// calculate average and send data, if $sampleAvgNum are reached
|
|
|
|
if (sampleCnt >= sampleAvgNum) {
|
|
|
|
|
|
|
|
INFO_PRINTLN("[INFO ] Enough samples collected -> send average to database");
|
2018-08-04 11:13:35 +00:00
|
|
|
temp = tempAccu / (float)sampleCnt;
|
|
|
|
humi = humiAccu / (float)sampleCnt;
|
2018-07-29 14:25:04 +00:00
|
|
|
#ifdef PRINT_INFO
|
|
|
|
Serial.printf("[INFO ] < Humidity: %5.2f Temperature: %5.2f\n", humi, temp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// turn wifi on again
|
|
|
|
DEBUG_PRINTLN("[DEBUG] Turning wifi on again and connecting");
|
|
|
|
if (wlanConnect()) {
|
|
|
|
|
|
|
|
// send data to influxdb
|
2018-08-24 09:23:04 +00:00
|
|
|
String influxDataLine = "weather,id=" + loggerId + ",sensor=" + loggerName + " temp=" + String(temp, 2);
|
2018-07-29 14:25:04 +00:00
|
|
|
influxDataLine += ",hum=" + String(humi, 2);
|
|
|
|
influxDataLine += ",batRaw=" + String(battLevel);
|
|
|
|
influxDataLine += ",bat=" + String(battPerc);
|
|
|
|
|
|
|
|
INFO_PRINT("[INFO ] Connecting to ");
|
|
|
|
INFO_PRINT(host);
|
|
|
|
WiFiClient client;
|
|
|
|
if (client.connect(host, httpPort)) {
|
|
|
|
INFO_PRINTLN(" > success");
|
|
|
|
|
|
|
|
String httpRequest = "POST " + serviceUri + " HTTP/1.1\r\n" + "Host: " + host + ":" + httpPort +
|
|
|
|
"\r\n" + "Content-Length: " + influxDataLine.length() + "\r\n" +
|
|
|
|
"Authorization: Basic " + DB_PASSWD + "=\r\n" + "Connection: close\r\n" +
|
|
|
|
"\r\n" + influxDataLine + "\r\n";
|
|
|
|
client.print(httpRequest);
|
|
|
|
delay(100);
|
|
|
|
|
|
|
|
while (client.available()) {
|
|
|
|
String line = client.readStringUntil('\n');
|
|
|
|
if (line.indexOf("HTTP") != -1) {
|
|
|
|
if (line.indexOf("204") == -1) { // expect a HTTP 204 status code
|
|
|
|
INFO_PRINTLN("[ERROR]: invalid http status code");
|
|
|
|
INFO_PRINTLN(line);
|
2018-08-04 11:13:35 +00:00
|
|
|
} else { // successful database submission
|
|
|
|
// reset average after successful database submission
|
|
|
|
sampleCnt = 0;
|
|
|
|
tempAccu = 0;
|
|
|
|
humiAccu = 0;
|
|
|
|
|
|
|
|
wlanConnectFailCnt = 0;
|
2018-07-29 14:25:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
INFO_PRINTLN(" > FAILED");
|
|
|
|
}
|
|
|
|
client.stop();
|
|
|
|
WiFi.disconnect(true);
|
|
|
|
// continue with deep sleep at end of main function
|
|
|
|
|
2018-08-07 22:23:41 +00:00
|
|
|
} else { // ELSE: !wlanConnect
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
/* WIFI CONNECT FAILED, WAIT MAX 5 MINUTES IN DEEP SLEEP */
|
2018-08-04 11:13:35 +00:00
|
|
|
wlanConnectFailCnt++;
|
|
|
|
#ifdef PRINT_INFO
|
|
|
|
Serial.printf("[INFO ] WiFi connect failed %d time(s) -> wait %03d s and try again\n",
|
|
|
|
wlanConnectFailCnt, sampleInterval_sec);
|
|
|
|
#endif
|
|
|
|
if (sampleInterval_sec > (5 * 60)) {
|
2018-07-29 14:25:04 +00:00
|
|
|
doDeepSleep(5 * 60);
|
|
|
|
} else {
|
2018-08-04 11:13:35 +00:00
|
|
|
doDeepSleep(sampleInterval_sec);
|
2018-07-29 14:25:04 +00:00
|
|
|
}
|
2018-08-07 22:23:41 +00:00
|
|
|
} // ENDIF: wlanConnect
|
2018-07-29 14:25:04 +00:00
|
|
|
|
|
|
|
} else { // ELSE: sampleCnt < sampleAvgNum
|
|
|
|
|
|
|
|
#ifdef PRINT_INFO
|
2018-08-07 22:23:41 +00:00
|
|
|
Serial.printf("[INFO ] %d of %d samples collected -> sleep %4d s\n", sampleCnt, sampleAvgNum,
|
|
|
|
sampleInterval_sec);
|
2018-07-29 14:25:04 +00:00
|
|
|
// continue with deep sleep at end of main function
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // ENDIF: sampleCnt
|
|
|
|
/* ---------- MAIN LOGIC END ---------- */
|
|
|
|
|
|
|
|
/* SAVE ENERGY BY WAITING IN DEEP SLEEP */
|
|
|
|
doDeepSleep(sampleInterval_sec);
|
|
|
|
}
|