Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <PubSubClient.h>
- #include <RCSwitch.h>
- /*
- * ===============================
- * RC-SWITCH SETTINGS
- * ===============================
- */
- RCSwitch mySwitch = RCSwitch();
- /*
- * ===============================
- * MQTT SETTINGS
- * ===============================
- */
- #define MQTT_VERSION MQTT_VERSION_3_1_1
- // MQTT: ID, server IP, port
- const PROGMEM char* MQTT_CLIENT_ID = "home-assistant-1";
- const PROGMEM char* MQTT_SERVER_IP = "192.168.1.xx";
- const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
- const PROGMEM char* MQTT_USER = "myuser";
- const PROGMEM char* MQTT_PASSWORD = "abcdefg";
- const char* MQTT_LIGHT_STATE_TOPIC = "main/desk/status";
- const char* MQTT_LIGHT_COMMAND_TOPIC = "main/desk/switch";
- const char* LIGHT_ON = "ON";
- const char* LIGHT_OFF = "OFF";
- boolean m_light_state = false; // light is turned off by default
- PubSubClient client;
- void statusLight() {
- if (m_light_state) {
- client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
- } else {
- client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
- }
- }
- // function called to turn on/off the light
- void setLight() {
- if (m_light_state) {
- mySwitch.send("00010101010100010101000");
- Serial.println("INFO: Turn light on...");
- } else {
- mySwitch.send("00010101010100010101000");
- Serial.println("INFO: Turn light off...");
- }
- }
- void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
- // concat the payload into a string
- String payload;
- for (uint8_t i = 0; i < p_length; i++) {
- payload.concat((char)p_payload[i]);
- }
- Serial.println("This is the payload");
- Serial.print(payload);
- // handle message topic
- if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
- // test if the payload is equal to "ON" or "OFF"
- if (payload.equals(String(LIGHT_ON))) {
- if (m_light_state != true) {
- m_light_state = true;
- setLight();
- statusLight();
- }
- } else if (payload.equals(String(LIGHT_OFF))) {
- if (m_light_state != false) {
- m_light_state = false;
- setLight();
- statusLight();
- }
- }
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("INFO: Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(MQTT_CLIENT_ID,MQTT_USER,MQTT_PASSWORD)) {
- Serial.println("INFO: connected");
- // Once connected, publish an announcement...
- statusLight();
- // ... and resubscribe
- client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
- } else {
- Serial.print("ERROR: failed, rc=");
- Serial.print(client.state());
- Serial.println("DEBUG: try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- /*
- * ===============================
- * ETHERNET SETTINGS
- * ===============================
- */
- byte MAC_ADDRESS[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- void setup() {
- // init the serial
- Serial.begin(115200);
- //RC-switch initialization
- mySwitch.enableTransmit(24);
- mySwitch.setPulseLength(296);
- mySwitch.setProtocol(1);
- mySwitch.setRepeatTransmit(15);
- setLight();
- if (Ethernet.begin(MAC_ADDRESS) == 0)
- {
- Serial.println("Failed to configure Ethernet using DHCP");
- return;
- }
- // init the MQTT connection
- client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
- client.setCallback(callback);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement