Naresh krish
Published

Node-RED Integration with RAK wireless DASH Button

This project shows you how to implement a simple Node-red integration when user presses the button on a RAK Wireless DASH-Button hardware

AdvancedFull instructions provided6 hours1,936
Node-RED Integration with RAK wireless DASH Button

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
RAKWireless dash button
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Node-RED
Node-RED
Openhab

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

RAK473 schematics

Code

Dash button firmware

Arduino
#include <HttpClient.h>
#include <WiFi.h>
#include <WiFiClient.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

void printWifiStatus();
void led_off();
void http_get(char* str);
void led_ctrl(uint8_t led_num, uint8_t rgb);

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

// Name of the server we want to connect to
const char kHostname[] = "sonoff_switch_ip_here:port";

const char kPath[] = "/togglelights";
const int kHttpPort = 80;
// Number of milliseconds to wait without receiving any data before we give up
const int kNetworkTimeout = 30*1000;
// Number of milliseconds to wait if no data is available before trying again
const int kNetworkDelay = 1000;
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); 

  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()
{
 led_off();
 checkButtonStatus(key1);
 delay(100);
}

void checkButtonStatus(int key){
  if (digitalRead(key) == 0) {
    delay(50);
    if (digitalRead(key) == 0) {
    led_ctrl(LED4,BLUE);  
    delay(500);
    Serial.print(key + " was pressed");
    http_get();
    }
  }  
}

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 http_get() {
  int err = 0;
  WiFiClient c;
  HttpClient http(c);
  char path[100];
  err = http.get(kHostname, kHttpPort, kPath);
  Serial.println(path);
  if (err == 0)
  {
    Serial.println("startedRequest ok");
    err = http.responseStatusCode();
    if (err >=0)
    {
      Serial.print("Got status code: ");
      Serial.println(err);
    }
    else
    {    
      Serial.print("Getting response failed: ");
      Serial.println(err);
    }
  }
  else
  {
    Serial.print("Connect failed: ");
    Serial.println(err);
  }
  http.stop();
}

void http_post(char* data) {
  int err = 0;
  WiFiClient client;
  while(1){
  if (!client.connect("xxxxxxx", 8080)) {                             //port can be 8080 or as configured in the openhab config
      Serial.println("Connect to server failed. Retry after 1s.");
      client.stop();
      delay(1000);
      continue;
   }else{
    break;
   }
  }
    Serial.println("connected to server");
    // Make a HTTP request:
    client.print("POST /rest/items/My_Item/");
    client.print("OFF");
    client.print("Host: ");
    client.println("xxxxxxx"); //you open hab url in the form abc.xyz.com
    client.println();

    while (!client.available()) delay(100);
    while (client.available()) {
      char c = client.read();
      Serial.write(c);
    }
    client.stop();
    
}

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;
  }
}

Node-red flow example

JSON
Json file for the flow provided in this tutorial example
[{"id":"55809f53.af20a","type":"openhab2-out","z":"7a5d96b3.1b2978","name":"ToggleOHLights","controller":"94b7105b.cc478","itemname":"Switch1","topic":"ItemCommand","payload":"TOGGLE","x":654,"y":874,"wires":[]},{"id":"fbf368e2.404b38","type":"http in","z":"7a5d96b3.1b2978","name":"toggle lights","url":"/togglelights","method":"get","upload":false,"swaggerDoc":"","x":416,"y":883,"wires":[["3493d584.f5ae0a","55809f53.af20a","ad2cd216.6c8ff","34da0c09.bcac54"]]},{"id":"3493d584.f5ae0a","type":"debug","z":"7a5d96b3.1b2978","name":"","active":true,"console":"false","complete":"payload","x":624,"y":789,"wires":[]},{"id":"ad2cd216.6c8ff","type":"http response","z":"7a5d96b3.1b2978","name":"","statusCode":"","headers":{},"x":639,"y":934,"wires":[]},{"id":"34da0c09.bcac54","type":"twitter out","z":"7a5d96b3.1b2978","name":"Tweet","x":692,"y":1001,"wires":[]},{"id":"94b7105b.cc478","type":"openhab2-controller","z":"","name":"MyOpenHab","protocol":"http","host":"ttn-gateway.local","port":"8080","path":"","username":"","password":""}]

Credits

Naresh krish

Naresh krish

22 projects • 70 followers
Maker, Hacker, Guitarist, Full time Android and iOS Dev.

Comments