Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include <SPI.h>
- #include <RF24.h>
- #include "Timer.h"
- // This is just the way the RF24 library works:
- // Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
- RF24 radio(7, 8);
- byte addresses[][6] = { "1Node","2Node" };
- char poslji[] = "BITCH9";
- char data;
- char dataToSend[32];
- int temp = 18;
- Timer t;
- // -----------------------------------------------------------------------------
- // SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
- // -----------------------------------------------------------------------------
- void setup() {
- Serial.begin(9600);
- Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
- // Initiate the radio object
- radio.begin();
- // Set the transmit power to lowest available to prevent power supply related issues
- radio.setPALevel(RF24_PA_MIN);
- // Set the speed of the transmission to the quickest available
- radio.setDataRate(RF24_2MBPS);
- // Use a channel unlikely to be used by Wifi, Microwave ovens etc
- radio.setChannel(124);
- // Open a writing and reading pipe on each radio, with opposite addresses
- // radio.openWritingPipe(addresses[0]);
- // radio.openReadingPipe(1, addresses[1]);
- // Start the radio listening for data
- radio.startListening();
- t.every(5000, sendTemperature);
- }
- // -----------------------------------------------------------------------------
- // We are LISTENING on this device only (although we do transmit a response)
- // -----------------------------------------------------------------------------
- void loop() {
- t.update();
- if (radio.available()) {
- char preberi[32];
- radio.read(&preberi, sizeof(preberi));
- // Serial.println(preberi);
- String dataReq(preberi);
- String type = dataReq.substring(0, 3);
- int pin = dataReq.substring(3, 5).toInt();
- int state = dataReq.substring(5, 6).toInt();
- // Serial.println("Type: " + type + " Pin: " + pin + " State: " + state);
- if (type == "RLY") {
- digitalWrite(pin, state);
- }
- }
- /*
- radio.stopListening();
- // radio.write(&poslji, sizeof(poslji));
- radio.write(&poslji, sizeof(poslji));
- // if (radio.write(&poslji, sizeof(poslji))) Serial.println("We did it!");
- radio.startListening();
- */
- }
- void sendTemperature() {
- String address = "2Node";
- // radio.openReadingPipe(1, addresses[1]);
- radio.openReadingPipe(1, "2Node");
- String temperature = String(temp) + ".50";
- // String temperature = "25.50";
- String sendData = "TMP=" + temperature + "2Node";
- sendData.toCharArray(dataToSend, 32);
- radio.stopListening();
- radio.write(&dataToSend, sizeof(dataToSend));
- radio.startListening();
- Serial.println(temperature);
- if (temp > 35) temp = 19;
- temp++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement