Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
THNG:STRUCTION - application part for HumidityAlerter template, using a BME280 via I2C.
/**
* Generated by THNG:STRUCTION, provided as a Creative Commons 1.0 Universal (CC0 1.0) Public Domain Dedication.
*
* Code is as-is and without any warranty; without even the implied
* warranty of merchantability or fitness for a particular purpose.
* See LICENSE file or visit https://creativecommons.org/publicdomain/zero/1.0/deed.en
*/
/*
========================================================================================================================
Industruino D21G Application part for Thing 'HumidityAlerter'
HumidityAlerter: Measures humidity and compares it against defined thresholds
========================================================================================================================
*/
#include <Arduino.h>
// ------------------------------------------------------------------------------------------------------------------
// LCD part
// ------------------------------------------------------------------------------------------------------------------
#include <UC1701.h>
extern UC1701 lcd;
// ------------------------------------------------------------------------------------------------------------------
// Application Handlers
// TODO: Implement application functionality here
// ------------------------------------------------------------------------------------------------------------------
/** Pushes a new event to the event FIFO */
void wot_events_push(const char *name, const char *description, unsigned long time);
/** TODO: Implement application-specific code here by filling the app_...(...) functions */
// HINT: #include your own libraries here if you need them (e.g. for sensor libraries)
#include <Seeed_BME280.h>
#include <Wire.h>
// HINT: declare/define your globals here
BME280 bme280;
static int HumidityThresholdVar = 100;
static bool bAboveThreshold = false;
/**
* app_setup() is called by setup() after Serial and networking has been made
* available. This is a good place to initialize your own hardware parts like
* sensors etc., and to initialize your own variables.
*/
void app_setup() {
// e.g.
// pinMode(D0, OUTPUT);
bme280.init();
// ...
}
/**
* Call this to raise a new event named "HumidityAboveThreshold". This
* event will be added to the circular events buffer and can be seen by API clients.
* Optionally add additional event-related code here, e.g. triggering an external API.
*/
void app_raise_HumidityAboveThreshold() {
wot_events_push("HumidityAboveThreshold", "Triggered when humidity goes above defined threshold", millis());
// your own event-related code
}
/**
* app_loop() gets called by loop() function in regular intervals. This is good
* place to check for conditions (e.g. from sensors) and raise events. Additionally,
* actions handling code should be performed here.
* It's not run in a separate thread, so please do not delay(..) in here, otherwise
* it can block the http procotol handling.
*/
void app_loop() {
float f = bme280.getHumidity();
if ( !bAboveThreshold) {
if ( f > HumidityThresholdVar) {
app_raise_HumidityAboveThreshold();
bAboveThreshold = true;
lcd.setCursor(0,5);
lcd.clearLine();
lcd.println("Above");
}
} else {
if ( f < HumidityThresholdVar) {
bAboveThreshold = false;
lcd.setCursor(0,5);
lcd.clearLine();
lcd.println("Below");
}
}
}
/**
* app_get_Humidity is called by http handlers every time property "Humidity"
* is requested by a client in a read operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request.
*/
int app_get_Humidity() {
return bme280.getHumidity();
}
/**
* app_set_Humidity is called by http handlers every time property "Humidity"
* is requested by a client in a write operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request.
*/
void app_set_Humidity(int v) {
}
/**
* app_get_HumidityThreshold is called by http handlers every time property "HumidityThreshold"
* is requested by a client in a read operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request.
*/
int app_get_HumidityThreshold() {
return HumidityThresholdVar;
}
/**
* app_set_HumidityThreshold is called by http handlers every time property "HumidityThreshold"
* is requested by a client in a write operation.
* TODO: Implement custom action logic here. Remember this function blocks
* processing of http request.
*/
void app_set_HumidityThreshold(int v) {
HumidityThresholdVar = v;
}
// ------------------------------------------------------------------------------------------------------------------
// END Application Handlers
// ------------------------------------------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.