Dear all,
I'm really glad to read that the Mysensors 2.0 now include by default the attachment of local sensors on gateways.
Then after one year in standby, i've started again my project to finish it.
I'm using an Arduino Uno + Eth Wizenet 5100 and a distance sensor HC-SR04 => Domoticz as controller.
I want to measure the instant volume in liters of my water tank and not a consumption.
Actually I can see properly the value sent to domoticz from the sensors attached to my ethernet gateway.
My question is regarding the type: what type shall I use to send and read simply the instant value in liters in domoticz (not as counter).
I've found someone who did exactly what i would, but it's done with json! (see the image link below for the oil volume).
https://www.domoticz.com/wiki/images/0/06/Oil.png
He write that the Volume sensor is (RFXMeter type 113, subtype 2).
I've found the pdf manual of mysensors but I only see the S_WATER with V_VOLUME type (this give me a water counter in domoticz and not an instant reading).
How can I adapt a mysensor type (S_VOLUME_INSTANT by exemple) by my self?
I give you my sketch to measure and convert the distance in liters and percent for my tank if it could help someone else :-).
Tank you in advance for your help :-).
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enable gateway ethernet module type
#define MY_GATEWAY_W5100
// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
//#define MY_W5100_SPI_EN 4
// Enable Soft SPI for NRF radio (note different radio wiring is required)
// The W5100 ethernet module seems to have a hard time co-operate with
// radio on the same spi bus.
#if !defined(MY_W5100_SPI_EN) && !defined(ARDUINO_ARCH_SAMD)
#define MY_SOFTSPI
#define MY_SOFT_SPI_SCK_PIN 14
#define MY_SOFT_SPI_MISO_PIN 16
#define MY_SOFT_SPI_MOSI_PIN 15
#endif
// When W5100 is connected we have to move CE/CSN pins for NRF radio
#ifndef MY_RF24_CE_PIN
#define MY_RF24_CE_PIN 5
#endif
#ifndef MY_RF24_CS_PIN
#define MY_RF24_CS_PIN 6
#endif
// Enable to UDP
//#define MY_USE_UDP
#define MY_IP_ADDRESS 192,168,1,113 // If this is disabled, DHCP is used to retrieve address
// Renewal period if using DHCP
//#define MY_IP_RENEWAL_INTERVAL 60000
// The port to keep open on node server mode / or port to contact in client mode
#define MY_PORT 5003
// Controller ip address. Enables client mode (default is "server" mode).
// Also enable this if MY_USE_UDP is used and you want sensor data sent somewhere.
//#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 254
// The MAC address can be anything you want but should be unique on your network.
// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
// Note that most of the Ardunio examples use "DEAD BEEF FEED" for the MAC address.
#define MY_MAC_ADDRESS 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
// Flash leds on rx/tx/err
#define MY_LEDS_BLINKING_FEATURE
// Set blinking period
#define MY_DEFAULT_LED_BLINK_PERIOD 300
// Enable inclusion mode
//#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
//#define MY_INCLUSION_BUTTON_FEATURE
// Set inclusion mode duration (in seconds)
//#define MY_INCLUSION_MODE_DURATION 60
// Digital pin used for inclusion mode button
//#define MY_INCLUSION_MODE_BUTTON_PIN 3
// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 7 // Error led pin
//#define MY_DEFAULT_RX_LED_PIN 8 // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN 9 // the PCB, on board LED
#include <SPI.h>
#if defined(MY_USE_UDP)
#include <EthernetUdp.h>
#endif
#include <Ethernet.h>
#include <MySensors.h>
#include <NewPing.h>
#define CHILD_ID 1
//constant for the sonar HC-SR04 sensor
#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define MAX_DISTANCE 350 //distance max measured
//constant to calculate the volume of the tank
#define high_water_level 200 //tank depth from the lowest point to the max water level
#define distance_sensor 30 //distance in cm between the max water level and the sensor
#define tank_volume 10000 //tank volume when it is fitted to the max water level
unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
MyMessage msg(CHILD_ID, V_VOLUME);
int lastDist;
bool metric = true;
void setup()
{ //present(255, 18);
//metric = getConfig().isMetric;
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Capteur Citerne", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID, S_WATER);
}
void loop()
{
Serial.print("Niveau d'eau ");
Serial.print(tank_level("liters"));
Serial.println("L");
/* if (dist != lastDist) {
send(msg.set(dist));
lastDist = dist;
}
*/
send(msg.set(tank_level("liters")));
sleep(SLEEP_TIME);
}
//function to measure and convert the tank volume
int tank_level(String unit)
{
int level_liters = 0,level_percent = 0, echo_us = 0, echo_cm = 0, water_level_cm = 0;
echo_us = sonar.ping_median(); //return the median time of 5measures between the ping and the echo in microseconds
echo_cm = echo_us / US_ROUNDTRIP_CM; //convert the echo time to distance in cm.
water_level_cm = high_water_level - (echo_cm - distance_sensor);
level_liters = water_level_cm * (tank_volume / high_water_level);
level_percent = (water_level_cm * 100) / high_water_level;
if (unit == "liters")
{
return level_liters;
}
else if (unit == "percent")
{
return level_percent;
}
else
{
return 0;
}
}