├── .gitattributes ├── README.md └── Awning_Control_ESP32 ├── Credentials.h └── Awning_Control_ESP32.ino /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hacking-433 2 | 3 | This is the code for this video: https://youtu.be/L0fSEbGEY-Q 4 | -------------------------------------------------------------------------------- /Awning_Control_ESP32/Credentials.h: -------------------------------------------------------------------------------- 1 | 2 | #define CREDENTIALS 1 3 | 4 | // WLAN 5 | #define mySSID "" 6 | #define myPASSWORD "" -------------------------------------------------------------------------------- /Awning_Control_ESP32/Awning_Control_ESP32.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This example uses FreeRTOS softwaretimers as there is no built-in Ticker library 3 | */ 4 | 5 | 6 | #include 7 | extern "C" { 8 | #include "freertos/FreeRTOS.h" 9 | #include "freertos/timers.h" 10 | } 11 | #include 12 | #include 13 | 14 | #define MQTT_HOST IPAddress(192, 168, 0, 203) 15 | #define MQTT_PORT 1883 16 | 17 | 18 | #define SHORT_SPACE 250 19 | #define SHORT_SIG 350 20 | #define LONG_SIG_DIST 2000 21 | #define BETW 16000 22 | 23 | #define SEND_PIN 4 24 | 25 | String extendCommand = "LGSPSPSPSPSPSPSPLGSPLGLGLGSPLGSPSPSN"; 26 | String retractCommand = "LGSPSPSPSPSPSPSPLGSPLGLGLGSPLGSPLGSN"; 27 | String manCommand = "LGSPSPSPSPSPSPSPLGSPLGLGLGLGLN"; 28 | String autoCommand = "LGSPSPSPSPSPSPSPLGSPLGLGSPLGLGLGLGSN"; 29 | 30 | AsyncMqttClient mqttClient; 31 | TimerHandle_t mqttReconnectTimer; 32 | TimerHandle_t wifiReconnectTimer; 33 | 34 | void connectToWifi() { 35 | Serial.println("Connecting to Wi-Fi..."); 36 | WiFi.begin(mySSID, myPASSWORD); 37 | } 38 | 39 | void connectToMqtt() { 40 | Serial.println("Connecting to MQTT..."); 41 | mqttClient.connect(); 42 | Serial.println("Connecting to MQTT... exit"); 43 | } 44 | 45 | void WiFiEvent(WiFiEvent_t event) { 46 | Serial.printf("[WiFi-event] event: %d\n", event); 47 | switch (event) { 48 | case SYSTEM_EVENT_STA_GOT_IP: 49 | Serial.println("WiFi connected"); 50 | Serial.println("IP address: "); 51 | Serial.println(WiFi.localIP()); 52 | connectToMqtt(); 53 | break; 54 | case SYSTEM_EVENT_STA_DISCONNECTED: 55 | Serial.println("WiFi lost connection"); 56 | xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi 57 | xTimerStart(wifiReconnectTimer, 0); 58 | break; 59 | } 60 | } 61 | 62 | void onMqttConnect(bool sessionPresent) { 63 | Serial.println("Connected to MQTT."); 64 | Serial.print("Session present: "); 65 | Serial.println(sessionPresent); 66 | uint16_t packetIdSub = mqttClient.subscribe("curtain/command", 2); 67 | Serial.print("Subscribing at QoS 2, packetId: "); 68 | Serial.println(packetIdSub); 69 | mqttClient.publish("curtain/status", 0, true, "connected"); 70 | Serial.println("Publishing at QoS 0"); 71 | } 72 | 73 | void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { 74 | Serial.println("Disconnected from MQTT."); 75 | 76 | if (WiFi.isConnected()) { 77 | xTimerStart(mqttReconnectTimer, 0); 78 | } 79 | } 80 | 81 | void onMqttSubscribe(uint16_t packetId, uint8_t qos) { 82 | Serial.println("Subscribe acknowledged."); 83 | Serial.print(" packetId: "); 84 | Serial.println(packetId); 85 | Serial.print(" qos: "); 86 | Serial.println(qos); 87 | } 88 | 89 | void onMqttUnsubscribe(uint16_t packetId) { 90 | Serial.println("Unsubscribe acknowledged."); 91 | Serial.print(" packetId: "); 92 | Serial.println(packetId); 93 | } 94 | 95 | void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { 96 | Serial.println("Message received."); 97 | Serial.print("Payload: "); 98 | Serial.println(payload); 99 | /* 100 | Serial.print(" topic: "); 101 | Serial.println(topic); 102 | Serial.print(" qos: "); 103 | Serial.println(properties.qos); 104 | Serial.print(" dup: "); 105 | Serial.println(properties.dup); 106 | Serial.print(" retain: "); 107 | Serial.println(properties.retain); 108 | Serial.print(" len: "); 109 | Serial.println(len); 110 | Serial.print(" index: "); 111 | Serial.println(index); 112 | Serial.print(" total: "); 113 | Serial.println(total); 114 | */ 115 | int dur = payload[1] - 48; 116 | // Serial.print(" duration: "); 117 | // Serial.println(dur); 118 | 119 | switch ( payload[0]) { 120 | case 'E': 121 | Serial.println("Extend Curtain"); 122 | sendSig(manCommand, 1); 123 | sendSig(extendCommand, 10 * dur); 124 | sendSig(retractCommand, 1); 125 | break; 126 | case 'R': 127 | Serial.println("Retract Curtain"); 128 | sendSig(manCommand, 1); 129 | sendSig(retractCommand, 2); 130 | break; 131 | default: 132 | Serial.println("Error"); 133 | break; 134 | } 135 | Serial.println("Done"); 136 | } 137 | 138 | void onMqttPublish(uint16_t packetId) { 139 | Serial.println("Publish acknowledged."); 140 | Serial.print(" packetId: "); 141 | Serial.println(packetId); 142 | } 143 | 144 | void setup() { 145 | Serial.begin(115200); 146 | Serial.println(); 147 | Serial.println(); 148 | pinMode(SEND_PIN, OUTPUT); 149 | digitalWrite(SEND_PIN, LOW); 150 | delay(100); 151 | sendSig(manCommand, 1); 152 | sendSig(retractCommand, 2); 153 | 154 | mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast(connectToMqtt)); 155 | wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast(connectToWifi)); 156 | 157 | WiFi.onEvent(WiFiEvent); 158 | 159 | mqttClient.onConnect(onMqttConnect); 160 | mqttClient.onDisconnect(onMqttDisconnect); 161 | mqttClient.onSubscribe(onMqttSubscribe); 162 | mqttClient.onUnsubscribe(onMqttUnsubscribe); 163 | mqttClient.onMessage(onMqttMessage); 164 | mqttClient.onPublish(onMqttPublish); 165 | mqttClient.setCredentials("admin", "admin"); 166 | mqttClient.setServer(MQTT_HOST, MQTT_PORT); 167 | connectToWifi(); 168 | } 169 | 170 | void loop() { 171 | } 172 | 173 | void sendSig(String sig, int duration) { 174 | if (duration < 1)duration = 1; 175 | if (duration > 90)duration = 90; 176 | unsigned long entry = millis(); 177 | while (millis() < entry + duration * 1000) { 178 | for (int i = 0; i < sig.length(); i++) { 179 | switch (sig[i]) { 180 | case 'S': // short pulse 181 | sigPuls(SHORT_SIG); 182 | break; 183 | case 'L': // long pulse 184 | sigPuls(LONG_SIG_DIST); 185 | break; 186 | case 'G': //short space 187 | delayMicroseconds(SHORT_SPACE); 188 | break; 189 | case 'P': // long space (pause) 190 | delayMicroseconds(LONG_SIG_DIST); 191 | break; 192 | case 'N': // new command 193 | delayMicroseconds(BETW); 194 | break; 195 | default: 196 | Serial.println("Error"); 197 | break; 198 | } 199 | } 200 | // Serial.println(sig); 201 | } 202 | } 203 | 204 | void sigPuls(int duration) { 205 | digitalWrite(SEND_PIN, HIGH); 206 | delayMicroseconds(duration); 207 | digitalWrite(SEND_PIN, LOW); 208 | } 209 | --------------------------------------------------------------------------------