I was doing a project using RF 433Hz to transmit and receive the current time, but i am unable to receive the minute and second at the receiver side. I am using two Arduinos, each of them has their own transmitter and receiver.
Here is my code for the first Arduino to transmit hour, minute and second
#include <DS3231.h>//Memanggil RTC3231 Library
#include <Wire.h> // i2C Conection Library
#include <LiquidCrystal.h> //Libraries
#include <EEPROM.h>
#include <VirtualWire.h>
LiquidCrystal lcd(12, 11, 6, 5, 4, 3); //Arduino pins to lcd
#define bt_time A0
#define bt_up A1
#define bt_down A2
#define bt_alarm A3
#define buzzer 8
//rf variables
char *controller;
char *temp;
DS3231 rtc(SDA, SCL);
// Init a Time-data structure
Time t; //pencacah string time()
int hh = 0, mm = 0, ss = 0, dd = 0, bb = 0, set_day;
int yy = 0;
String Day = " ";
int AlarmHH = 21, AlarmMM = 22, AlarmSS = 23, setMode = 0, setAlarm = 0, alarmMode=0;
int stop =0, mode=0, flag=0;
//Eeprom Store Variable
uint8_t HH;
uint8_t MM;
byte bell_symbol[8] = {
B00100,
B01110,
B01110,
B01110,
B01110,
B11111,
B01000,
B00100};
byte thermometer_symbol[8] = {
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110};
void setup(){
// Setup Serial connection
Serial.begin(9600);
rtc.begin(); // memulai koneksi i2c dengan RTC
pinMode(bt_time, INPUT_PULLUP);
pinMode(bt_up, INPUT_PULLUP);
pinMode(bt_down, INPUT_PULLUP);
pinMode(bt_alarm, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
lcd.createChar(1, thermometer_symbol);
lcd.createChar(2, bell_symbol);
lcd.begin(16, 2); // Configura lcd numero columnas y filas
lcd.setCursor(0,0); //Show "TIME" on the LCD
lcd.setCursor (0,0);
lcd.print(" RF-Based Clock ");
lcd.setCursor (0,1);
lcd.print(" With Alarm ");
delay (2000);
lcd.clear();
stop=EEPROM.read(50);
if(stop==0){
}else{WriteEeprom ();}
EEPROM.write(50,0);
ReadEeprom();
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(7);
vw_set_tx_pin(9);
vw_setup(4000); // Bits per sec
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
vw_rx_start(); // Start the receiver PLL running
}
void loop(){
t = rtc.getTime();
Day = rtc.getDOWStr(1);
if (setMode == 0){
hh = t.hour,DEC;
mm = t.min,DEC;
ss = t.sec,DEC;
dd = t.date,DEC;
bb = t.mon,DEC;
yy = t.year,DEC;
}
if(setAlarm==0){
lcd.setCursor(0,0);
lcd.print((hh/10)%10);
lcd.print(hh % 10);
lcd.print(":");
lcd.print((mm/10)%10);
lcd.print(mm % 10);
lcd.print(":");
lcd.print((ss/10)%10);
lcd.print(ss % 10);
lcd.print(" ");
if(mode==1){lcd.write(2);}
else{lcd.print(" ");}
lcd.print(" ");
lcd.write(1);
lcd.print(rtc.getTemp(),0);
lcd.write(223);
lcd.print("C");
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(Day);
lcd.print(" ");
lcd.print((dd/10)%10);
lcd.print(dd % 10);
lcd.print("/");
lcd.print((bb/10)%10);
lcd.print(bb % 10);
lcd.print("/");
lcd.print((yy/1000)%10);
lcd.print((yy/100)%10);
lcd.print((yy/10)%10);
lcd.print(yy % 10);
}
setupClock();
setTimer();
delay (100);
blinking();
//Alarm
if (alarmMode==1 && mode==1 && hh==AlarmHH && mm==AlarmMM && ss>=AlarmSS) {
digitalWrite(buzzer, HIGH);
delay (300);
digitalWrite(buzzer, LOW);
}else{digitalWrite(buzzer, LOW);}
delay (100);
Received ();
}
void blinking (){
//BLINKING SCREEN
if (setAlarm <2 && setMode == 1){lcd.setCursor(0,0); lcd.print(" ");}
if (setAlarm <2 && setMode == 2){lcd.setCursor(3,0); lcd.print(" ");}
if (setAlarm <2 && setMode == 3){lcd.setCursor(6,0); lcd.print(" ");}
if (setAlarm <2 && setMode == 4){lcd.setCursor(1,1); lcd.print(" ");}
if (setAlarm <2 && setMode == 5){lcd.setCursor(5,1); lcd.print(" ");}
if (setAlarm <2 && setMode == 6){lcd.setCursor(8,1); lcd.print(" ");}
if (setAlarm <2 && setMode == 7){lcd.setCursor(11,1); lcd.print(" "); }
//Alarm
if (setMode == 0 && setAlarm == 1){lcd.setCursor(6,0); lcd.print(" "); }
if (setMode == 0 && setAlarm == 2){lcd.setCursor(4,1); lcd.print(" "); }
if (setMode == 0 && setAlarm == 3){lcd.setCursor(7,1); lcd.print(" "); }
if (setMode == 0 && setAlarm == 4){lcd.setCursor(10,1);lcd.print(" "); }
}
//Seting Jam ,Tanggal,Alarm/Timer
void setupClock (void) {
if (setMode == 8){
lcd.setCursor (0,0);
lcd.print (F("Set Date Finish "));
lcd.setCursor (0,1);
lcd.print (F("Set Time Finish "));
delay (1000);
rtc.setTime (hh, mm, ss);
rtc.setDate (dd, bb, yy);
lcd.clear();
setMode = 0;
}
if (setAlarm == 5){
lcd.setCursor (0,0);
lcd.print (F("Set Alarm Finish"));
lcd.setCursor (0,1);
lcd.print (F("-EEPROM Updated-"));
WriteEeprom();
delay (2000);
lcd.clear();
setAlarm=0;
alarmMode=1;
}
if (setAlarm >0){ alarmMode=0;}
if(digitalRead (bt_time) == 0 && flag==0) {flag=1;
if(setAlarm>0){setAlarm=5;}
else{setMode = setMode+1;}
}
if(digitalRead (bt_alarm) == 0 && flag==0){flag=1;
if(setMode>0){setMode=8;}
else{setAlarm = setAlarm+1;}
lcd.clear();}
if(digitalRead (bt_time) == 1 && digitalRead (bt_alarm) == 1){flag=0;}
if(digitalRead (bt_up) == 0){
if (setAlarm<2 && setMode==1)hh=hh+1;
if (setAlarm<2 && setMode==2)mm=mm+1;
if (setAlarm<2 && setMode==3)ss=ss+1;
if (setAlarm<2 && setMode==4)set_day=set_day+1;
if (setAlarm<2 && setMode==5)dd=dd+1;
if (setAlarm<2 && setMode==6)bb=bb+1;
if (setAlarm<2 && setMode==7)yy=yy+1;
//Alarm
if (setMode==0 && setAlarm==1)mode=1;
if (setMode==0 && setAlarm==2 && AlarmHH<23)AlarmHH=AlarmHH+1;
if (setMode==0 && setAlarm==3 && AlarmMM<59)AlarmMM=AlarmMM+1;
if (setMode==0 && setAlarm==4 && AlarmSS<59)AlarmSS=AlarmSS+1;
if(hh>23)hh=0;
if(mm>59)mm=0;
if(ss>59)ss=0;
if(set_day>7)set_day=0;
if(dd>31)dd=0;
if(bb>12)bb=0;
if(yy>2030)yy=2000;
rtc.setDOW(set_day);
}
if(digitalRead (bt_down) == 0){
if (setAlarm<2 && setMode==1)hh=hh-1;
if (setAlarm<2 && setMode==2)mm=mm-1;
if (setAlarm<2 && setMode==3)ss=ss-1;
if (setAlarm<2 && setMode==4)set_day=set_day-1;
if (setAlarm<2 && setMode==5)dd=dd-1;
if (setAlarm<2 && setMode==6)bb=bb-1;
if (setAlarm<2 && setMode==7)yy=yy-1;
//Alarm
if (setMode==0 && setAlarm==1 )mode=0;
if (setMode==0 && setAlarm==2 && AlarmHH>0)AlarmHH=AlarmHH-1;
if (setMode==0 && setAlarm==3 && AlarmMM>0)AlarmMM=AlarmMM-1;
if (setMode==0 && setAlarm==4 && AlarmSS>0)AlarmSS=AlarmSS-1;
if(hh<0)hh=23;
if(mm<0)mm=59;
if(ss<0)ss=59;
if(set_day<0)set_day=7;
if(dd<0)dd=31;
if(bb<0)bb=12;
if(yy<0)yy=2030;
rtc.setDOW(set_day);
}
}
void setTimer (){
//Alarm
if (setMode == 0 && setAlarm >0){
lcd.setCursor (0,0);
lcd.print("Alarm ");
if(mode==0){lcd.print("Deactivate");}
else{lcd.print("Activated ");}
lcd.setCursor (4,1);
lcd.print((AlarmHH/10)%10);
lcd.print(AlarmHH % 10);
lcd.print(":");
lcd.print((AlarmMM/10)%10);
lcd.print(AlarmMM % 10);
lcd.print(":");
lcd.print((AlarmSS/10)%10);
lcd.print(AlarmSS % 10);
}
}
void ReadEeprom () {
AlarmHH=EEPROM.read(1);
AlarmMM=EEPROM.read(2);
AlarmSS=EEPROM.read(3);
mode=EEPROM.read(4);
}
void WriteEeprom () {
EEPROM.write(1,AlarmHH);
EEPROM.write(2,AlarmMM);
EEPROM.write(3,AlarmSS);
EEPROM.write(4,mode);
}
void Received ()
{
//String timeString = String(t.hour, DEC) + String(t.min, DEC) + String(t.sec, DEC);
//Serial.println(timeString);
//// Convert the current time to a string
//String timeStr = String(t.hour, DEC) + ":" + String(t.min, DEC) + ":" + String(t.sec, DEC);
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
Serial.println(controller);
if (vw_get_message(buf, &buflen)) // Non-blocking
{
if(buf[0]=='1')
{
digitalWrite(13,HIGH);
controller = "8";
// // Send the current time to the receiver
// vw_send((uint8_t *)timeStr.c_str(), timeStr.length());
// vw_wait_tx();
// Print the current time to the Serial Monitor
// Serial.println(timeStr);
// // Send the current time to the receiver
char timeStr[20];
sprintf(timeStr, "%02d:%02d:%02d", t.hour, t.min, t.sec);
vw_send((uint8_t *)timeStr, strlen(timeStr));
vw_wait_tx();
// //vw_send((uint8_t *)controller, strlen(controller));
// //vw_wait_tx();
// delay(100);
// vw_send((uint8_t *)timeString.c_str(), strlen(timeString.c_str()));
// vw_wait_tx();
delay(1000);
// Print the current time to the Serial Monitor
Serial.println(timeStr);
digitalWrite(2,HIGH);
}
else
{
digitalWrite(13,LOW);
controller="2";
// vw_send((uint8_t *)controller, strlen(controller));
// vw_wait_tx();
// delay(100);
// vw_send((uint8_t *)timeStr.c_str(), timeStr.length());
// vw_wait_tx();
delay(1000);
digitalWrite(2,HIGH);
}
}
}
Her is the other code used to receive the transmitted hour, minute and second
#include <VirtualWire.h>
#include <Time.h>
char *controller;
char *msg;
const int buttonPin1 = 2;
int buttonState1 = 0;
// Define the pins used by the receiver
const int receiverPin = 12;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
// Set the receiver pin as an input
pinMode(receiverPin, INPUT);
// Initialize the Time library
// setTime(10, 30, 0, 1, 1, 2023);
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_set_rx_pin(9);
vw_setup(4000);// speed of data transfer Kbps
delay(100);
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
Serial.println(" controller ");
Serial.println(controller);
if (buttonState1 == HIGH)
{
controller = "1" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
delay(100);
digitalWrite(13, LOW);
}
else
{
controller = "0" ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, HIGH);
}
Serial.println(" ");
// The loop function runs repeatedly, forever
// Define the buffer
char buf[VW_MAX_PAYLOAD];
// Check if there is a message from the transmitter
if (vw_get_message(buf, VW_MAX_PAYLOAD)) {
// Print the message from the transmitter
for (int i = 0; i < VW_MAX_PAYLOAD; i++) {
Serial.print(" buf[i] ");
Serial.print(buf[i]);
int hour = buf[0] * 10 + buf[1];
int minute = buf[2] * 10 + buf[3];
int second = buf[4] * 10 + buf[5];
// Print the hour, minute, and second separately
Serial.print("Hour: ");
Serial.println(hour);
Serial.print("Minute: ");
Serial.println(minute);
Serial.print("Second: ");
Serial.println(second);
Serial.print(" buf[0] ");
Serial.print(buf[0]);
Serial.println();
Serial.print("buf[1]");
Serial.print(buf[1]);
Serial.println();
Serial.print("buf[2]");
Serial.print(buf[2]);
Serial.println();
Serial.print("buf[3]");
Serial.print(buf[3]);
}
Serial.println();
}
}