Test environment
Mac OS
Arduino UNO
Arudino 1.8.9
Function
The project is one of the IoT End-to-end system, in order to control an infrared remote send signal form Rpi
UNO reads instructions from the Raspberry Pi serial port, and then send and receive to control the light turn on or turn off
Rpi will send the “XY” code, X is device, Y is command
PS
- To prevent the infrared from being properly oriented, three signals were sent
- Infrared coding has no scientific basis, just to test the demo
- There is no define of Case 2 to Case 5
- IRremote.h can be installed in “Arudino - tools - manage library”
/*
IRsend Test of IoT demo
Date:2019/12/28
Receive command from Rpi and test the IRremote demo.
PIN3 is defined in head file.
IRsend PIN can only be defined in head file.
Decode message,example:recData="XY",device = X, command = Y.
*/
#include <IRremote.h>
IRsend irsend; //Create IRsent objects
int device;
int command;
String recData = "";
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
recData += char(Serial.read());
delay(2);
}
//Serial.println(recData);
if (recData.length() > 0)
{
int temp = recData.toInt();
command = temp % 10;
device = (temp - command) / 10;
//Switch command case
if(device == 1 )
{
switch (command) {
case 0:
//Send command three times to make sure it can be received
irsend.sendNEC(0xFF6897, 32);
delay(100);
break;
irsend.sendNEC(0xFF6897, 32);
delay(100);
break;
irsend.sendNEC(0xFF6897, 32);
delay(100);
break;
case 1:
irsend.sendNEC(0xFF02FD, 32);
delay(100);
break;
irsend.sendNEC(0xFF02FD, 32);
delay(100);
break;
irsend.sendNEC(0xFF02FD, 32);
delay(100);
break;
case 2:
digitalWrite(3, HIGH);
break;
case 3:
digitalWrite(4, HIGH);
break;
case 4:
digitalWrite(5, HIGH);
break;
case 5:
digitalWrite(6, HIGH);
break;
}
}
//Reset received data
recData = "";
}
}
/*
IRreceive Test of IoT demo
Date:2019/12/28
Receive signal from IRsend to turn on or turn off the light.
*/
#include <IRremote.h>
IRrecv irrecv(6); //Create IRreceive objects and choose the IRreceive pin
decode_results results; //Create the object of decode result
int LED_PIN=4;
void setup()
{
irrecv.enableIRIn(); //Star reveiving
Serial.begin(9600);
pinMode(LED_PIN,OUTPUT);
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX); //Output the received signal value in hexadecimal
Serial.println();
//Decode
if(results.value == 0xFF6897)
{
digitalWrite(LED_PIN,LOW);//LED turn off
}
if(results.value == 0xFF02FD)
{
digitalWrite(LED_PIN,HIGH);//LED turn on
}
irrecv.resume(); //Receive next signal
}
}
Reference:https://blog.csdn.net/qq_39591322/article/details/88838454