Skip to content
Snippets Groups Projects
Commit 58e8625f authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
.DS_Store
*.autosave
*.save
*~
*.tmp
*.part
*.rsls
#ifndef SensorVL53L0X_WaterLevel_h
#define SensorVL53L0X_WaterLevel_h
#include <sensors/DisplaySS1306.h>
/****************************************************************************
* DisplayWaterLevel - derived from DisplaySSD1306 (i2c OLED)
* Displays Water levels from a particular laser distance sensor
*/
class DisplayWaterLevel: public DisplaySSD1306 {
public:
DisplayWaterLevel(int child_id=255) : DisplaySSD1306(child_id) { setFontSize(1); }
protected:
void _display(const char*displaystr = 0) {
_oled->setCursor(0, 0);
if (displaystr) {
if (_caption_fontsize >= 2 )
_oled->set2X();
else
_oled->set1X();
_oled->print(displaystr);
_oled->clearToEOL();
_oled->println();
}
if (_fontsize >= 2 )
_oled->set2X();
else
_oled->set1X();
_oled->print(F("Distance: ")); _oled->clearToEOL(); _oled->println();
int val = ((ChildInt*)(vl53l0x.children.get(1)))->getValueInt();
if (val > 0) { // phase failures have incorrect data
// Serial.print("Distance (mm): "); Serial.println(val);
_oled->print(val); _oled->print(" mm"); _oled->clearToEOL(); _oled->println();
int valperc = ((ChildInt*)(vl53l0x.children.get(2)))->getValueInt();
_oled->print(F("(")); _oled->print(valperc); _oled->print(F(" %)")); _oled->clearToEOL(); _oled->println();
} else {
// Serial.println(" out of range ");
_oled->print("Out-of-range"); _oled->clearToEOL(); _oled->println();
}
// The current row starts with index 0, so we need to offset by one
// if (_oled->row() + 1 < _oled->displayRows()) {
// _oled->clear(0, _oled->displayWidth() - 1, _oled->row() + 1, _oled->displayRows());
// }
};
};
#endif
/*
* The MySensors Arduino library handles the wireless radio link and protocol
* between your home built sensors/actuators and HA controller of choice.
* The sensors forms a self healing radio network with optional repeaters. Each
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
* network topology allowing messages to be routed to nodes.
*
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
* Copyright (C) 2013-2017 Sensnology AB
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
*
* Documentation: http://www.mysensors.org
* Support Forum: http://forum.mysensors.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
DESCRIPTION
NodeManager is intended to take care on your behalf of all those common tasks a MySensors node has to accomplish,
speeding up the development cycle of your projects.
Documentation available on: https://github.com/mysensors/NodeManager
NodeManager provides built-in implementation of a number of sensors through ad-hoc classes.
/**********************************
* MySensors node configuration
*/
// General settings
#define SKETCH_NAME "Plant Water Level"
#define SKETCH_VERSION "0.01"
//#define MY_DEBUG
#define MY_NODE_ID 53
//#define USE_DISPLAY
#define MY_RADIO_NRF5_ESB
#define MY_NRF5_ESB_CHANNEL 85
// NRF24 radio settings
//#define MY_RADIO_NRF24
//#define MY_RF24_CHANNEL 85
//#define MY_RF24_CHANNEL 125
//#define MY_DEBUG_VERBOSE_RF24
// Advanced settings
//#define My_BAUD_RATE 115200
#define MY_SMART_SLEEP_WAIT_DURATION_MS 500
#define MY_SPLASH_SCREEN_DISABLED
//#define MY_DISABLE_RAM_ROUTING_TABLE_FEATURE
//#define MY_SIGNAL_REPORT_ENABLED
// Optimizations when running on 2032 Coin Cell. Also set nodeManager.setSleepBetweenSend(500) and run the board at 1Mhz
#define MY_TRANSPORT_UPLINK_CHECK_DISABLED
#define MY_TRANSPORT_WAIT_READY_MS 10000
#define MY_SLEEP_TRANSPORT_RECONNECT_TIMEOUT_MS 5000
#define MY_PARENT_NODE_ID 0
#define MY_PARENT_NODE_IS_STATIC
/***********************************
* NodeManager configuration
*/
#define NODEMANAGER_DEBUG ON
#define NODEMANAGER_INTERRUPTS ON
#define NODEMANAGER_SLEEP ON
#define NODEMANAGER_RECEIVE ON
#define NODEMANAGER_DEBUG_VERBOSE OFF
#define NODEMANAGER_POWER_MANAGER OFF
#define NODEMANAGER_CONDITIONAL_REPORT OFF
#define NODEMANAGER_EEPROM OFF
#define NODEMANAGER_TIME OFF
#define NODEMANAGER_RTC OFF
#define NODEMANAGER_SD OFF
#define NODEMANAGER_HOOKING OFF
#define NODEMANAGER_OTA_CONFIGURATION OFF
#define NODEMANAGER_SERIAL_INPUT OFF
// import NodeManager library (a nodeManager object will be then made available)
#include <MySensors_NodeManager.h>
/***********************************
* Add your sensors
*/
//PowerManager power(5,6);
#include <sensors/SensorBattery.h>
SensorBattery battery;
#include <sensors/SensorConfiguration.h>
SensorConfiguration configuration;
#include <sensors/SensorSignal.h>
SensorSignal signal;
#define XSHUT_PIN (03u)
//#include <sensors/SensorVL53L0X.h>
//SensorVL53L0X vl53l0x(XSHUT_PIN);
#include "SensorVL53L0X_WaterLevel.h"
SensorVL53L0X_WaterLevel vl53l0x(XSHUT_PIN, 200, 60);
#ifdef USE_DISPLAY
#include <sensors/DisplaySSD1306.h>
DisplaySSD1306 ssd1306;
// #include "DisplayWaterLevel.h"
// DisplayWaterLevel ssd1306(node);
#endif
void blinkLED(uint16_t len = 50, uint16_t pause = 50, uint8_t pin = LED_BUILTIN) {
digitalWrite(pin, HIGH);
delay(len);
digitalWrite(pin, LOW);
delay(pause);
}
void blinkityBlink(uint8_t repetitions) {
for (int x=0;x<repetitions;x++) {
blinkLED(10, 100);
blinkLED(10, (x<(repetitions-1))?500:0); //skip waiting at the end of the final repetition
}
};
/***********************************
* Main Sketch
*/
// before
void before() {
nodeManager.setSleepBetweenSend(500);
// sleep 20 sec, report 20 sec
const int reportSecs = 20;
const int sleepSecs = 20;
blinkityBlink(1);
blinkityBlink(1);
blinkityBlink(1);
// setup the serial port baud rate
#ifdef CHIP_NRF5
// Reassign Serial Pins as given in the board definition
Serial.setPins(PIN_SERIAL_RX, PIN_SERIAL_TX);
#endif
Serial.begin(MY_BAUD_RATE);
/*
* Configure your sensors
*/
nodeManager.setSleepSeconds(sleepSecs);
nodeManager.setReportIntervalSeconds(reportSecs);
#ifdef USE_SIGNAL
signal.setReportIntervalSeconds(reportSecs);
#endif
#ifdef USE_CONFIGURATION
configuration.setReportIntervalSeconds(reportSecs);
// configuration.children.get(1)->setDescription("Configuration");
#endif
#ifdef USE_BATTERY
battery.setReportIntervalSeconds(reportSecs);
battery.setMinVoltage(1.8);
battery.setMaxVoltage(3.3);
#endif
//nodeManager.setPowerManager(power);
vl53l0x.children.get(1)->setDescription("Distance");
vl53l0x.setReportIntervalSeconds(reportSecs);
#ifdef USE_DISPLAY
ssd1306.setCaption("VL53L0X");
// Display-related settings for SSD1306 must be made in setup() after nodeManager.setup()!
#endif
// call NodeManager before routine
nodeManager.before();
}
// presentation
void presentation() {
// call NodeManager presentation routine
nodeManager.presentation();
}
// setup
void setup() {
hwPinMode(LED_BUILTIN,OUTPUT_D0H1);
blinkityBlink(3); //signify power-up and start of operations
NRF_CLOCK->INTENSET = B11; // enable interrupts for EVENTS_HFCLKSTARTED and EVENTS_LFCLKSTARTED
NRF_CLOCK->TASKS_HFCLKSTART = 1; //start the high frequency crystal oscillator clock
while (!(NRF_CLOCK->EVENTS_HFCLKSTARTED)) {} //wait until high frequency crystal oscillator clock is up to speed and working
#ifdef CHIP_NRF5
// Setup I2C pins (custom pins given in the board definition!)
Wire.setPins(SDA, SCL);
#endif
// call NodeManager setup routine
nodeManager.setup();
#ifdef USE_DISPLAY
// ssd1306.rotateDisplay(true);
#endif
}
// loop
void loop() {
blinkityBlink(1);
Wire.begin();
NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
// call NodeManager loop routine
nodeManager.loop();
Wire.end();
NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
}
#if NODEMANAGER_RECEIVE == ON
// receive
void receive(const MyMessage &message) {
// call NodeManager receive routine
nodeManager.receive(message);
}
#endif
#if NODEMANAGER_TIME == ON
// receiveTime
void receiveTime(unsigned long ts) {
// call NodeManager receiveTime routine
nodeManager.receiveTime(ts);
}
#endif
#ifndef SensorVL53L0X_WaterLevel_h
#define SensorVL53L0X_WaterLevel_h
#include <sensors/SensorVL53L0X.h>
/****************************************************************************
* SensorWaterLevel - derived from SensorVL53L0X (i2c laser-distance sensor)
* Adds Level variable in % of the given min-max distances
*/
class SensorVL53L0X_WaterLevel: public SensorVL53L0X {
protected:
int _minLevel, _maxLevel;
public:
SensorVL53L0X_WaterLevel(int xshut_pin, int minLevel, int maxLevel, int child_id = 255): SensorVL53L0X(xshut_pin, child_id) {
_name = "WTR";
_minLevel = minLevel;
_maxLevel = maxLevel;
children.allocateBlocks(2);
new Child(this, INT, nodeManager.getAvailableChildId(child_id), S_MOISTURE, V_LEVEL, "Level");
}
void onLoop(Child *child) {
Child *ch1 = children.get(1);
if (child == ch1) {
SensorVL53L0X::onLoop(child);
} else {
int dist = ch1->getValueInt();
int val = -1;
if (dist < 0) {
val = -1;
} else {
val = max(0, min(100, int(100 * (_minLevel - dist) / (_minLevel - _maxLevel))));
}
child->setValue(val);
}
}
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment