Trial this

This commit is contained in:
Daniël Koek 2024-03-26 11:30:31 +00:00
parent f86424d556
commit 3543af84ed
2 changed files with 52 additions and 54 deletions

View file

@ -3,6 +3,17 @@
namespace esphome { namespace esphome {
namespace lora { namespace lora {
void Lora::update() { void Lora::update() {
// all good!
if (!this->pin_aux_->digital_read()) {
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
}
// it has taken too long to complete, error out!
if ((millis() - this->starting_to_check_) > this->time_out_after_) {
ESP_LOGD(TAG, "Timeout error! Resetting timers");
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
}
if (!this->update_needed_) if (!this->update_needed_)
return; return;
if (this->rssi_sensor_ != nullptr) if (this->rssi_sensor_ != nullptr)
@ -33,19 +44,14 @@ void Lora::setup() {
this->pin_m1_->digital_write(true); this->pin_m1_->digital_write(true);
} }
bool status = set_mode_(MODE_0_NORMAL); set_mode_(MODE_0_NORMAL);
if (status) {
ESP_LOGD(TAG, "Setup success"); ESP_LOGD(TAG, "Setup success");
} else {
ESP_LOGD(TAG, "Something went wrong");
}
} }
bool Lora::set_mode_(ModeType mode) { void Lora::set_mode_(ModeType mode) {
// data sheet claims module needs some extra time after mode setting (2ms) if (!Lora::can_send_message_()) {
// most of my projects uses 10 ms, but 40ms is safer return;
}
delay(40);
if (this->pin_m0_ == nullptr && this->pin_m1_ == nullptr) { if (this->pin_m0_ == nullptr && this->pin_m1_ == nullptr) {
ESP_LOGD(TAG, "The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!"); ESP_LOGD(TAG, "The M0 and M1 pins is not set, this mean that you are connect directly the pins as you need!");
} else { } else {
@ -73,46 +79,32 @@ bool Lora::set_mode_(ModeType mode) {
this->pin_m1_->digital_write(true); this->pin_m1_->digital_write(true);
ESP_LOGD(TAG, "MODE SLEEP CONFIG!"); ESP_LOGD(TAG, "MODE SLEEP CONFIG!");
break; break;
default:
return false;
} }
} }
// data sheet says 2ms later control is returned, let's give just a bit more time
// these modules can take time to activate pins
delay(40);
// wait until aux pin goes back low // wait until aux pin goes back low
bool result = this->wait_complete_response_(1000); this->setup_wait_response_(1000);
if (result) {
this->mode_ = mode; this->mode_ = mode;
ESP_LOGD(TAG, "Mode set"); ESP_LOGD(TAG, "Mode is going to be set");
return true; }
} else { bool Lora::can_send_message_() {
ESP_LOGD(TAG, "No success setting mode"); // if the pin is still high, we should not be doing anything
if (this->pin_aux_->digital_read()) {
ESP_LOGD(TAG, "Aux pin is still high!");
return false; return false;
} else {
// the pin isn't high anymore, but the system isn't updated yet
this->starting_to_check_ = 0;
this->time_out_after_ = 0;
return true;
} }
} }
bool Lora::wait_complete_response_(uint32_t timeout) { void Lora::setup_wait_response_(uint32_t timeout) {
uint32_t start_millis = millis(); if (this->starting_to_check_ != 0 || this->time_out_after_ != 0) {
ESP_LOGD(TAG, "Wait response already set!! %u", timeout);
// make darn sure millis() is not about to reach max data type limit and start over
if ((start_millis + timeout) == 0) {
start_millis = 0;
} }
ESP_LOGD(TAG, "Checking if response was complete"); ESP_LOGD(TAG, "Setting a timer for %u", timeout);
// loop till not high, or the timeout is reached this->starting_to_check_ = millis();
while (!this->pin_aux_->digital_read()) { this->time_out_after_ = timeout;
if ((millis() - start_millis) > timeout) {
ESP_LOGD(TAG, "Timeout error!");
return false;
}
delayMicroseconds(2);
}
// per data sheet control after aux goes high is 2ms so delay for at least that long)
delayMicroseconds(2);
return true;
} }
void Lora::dump_config() { void Lora::dump_config() {
ESP_LOGCONFIG(TAG, "Ebyte Lora E220"); ESP_LOGCONFIG(TAG, "Ebyte Lora E220");
@ -121,7 +113,10 @@ void Lora::dump_config() {
LOG_PIN("M1 Pin:", this->pin_m1_); LOG_PIN("M1 Pin:", this->pin_m1_);
}; };
void Lora::digital_write(uint8_t pin, bool value) { this->send_pin_info_(pin, value); } void Lora::digital_write(uint8_t pin, bool value) { this->send_pin_info_(pin, value); }
bool Lora::send_pin_info_(uint8_t pin, bool value) { void Lora::send_pin_info_(uint8_t pin, bool value) {
if (!Lora::can_send_message_()) {
return;
}
uint8_t data[3]; uint8_t data[3];
data[1] = 0xA5; // just some bit to indicate, yo this is pin info data[1] = 0xA5; // just some bit to indicate, yo this is pin info
data[1] = pin; // Pin to send data[1] = pin; // Pin to send
@ -130,13 +125,13 @@ bool Lora::send_pin_info_(uint8_t pin, bool value) {
ESP_LOGD(TAG, "PIN: %u ", data[1]); ESP_LOGD(TAG, "PIN: %u ", data[1]);
ESP_LOGD(TAG, "VALUE: %u ", data[2]); ESP_LOGD(TAG, "VALUE: %u ", data[2]);
this->write_array(data, sizeof(data)); this->write_array(data, sizeof(data));
bool return_value = this->wait_complete_response_(5000); this->setup_wait_response_(5000);
this->flush(); ESP_LOGD(TAG, "Successfully put in queue");
if (return_value)
ESP_LOGD(TAG, "Success!");
return return_value;
} }
void Lora::loop() { void Lora::loop() {
if (!Lora::can_send_message_()) {
return;
}
std::string buffer; std::string buffer;
std::vector<uint8_t> data; std::vector<uint8_t> data;
bool pin_data_found = false; bool pin_data_found = false;

View file

@ -48,16 +48,19 @@ class Lora : public PollingComponent, public uart::UARTDevice {
private: private:
ModeType mode_ = MODE_0_NORMAL; ModeType mode_ = MODE_0_NORMAL;
// set WOR mode // set WOR mode
bool set_mode_(ModeType mode); void set_mode_(ModeType mode);
// checks the aux port to see if it is done setting // checks the aux port to see if it is done setting
bool wait_complete_response_(uint32_t timeout = 1000); void setup_wait_response_(uint32_t timeout = 1000);
bool send_pin_info_(uint8_t pin, bool value); bool can_send_message_();
void send_pin_info_(uint8_t pin, bool value);
protected: protected:
bool update_needed_ = false; bool update_needed_ = false;
int rssi_ = 0; int rssi_ = 0;
float latitude_ = -1; float latitude_ = -1;
float longitude_ = -1; float longitude_ = -1;
uint32_t starting_to_check_;
uint32_t time_out_after_;
std::string raw_message_; std::string raw_message_;
text_sensor::TextSensor *message_text_sensor_{nullptr}; text_sensor::TextSensor *message_text_sensor_{nullptr};
sensor::Sensor *rssi_sensor_{nullptr}; sensor::Sensor *rssi_sensor_{nullptr};