tamox.net |
||
Arduino nano / nRF24L01 published on 2021/Oct/26 宅配ボックスへの応用 |
||
電波到達距離の関係で、実用にはたえなかった。 送信器側だけでも、ポストにリードスイッチを付け利用はできている。 |
||
Photo1 蓋が閉まっているとき(リードスイッチが接しているとき),送信器、受信器ともに緑LEDがON Photo2 蓋が開いているとき(リードスイッチが離れるたとき),送信器、受信器ともに赤LEDがON Photo3 蓋が閉じられたとき(リードスイッチが接している),送信器、受信器ともに緑LEDがON 赤色LEDは、蓋が開けられた形跡により、送信器、受信器ともに赤色LEDはONのまま 送信側のリセットスイッチを押すことにより、赤LEDはOFF Photo4 |
||
|
||
回路図 | ||
![]() ![]() |
||
送信器側:リードスイッチ(D2) リセットボタンスイッチ(D7) 受信器側:リードスイッチなし リセットボタンスイッチなし |
||
|
||
IDE code | ||
//skech_oct23a 送信器側コード #include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(9, 10); // CE, CSN const byte address[6] = "00003"; #define btn_on 2 #define btn_rst 7 #define led_g 3 #define led_r 17 boolean button_state = 0; boolean button_rst_state = 0; boolean alter = 0; char text[] = ""; char text_r[] = ""; void setup() { pinMode(btn_on, INPUT); pinMode(btn_rst, INPUT); pinMode(led_g, OUTPUT); pinMode(led_r, OUTPUT); Serial.begin(9600); radio.begin(); //Starting the Wireless communication radio.openWritingPipe(address); //Setting the address where we will send the data radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. radio.stopListening(); //This sets the module as transmitter alter=0; } void loop() { button_state = digitalRead(btn_on); button_rst_state = digitalRead(btn_rst); if (button_rst_state==HIGH){ alter=0; } if (alter == 1){ digitalWrite(led_r, HIGH); if(button_state == HIGH){ digitalWrite(led_g,HIGH); const char text[] = "on"; radio.write(&text, sizeof(text)); //Sending the message to receiver digitalWrite(led_g,HIGH); } else { digitalWrite(led_g, LOW); const char text[] = "off"; radio.write(&text, sizeof(text)); const char text_r[] = "on"; radio.write(&text_r, sizeof(text_r)); //Sending the message to receiver } } if(alter ==0){ if(button_state == HIGH){ const char text[] = "on"; radio.write(&text, sizeof(text)); //Sending the message to receiver digitalWrite(led_g,HIGH); digitalWrite(led_r,LOW); const char text_r[] = "off"; radio.write(&text_r, sizeof(text_r)); //Sending the message to receiver } else { digitalWrite(led_g, LOW); const char text[] = "off"; radio.write(&text, sizeof(text)); //Sending the message to receiver alter = 1; } } delay(1000); } //----------------------------------------------------------------------- //skech_oct23b 受信器側コード #include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(9, 10); // CE, CSN const byte address[6] = "00003"; #define led_g 3 #define led_r 17 boolean button_state = 0; char text[32] = ""; //Saving the incoming data char text_r[32] = ""; void setup() { pinMode(led_g, OUTPUT); pinMode(led_r, OUTPUT); Serial.begin(9600); radio.begin(); radio.openReadingPipe(0, address); //Setting the address at which we will receive the data radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver. radio.startListening(); //This sets the module as receiver } void loop() { while (radio.available()){ radio.read(&text, sizeof(text)); //Reading the data delay(5); if(strcmp(text,"on") == 0){ digitalWrite(led_g, HIGH); } if(strcmp(text,"off") == 0){ digitalWrite(led_g, LOW); } radio.read(&text_r, sizeof(text_r)); //Reading the data delay(5); if(strcmp(text_r,"on") == 0){ digitalWrite(led_r, HIGH); } if(strcmp(text_r,"off") == 0){ digitalWrite(led_r, LOW); } } } |