Skip to content

Commit

Permalink
Support for HC-SR04 ultrasonic sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
xoseperez committed Apr 9, 2018
1 parent 2506b6a commit 94c6506
Show file tree
Hide file tree
Showing 7 changed files with 2,165 additions and 2,008 deletions.
39 changes: 34 additions & 5 deletions code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
#define SENSOR_GUVAS12SD_ID 0x20
#define SENSOR_CSE7766_ID 0x21
#define SENSOR_TMP3X_ID 0x22
#define SENSOR_HCSR04_ID 0x23

//--------------------------------------------------------------------------------
// Magnitudes
Expand All @@ -136,8 +137,9 @@
#define MAGNITUDE_CO2 18
#define MAGNITUDE_LUX 19
#define MAGNITUDE_UV 20
#define MAGNITUDE_DISTANCE 21

#define MAGNITUDE_MAX 21
#define MAGNITUDE_MAX 22

// =============================================================================
// Specific data for each sensor
Expand Down Expand Up @@ -390,6 +392,23 @@

#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
// Enable support by passing HLW8012_SUPPORT=1 build flag
Expand Down Expand Up @@ -601,7 +620,8 @@
|| EMON_ANALOG_SUPPORT || EVENTS_SUPPORT || HLW8012_SUPPORT \
|| MHZ19_SUPPORT || PMSX003_SUPPORT || SHT3X_I2C_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
#else
#define SENSOR_SUPPORT 0
Expand Down Expand Up @@ -657,7 +677,8 @@ PROGMEM const unsigned char magnitude_decimals[] = {
3, 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";
Expand All @@ -681,6 +702,7 @@ PROGMEM const char magnitude_pm10_topic[] = "pm10";
PROGMEM const char magnitude_co2_topic[] = "co2";
PROGMEM const char magnitude_lux_topic[] = "lux";
PROGMEM const char magnitude_uv_topic[] = "uv";
PROGMEM const char magnitude_distance_topic[] = "distance";

PROGMEM const char* const magnitude_topics[] = {
magnitude_unknown_topic, magnitude_temperature_topic, magnitude_humidity_topic,
Expand All @@ -689,7 +711,8 @@ PROGMEM const char* const magnitude_topics[] = {
magnitude_power_factor_topic, magnitude_energy_topic, magnitude_energy_delta_topic,
magnitude_analog_topic, magnitude_digital_topic, magnitude_events_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[] = "";
Expand All @@ -707,6 +730,7 @@ PROGMEM const char magnitude_ugm3[] = "µg/m3";
PROGMEM const char magnitude_ppm[] = "ppm";
PROGMEM const char magnitude_lux[] = "lux";
PROGMEM const char magnitude_uv[] = "uv";
PROGMEM const char magnitude_distance[] = "m";

PROGMEM const char* const magnitude_units[] = {
magnitude_empty, magnitude_celsius, magnitude_percentage,
Expand All @@ -715,7 +739,8 @@ PROGMEM const char* const magnitude_units[] = {
magnitude_percentage, magnitude_joules, magnitude_joules,
magnitude_empty, magnitude_empty, magnitude_empty,
magnitude_ugm3, magnitude_ugm3, magnitude_ugm3,
magnitude_ppm, magnitude_lux, magnitude_uv
magnitude_ppm, magnitude_lux, magnitude_uv,
magnitude_distance
};

#include "../sensors/BaseSensor.h"
Expand Down Expand Up @@ -778,6 +803,10 @@ PROGMEM const char* const magnitude_units[] = {
#include "../sensors/GUVAS12SDSensor.h"
#endif

#if HCSR04_SUPPORT
#include "../sensors/HCSR04Sensor.h"
#endif

#if HLW8012_SUPPORT
#include <HLW8012.h>
#include "../sensors/HLW8012Sensor.h"
Expand Down
Binary file modified code/espurna/data/index.html.gz
Binary file not shown.
9 changes: 9 additions & 0 deletions code/espurna/sensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ void _sensorLoad() {
}
#endif

#if HCSR04_SUPPORT
{
HCSR04Sensor * sensor = new HCSR04Sensor();
sensor->setTrigger(HCSR04_TRIGGER);
sensor->setEcho(HCSR04_ECHO);
_sensors.push_back(sensor);
}
#endif

#if HLW8012_SUPPORT
{
HLW8012Sensor * sensor = new HLW8012Sensor();
Expand Down
119 changes: 119 additions & 0 deletions code/espurna/sensors/HCSR04Sensor.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions code/espurna/sensors/TMP3XSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ class TMP3XSensor : public BaseSensor {
}

void setType(unsigned char type) {
if (type == _type) return;
if (35 <= type && type <= 37) {
_type = type;
_dirty = true;
}
}

Expand Down
Loading

0 comments on commit 94c6506

Please sign in to comment.