Skip to content

Commit

Permalink
Increase timing for prog command responses for slow motor pairing #121
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrouse committed Aug 18, 2023
1 parent 0314760 commit ff2d150
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 157 deletions.
5 changes: 3 additions & 2 deletions Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ void Network::loop() {
this->emitSockets();
if(!this->connected()) return;
}
if(this->connected() && millis() - this->lastMDNS > 60000) {
if(this->connected() && millis() - this->lastMDNS > 10000) {
if(this->lastMDNS != 0) MDNS.setInstanceName(settings.hostname);
this->lastMDNS = millis();
}
sockEmit.loop();
if(settings.ssdpBroadcast) {
if(!SSDP.isStarted) SSDP.begin();
SSDP.loop();
Expand All @@ -74,13 +73,15 @@ void Network::emitSockets() {
sockEmit.sendToClients("wifiStrength", buf);
this->lastRSSI = WiFi.RSSI();
this->lastChannel = WiFi.channel();
sockEmit.loop();
}
}
else {
if(this->connType == conn_types::ethernet && this->lastRSSI != -100 && this->lastChannel != -1) {
sockEmit.sendToClients("wifiStrength", "{\"ssid\":\"\", \"strength\":-100,\"channel\":-1}");
this->lastRSSI = -100;
this->lastChannel = -1;
sockEmit.loop();
}
}
}
Expand Down
67 changes: 44 additions & 23 deletions Somfy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,11 @@ void SomfyShade::emitCommand(uint8_t num, somfy_commands cmd, const char *source
e.appendMessage(buf);
snprintf(buf, sizeof(buf), ",\"source\":\"%s\"", source);
e.appendMessage(buf);
snprintf(buf, sizeof(buf), ",\"rcode\":%d", this->lastRollingCode);
e.appendMessage(buf);
snprintf(buf, sizeof(buf), ",\"sourceAddress\":%d}", sourceAddress);
e.appendMessage(buf);

if(num >= 255) sockEmit.sendToClients(&e);
else sockEmit.sendToClient(num, &e);
}
Expand Down Expand Up @@ -2514,32 +2517,48 @@ somfy_commands SomfyRemote::transformCommand(somfy_commands cmd) {
}
void SomfyRemote::sendCommand(somfy_commands cmd) { this->sendCommand(cmd, this->repeats); }
void SomfyRemote::sendCommand(somfy_commands cmd, uint8_t repeat) {
somfy_frame_t frame;
frame.rollingCode = this->getNextRollingCode();
frame.remoteAddress = this->getRemoteAddress();
frame.cmd = this->transformCommand(cmd);
frame.repeats = repeat;
frame.bitLength = this->bitLength;
this->lastFrame.rollingCode = this->getNextRollingCode();
this->lastFrame.remoteAddress = this->getRemoteAddress();
this->lastFrame.cmd = this->transformCommand(cmd);
this->lastFrame.repeats = repeat;
this->lastFrame.bitLength = this->bitLength;
// Match the encKey to the rolling code. These keys range from 160 to 175.
frame.encKey = 0xA0 | static_cast<uint8_t>(frame.rollingCode & 0x000F);
frame.proto = this->proto;
if(frame.bitLength == 0) frame.bitLength = bit_length;
this->lastRollingCode = frame.rollingCode;
somfy.sendFrame(frame, repeat);
somfy.processFrame(frame, true);
}
void SomfyShadeController::sendFrame(somfy_frame_t &frame, uint8_t repeat) {
somfy.transceiver.beginTransmit();
//Serial.println("----------- Sending Raw -------------");
this->lastFrame.encKey = 0xA0 | static_cast<uint8_t>(this->lastFrame.rollingCode & 0x000F);
this->lastFrame.proto = this->proto;
if(this->lastFrame.bitLength == 0) this->lastFrame.bitLength = bit_length;
if(this->lastFrame.rollingCode == 0) Serial.println("ERROR: Setting rcode to 0");
this->lastRollingCode = this->lastFrame.rollingCode;
Serial.print("CMD:");
Serial.print(translateSomfyCommand(frame.cmd));
Serial.print(translateSomfyCommand(this->lastFrame.cmd));
Serial.print(" ADDR:");
Serial.print(frame.remoteAddress);
Serial.print(this->lastFrame.remoteAddress);
Serial.print(" RCODE:");
Serial.print(frame.rollingCode);
Serial.print(this->lastFrame.rollingCode);
Serial.print(" REPEAT:");
Serial.println(repeat);

somfy.sendFrame(this->lastFrame, repeat);
somfy.processFrame(this->lastFrame, true);
}
bool SomfyRemote::isLastCommand(somfy_commands cmd) {
if(this->lastFrame.cmd != cmd || this->lastFrame.rollingCode != this->lastRollingCode) {
Serial.printf("Not the last command %d: %d - %d\n", static_cast<uint8_t>(this->lastFrame.cmd), this->lastFrame.rollingCode, this->lastRollingCode);
return false;
}
return true;
}
void SomfyRemote::repeatFrame(uint8_t repeat) {
somfy.transceiver.beginTransmit();
byte frm[10];
this->lastFrame.encodeFrame(frm);
somfy.transceiver.sendFrame(frm, this->bitLength == 56 ? 2 : 12, this->bitLength);
for(uint8_t i = 0; i < repeat; i++) {
somfy.transceiver.sendFrame(frm, this->bitLength == 56 ? 7 : 6, this->bitLength);
}
somfy.transceiver.endTransmit();
//somfy.processFrame(this->lastFrame, true);
}
void SomfyShadeController::sendFrame(somfy_frame_t &frame, uint8_t repeat) {
somfy.transceiver.beginTransmit();
byte frm[10];
frame.encodeFrame(frm);
this->transceiver.sendFrame(frm, frame.bitLength == 56 ? 2 : 12, frame.bitLength);
Expand Down Expand Up @@ -2604,6 +2623,7 @@ uint16_t SomfyRemote::getNextRollingCode() {
pref.putUShort(this->m_remotePrefId, code);
pref.end();
this->lastRollingCode = code;
Serial.printf("Getting Next Rolling code %d\n", this->lastRollingCode);
return code;
}
uint16_t SomfyRemote::setRollingCode(uint16_t code) {
Expand All @@ -2612,6 +2632,7 @@ uint16_t SomfyRemote::setRollingCode(uint16_t code) {
pref.putUShort(this->m_remotePrefId, code);
pref.end();
this->lastRollingCode = code;
Serial.printf("Setting Last Rolling code %d\n", this->lastRollingCode);
}
return code;
}
Expand Down Expand Up @@ -3035,15 +3056,15 @@ void Transceiver::clearReceived(void) {
attachInterrupt(interruptPin, handleReceive, CHANGE);
}
void Transceiver::enableReceive(void) {
uint32_t timing = millis();
if(rxmode > 0) return;
if(this->config.enabled) {
Serial.print("Enabling receive on Pin #");
Serial.println(this->config.RXPin);
rxmode = 1;
pinMode(this->config.RXPin, INPUT);
interruptPin = digitalPinToInterrupt(this->config.RXPin);
ELECHOUSE_cc1101.SetRx();
attachInterrupt(interruptPin, handleReceive, CHANGE);
Serial.printf("Enabled receive on Pin #%d Timing: %d\n", this->config.RXPin, millis() - timing);
}
}
void Transceiver::disableReceive(void) {
Expand Down Expand Up @@ -3382,7 +3403,7 @@ void Transceiver::beginTransmit() {
void Transceiver::endTransmit() {
if(this->config.enabled) {
ELECHOUSE_cc1101.setSidle();
delay(100);
//delay(100);
this->enableReceive();
}
}
6 changes: 4 additions & 2 deletions Somfy.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,24 @@ class SomfyRemote {
uint32_t m_remoteAddress = 0;
public:
radio_proto proto = radio_proto::RTS;
somfy_frame_t lastFrame;
bool flipCommands = false;
uint16_t lastRollingCode = 0;
uint8_t flags = 0;
uint8_t bitLength = 0;
uint8_t repeats = 1;
virtual bool isLastCommand(somfy_commands cmd);
char *getRemotePrefId() {return m_remotePrefId;}
virtual bool toJSON(JsonObject &obj);
virtual void setRemoteAddress(uint32_t address);
virtual uint32_t getRemoteAddress();
virtual uint16_t getNextRollingCode();
virtual uint16_t setRollingCode(uint16_t code);
uint16_t lastRollingCode = 0;
bool hasSunSensor();
void setSunSensor(bool bHasSensor);
virtual void sendCommand(somfy_commands cmd);
virtual void sendCommand(somfy_commands cmd, uint8_t repeat);
void repeatFrame(uint8_t repeat);
somfy_commands transformCommand(somfy_commands cmd);
};
class SomfyLinkedRemote : public SomfyRemote {
Expand Down Expand Up @@ -222,7 +225,6 @@ class SomfyShade : public SomfyRemote {
tilt_types tiltType = tilt_types::none;
void load();
somfy_tx_queue_t txQueue;
somfy_frame_t lastFrame;
float currentPos = 0.0f;
float currentTiltPos = 0.0f;
//uint16_t movement = 0;
Expand Down
10 changes: 10 additions & 0 deletions SomfyController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ void loop() {
Serial.println("ms");
ESP.restart();
}
uint32_t timing = millis();
net.loop();
if(millis() - timing > 100) Serial.printf("Timing Net: %dms\n", millis() - timing);
timing = millis();
somfy.loop();
if(millis() - timing > 100) Serial.printf("Timing Somfy: %dms\n", millis() - timing);
timing = millis();
if(net.connected()) {
webServer.loop();
if(millis() - timing > 200) Serial.printf("Timing WebServer: %dms\n", millis() - timing);
timing = millis();
sockEmit.loop();
if(millis() - timing > 100) Serial.printf("Timing Socket: %dms\n", millis() - timing);
timing = millis();

}
if(rebootDelay.reboot && millis() > rebootDelay.rebootTime) {
net.end();
Expand Down
Binary file modified SomfyController.ino.esp32.bin
Binary file not shown.
Binary file modified SomfyController.littlefs.bin
Binary file not shown.
Loading

0 comments on commit ff2d150

Please sign in to comment.