config files on 28-11-2023 12:46:43
This commit is contained in:
102
esphome/include/SEN0395/SEN0395_distance.h
Executable file
102
esphome/include/SEN0395/SEN0395_distance.h
Executable file
@@ -0,0 +1,102 @@
|
||||
#include "esphome.h"
|
||||
#include <string>
|
||||
|
||||
class Sen0395Distance : public Component, public UARTDevice {
|
||||
public:
|
||||
Sen0395Distance(UARTComponent *parent) : UARTDevice(parent) {}
|
||||
|
||||
float t_snr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
float t_distance[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
bool t_active[10] = {false, false, false, false, false, false, false, false, false, false};
|
||||
|
||||
void setup() override {
|
||||
// nothing to do here
|
||||
}
|
||||
|
||||
int readline(int readch, char *buffer, int len) {
|
||||
static int pos = 0;
|
||||
int rpos;
|
||||
|
||||
if (readch > 0) {
|
||||
switch (readch) {
|
||||
case '\n': // Ignore new-lines
|
||||
break;
|
||||
case '\r': // Return on CR
|
||||
rpos = pos;
|
||||
pos = 0; // Reset position index ready for next time
|
||||
return rpos;
|
||||
default:
|
||||
if (pos < len - 1) {
|
||||
buffer[pos++] = readch;
|
||||
buffer[pos] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// No end of line has been found, so return -1.
|
||||
return -1;
|
||||
}
|
||||
|
||||
void loop() override {
|
||||
const int max_line_length = 40;
|
||||
static char buffer[max_line_length];
|
||||
|
||||
while (available()) {
|
||||
if (readline(read(), buffer, max_line_length) >= 4) {
|
||||
std::string line = buffer;
|
||||
|
||||
if (line.substr(0, 6) == "$JYRPO") {
|
||||
std::string vline = line.substr(6);
|
||||
std::vector<std::string> v;
|
||||
for (int i = 0; i < vline.length(); i++) {
|
||||
if (vline[i] == ',') {
|
||||
v.push_back("");
|
||||
} else {
|
||||
v.back() += vline[i];
|
||||
}
|
||||
}
|
||||
int target_count = parse_number<int>(v[0]).value();
|
||||
int target_index = parse_number<int>(v[1]).value();
|
||||
float target_distance = parse_number<float>(v[2]).value();
|
||||
float target_snr = parse_number<float>(v[4]).value();
|
||||
|
||||
// Update the SNR, distance, and active status for the current target index
|
||||
t_snr[target_index] = target_snr;
|
||||
t_distance[target_index] = target_distance;
|
||||
t_active[target_index] = true;
|
||||
|
||||
if (target_count == target_index) {
|
||||
// Publish the data for each target without sorting
|
||||
for (int i = 1; i <= target_count; i++) {
|
||||
auto get_sensors = App.get_sensors();
|
||||
for (int j = 0; j < get_sensors.size(); j++) {
|
||||
std::string name = get_sensors[j]->get_name();
|
||||
if (name == "Target " + std::to_string(i) + " Distance") {
|
||||
get_sensors[j]->publish_state(t_distance[i]);
|
||||
} else if (name == "Target " + std::to_string(i) + " SNR") {
|
||||
get_sensors[j]->publish_state(t_snr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Publish the state of the binary sensor for each target
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
auto get_binary_sensors = App.get_binary_sensors();
|
||||
for (int j = 0; j < get_binary_sensors.size(); j++) {
|
||||
std::string name = get_binary_sensors[j]->get_name();
|
||||
if (name == "Target " + std::to_string(i) + " Active") {
|
||||
get_binary_sensors[j]->publish_state(t_active[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = target_count + 1; i <= 9; i++) {
|
||||
t_active[i] = false;
|
||||
}
|
||||
|
||||
//ESP_LOGD("custom", "Target %d Distance: %f, SNR: %f", i, t_distance[i], t_snr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
1
esphome/include/SEN0395/__init__.py
Executable file
1
esphome/include/SEN0395/__init__.py
Executable file
@@ -0,0 +1 @@
|
||||
#__init__.py
|
||||
102
esphome/include/SEN0395_distance.h
Executable file
102
esphome/include/SEN0395_distance.h
Executable file
@@ -0,0 +1,102 @@
|
||||
#include "esphome.h"
|
||||
#include <string>
|
||||
|
||||
class Sen0395Distance : public Component, public UARTDevice {
|
||||
public:
|
||||
Sen0395Distance(UARTComponent *parent) : UARTDevice(parent) {}
|
||||
|
||||
float t_snr[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
float t_distance[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
bool t_active[10] = {false, false, false, false, false, false, false, false, false, false};
|
||||
|
||||
void setup() override {
|
||||
// nothing to do here
|
||||
}
|
||||
|
||||
int readline(int readch, char *buffer, int len) {
|
||||
static int pos = 0;
|
||||
int rpos;
|
||||
|
||||
if (readch > 0) {
|
||||
switch (readch) {
|
||||
case '\n': // Ignore new-lines
|
||||
break;
|
||||
case '\r': // Return on CR
|
||||
rpos = pos;
|
||||
pos = 0; // Reset position index ready for next time
|
||||
return rpos;
|
||||
default:
|
||||
if (pos < len - 1) {
|
||||
buffer[pos++] = readch;
|
||||
buffer[pos] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// No end of line has been found, so return -1.
|
||||
return -1;
|
||||
}
|
||||
|
||||
void loop() override {
|
||||
const int max_line_length = 40;
|
||||
static char buffer[max_line_length];
|
||||
|
||||
while (available()) {
|
||||
if (readline(read(), buffer, max_line_length) >= 4) {
|
||||
std::string line = buffer;
|
||||
|
||||
if (line.substr(0, 6) == "$JYRPO") {
|
||||
std::string vline = line.substr(6);
|
||||
std::vector<std::string> v;
|
||||
for (int i = 0; i < vline.length(); i++) {
|
||||
if (vline[i] == ',') {
|
||||
v.push_back("");
|
||||
} else {
|
||||
v.back() += vline[i];
|
||||
}
|
||||
}
|
||||
int target_count = parse_number<int>(v[0]).value();
|
||||
int target_index = parse_number<int>(v[1]).value();
|
||||
float target_distance = parse_number<float>(v[2]).value();
|
||||
float target_snr = parse_number<float>(v[4]).value();
|
||||
|
||||
// Update the SNR, distance, and active status for the current target index
|
||||
t_snr[target_index] = target_snr;
|
||||
t_distance[target_index] = target_distance;
|
||||
t_active[target_index] = true;
|
||||
|
||||
if (target_count == target_index) {
|
||||
// Publish the data for each target without sorting
|
||||
for (int i = 1; i <= target_count; i++) {
|
||||
auto get_sensors = App.get_sensors();
|
||||
for (int j = 0; j < get_sensors.size(); j++) {
|
||||
std::string name = get_sensors[j]->get_name();
|
||||
if (name == "Target " + std::to_string(i) + " Distance") {
|
||||
get_sensors[j]->publish_state(t_distance[i]);
|
||||
} else if (name == "Target " + std::to_string(i) + " SNR") {
|
||||
get_sensors[j]->publish_state(t_snr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Publish the state of the binary sensor for each target
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
auto get_binary_sensors = App.get_binary_sensors();
|
||||
for (int j = 0; j < get_binary_sensors.size(); j++) {
|
||||
std::string name = get_binary_sensors[j]->get_name();
|
||||
if (name == "Target " + std::to_string(i) + " Active") {
|
||||
get_binary_sensors[j]->publish_state(t_active[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = target_count + 1; i <= 9; i++) {
|
||||
t_active[i] = false;
|
||||
}
|
||||
|
||||
//ESP_LOGD("custom", "Target %d Distance: %f, SNR: %f", i, t_distance[i], t_snr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
34
esphome/include/acs712_component.h
Normal file
34
esphome/include/acs712_component.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "ACS712.h"
|
||||
|
||||
class ACS712Sensor : public PollingComponent {
|
||||
public:
|
||||
ACS712 *ACS = new ACS712(A0, 5.0, 1023, 100);
|
||||
Sensor *current_sensor = new Sensor();
|
||||
Sensor *power_sensor = new Sensor();
|
||||
|
||||
ACS712Sensor() : PollingComponent(15000) {}
|
||||
|
||||
void setup() override {
|
||||
ACS->autoMidPoint();
|
||||
ESP_LOGD("acs712", "MidPoint: %d", ACS->getMidPoint());
|
||||
ACS->setNoisemV(43);
|
||||
ESP_LOGD("acs712", "Noise mV: %d", ACS->getNoisemV());
|
||||
}
|
||||
|
||||
void update() override {
|
||||
float average = 0;
|
||||
//uint32_t start = millis();
|
||||
int count = 5;
|
||||
for (int i = 0; i < count; i++) {
|
||||
average += ACS->mA_DC();
|
||||
}
|
||||
float amps = average / count / 1000.0;
|
||||
// float mA = ACS.mA_AC(50,10);
|
||||
//uint32_t duration = millis() - start;
|
||||
|
||||
//ESP_LOGD("acs712", "Time: %d A: ", duration, amps);
|
||||
|
||||
current_sensor->publish_state(amps);
|
||||
power_sensor->publish_state(amps * 19);
|
||||
}
|
||||
};
|
||||
@@ -2,37 +2,37 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#define ICON_stop "\U000F04DB"
|
||||
#define ICON_play "\U000F040A"
|
||||
#define ICON_pause "\U000F03E4"
|
||||
// #define ICON_stop "\U000F04DB"
|
||||
// #define ICON_play "\U000F040A"
|
||||
// #define ICON_pause "\U000F03E4"
|
||||
|
||||
std::string playbackStatusToIcon(bool playing, bool paused) {
|
||||
if (playing) return ICON_play;
|
||||
else if (paused) return ICON_pause;
|
||||
else return ICON_stop;
|
||||
}
|
||||
// std::string playbackStatusToIcon(bool playing, bool paused) {
|
||||
// if (playing) return ICON_play;
|
||||
// else if (paused) return ICON_pause;
|
||||
// else return ICON_stop;
|
||||
// }
|
||||
|
||||
#define ICON_moon_first_quarter "\U000F0F61"
|
||||
#define ICON_moon_full "\U000F0F62"
|
||||
#define ICON_moon_last_quarter "\U000F0F63"
|
||||
#define ICON_moon_new "\U000F0F64"
|
||||
#define ICON_moon_waning_crescent "\U000F0F65"
|
||||
#define ICON_moon_waning_gibbous "\U000F0F66"
|
||||
#define ICON_moon_waxing_crescent "\U000F0F67"
|
||||
#define ICON_moon_waxing_gibbous "\U000F0F68"
|
||||
// #define ICON_moon_first_quarter "\U000F0F61"
|
||||
// #define ICON_moon_full "\U000F0F62"
|
||||
// #define ICON_moon_last_quarter "\U000F0F63"
|
||||
// #define ICON_moon_new "\U000F0F64"
|
||||
// #define ICON_moon_waning_crescent "\U000F0F65"
|
||||
// #define ICON_moon_waning_gibbous "\U000F0F66"
|
||||
// #define ICON_moon_waxing_crescent "\U000F0F67"
|
||||
// #define ICON_moon_waxing_gibbous "\U000F0F68"
|
||||
|
||||
std::string moonToIcon(std::string moonPhase)
|
||||
{
|
||||
if (moonPhase == "new_moon") return ICON_moon_new;
|
||||
if (moonPhase == "waxing_crescent") return ICON_moon_waxing_crescent;
|
||||
if (moonPhase == "first_quarter") return ICON_moon_first_quarter;
|
||||
if (moonPhase == "waxing_gibbous") return ICON_moon_waxing_gibbous;
|
||||
if (moonPhase == "full_moon") return ICON_moon_full;
|
||||
if (moonPhase == "waning_gibbous") return ICON_moon_waning_gibbous;
|
||||
if (moonPhase == "last_quarter") return ICON_moon_last_quarter;
|
||||
if (moonPhase == "waning_crescent") return ICON_moon_waning_crescent;
|
||||
return "";
|
||||
}
|
||||
// std::string moonToIcon(std::string moonPhase)
|
||||
// {
|
||||
// if (moonPhase == "new_moon") return ICON_moon_new;
|
||||
// if (moonPhase == "waxing_crescent") return ICON_moon_waxing_crescent;
|
||||
// if (moonPhase == "first_quarter") return ICON_moon_first_quarter;
|
||||
// if (moonPhase == "waxing_gibbous") return ICON_moon_waxing_gibbous;
|
||||
// if (moonPhase == "full_moon") return ICON_moon_full;
|
||||
// if (moonPhase == "waning_gibbous") return ICON_moon_waning_gibbous;
|
||||
// if (moonPhase == "last_quarter") return ICON_moon_last_quarter;
|
||||
// if (moonPhase == "waning_crescent") return ICON_moon_waning_crescent;
|
||||
// return "";
|
||||
// }
|
||||
|
||||
// Map weather states to MDI characters.
|
||||
std::map<std::string, std::string> weather_icon_map
|
||||
@@ -50,13 +50,13 @@ std::map<std::string, std::string> weather_icon_map
|
||||
{"night-partly-cloudy", "\U000F0F31"},
|
||||
{"partlycloudy", "\U000F0595"},
|
||||
{"partly-lightning", "\U000F0F32"},
|
||||
{"partly-rainy", "\U000F0F33"},
|
||||
{"partly-snowy", "\U000F0F34"},
|
||||
{"partly-rainy", "\U000F0597"}, //compacting U000F0F33
|
||||
{"partly-snowy", "\U000F0598"}, //compacting U000F0F34
|
||||
{"partly-snowy-rainy", "\U000F0F35"},
|
||||
{"pouring", "\U000F0596"},
|
||||
{"rainy", "\U000F0597"},
|
||||
{"snowy", "\U000F0598"},
|
||||
{"snowy-heavy", "\U000F0F36"},
|
||||
{"snowy-heavy", "\U000F0598"}, //compacting U000F0F36
|
||||
{"snowy-rainy", "\U000F067F"},
|
||||
{"sunny", "\U000F0599"},
|
||||
{"sunny-alert", "\U000F0F37"},
|
||||
@@ -64,22 +64,22 @@ std::map<std::string, std::string> weather_icon_map
|
||||
{"sunset", "\U000F059A"},
|
||||
{"sunset-down", "\U000F059B"},
|
||||
{"sunset-up", "\U000F059C"},
|
||||
{"tornado", "\U000F0F38"},
|
||||
{"tornado", "\U000F059D"}, //compacting U000F0F38
|
||||
{"windy", "\U000F059D"},
|
||||
{"windy-variant", "\U000F059E"},
|
||||
{"windy-variant", "\U000F059D"}, //compacting U000F059E
|
||||
{"car", "\U000f010b"},
|
||||
{"trash", "\U000F0819"},
|
||||
};
|
||||
std::map<std::string, std::string> moon_icon_map
|
||||
{
|
||||
{ "mdi:moon-waxing-crescent", "\U000f0f67" },
|
||||
{ "mdi:moon-first-quarter", "\U000F0F61" },
|
||||
{ "mdi:moon-waxing-gibbous", "\U000F0F68" },
|
||||
{ "mdi:moon-full", "\U000F0F62" },
|
||||
{ "mdi:moon-waning-gibbous", "\U000F0F66" },
|
||||
{ "mdi:moon-last-quarter", "\U000F0F63" },
|
||||
{ "mdi:moon-waning-crescent", "\U000F0F65" },
|
||||
};
|
||||
// std::map<std::string, std::string> moon_icon_map
|
||||
// {
|
||||
// { "mdi:moon-waxing-crescent", "\U000f0f67" },
|
||||
// { "mdi:moon-first-quarter", "\U000F0F61" },
|
||||
// { "mdi:moon-waxing-gibbous", "\U000F0F68" },
|
||||
// { "mdi:moon-full", "\U000F0F62" },
|
||||
// { "mdi:moon-waning-gibbous", "\U000F0F66" },
|
||||
// { "mdi:moon-last-quarter", "\U000F0F63" },
|
||||
// { "mdi:moon-waning-crescent", "\U000F0F65" },
|
||||
// };
|
||||
|
||||
|
||||
#define ICON_w_clear_night "\U000F0594"
|
||||
|
||||
61
esphome/include/us100.h
Executable file
61
esphome/include/us100.h
Executable file
@@ -0,0 +1,61 @@
|
||||
#include "esphome.h"
|
||||
|
||||
/* To start measuring the distance, output a 0x55 over the serial port and
|
||||
* read back the two byte distance in high byte, low byte format. The
|
||||
* distance returned is measured in millimeters. Use the following formula
|
||||
* to obtain the distance as millimeters:
|
||||
*
|
||||
* Millimeters = FirstByteRead * 256 + SecondByteRead
|
||||
*
|
||||
* This module can also output the temperature when using serial output
|
||||
* mode. To read the temperature, output a 0x50 byte over the serial port
|
||||
* and read back a single temperature byte. The actual temperature is
|
||||
* obtained by using the following formula:
|
||||
*
|
||||
* Celsius = ByteRead - 45
|
||||
*/
|
||||
|
||||
class US100 : public PollingComponent, public UARTDevice {
|
||||
|
||||
public:
|
||||
US100(UARTComponent *parent) : PollingComponent(10000), UARTDevice(parent) {}
|
||||
|
||||
Sensor *tempsensor = new Sensor();
|
||||
Sensor *distsensor = new Sensor();
|
||||
|
||||
void setup() override {
|
||||
}
|
||||
|
||||
void loop() override {
|
||||
if(bytesexpected==2 && available() >= 2) {
|
||||
// we're expecting a distance measurement to come in, and there are
|
||||
// enough bytes for it, process it
|
||||
unsigned int mm = read() * 256 + read();
|
||||
if((mm>1) && (mm<10000)) {
|
||||
//ESP_LOGD("us100","distance is %u",mm);
|
||||
distsensor->publish_state(mm);
|
||||
}
|
||||
// finished with distance measurement, move on to temperature
|
||||
flush();
|
||||
write(0x50); // tell the US100 to start a temperature measurement
|
||||
bytesexpected=1; // we should start looking for a temperature reading
|
||||
} else if(bytesexpected==1 && available() >= 1) {
|
||||
// we are looking for a temperature and there are bytes to read
|
||||
int temp = read();
|
||||
if((temp>1) && (temp<130)) {
|
||||
temp -= 45;
|
||||
//ESP_LOGD("us100","temperature is %d",temp);
|
||||
tempsensor->publish_state(temp);
|
||||
}
|
||||
bytesexpected=0; // stop looking for bytes
|
||||
}
|
||||
}
|
||||
void update() override {
|
||||
flush();
|
||||
write(0x55); // tell the US100 to start a distance measurement
|
||||
bytesexpected=2; // tell loop() that it should start looking for a distance
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int bytesexpected=0;
|
||||
};
|
||||
1
esphome/include/www.js
Executable file
1
esphome/include/www.js
Executable file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user