This repository has been archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathESPHomeRoombaComponent.h
203 lines (173 loc) · 6.7 KB
/
ESPHomeRoombaComponent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "esphome.h"
#include <Roomba.h>
static const char *TAG = "component.Roomba";
class RoombaComponent : public PollingComponent, public CustomMQTTDevice
{
protected:
uint8_t brcPin;
uint32_t updateInterval;
std::string stateTopic;
std::string commandTopic;
Roomba roomba;
public:
Sensor *distanceSensor;
Sensor *voltageSensor;
Sensor *currentSensor;
Sensor *chargeSensor;
Sensor *capacitySensor;
BinarySensor *chargingBinarySensor;
BinarySensor *dockedBinarySensor;
BinarySensor *cleaningBinarySensor;
static RoombaComponent* instance(const std::string &stateTopic, const std::string &commandTopic, uint8_t brcPin, uint32_t updateInterval)
{
static RoombaComponent* INSTANCE = new RoombaComponent(stateTopic, commandTopic, brcPin, updateInterval);
return INSTANCE;
}
void setup() override
{
ESP_LOGD(TAG, "Setting up roomba.");
pinMode(this->brcPin, OUTPUT);
digitalWrite(this->brcPin, HIGH);
this->roomba.start();
ESP_LOGD(TAG, "Attempting to subscribe to MQTT.");
subscribe(this->commandTopic, &RoombaComponent::on_message);
}
void update() override
{
ESP_LOGD(TAG, "Attempting to update sensor values.");
int16_t distance;
uint16_t voltage;
int16_t current;
uint16_t charge;
uint16_t capacity;
uint8_t charging;
bool cleaningState;
bool dockedState;
bool chargingState;
bool publishJson;
// Flush serial buffers
while (Serial.available())
{
Serial.read();
}
uint8_t sensors[] = {
Roomba::SensorDistance, // 2 bytes, mm, signed
Roomba::SensorChargingState, // 1 byte
Roomba::SensorVoltage, // 2 bytes, mV, unsigned
Roomba::SensorCurrent, // 2 bytes, mA, signed
Roomba::SensorBatteryCharge, // 2 bytes, mAh, unsigned
Roomba::SensorBatteryCapacity // 2 bytes, mAh, unsigned
};
uint8_t values[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Serial reading timeout -- https://community.home-assistant.io/t/add-wifi-to-an-older-roomba/23282/52
bool success = this->roomba.getSensorsList(sensors, sizeof(sensors), values, sizeof(values));
if (!success)
{
ESP_LOGD(TAG, "Unable to read sensors from the roomba.");
return;
}
distance = values[0] * 256 + values[1];
voltage = values[3] * 256 + values[4];
current = values[5] * 256 + values[6];
charge = values[7] * 256 + values[8];
capacity = values[9] * 256 + values[10];
charging = values[2];
cleaningState = current < -300;
dockedState = current > -50;
chargingState = charging == Roomba::ChargeStateReconditioningCharging || charging == Roomba::ChargeStateFullChanrging || charging == Roomba::ChargeStateTrickleCharging;
// Only publish new states if there was a change
if (this->distanceSensor->state != distance) {
this->distanceSensor->publish_state(distance);
}
if (this->voltageSensor->state != voltage) {
this->voltageSensor->publish_state(voltage);
}
if (this->currentSensor->state != current) {
this->currentSensor->publish_state(current);
}
if (this->chargeSensor->state != charge) {
this->chargeSensor->publish_state(charge);
}
if (this->capacitySensor->state != capacity) {
this->capacitySensor->publish_state(capacity);
}
if (this->chargingBinarySensor->state != chargingState) {
this->chargingBinarySensor->publish_state(chargingState);
}
if (this->dockedBinarySensor->state != dockedState) {
this->dockedBinarySensor->publish_state(dockedState);
}
if (this->cleaningBinarySensor->state != cleaningState) {
this->cleaningBinarySensor->publish_state(cleaningState);
}
static std::string lastBatteryLevel = "0.0";
static std::string lastState;
std::string batteryLevel = value_accuracy_to_string(100.0 * ((1.0 * charge) / (1.0 * capacity)), 2);
std::string state = cleaningState ? "cleaning" :
dockedState ? "docked" :
chargingState ? "idle" :
"idle";
// Publish to the state topic a json document; necessary for the 'state' schema
if (batteryLevel != lastBatteryLevel || state != lastState) {
lastBatteryLevel = batteryLevel;
lastState = state;
publish_json(this->stateTopic, [=](JsonObject &root) {
root["battery_level"] = parse_float(batteryLevel).value();
root["state"] = state;
root["fan_speed"] = "off";
});
}
}
void on_message(const std::string &payload)
{
ESP_LOGD(TAG, "Got values %s", payload.c_str());
// Roomba Wakeup
digitalWrite(this->brcPin, HIGH);
delay(100);
digitalWrite(this->brcPin, LOW);
delay(500);
digitalWrite(this->brcPin, HIGH);
delay(100);
if (payload == "turn_on" || payload == "turn_off" ||
payload == "start" || payload == "stop")
{
this->roomba.cover();
}
else if (payload == "dock" || payload == "return_to_base")
{
this->roomba.dock();
}
else if (payload == "locate")
{
this->roomba.playSong(1);
}
else if (payload == "spot" || payload == "clean_spot")
{
this->roomba.spot();
}
else
{
ESP_LOGW(TAG, "Received unknown status payload: %s", payload.c_str());
this->status_momentary_warning("state", 5000);
}
delay(500);
this->update();
}
private:
RoombaComponent(const std::string &stateTopic, const std::string &commandTopic, uint8_t brcPin, uint32_t updateInterval) :
PollingComponent(updateInterval), roomba(&Serial, Roomba::Baud115200)
{
this->brcPin = brcPin;
this->updateInterval = updateInterval;
this->stateTopic = stateTopic;
this->commandTopic = commandTopic;
this->distanceSensor = new Sensor();
this->voltageSensor = new Sensor();
this->currentSensor = new Sensor();
this->chargeSensor = new Sensor();
this->capacitySensor = new Sensor();
this->chargingBinarySensor = new BinarySensor();
this->dockedBinarySensor = new BinarySensor();
this->cleaningBinarySensor = new BinarySensor();
}
};