Advertisement
seston

arduino easyiot moisture

Dec 19th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. /*
  2. V1.0 - first version
  3.  
  4. Created by Igor Jarc <admin@iot-playground.com>
  5. See http://iot-playground.com for details
  6. Do not contact avtor dirrectly, use community forum on website.
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. version 2 as published by the Free Software Foundation.
  11. */
  12. #include <Irrigation.h>
  13. #include <SPI.h>
  14. #include <MySensor.h>
  15.  
  16.  
  17. #define CHILD_ID_SWITCH_IRRIGATE 0
  18. #define CHILD_ID_AUTO_MODE 1
  19. #define CHILD_ID_SOIL_HUMIDITY 2
  20. #define CHILD_ID_SOIL_HUMIDITY_AO 3
  21.  
  22. #define SENORS_PER_IRRIGATOR 1
  23. #define START_DO_PIN 2
  24.  
  25. MySensor mys;
  26. Irrigation irr;
  27.  
  28.  
  29. // the setup function runs once when you press reset or power the board
  30. void setup() {
  31.  
  32. for (int i=0; i < irr.getIrrigatorsCount(); i++) {
  33. pinMode(START_DO_PIN + i, OUTPUT);
  34. digitalWrite(START_DO_PIN + i, HIGH);
  35. }
  36.  
  37.  
  38. mys.begin(incomingMessage, AUTO, true);
  39.  
  40. for (int i=0; i < irr.getIrrigatorsCount(); i++) {
  41. mys.present(CHILD_ID_SWITCH_IRRIGATE + SENORS_PER_IRRIGATOR * i, S_LIGHT); // irrigation mottor switch
  42. delay500();
  43. mys.present(CHILD_ID_AUTO_MODE + SENORS_PER_IRRIGATOR * i, S_LIGHT); // irrigator mode - 1 auto, 0 - manual
  44. delay500();
  45. mys.present(CHILD_ID_SOIL_HUMIDITY + SENORS_PER_IRRIGATOR * i, S_HUM); // soil humidity
  46. delay500();
  47. mys.present(CHILD_ID_SOIL_HUMIDITY_AO + SENORS_PER_IRRIGATOR * i, S_HUM); // soil humidity treshold - change later in EasyIoT server UI to humidity output
  48. delay500();
  49. }
  50.  
  51.  
  52. irr.begin(&readAi, &sendHumidity, &setMotor);
  53.  
  54. // request treshold humidity
  55. for (int i=0; i < irr.getIrrigatorsCount(); i++) {
  56. mys.request(CHILD_ID_SOIL_HUMIDITY_AO + SENORS_PER_IRRIGATOR * i, V_HUM);
  57. delay500();
  58. }
  59.  
  60. // request auto mode
  61. for (int i=0; i < irr.getIrrigatorsCount(); i++) {
  62. mys.request(CHILD_ID_AUTO_MODE + SENORS_PER_IRRIGATOR * i, V_LIGHT);
  63. delay500();
  64. }
  65. }
  66.  
  67. // the loop function runs over and over again forever
  68. void loop() {
  69. mys.process();
  70. irr.process();
  71. }
  72.  
  73.  
  74. void delay500()
  75. {
  76. for (int i=0; i < 50; i++) {
  77. delay(10);
  78. mys.process();
  79. }
  80. }
  81.  
  82.  
  83. int readAi(int i)
  84. {
  85. if (i < irr.getIrrigatorsCount())
  86. {
  87. if (i==0)
  88. return analogRead(A0);
  89. else if (i==1)
  90. return analogRead(A1);
  91. else if (i==2)
  92. return analogRead(A2);
  93. else if (i==3)
  94. return analogRead(A3);
  95. else if (i==4)
  96. return analogRead(A4);
  97. else if (i==5)
  98. return analogRead(A5);
  99. else if (i==6)
  100. return analogRead(A6);
  101. }
  102.  
  103. return 0;
  104. }
  105.  
  106.  
  107. void sendHumidity(int i, int humidity)
  108. {
  109. if (i < irr.getIrrigatorsCount())
  110. {
  111. MyMessage msgHum(CHILD_ID_SOIL_HUMIDITY + SENORS_PER_IRRIGATOR * i, V_HUM);
  112. mys.send(msgHum.set(humidity, 1));
  113. }
  114. }
  115.  
  116.  
  117. void setMotor(int i, bool state)
  118. {
  119. if (i < irr.getIrrigatorsCount())
  120. {
  121. digitalWrite(START_DO_PIN + i, state?LOW:HIGH);
  122.  
  123. MyMessage msgState(CHILD_ID_SWITCH_IRRIGATE + SENORS_PER_IRRIGATOR * i, V_LIGHT);
  124. mys.send(msgState.set(state));
  125. }
  126. }
  127.  
  128.  
  129. void incomingMessage(const MyMessage &message) {
  130. Serial.println("");
  131. Serial.print("Incoming change for sensor:");
  132. Serial.println(message.sensor);
  133.  
  134. int irrigator = message.sensor / SENORS_PER_IRRIGATOR;
  135. int sensor = message.sensor % SENORS_PER_IRRIGATOR;
  136.  
  137. if (message.type == V_LIGHT) {
  138. // process mode commnd
  139. if (sensor == CHILD_ID_AUTO_MODE)
  140. {
  141. irr.setAutoMode(irrigator, message.getBool());
  142. }
  143.  
  144. // process motor switch command
  145. else if (sensor == CHILD_ID_SWITCH_IRRIGATE)
  146. {
  147. irr.setIrrigate(irrigator, message.getBool());
  148. }
  149. }else if (message.type == V_HUM && sensor == CHILD_ID_SOIL_HUMIDITY_AO) {
  150. // set humidity treshold for automatic irrigation
  151. irr.setSoilHumidityTreshold(irrigator, message.getInt());
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement