Advertisement
rozman50

ArduinoNode - sends/receive data via WiFi with function

May 17th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include <SPI.h>
  3. #include <RF24.h>
  4. #include "Timer.h"
  5.  
  6. // This is just the way the RF24 library works:
  7. // Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
  8. RF24 radio(7, 8);
  9.  
  10. byte addresses[][6] = { "1Node","2Node" };
  11.  
  12.  
  13. char poslji[] = "BITCH9";
  14. char data;
  15. char dataToSend[32];
  16.  
  17. int temp = 18;
  18.  
  19. Timer t;
  20. // -----------------------------------------------------------------------------
  21. // SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  22. // -----------------------------------------------------------------------------
  23. void setup() {
  24. Serial.begin(9600);
  25. Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
  26.  
  27. // Initiate the radio object
  28. radio.begin();
  29.  
  30. // Set the transmit power to lowest available to prevent power supply related issues
  31. radio.setPALevel(RF24_PA_MIN);
  32.  
  33. // Set the speed of the transmission to the quickest available
  34. radio.setDataRate(RF24_2MBPS);
  35.  
  36. // Use a channel unlikely to be used by Wifi, Microwave ovens etc
  37. radio.setChannel(124);
  38.  
  39. // Open a writing and reading pipe on each radio, with opposite addresses
  40. // radio.openWritingPipe(addresses[0]);
  41. // radio.openReadingPipe(1, addresses[1]);
  42.  
  43. // Start the radio listening for data
  44. radio.startListening();
  45.  
  46. t.every(5000, sendTemperature);
  47.  
  48. }
  49.  
  50. // -----------------------------------------------------------------------------
  51. // We are LISTENING on this device only (although we do transmit a response)
  52. // -----------------------------------------------------------------------------
  53. void loop() {
  54.  
  55. t.update();
  56.  
  57. if (radio.available()) {
  58. char preberi[32];
  59. radio.read(&preberi, sizeof(preberi));
  60. // Serial.println(preberi);
  61.  
  62. String dataReq(preberi);
  63.  
  64. String type = dataReq.substring(0, 3);
  65. int pin = dataReq.substring(3, 5).toInt();
  66. int state = dataReq.substring(5, 6).toInt();
  67.  
  68. // Serial.println("Type: " + type + " Pin: " + pin + " State: " + state);
  69.  
  70. if (type == "RLY") {
  71. digitalWrite(pin, state);
  72. }
  73.  
  74. }
  75.  
  76. /*
  77. radio.stopListening();
  78. // radio.write(&poslji, sizeof(poslji));
  79. radio.write(&poslji, sizeof(poslji));
  80. // if (radio.write(&poslji, sizeof(poslji))) Serial.println("We did it!");
  81. radio.startListening();
  82. */
  83. }
  84.  
  85.  
  86.  
  87. void sendTemperature() {
  88. String address = "2Node";
  89. // radio.openReadingPipe(1, addresses[1]);
  90. radio.openReadingPipe(1, "2Node");
  91.  
  92. String temperature = String(temp) + ".50";
  93. // String temperature = "25.50";
  94.  
  95. String sendData = "TMP=" + temperature + "2Node";
  96. sendData.toCharArray(dataToSend, 32);
  97.  
  98. radio.stopListening();
  99.  
  100. radio.write(&dataToSend, sizeof(dataToSend));
  101.  
  102. radio.startListening();
  103. Serial.println(temperature);
  104. if (temp > 35) temp = 19;
  105. temp++;
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement