Naresh krish
Published © GPL3+

DIY Remote WiFi Switch Using the RAK DashButton

This project shows how to use the RAK DashButton to create a safe, Wifi based switch to control your electrical appliances.

AdvancedFull instructions provided6 hours1,993
DIY Remote WiFi Switch Using the RAK DashButton

Things used in this project

Hardware components

RAKwireless RAK creator pro
×1
RAKwireless RAK DASH Button
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
XUP USB-JTAG Programming Cable
Xilinx XUP USB-JTAG Programming Cable
×1
Sonoff Basic
Itead Sonoff Basic
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

RAK 473 schematics

Code

Arduino code

Arduino
Code that helps send mqtt data to the sonoff tasmota switches
//#include <HttpClient.h>
#include <WiFi.h>
//#include <WiFiClient.h>
#include <PubSubClient.h>

#define LED1  0
#define LED2  1
#define LED3  2
#define LED4  3
#define RED   0
#define GREEN 1
#define BLUE  2
#define OFF   3

char mqttServer[]     = "broker.hivemq.com";
char clientId[]       = "amebaClient";
char publishTopic[]   = "outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "inTopic";

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientId)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish(publishTopic, publishPayload);
      // ... and resubscribe
      client.subscribe(subscribeTopic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void printWifiStatus();
void led_off();
void led_ctrl(uint8_t led_num, uint8_t rgb);

char ssid[] = "narioinc89"; //  your network SSID (name) 
char pass[] = "Cerebrum123";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
/* power enable */
int pwr_en = 15;
/* leds */
int led1_r = 25;
int led1_g = 24;
int led1_b = 19;
int led2_r = 0;
int led2_g = 2;
int led2_b = 6;
int led4_r = 12;
int led4_g = 11;
int led4_b = 13;
int led3_r = 22;
int led3_g = 21;
int led3_b = 1;
/* keys */
int key1 = 23;
int key2 = 14;
int key3 = 10;
int key4 = 20;

void setup()
{
  Serial.begin(9600); 

  //connect to the mqtt broken and set the connection callback
  client.setServer(mqttServer, 1883);
  client.setCallback(callback);

  pinMode(pwr_en, OUTPUT);
  digitalWrite(pwr_en, 1);

  pinMode(led2_r, OUTPUT);
  pinMode(led2_g, OUTPUT);
  pinMode(led2_b, OUTPUT);

  pinMode(led1_b, OUTPUT);
  pinMode(led3_r, OUTPUT);
  pinMode(led3_g, OUTPUT);
  pinMode(led3_b, OUTPUT);
  pinMode(led4_b, OUTPUT);

  pinMode(key2, INPUT_PULLUP);
  pinMode(key3, INPUT_PULLUP);
  pinMode(key4, INPUT_PULLUP);

#if 1
/*  
 *   Pin D21-D25 can not be used as digital IO ,when in debug mode(Enable JTAG). 
 *   D21-D25 can be used as digital IO when in factory mode.(Disable JTAG)  
*/
  //D21-D25
  pinMode(led4_g, OUTPUT);
  pinMode(led4_r, OUTPUT);
  pinMode(led1_g, OUTPUT);
  pinMode(led1_r, OUTPUT);
  pinMode(key1, INPUT_PULLUP);
#endif

  led_off();
 
  while ( status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();
}

void loop()
{

 if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
  led_off();
  if (digitalRead(key1) == 0) {
    delay(50);
    if (digitalRead(key1) == 0) {
    led_ctrl(LED1,BLUE); 
    toggleSwitch();
    delay(500); 
    Serial.print("key1");
    //http_get("1");
    }
  }
  if (digitalRead(key2) == 0) {
    delay(50);
    if (digitalRead(key2) == 0) {
    led_ctrl(LED2,BLUE);  
    delay(500);
    Serial.print("key2");
    //http_get("2");
    }
  }
  if (digitalRead(key3) == 0) {
    delay(50);
    if (digitalRead(key3) == 0) {
    led_ctrl(LED3,BLUE);  
    delay(500);
    Serial.print("key3");
    //http_get("3");
    }
  }
  if (digitalRead(key4) == 0) {
    delay(50);
    if (digitalRead(key4) == 0) {
    led_ctrl(LED4,BLUE);  
    delay(500);
    Serial.print("key4");
    //http_get("4");
    }
  }  
delay(100);
}

// function toggle the switch 1 of a sonoff switch
void toggleSwitch(){
  if(client.connected()){
    client.publish("tasmota/sonoff-mylight/cmnd/Power1", "toggle");
  }else{
    Serial.println("client is not connected...")
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  led_ctrl(LED1,RED);
  led_ctrl(LED2,RED);
  led_ctrl(LED3,RED);
  led_ctrl(LED4,RED);
  delay(500);
}

void led_off() {
   led_ctrl(LED1,OFF);
   led_ctrl(LED2,OFF);
   led_ctrl(LED3,OFF);
   led_ctrl(LED4,OFF);
}

void led_ctrl(uint8_t led_num, uint8_t rgb)
{
  switch (led_num) {
    case LED1:
      if (rgb == RED) {
        digitalWrite(led1_r, 0);
        digitalWrite(led1_g, 1);
        digitalWrite(led1_b, 1);
      }
      else if (rgb == GREEN) {
        digitalWrite(led1_r, 1);
        digitalWrite(led1_g, 0);
        digitalWrite(led1_b, 1);
      }
      else if (rgb == BLUE) {
        digitalWrite(led1_r, 1);
        digitalWrite(led1_g, 1);
        digitalWrite(led1_b, 0);
      }
      else if (rgb == OFF) {
        digitalWrite(led1_r, 1);
        digitalWrite(led1_g, 1);
        digitalWrite(led1_b, 1);
      }
      break;

    case LED2:
      if (rgb == RED) {
        digitalWrite(led2_r, 0);
        digitalWrite(led2_g, 1);
        digitalWrite(led2_b, 1);
      }
      else if (rgb == GREEN) {
        digitalWrite(led2_r, 1);
        digitalWrite(led2_g, 0);
        digitalWrite(led2_b, 1);
      }
      else if (rgb == BLUE) {
        digitalWrite(led2_r, 1);
        digitalWrite(led2_g, 1);
        digitalWrite(led2_b, 0);
      }
      else if (rgb == OFF) {
        digitalWrite(led2_r, 1);
        digitalWrite(led2_g, 1);
        digitalWrite(led2_b, 1);
      }
      break;

    case LED3:
      if (rgb == RED) {
        digitalWrite(led3_r, 0);
        digitalWrite(led3_g, 1);
        digitalWrite(led3_b, 1);
      }
      else if (rgb == GREEN) {
        digitalWrite(led3_r, 1);
        digitalWrite(led3_g, 0);
        digitalWrite(led3_b, 1);
      }
      else if (rgb == BLUE) {
        digitalWrite(led3_r, 1);
        digitalWrite(led3_g, 1);
        digitalWrite(led3_b, 0);
      }
      else if (rgb == OFF) {
        digitalWrite(led3_r, 1);
        digitalWrite(led3_g, 1);
        digitalWrite(led3_b, 1);
      }
      break;

    case LED4:
      if (rgb == RED) {
        digitalWrite(led4_r, 0);
        digitalWrite(led4_g, 1);
        digitalWrite(led4_b, 1);
      }
      else if (rgb == GREEN) {
        digitalWrite(led4_r, 1);
        digitalWrite(led4_g, 0);
        digitalWrite(led4_b, 1);
      }
      else if (rgb == BLUE) {
        digitalWrite(led4_r, 1);
        digitalWrite(led4_g, 1);
        digitalWrite(led4_b, 0);
      }
      else if (rgb == OFF) {
        digitalWrite(led4_r, 1);
        digitalWrite(led4_g, 1);
        digitalWrite(led4_b, 1);
      }
      break;
    default:
      break;
  }
}

sonoff tasmota firmware

RAK AMEBA SDK repo

Credits

Naresh krish

Naresh krish

22 projects • 90 followers
Maker, Hacker, Guitarist, Full time Android and iOS Dev.
Thanks to RAk Wireless.

Comments