Guest User

Untitled

a guest
Sep 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. /** RF24Mesh_Example.ino by TMRh20
  2. *
  3. * This example sketch shows how to manually configure a node via RF24Mesh, and send data to the
  4. * master node.
  5. * The nodes will refresh their network address as soon as a single write fails. This allows the
  6. * nodes to change position in relation to each other and the master node.
  7. */
  8.  
  9.  
  10. #include "RF24.h"
  11. #include "RF24Network.h"
  12. #include "RF24Mesh.h"
  13. #include <SPI.h>
  14. #include <EEPROM.h>
  15. #include <dht.h>
  16. //#include <printf.h>
  17.  
  18.  
  19. /**** Configure the nrf24l01 CE and CS pins ****/
  20. RF24 radio(7, 8);
  21. RF24Network network(radio);
  22. RF24Mesh mesh(radio, network);
  23.  
  24. /**
  25. * User Configuration: nodeID - A unique identifier for each radio. Allows addressing
  26. * to change dynamically with physical changes to the mesh.
  27. *
  28. * In this example, configuration takes place below, prior to uploading the sketch to the device
  29. * A unique value from 1-255 must be configured for each node.
  30. * This will be stored in EEPROM on AVR devices, so remains persistent between further uploads, loss of power, etc.
  31. *
  32. **/
  33. #define nodeID 5
  34.  
  35. dht DHT;
  36. #define DHT11_PIN 2
  37.  
  38. uint32_t displayTimer = 0;
  39.  
  40. struct payload_t {
  41. uint32_t soilMoisture;
  42. uint32_t temp;
  43. uint32_t humidity;
  44. };
  45.  
  46. //moisture sensor pin
  47. int mostureSensor = 0;
  48.  
  49. void setup() {
  50.  
  51. Serial.begin(115200);
  52. //printf_begin();
  53. // Set the nodeID manually
  54. mesh.setNodeID(nodeID);
  55. // Connect to the mesh
  56. Serial.println(F("Connecting to the mesh..."));
  57. mesh.begin();
  58. }
  59.  
  60.  
  61.  
  62. void loop() {
  63.  
  64. mesh.update();
  65.  
  66. // Send to the master node every second
  67. if (millis() - displayTimer >= 5000) {
  68. payload_t payload;
  69.  
  70.  
  71. displayTimer = millis();
  72.  
  73. //payload_t sensorValue;
  74. payload.soilMoisture = analogRead(mostureSensor);
  75.  
  76. int chk = DHT.read11(DHT11_PIN);
  77. payload.temp = DHT.temperature;
  78. payload.humidity = DHT.humidity;
  79.  
  80. //TODO: send node value as well
  81. Serial.print("\n SM ");
  82. Serial.println(payload.soilMoisture);
  83. Serial.print("Temp ");
  84. Serial.println(payload.temp);
  85. Serial.print("Humidity ");
  86. Serial.println(payload.humidity);
  87.  
  88.  
  89. if (!mesh.write(&payload, 'M', sizeof(payload))) {
  90.  
  91. // If a write fails, check connectivity to the mesh network
  92. if ( ! mesh.checkConnection() ) {
  93. //refresh the network address
  94. Serial.println("Renewing Address");
  95. mesh.renewAddress();
  96. } else {
  97. Serial.println("Send fail, Test OK");
  98. }
  99. } else {
  100. Serial.print("Send OK: "); Serial.println(sizeof(payload));// Serial.print(" "); Serial.println(sensorValue);
  101. }
  102. }
  103.  
  104. while (network.available()) {
  105. RF24NetworkHeader header;
  106. payload_t payload;
  107. network.read(header, &payload, sizeof(payload));
  108. Serial.print("Received packet #");
  109. Serial.print(payload.soilMoisture);
  110. Serial.print(" at ");
  111. Serial.println(payload.humidity);
  112. }
  113. }
Add Comment
Please, Sign In to add comment