sankmv
Published © LGPL

Remote control of actuators via radio channel

The device consists of a push-button remote control and a block of 8 relays for switching on and off the actuators. Communication via radio

BeginnerFull instructions providedOver 1 day82
Remote control of actuators via radio channel

Things used in this project

Story

Read more

Schematics

Remote control

Executive unit

Code

sketch remote control buttons

C/C++
#define YES 0
#define NO 1

#define LED 13

#include <SPI.h>
#include <RF22.h>
#include "TM1637Display.h"
#include <EEPROM.h>

TM1637Display display(8, 9); // CLC, DIO

// for display
const uint8_t n00[] = {
  //SEG_D  // _
};
const uint8_t n00_2[] = {
  SEG_DP  //SEG_D  // _:
};
const uint8_t n11[] = {
  SEG_B | SEG_C |  SEG_E | SEG_F  // |_|
};
const uint8_t n11_2[] = {
  SEG_B | SEG_C |  SEG_E | SEG_F | SEG_DP  // |_|:
};
const uint8_t n10[] = {
  SEG_E | SEG_F  // |_
};
const uint8_t n10_2[] = {
  SEG_E | SEG_F | SEG_DP  // |_
};
const uint8_t n01[] = {
  SEG_B | SEG_C  // _|
};
const uint8_t n01_2[] = {
  SEG_B | SEG_C | SEG_DP  // _|:
};
const uint8_t err[] = {
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,            // E
  SEG_D ,           // _
  SEG_E | SEG_G,           // r
  SEG_A | SEG_E | SEG_F | SEG_G            // F
};
const uint8_t err1[] = {
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,            // E
  SEG_D | SEG_DP ,           // _
  SEG_E | SEG_G,           // r
  SEG_A | SEG_E | SEG_F | SEG_G            // F
};

// buttons pins
//int pinButtons[]={A5,A1,A4,A0,6,A3,7,A2};
int pinButtons[]={A5,A6,A4,A0,6,A3,7,A2};  // bad - A6
int lastButtons[]={NO,NO,NO,NO,NO,NO,NO,NO};
int currentButtons[]={NO,NO,NO,NO,NO,NO,NO,NO};
boolean change=false;
//uint8_t status_click[]={1,1,1,1,1,1,1,1,1,1};
uint8_t status_click[]={48,48,48,48,48,48,48,48};

// Singleton instance of the radio
RF22 rf22;

void(* resetFunc) (void) = 0;

int counterr=0;
unsigned long millist=0;
int cntm=0;

void setup() {
  //  
  Serial.begin(9600);
  Serial.println("start");
  // ini
  if(EEPROM.read(11) != 55)
    iniDataEEPROM();
  readDataEEPROM();  
  //  
  for(int i=0;i<8;i++) {
    pinMode(pinButtons[i],INPUT_PULLUP);
  }
  //  13:  - ok,  - False
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  // display
  display.setBrightness(4); //   0  7, true/false
  display.clear();
  // connect
  if (!rf22.init()){
    // Defaults after init are 434.0MHz, 0.05MHz AFC pull-in, modulation FSK_Rb2_4Fd36
    Serial.println("RF22 init failed");
    counterr++;
    if(counterr%2==0)
      display.setSegments(err);
    else
      display.setSegments(err1);
    delay(500);
    if(counterr>5)
      resetFunc();
   }
  delay(200); 
  display.clear();
  digitalWrite(LED,HIGH);
  sendrf();
}

void loop()
{
  //  
  for(int i=0;i<8;i++) {
   
   currentButtons[i] = debounce(lastButtons[i],pinButtons[i]);
   //  ...
   if (lastButtons[i] == NO && currentButtons[i] == YES) {
     ; 
   }
   // 
   else if (lastButtons[i] == YES && currentButtons[i] == NO) {
      Serial.print("onclick=");Serial.println(i);
      if(status_click[i]==48)
         status_click[i]=49;
      else
         status_click[i]=48;
      change=true;
   }   
   lastButtons[i]  = currentButtons[i]; 
  
  }
  // send if change=true
  if(change==true) {
     sendrf();
  }
  if((millis()-millist) > 20*1000) {
    cntm=(cntm+1)%10;
    if(cntm==0){
        sendrf();Serial.println(millist);
    }
    millist=millis();
    
  }
}

sketch remote control relays

C/C++
#define ON 1
#define OFF 0

#define LED 13

#include <SPI.h>
#include <RF22.h>

// relays pins
//int pinRelays[]={A3,7,A2,6,5,6,A0,4};
int pinRelays[]={A3,7,A2,6,A1,5,A0,4};
uint8_t status_relays[]={0,0,0,0,0,0,0,0};

// Singleton instance of the radio
RF22 rf22;

void(* resetFunc) (void) = 0;

void setup() {
  Serial.begin(9600);
  // pins relay ini
  for(int i=0;i<8;i++) {
    pinMode(pinRelays[i],OUTPUT);
    digitalWrite(pinRelays[i],OFF);
  }
  //  13:  - ok,  - False
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  //
  if (!rf22.init()){
    Serial.println("RF22 init failed");
    digitalWrite(LED,HIGH);
    // 
    delay(3000);
    resetFunc();
  }
}

void loop()
{
  while (1)
  {
    rf22.waitAvailable();
    //Serial.println(millis());
    // Should be a message for us now   
    //uint8_t buf[RF22_MAX_MESSAGE_LEN];
    uint8_t buf[9];
    uint8_t len = sizeof(buf);
    if (rf22.recv(buf, &len))
    {
      Serial.println("got request: ");
      Serial.println((char*)buf);

      // relay 
      for(int i=7;i>=0;i--){
         status_relays[i]=buf[7-i]-48;
         Serial.print(status_relays[i]);
      }
      // / 
      for(int i=0;i<8;i++){
         if(status_relays[i]==1)
          digitalWrite(pinRelays[i],ON);
         else
          digitalWrite(pinRelays[i],OFF);
          Serial.print("pinRelay ");Serial.print(pinRelays[i]);Serial.print("=");Serial.println(status_relays[i]);
      }
      //delay(500);      
      // Send a reply
      uint8_t data[9]={0,0,0,0,0,0,0,0,0};
      for(int i=0;i<8;i++) {
         data[i]=status_relays[i]+48;      
      }
      rf22.send(data, sizeof(data));
      rf22.waitPacketSent();
      Serial.println("Sent a reply");
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

Credits

sankmv
5 projects • 8 followers
Contact

Comments