Browse Source

Support for HC-SR04 ultrasonic sensor

rfm69
Xose Pérez 6 years ago
parent
commit
94c65068df
7 changed files with 2165 additions and 2008 deletions
  1. +34
    -5
      code/espurna/config/sensors.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +9
    -0
      code/espurna/sensor.ino
  4. +119
    -0
      code/espurna/sensors/HCSR04Sensor.h
  5. +0
    -2
      code/espurna/sensors/TMP3XSensor.h
  6. +2000
    -1999
      code/espurna/static/index.html.gz.h
  7. +3
    -2
      code/html/custom.js

+ 34
- 5
code/espurna/config/sensors.h View File

@ -110,6 +110,7 @@
#define SENSOR_GUVAS12SD_ID 0x20 #define SENSOR_GUVAS12SD_ID 0x20
#define SENSOR_CSE7766_ID 0x21 #define SENSOR_CSE7766_ID 0x21
#define SENSOR_TMP3X_ID 0x22 #define SENSOR_TMP3X_ID 0x22
#define SENSOR_HCSR04_ID 0x23
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
// Magnitudes // Magnitudes
@ -136,8 +137,9 @@
#define MAGNITUDE_CO2 18 #define MAGNITUDE_CO2 18
#define MAGNITUDE_LUX 19 #define MAGNITUDE_LUX 19
#define MAGNITUDE_UV 20 #define MAGNITUDE_UV 20
#define MAGNITUDE_DISTANCE 21
#define MAGNITUDE_MAX 21
#define MAGNITUDE_MAX 22
// ============================================================================= // =============================================================================
// Specific data for each sensor // Specific data for each sensor
@ -390,6 +392,23 @@
#define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis #define EVENTS_DEBOUNCE 50 // Do not register events within less than 10 millis
//------------------------------------------------------------------------------
// HC-SR04
// Enable support by passing HCSR04_SUPPORT=1 build flag
//------------------------------------------------------------------------------
#ifndef HCSR04_SUPPORT
#define HCSR04_SUPPORT 0
#endif
#ifndef HCSR04_TRIGGER
#define HCSR04_TRIGGER 12 // GPIO for the trigger pin (output)
#endif
#ifndef HCSR04_ECHO
#define HCSR04_ECHO 14 // GPIO for the echo pin (input)
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// HLW8012 Energy monitor IC // HLW8012 Energy monitor IC
// Enable support by passing HLW8012_SUPPORT=1 build flag // Enable support by passing HLW8012_SUPPORT=1 build flag
@ -601,7 +620,8 @@
|| EMON_ANALOG_SUPPORT || EVENTS_SUPPORT || HLW8012_SUPPORT \ || EMON_ANALOG_SUPPORT || EVENTS_SUPPORT || HLW8012_SUPPORT \
|| MHZ19_SUPPORT || PMSX003_SUPPORT || SHT3X_I2C_SUPPORT \ || MHZ19_SUPPORT || PMSX003_SUPPORT || SHT3X_I2C_SUPPORT \
|| SI7021_SUPPORT || V9261F_SUPPORT || AM2320_SUPPORT \ || SI7021_SUPPORT || V9261F_SUPPORT || AM2320_SUPPORT \
|| GUVAS12SD_SUPPORT || CSE7766_SUPPORT || TMP3X_SUPPORT
|| GUVAS12SD_SUPPORT || CSE7766_SUPPORT || TMP3X_SUPPORT \
|| HCSR04_SUPPORT
#define SENSOR_SUPPORT 1 #define SENSOR_SUPPORT 1
#else #else
#define SENSOR_SUPPORT 0 #define SENSOR_SUPPORT 0
@ -657,7 +677,8 @@ PROGMEM const unsigned char magnitude_decimals[] = {
3, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0
0, 0,
2, 3
}; };
PROGMEM const char magnitude_unknown_topic[] = "unknown"; PROGMEM const char magnitude_unknown_topic[] = "unknown";
@ -681,6 +702,7 @@ PROGMEM const char magnitude_pm10_topic[] = "pm10";
PROGMEM const char magnitude_co2_topic[] = "co2"; PROGMEM const char magnitude_co2_topic[] = "co2";
PROGMEM const char magnitude_lux_topic[] = "lux"; PROGMEM const char magnitude_lux_topic[] = "lux";
PROGMEM const char magnitude_uv_topic[] = "uv"; PROGMEM const char magnitude_uv_topic[] = "uv";
PROGMEM const char magnitude_distance_topic[] = "distance";
PROGMEM const char* const magnitude_topics[] = { PROGMEM const char* const magnitude_topics[] = {
magnitude_unknown_topic, magnitude_temperature_topic, magnitude_humidity_topic, magnitude_unknown_topic, magnitude_temperature_topic, magnitude_humidity_topic,
@ -689,7 +711,8 @@ PROGMEM const char* const magnitude_topics[] = {
magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic, magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic,
magnitude_analog_topic, magnitude_digital_topic, magnitude_events_topic, magnitude_analog_topic, magnitude_digital_topic, magnitude_events_topic,
magnitude_pm1dot0_topic, magnitude_pm2dot5_topic, magnitude_pm10_topic, magnitude_pm1dot0_topic, magnitude_pm2dot5_topic, magnitude_pm10_topic,
magnitude_co2_topic, magnitude_lux_topic, magnitude_uv_topic
magnitude_co2_topic, magnitude_lux_topic, magnitude_uv_topic,
magnitude_distance_topic
}; };
PROGMEM const char magnitude_empty[] = ""; PROGMEM const char magnitude_empty[] = "";
@ -707,6 +730,7 @@ PROGMEM const char magnitude_ugm3[] = "µg/m3";
PROGMEM const char magnitude_ppm[] = "ppm"; PROGMEM const char magnitude_ppm[] = "ppm";
PROGMEM const char magnitude_lux[] = "lux"; PROGMEM const char magnitude_lux[] = "lux";
PROGMEM const char magnitude_uv[] = "uv"; PROGMEM const char magnitude_uv[] = "uv";
PROGMEM const char magnitude_distance[] = "m";
PROGMEM const char* const magnitude_units[] = { PROGMEM const char* const magnitude_units[] = {
magnitude_empty, magnitude_celsius, magnitude_percentage, magnitude_empty, magnitude_celsius, magnitude_percentage,
@ -715,7 +739,8 @@ PROGMEM const char* const magnitude_units[] = {
magnitude_percentage, magnitude_joules, magnitude_joules, magnitude_percentage, magnitude_joules, magnitude_joules,
magnitude_empty, magnitude_empty, magnitude_empty, magnitude_empty, magnitude_empty, magnitude_empty,
magnitude_ugm3, magnitude_ugm3, magnitude_ugm3, magnitude_ugm3, magnitude_ugm3, magnitude_ugm3,
magnitude_ppm, magnitude_lux, magnitude_uv
magnitude_ppm, magnitude_lux, magnitude_uv,
magnitude_distance
}; };
#include "../sensors/BaseSensor.h" #include "../sensors/BaseSensor.h"
@ -778,6 +803,10 @@ PROGMEM const char* const magnitude_units[] = {
#include "../sensors/GUVAS12SDSensor.h" #include "../sensors/GUVAS12SDSensor.h"
#endif #endif
#if HCSR04_SUPPORT
#include "../sensors/HCSR04Sensor.h"
#endif
#if HLW8012_SUPPORT #if HLW8012_SUPPORT
#include <HLW8012.h> #include <HLW8012.h>
#include "../sensors/HLW8012Sensor.h" #include "../sensors/HLW8012Sensor.h"


BIN
code/espurna/data/index.html.gz View File


+ 9
- 0
code/espurna/sensor.ino View File

@ -426,6 +426,15 @@ void _sensorLoad() {
} }
#endif #endif
#if HCSR04_SUPPORT
{
HCSR04Sensor * sensor = new HCSR04Sensor();
sensor->setTrigger(HCSR04_TRIGGER);
sensor->setEcho(HCSR04_ECHO);
_sensors.push_back(sensor);
}
#endif
#if HLW8012_SUPPORT #if HLW8012_SUPPORT
{ {
HLW8012Sensor * sensor = new HLW8012Sensor(); HLW8012Sensor * sensor = new HLW8012Sensor();


+ 119
- 0
code/espurna/sensors/HCSR04Sensor.h View File

@ -0,0 +1,119 @@
// -----------------------------------------------------------------------------
// HC-SR04 Ultrasonic sensor
// Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
// -----------------------------------------------------------------------------
#if SENSOR_SUPPORT && HCSR04_SUPPORT
#pragma once
#include "Arduino.h"
#include "BaseSensor.h"
class HCSR04Sensor : public BaseSensor {
public:
// ---------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------
HCSR04Sensor(): BaseSensor() {
_count = 1;
_sensor_id = SENSOR_HCSR04_ID;
}
// ---------------------------------------------------------------------
void setEcho(unsigned char echo) {
_echo = echo;
}
void setTrigger(unsigned char trigger) {
_trigger = trigger;
}
// ---------------------------------------------------------------------
unsigned char getEcho() {
return _echo;
}
unsigned char getTrigger() {
return _trigger;
}
// ---------------------------------------------------------------------
// Sensor API
// ---------------------------------------------------------------------
// Initialization method, must be idempotent
void begin() {
pinMode(_echo, INPUT);
pinMode(_trigger, OUTPUT);
digitalWrite(_trigger, LOW);
_ready = true;
}
// Descriptive name of the sensor
String description() {
char buffer[24];
snprintf(buffer, sizeof(buffer), "HCSR04 @ GPIO(%u, %u)", _trigger, _echo);
return String(buffer);
}
// Descriptive name of the slot # index
String slot(unsigned char index) {
return description();
};
// Address of the sensor (it could be the GPIO or I2C address)
String address(unsigned char index) {
return String(_trigger);
}
// Type for slot # index
unsigned char type(unsigned char index) {
if (index == 0) return MAGNITUDE_DISTANCE;
return MAGNITUDE_NONE;
}
// Current value for slot # index
double value(unsigned char index) {
if (index == 0) {
// Trigger pulse
digitalWrite(_trigger, HIGH);
delayMicroseconds(10);
digitalWrite(_trigger, LOW);
// Wait for echo pulse low-high-low
while ( digitalRead(_echo) == 0 ) yield();
unsigned long start = micros();
while ( digitalRead(_echo) == 1 ) yield();
unsigned long travel_time = micros() - start;
// Assuming a speed of sound of 340m/s
// Dividing by 2 since it is a round trip
return 340.0 * (double) travel_time / 1000000.0 / 2;
}
return 0;
}
protected:
// ---------------------------------------------------------------------
// Protected
// ---------------------------------------------------------------------
unsigned char _trigger;
unsigned char _echo;
};
#endif // SENSOR_SUPPORT && HCSR04_SUPPORT

+ 0
- 2
code/espurna/sensors/TMP3XSensor.h View File

@ -28,10 +28,8 @@ class TMP3XSensor : public BaseSensor {
} }
void setType(unsigned char type) { void setType(unsigned char type) {
if (type == _type) return;
if (35 <= type && type <= 37) { if (35 <= type && type <= 37) {
_type = type; _type = type;
_dirty = true;
} }
} }


+ 2000
- 1999
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 3
- 2
code/html/custom.js View File

@ -37,7 +37,8 @@ function sensorName(id) {
"DHT", "Dallas", "Emon Analog", "Emon ADC121", "Emon ADS1X15", "DHT", "Dallas", "Emon Analog", "Emon ADC121", "Emon ADS1X15",
"HLW8012", "V9261F", "ECH1560", "Analog", "Digital", "HLW8012", "V9261F", "ECH1560", "Analog", "Digital",
"Events", "PMSX003", "BMX280", "MHZ19", "SI7021", "Events", "PMSX003", "BMX280", "MHZ19", "SI7021",
"SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD"
"SHT3X I2C", "BH1750", "PZEM004T", "AM2320 I2C", "GUVAS12SD",
"TMP3X", "HC-SR04"
]; ];
if (1 <= id && id <= names.length) { if (1 <= id && id <= names.length) {
return names[id - 1]; return names[id - 1];
@ -51,7 +52,7 @@ function magnitudeType(type) {
"Current", "Voltage", "Active Power", "Apparent Power", "Current", "Voltage", "Active Power", "Apparent Power",
"Reactive Power", "Power Factor", "Energy", "Energy (delta)", "Reactive Power", "Power Factor", "Energy", "Energy (delta)",
"Analog", "Digital", "Events", "Analog", "Digital", "Events",
"PM1.0", "PM2.5", "PM10", "CO2", "Lux", "UV"
"PM1.0", "PM2.5", "PM10", "CO2", "Lux", "UV", "Distance"
]; ];
if (1 <= type && type <= types.length) { if (1 <= type && type <= types.length) {
return types[type - 1]; return types[type - 1];


Loading…
Cancel
Save