Advertisement
noam76

Main.ino

Feb 23rd, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 KB | None | 0 0
  1. /* Irrigation controller for each plant individually based to their soil moisture sensor
  2.  * I am using an extended GPIO port MCP23017 because I don't have enough GPIO port on the ESP
  3.  * Each plant have a soil moisture sensor and a tap solenoid
  4.  * Main pump is controlled if the tap solenoids are open or closed
  5.  * Read saving data by User from the JSON file from the SPIFFS
  6.  * in this program I use 7 soil sensor and tap solenoid, the max is 10
  7.  */
  8.  
  9. #include <ESP8266WiFi.h>
  10. #include "FS.h"
  11. #include <ArduinoJson.h>
  12. #include <Wire.h>
  13. #include "Adafruit_MCP23017.h"
  14.  
  15. const char* CONFIG_HTML = "/html/config.html";
  16. const char* UPDATE_HTML = "/html/Update.html";
  17. const char* DISPLAY_DATA_HTML = "/html/display_data.html";
  18.      
  19. const char* ssid = "ASUS";
  20. const char* password = "Famillejacobi";
  21. const uint16_t HTTPPort = 80;
  22. WiFiServer serveurWeb(HTTPPort); // crée un serveur sur le port HTTP standard // create server on http port
  23.  
  24. const byte maxHTTPLine = 100;
  25. char httpLine[maxHTTPLine + 1]; //+1 pour avoir la place du '\0' // +1 stay space for the '\0'
  26. const byte maxURL = 50;
  27. char urlRequest[maxURL + 1]; //+1 pour avoir la place du '\0' // +1 stay space for the '\0'
  28.  
  29. Adafruit_MCP23017 mcp;
  30.  
  31. #define ReadSoilSensor A0    //define analog Pin A0 read sensors data
  32. #define SoilS1EnablePin1 D3  //Connect Sens1 to Pin D3 on the ESP
  33. #define tap_solenoid1 D4     //Connect tap1 for the Sens1 to D6
  34.  
  35. #define SoilS1EnablePin2 D5  //Connect Sens2 to Pin D5 on the ESP
  36. #define tap_solenoid2 D6     //Connect tap2 for the Sens2 to D6
  37.  
  38. #define SoilS1EnablePin3 D7   //Connect Sens3 to pin D7 on the ESP
  39. #define tap_solenoid3 D8      //Connect tap3 for Sens3 to ESP
  40.  
  41. #define SoilS1EnablePin4 0    //Connect Sens4 to Pin GPA0 on the mcp
  42. #define tap_solenoid4 1       //Connect tap4 for Sens4 to MCP GPA1
  43.  
  44. #define SoilS1EnablePin5 2    //Connect Sens5 to MCP GPA2
  45. #define tap_solenoid5 3       //Connect tap5 for Sens5 to MCP GPA3
  46.  
  47. #define SoilS1EnablePin6 4    //Connect Sensor 6 to MCP GPA4
  48. #define tap_solenoid6 5       //Connect tap6 for Sens6 to MCP GPA5
  49.  
  50. #define SoilS1EnablePin7 6    //Connect Sensor 7 to MCP GPA6
  51. #define tap_solenoid7 7       //Connect tap7 for SENS 777789mcp GPA7
  52.  
  53. #define PumpPin D0            //Connect pump pin to ESP D0
  54.  
  55. //Configure the soil moisture
  56. const int MintSoilMoisture = 250;//Menth
  57. const int ParsleySoilMoisture = 230;//Persil
  58. const int BasilSoilMoisture = 260;//Basilic
  59. const int CorianderSoilMoisture = 300;//Coriande, Cousbara
  60. const int DillSoilMoisture = 2700;//Aneth, shamir
  61. const int SageSoilMoisture = 310;//Sauge, Marva
  62. const int rosemarySoilMoisture = 200;//Romarin
  63.  
  64. int read_s1_value,read_s2_value,read_s3_value,read_s4_value,read_s5_value,read_s6_value,read_s7_value; //save reading soil sensor data
  65. int PlantS1,PlantS2,PlantS3,PlantS4,PlantS5,PlantS6,PlantS7; //Keep Selected Plant by User from WebForm
  66. boolean tap_motor1=false,tap_motor2=false,tap_motor3=false,tap_motor4=false,tap_motor5=false,tap_motor6=false,tap_motor7=false; //mentioned tap solenoid if open or close
  67. const int onTime = 1000; // in ms
  68. const int offTime = 5000; // in ms
  69. boolean currentlyOn = false;
  70. unsigned long startTime;
  71.  
  72. void printHTTPServerInfo()
  73. {
  74.  Serial.print(F("Site web http: "));
  75.  Serial.print(WiFi.localIP());
  76.  if (HTTPPort != 80)
  77.  {
  78.   Serial.print(F(":"));
  79.   Serial.print(HTTPPort);
  80.  }
  81.  Serial.println();
  82. }
  83.  
  84. #include "config_json_file.h" // include the function for saving and loading config.json
  85. #include "spiffs_page_display.h" // include the function to display web page
  86. #include "waterControl.h" // include the irrigation system
  87.  
  88. /**
  89. * Read Datas Sensors Pins connected to ESP
  90. */
  91. int read_Sensors_Values(int EnableSoilPin)
  92. {
  93.  int ReadSensor;
  94.  digitalWrite(EnableSoilPin,HIGH);
  95.  for(int SensorReading = 0;SensorReading < 5;SensorReading++)
  96.  {
  97.   ReadSensor = analogRead(ReadSoilSensor);
  98.  }
  99.  digitalWrite(EnableSoilPin,LOW);
  100.  return ReadSensor;
  101. }
  102.  
  103. /**
  104. * Read Datas Sensors Pins connected to MCP
  105. */
  106. int read_mcp_sensor_values(int EnableSoilPin)
  107. {
  108.  int ReadSensor;
  109.  mcp.digitalWrite(EnableSoilPin,HIGH);
  110.  for(int SensorReading = 0;SensorReading < 5;SensorReading++)
  111.  {
  112.   ReadSensor = analogRead(ReadSoilSensor);
  113.  }
  114.  mcp.digitalWrite(EnableSoilPin,LOW);
  115.  return ReadSensor;
  116. }
  117.  
  118. /**
  119. * Assign the correct Soil Target for the Plant Sensor connected after user choice from the WebForm
  120. */
  121. int SoilSensorWebUser(int WebSoilSensor)
  122. {
  123.  switch(WebSoilSensor)
  124.  {
  125.   case 0:
  126.     return(0);
  127.   break;
  128.   case 1:
  129.     return(MintSoilMoisture);
  130.   break;
  131.   case 2:
  132.     return(ParsleySoilMoisture);
  133.   break;
  134.   case 3:
  135.     return(BasilSoilMoisture);
  136.   break;
  137.   case 4:
  138.     return(CorianderSoilMoisture);
  139.   break;
  140.   case 5:
  141.     return(DillSoilMoisture);
  142.   break;
  143.   case 6:
  144.     return(SageSoilMoisture);
  145.   break;
  146.   case 7:
  147.     return(rosemarySoilMoisture);
  148.   break;
  149.  }
  150. }
  151.  
  152. //Define GPIO for Input/Output
  153. void PinsConfiguration()
  154. {
  155.  pinMode(SoilS1EnablePin1,OUTPUT);
  156.  pinMode(tap_solenoid1,OUTPUT);
  157.  pinMode(SoilS1EnablePin2,OUTPUT);
  158.  pinMode(tap_solenoid2,OUTPUT);
  159.  pinMode(SoilS1EnablePin3,OUTPUT);
  160.  pinMode(tap_solenoid3,OUTPUT);
  161.  
  162.  mcp.pinMode(SoilS1EnablePin4, OUTPUT);
  163.  mcp.pinMode(tap_solenoid4, OUTPUT);
  164.  mcp.pinMode(SoilS1EnablePin5, OUTPUT);
  165.  mcp.pinMode(tap_solenoid5, OUTPUT);
  166.  mcp.pinMode(SoilS1EnablePin6, OUTPUT);
  167.  mcp.pinMode(tap_solenoid6, OUTPUT);
  168.  mcp.pinMode(SoilS1EnablePin7, OUTPUT);
  169.  mcp.pinMode(tap_solenoid7, OUTPUT);
  170.  pinMode(PumpPin,OUTPUT);
  171. }
  172.  
  173. void setup()
  174. {
  175.  Serial.begin(115200);
  176.  mcp.begin(); // use default address 0
  177.  PinsConfiguration(); //Define GPIO for Input/Output
  178.  
  179.  Serial.println("\n\nMounting FS...\n");
  180.  // on démarre le SPIFSS
  181.  if (!SPIFFS.begin())
  182.  {
  183.   Serial.println("erreur SPIFFS");
  184.   while (true); // on ne va pas plus loin
  185.  }
  186.  
  187.  loadConfig();
  188.  /*if (!loadConfig())
  189.  {Serial.println("Failed to load config");}
  190.  else
  191.  {Serial.println("Config loaded");}*/
  192.  
  193.  WiFi.begin(ssid, password);
  194.  Serial.println();
  195.  while (WiFi.status() != WL_CONNECTED)
  196.  {
  197.   delay(500);
  198.   Serial.write('.');
  199.  }
  200.  Serial.println();
  201.  // on démarre le serveur
  202.  serveurWeb.begin();
  203.  printHTTPServerInfo();
  204. }
  205.  
  206.  void loop()
  207. {
  208.  testRequeteWeb();
  209.  // --- read sensors value Every 5sec for 1sec --- //
  210.  if (currentlyOn && millis()>startTime+onTime) // Switch off
  211.  {
  212.   currentlyOn = false;
  213.   startTime=millis(); // Reset timer
  214.  }
  215.  if (!currentlyOn && millis()>startTime+offTime) // Switch on
  216.  {
  217.   //Enable Pin Sensor connected to ESP
  218.   read_s1_value = read_Sensors_Values(SoilS1EnablePin1);
  219.   read_s2_value = read_Sensors_Values(SoilS1EnablePin2);
  220.   read_s3_value = read_Sensors_Values(SoilS1EnablePin3);
  221.   //Enable Pin Sensor connected to MCP
  222.   read_s4_value = read_mcp_sensor_values(SoilS1EnablePin4);
  223.   read_s5_value = read_mcp_sensor_values(SoilS1EnablePin5);
  224.   read_s6_value = read_mcp_sensor_values(SoilS1EnablePin6);
  225.   read_s7_value = read_mcp_sensor_values(SoilS1EnablePin7);
  226.   currentlyOn = true;
  227.   startTime=millis(); // Reset timer
  228.  }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement