Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.95 KB | None | 0 0
  1. // Enable debug prints
  2. #define MY_DEBUG
  3.  
  4. // Required for MQTT as gateway does not automagically assign ID's
  5. #define MY_NODE_ID 3
  6.  
  7. // Set parent node ID manually
  8. #define MY_PARENT_NODE_ID 0
  9.  
  10. // Enable and select radio type attached
  11. #define MY_RADIO_NRF24
  12. // #define MY_RADIO_RFM69
  13.  
  14. // Set RF24L01 channel number
  15. #define MY_RF24_CHANNEL 125
  16.  
  17. // Enabled repeater feature for this node
  18. #define MY_REPEATER_FEATURE
  19.  
  20. // Define radio wait time between sends
  21. #define RADIO_PAUSE 0 // This allows for radio to come back to power after a transmission, ideally 0..
  22.  
  23. // Define end of loop pause time
  24. #define LOOP_PAUSE 30000
  25.  
  26. // Define time between sensors blocks
  27. #define SENSORS_DELAY 0 // This allows for time between sensor readings, allows VCC to steady, ideally 0..
  28.  
  29. #include <SPI.h>
  30. #include <MySensor.h>
  31. #include <BH1750.h>
  32. #include <NewPing.h>
  33. #include <Adafruit_NeoPixel.h>
  34. #include <elapsedMillis.h>
  35.  
  36. #define NEO_PIN 2
  37. #define NUM_LEDS 8
  38. Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(NUM_LEDS, NEO_PIN, NEO_GRB + NEO_KHZ800);
  39.  
  40. // Power pin moisture sensor
  41. #define MOISTURE_POWER_PIN 5
  42. // Digital input pin moisture sensor
  43. #define DIGITAL_INPUT_MOISTURE 6
  44. // Send only if changed? 1 = Yes 0 = No
  45. #define COMPARE_MOISTURE 0
  46.  
  47. // Power pin rain sensor
  48. #define RAIN_POWER_PIN 7
  49. // Digital input pin rain sensor
  50. #define DIGITAL_INPUT_RAIN 8
  51. // Send only if changed? 1 = Yes 0 = No
  52. #define COMPARE_RAIN 0
  53.  
  54. // Ultrasonic trigger pin
  55. #define TRIGGER_PIN 4
  56. // Ultrasonic echo pin
  57. #define ECHO_PIN 3
  58. // Maximum distance we want to ping for (in cms), maximum sensor distance is rated at 400-500cm
  59. #define MAX_DISTANCE 400
  60. // NewPing setup of pins and maximum distance
  61. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
  62. // Send only if changed? 1 = Yes 0 = No
  63. #define COMPARE_DIST 0
  64.  
  65. // Set BH1750 name
  66. BH1750 lightSensor;
  67. // Send only if changed? 1 = Yes 0 = No
  68. #define COMPARE_LUX 0
  69.  
  70. // Landroid settings
  71. boolean landroidWaiting = false;
  72. boolean landroidWaitingTriggered = false;
  73. boolean landroidHome = true;
  74. elapsedMillis timeElapsed;
  75.  
  76. // Landroid timers
  77. #define TIMER1 3200000 // 60 minutes * 60 seconds * 1000 millis = 3200000
  78. #define TIMER2 7200000 // 120 minutes * 60 seconds * 1000 millis = 7200000
  79. #define TIMER3 10800000 // 180 minutes * 60 seconds * 1000 millis = 10800000
  80. #define TIMER4 14400000 // 240 minutes * 60 seconds * 1000 millis = 14400000
  81.  
  82. // Store last moisture reading for comparison
  83. int lastMoistureValue = -1;
  84. // Store last rain reading for comparison
  85. int lastRainValue = -1;
  86. // Store last LUX reading for comparison
  87. uint16_t lastlux;
  88. // Store last distance reading for comparison
  89. int lastDist;
  90. // Set default value for metric / imperial
  91. boolean metric = true;
  92.  
  93. // Define sensor children for MySensors
  94. #define CHILD_ID1 1 // ID of the sensor child (Moisture)
  95. #define CHILD_ID2 2 // ID of the sensor child (Rain)
  96. #define CHILD_ID3 3 // ID of the sensor child (Light)
  97. #define CHILD_ID4 4 // ID of the sensor child (Distance)
  98. #define CHILD_ID10 10 // ID of the sensor child (Landroid home boolean)
  99. #define CHILD_ID11 11 // ID of the sensor child (Landroid waiting boolean)
  100. #define CHILD_ID12 12 // ID of the sensor child (Landroid time elapsed)
  101.  
  102. // Define MySensors message formats
  103. MyMessage msg1(CHILD_ID1, V_TRIPPED); // Setup message
  104. MyMessage msg2(CHILD_ID2, V_TRIPPED); // Setup message
  105. MyMessage msg3(CHILD_ID3, V_LEVEL); // Setup message
  106. MyMessage msg4(CHILD_ID4, V_DISTANCE); // Setup message
  107. MyMessage msg10(CHILD_ID10, V_TRIPPED); // Setup message
  108. MyMessage msg11(CHILD_ID11, V_TRIPPED); // Setup message
  109. MyMessage msg12(CHILD_ID12, V_CUSTOM); // Setup message
  110.  
  111. void setup()
  112. {
  113. // Send the sketch version information to the gateway and Controller
  114. sendSketchInfo("R+M+L+T+D+Rpt", "1.1");
  115. wait(RADIO_PAUSE);
  116. // Register all sensors to gw (they will be created as child devices)
  117. present(CHILD_ID1, S_MOTION);
  118. wait(RADIO_PAUSE);
  119. present(CHILD_ID2, S_MOTION);
  120. wait(RADIO_PAUSE);
  121. present(CHILD_ID3, S_LIGHT_LEVEL);
  122. wait(RADIO_PAUSE);
  123. present(CHILD_ID4, S_DISTANCE);
  124. wait(RADIO_PAUSE);
  125. present(CHILD_ID10, S_MOTION);
  126. wait(RADIO_PAUSE);
  127. present(CHILD_ID11, S_MOTION);
  128. wait(RADIO_PAUSE);
  129. present(CHILD_ID12, S_CUSTOM);
  130. wait(RADIO_PAUSE);
  131. // Set the moisture sensor digital pin as input
  132. pinMode(DIGITAL_INPUT_MOISTURE, INPUT);
  133. // Set the rain sensor digital pin as input
  134. pinMode(DIGITAL_INPUT_RAIN, INPUT);
  135. // Set the moisture sensor power pin as output
  136. pinMode(MOISTURE_POWER_PIN, OUTPUT);
  137. // Set the rain sensor power pin as output
  138. pinMode(RAIN_POWER_PIN, OUTPUT);
  139. // Set to LOW so no power is flowing through the moisture sensor
  140. digitalWrite(MOISTURE_POWER_PIN, LOW);
  141. // Set to LOW so no power is flowing through the rain sensor
  142. digitalWrite(RAIN_POWER_PIN, LOW);
  143. // Check gateway for metric setting
  144. boolean metric = getConfig().isMetric;
  145. // Start BH1750 light sensor
  146. lightSensor.begin();
  147. // Start NeoPixel LED strip
  148. strip1.begin();
  149. // Initialise all Neopixel LEDs off
  150. strip1.show();
  151. }
  152.  
  153. void loop()
  154. {
  155. {
  156. //--- Moisture sensor ---//
  157. digitalWrite(MOISTURE_POWER_PIN, HIGH); // Turn moisture power pin on
  158. wait(200); // Set a delay to ensure the moisture sensor has powered up
  159. int moistureValue = digitalRead(DIGITAL_INPUT_MOISTURE); // Read digital moisture value
  160. digitalWrite(MOISTURE_POWER_PIN, LOW); // Turn moisture power pin off
  161. #if COMPARE_MOISTURE == 1
  162. if (moistureValue != lastMoistureValue)
  163. #else
  164. #endif
  165. {
  166. #ifdef MY_DEBUG
  167. Serial.print("Moisture Sensor: ");
  168. Serial.println(moistureValue == 0 ? 1 : 0);
  169. #endif
  170. send(msg1.set(moistureValue == 0 ? 1 : 0)); // Send the inverse
  171. wait(RADIO_PAUSE);
  172. lastMoistureValue = moistureValue; // For testing can be 0 or 1 or back to moistureValue
  173. }
  174. }
  175. wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
  176. {
  177. //--- Rain sensor ---//
  178. digitalWrite(RAIN_POWER_PIN, HIGH); // Turn rain power pin on
  179. wait(200); // Set a delay to ensure the moisture sensor has powered up
  180. int rainValue = digitalRead(DIGITAL_INPUT_RAIN); // Read digital rain value
  181. digitalWrite(RAIN_POWER_PIN, LOW); // Turn rain power pin off
  182. #if COMPARE_RAIN == 1
  183. if (rainValue != lastRainValue) // Check value against saved value
  184. #else
  185. #endif
  186. {
  187. #ifdef MY_DEBUG
  188. Serial.print("Rain Sensor: ");
  189. Serial.println(rainValue == 0 ? 1 : 0);
  190. #endif
  191. send(msg2.set(rainValue == 0 ? 1 : 0)); // Send the inverse
  192. wait(RADIO_PAUSE);
  193. lastRainValue = rainValue; // For testing can be 0 or 1 or back to rainValue
  194. }
  195. }
  196. wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
  197. {
  198. //--- Light sensor ---//
  199. uint16_t lux = lightSensor.readLightLevel(); // Get Lux value
  200. #if COMPARE_LUX == 1
  201. if (lux != lastlux)
  202. #else
  203. #endif
  204. {
  205. #ifdef MY_DEBUG
  206. Serial.print("LUX: ");
  207. Serial.println(lux);
  208. #endif
  209. send(msg3.set(lux));
  210. wait(RADIO_PAUSE);
  211. lastlux = lux;
  212. }
  213. }
  214. wait(SENSORS_DELAY); // Wait between sensor readings, seems to help reliability of second reading
  215. {
  216. //--- Distance sensor ---//
  217. int dist = metric ? sonar.ping_cm() : sonar.ping_in();
  218. #if COMPARE_DIST == 1
  219. if (dist != lastDist)
  220. #else
  221. #endif
  222. {
  223. #ifdef MY_DEBUG
  224. Serial.print("Distance: ");
  225. Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  226. Serial.println(metric ? " cm" : " in");
  227. #endif
  228. send(msg4.set(dist));
  229. wait(RADIO_PAUSE);
  230. lastDist = dist;
  231. }
  232. }
  233.  
  234. //--- Analyse readings, set Neopixels, booleans, and send status messages ---//
  235. if (lastMoistureValue == 0 || lastRainValue == 0)
  236. {
  237. landroidWaiting = true;
  238. landroidWaitingTriggered = true;
  239. timeElapsed = 0;
  240. strip1.setPixelColor(0, 0, 0, 0);
  241. strip1.setPixelColor(1, 0, 0, 0);
  242. strip1.setPixelColor(2, 0, 0, 0);
  243. strip1.setPixelColor(3, 0, 0, 0);
  244. strip1.show();
  245. #ifdef MY_DEBUG
  246. Serial.print("Rain or moisture detected, landroid is waiting: ");
  247. Serial.print("Status");
  248. Serial.print("(");
  249. Serial.print(landroidWaiting);
  250. Serial.println(")");
  251. #endif
  252. }
  253.  
  254. else
  255. {
  256. landroidWaiting = false;
  257. #ifdef MY_DEBUG
  258. Serial.print("No rain or moisture detected, landroid is not waiting: ");
  259. Serial.print("Status");
  260. Serial.print("(");
  261. Serial.print(landroidWaiting);
  262. Serial.println(")");
  263. #endif
  264. }
  265.  
  266. if ( landroidWaiting == false && landroidWaitingTriggered == false )
  267. {
  268. strip1.setPixelColor(0, 0, 127, 0);
  269. strip1.setPixelColor(1, 0, 127, 0);
  270. strip1.setPixelColor(2, 0, 127, 0);
  271. strip1.setPixelColor(3, 0, 127, 0);
  272. strip1.show();
  273. #ifdef MY_DEBUG
  274. Serial.print("Landroid is free to go and is now waiting on the schedule: ");
  275. Serial.print("Status");
  276. Serial.print("(");
  277. Serial.print(landroidWaiting);
  278. Serial.println(")");
  279. #endif
  280. }
  281.  
  282. if ( landroidWaiting == false && landroidWaitingTriggered == true && timeElapsed < TIMER1 ) // Logic for timer trigger
  283. {
  284. strip1.setPixelColor(0, 0, 0, 0);
  285. strip1.setPixelColor(1, 0, 0, 0);
  286. strip1.setPixelColor(2, 0, 0, 0);
  287. strip1.setPixelColor(3, 0, 0, 0);
  288. strip1.show();
  289. #ifdef MY_DEBUG
  290. Serial.print("0 Hours Passed: ");
  291. Serial.println("0 LEDs illuminated");
  292. Serial.print("Status");
  293. Serial.print("(");
  294. Serial.print(landroidWaiting);
  295. Serial.println(")");
  296. Serial.print("Time elapsed: ");
  297. Serial.println(timeElapsed / 1000);
  298. #endif
  299. }
  300.  
  301. if ( landroidWaiting == false && landroidWaitingTriggered == true && timeElapsed > TIMER1 ) // Logic for timer trigger
  302. {
  303. strip1.setPixelColor(0, 255, 0, 0);
  304. strip1.setPixelColor(1, 0, 0, 0);
  305. strip1.setPixelColor(2, 0, 0, 0);
  306. strip1.setPixelColor(3, 0, 0, 0);
  307. strip1.show();
  308. #ifdef MY_DEBUG
  309. Serial.print("1 Hour Passed: ");
  310. Serial.println("1 LED illuminated");
  311. Serial.print("Status");
  312. Serial.print("(");
  313. Serial.print(landroidWaiting);
  314. Serial.println(")");
  315. Serial.print("Time elapsed: ");
  316. Serial.println(timeElapsed / 1000);
  317. #endif
  318. }
  319.  
  320. if ( landroidWaiting == false && landroidWaitingTriggered == true && timeElapsed > TIMER2 ) // Logic for timer trigger
  321. {
  322. strip1.setPixelColor(0, 255, 0, 0);
  323. strip1.setPixelColor(1, 255, 0, 0);
  324. strip1.setPixelColor(2, 0, 0, 0);
  325. strip1.setPixelColor(3, 0, 0, 0);
  326. strip1.show();
  327. #ifdef MY_DEBUG
  328. Serial.print("2 Hours Passed: ");
  329. Serial.println("2 LEDs illuminated");
  330. Serial.print("Status");
  331. Serial.print("(");
  332. Serial.print(landroidWaiting);
  333. Serial.println(")");
  334. Serial.print("Time elapsed: ");
  335. Serial.println(timeElapsed / 1000);
  336. #endif
  337. }
  338.  
  339. if ( landroidWaiting == false && landroidWaitingTriggered == true && timeElapsed > TIMER3 ) // Logic for timer trigger
  340. {
  341. strip1.setPixelColor(0, 255, 0, 0);
  342. strip1.setPixelColor(1, 255, 0, 0);
  343. strip1.setPixelColor(2, 255, 0, 0);
  344. strip1.setPixelColor(3, 0, 0, 0);
  345. strip1.show();
  346. #ifdef MY_DEBUG
  347. Serial.print("3 Hours Passed: ");
  348. Serial.println("3 LEDs illuminated");
  349. Serial.print("Status");
  350. Serial.print("(");
  351. Serial.print(landroidWaiting);
  352. Serial.println(")");
  353. Serial.print("Time elapsed: ");
  354. Serial.println(timeElapsed / 1000);
  355. #endif
  356. }
  357.  
  358. if ( landroidWaiting == false && landroidWaitingTriggered == true && timeElapsed > TIMER4 ) // Logic for timer trigger
  359. {
  360. landroidWaitingTriggered = false;
  361. strip1.setPixelColor(0, 0, 127, 0);
  362. strip1.setPixelColor(1, 0, 127, 0);
  363. strip1.setPixelColor(2, 0, 127, 0);
  364. strip1.setPixelColor(3, 0, 127, 0);
  365. strip1.show();
  366. #ifdef MY_DEBUG
  367. Serial.print("4 Hours Passed: ");
  368. Serial.println("Landroid is free to go and is now waiting on the schedule");
  369. Serial.print("Status");
  370. Serial.print("(");
  371. Serial.print(landroidWaiting);
  372. Serial.println(")");
  373. Serial.print("Time elapsed: ");
  374. Serial.println(timeElapsed / 1000);
  375. #endif
  376. }
  377.  
  378. if (lastDist < 30)
  379. {
  380. landroidHome = true;
  381. strip1.setPixelColor(4, 0, 127, 0);
  382. strip1.setPixelColor(5, 0, 127, 0);
  383. strip1.setPixelColor(6, 0, 127, 0);
  384. strip1.setPixelColor(7, 0, 127, 0);
  385. strip1.show();
  386. #ifdef MY_DEBUG
  387. Serial.println("Landroid is home charging!");
  388. #endif
  389. }
  390.  
  391. else
  392. {
  393. landroidHome = false;
  394. strip1.setPixelColor(4, 255, 0, 0);
  395. strip1.setPixelColor(5, 255, 0, 0);
  396. strip1.setPixelColor(6, 255, 0, 0);
  397. strip1.setPixelColor(7, 255, 0, 0);
  398. strip1.show();
  399. #ifdef MY_DEBUG
  400. Serial.println("Landroid is out cutting the grass!");
  401. #endif
  402. }
  403.  
  404. // Send landroid home status to gateway
  405. #ifdef MY_DEBUG
  406. Serial.print("Sending landroidHome ");
  407. Serial.print("(");
  408. Serial.print(landroidHome);
  409. Serial.print(")");
  410. Serial.print(" status to gateway: ");
  411. #endif
  412. send(msg10.set(landroidHome));
  413. wait(RADIO_PAUSE);
  414.  
  415. // Send landroid waiting status to gateway
  416. #ifdef MY_DEBUG
  417. Serial.print("Sending landroidWaitingTriggered ");
  418. Serial.print("(");
  419. Serial.print(landroidWaitingTriggered);
  420. Serial.print(")");
  421. Serial.print(" status to gateway: ");
  422. #endif
  423. send(msg11.set(landroidWaitingTriggered));
  424. wait(RADIO_PAUSE);
  425.  
  426.  
  427. // Send landroid waiting timer status to gateway
  428. if ( landroidWaitingTriggered == true && timeElapsed > LOOP_PAUSE && timeElapsed < TIMER4)
  429. {
  430. #ifdef MY_DEBUG
  431. Serial.print("Sending timeElapsed ");
  432. Serial.print("(");
  433. Serial.print(timeElapsed / 1000);
  434. Serial.print(")");
  435. Serial.print(" status to gateway: ");
  436. #endif
  437. send(msg12.set(timeElapsed / 1000));
  438. wait(RADIO_PAUSE);
  439. }
  440.  
  441. else
  442. {
  443. #ifdef MY_DEBUG
  444. Serial.print("Sending timeElapsed ");
  445. Serial.print("(");
  446. Serial.print(0);
  447. Serial.print(")");
  448. Serial.print(" status to gateway: ");
  449. #endif
  450. send(msg12.set(0));
  451. wait(RADIO_PAUSE);
  452. }
  453.  
  454. wait(LOOP_PAUSE); // Sleep or wait (repeater)
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement