Skip to content

Instantly share code, notes, and snippets.

@simkim
Last active January 29, 2024 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simkim/9f383eb82bff9e3ba227ecbdacccbf06 to your computer and use it in GitHub Desktop.
Save simkim/9f383eb82bff9e3ba227ecbdacccbf06 to your computer and use it in GitHub Desktop.
somfy arduino remote
#include <EEPROM.h>
#include <EEPROMRollingCodeStorage.h>
#include <ELECHOUSE_CC1101_SRC_DRV.h>
#include <SomfyRemote.h>
#define EMITTER_GPIO 2
#define EEPROM_ADDRESS 0
#define CC1101_FREQUENCY 433.42
class CLICommand {
String command;
uint32_t remoteAddr;
int eeprom_slot;
public:
void readLine() {
const String line = Serial.readStringUntil('\n');
int wordPtr = line.indexOf(" ");
const String remoteString = line.substring(0, wordPtr);
remoteAddr = remoteString.toInt();
line.remove(0, wordPtr+1);
wordPtr = line.indexOf(" ");
const String indexString = line.substring(0, wordPtr);
eeprom_slot = indexString.toInt();
line.remove(0, wordPtr+1);
wordPtr = line.indexOf("\n");
command = line.substring(0, wordPtr-1);
}
void debug() {
Serial.print("command ");
Serial.print(command);
Serial.print(" to ");
Serial.println(remoteAddr);
Serial.print(" index ");
Serial.print(eeprom_slot);
Serial.print(" code ");
uint16_t code;
EEPROM.get(eeprom_slot*2, code);
Serial.println(code);
}
void send() {
const Command scommand = getSomfyCommand(command);
EEPROMRollingCodeStorage rollingCodeStorage(eeprom_slot*2);
const SomfyRemote somfyRemote(EMITTER_GPIO, remoteAddr, &rollingCodeStorage);
ELECHOUSE_cc1101.SetTx();
somfyRemote.sendCommand(scommand);
ELECHOUSE_cc1101.setSidle();
}
};
void setup() {
Serial.begin(115200);
pinMode(EMITTER_GPIO, OUTPUT);
digitalWrite(EMITTER_GPIO, LOW);
ELECHOUSE_cc1101.Init();
ELECHOUSE_cc1101.setMHZ(CC1101_FREQUENCY);
#if defined(ESP32)
if (!EEPROM.begin(4)) {
Serial.println("failed to initialise EEPROM");
delay(1000);
}
#elif defined(ESP8266)
EEPROM.begin(4);
#endif
}
void loop() {
CLICommand cli;
if (Serial.available() > 0) {
cli.readLine();
cli.debug();
cli.send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment