Advertisement
noam76

Main

Jan 23rd, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include "FS.h" // pour le SPIFFS
  3. #include <ArduinoJson.h>
  4. #include <dampermotor.h>
  5. #include <Wire.h>
  6. // #include "Adafruit_MCP23017.h"
  7.  
  8. // Adafruit_MCP23017 mcp;
  9.  
  10. const char* CONFIG_HTML = "/html/config.html";
  11. const char* UPDATE_HTML = "/html/Update.html";
  12. const char* DISPLAY_DATA_HTML = "/html/display_data.html";
  13.      
  14. const char* ssid = "ASUS";
  15. const char* password = "Famillejacobi";
  16. const uint16_t HTTPPort = 80;
  17. WiFiServer serveurWeb(HTTPPort); // crée un serveur sur le port HTTP standard
  18. const byte maxHTTPLine = 100;
  19. char httpLine[maxHTTPLine + 1]; //+1 pour avoir la place du '\0'
  20. const byte maxURL = 50;
  21. char urlRequest[maxURL + 1]; //+1 pour avoir la place du '\0'
  22.  
  23. #define SoilSensor A0
  24. #define SoilS1EnablePin1 D0
  25. #define SoilS1EnablePin2 D1
  26. #define SoilS1EnablePin3 D2
  27. #define SoilS1EnablePin4 D3
  28. #define tap_solenoid1 0
  29. #define tap_solenoid2 1
  30. #define tap_solenoid3 2
  31. #define tap_solenoid4 3
  32. #define tap_solenoid_timeout 1500 // delay for 3 sec.
  33. #define PumpPin 2
  34.  
  35. //Configure the soil moisture
  36. int MintSoilMoisture = 250;//Menth
  37. const int ParsleySoilMoisture = 230;//Persil
  38. const int BasilSoilMoisture = 260;//Basilic
  39. const int CorianderSoilMoisture = 300;//Coriande, Cousbara
  40. const int DillSoilMoisture = 2700;//Aneth, shamir
  41. const int SageSoilMoisture = 310;//Sauge, Marva
  42. const int rosemarySoilMoisture = 200;//Romarin
  43.  
  44. int read_s1_value,read_s2_value,read_s3_value,read_s4_value; //read data from sensor
  45. int PlantS1,PlantS2,PlantS3,PlantS4; //User final target after converted from WebForm
  46. boolean electric_tap_motor1,electric_tap_motor2,electric_tap_motor3,electric_tap_motor4; //Off or On motor's electric tap
  47. const int onTime = 1000; // in ms
  48. const int offTime = 5000; // in ms
  49. boolean currentlyOn = false;
  50. unsigned long startTime;
  51.  
  52. dampermotor tap_motor_sensor1(D4,D5,tap_solenoid_timeout); // define pin 4,5 to drive the tap_motor_sensor1
  53. dampermotor tap_motor_sensor2(D6,D7,tap_solenoid_timeout); // define pin 6,7 to drive the tap_motor_sensor1
  54. dampermotor tap_motor_sensor3(D8,D9,tap_solenoid_timeout); // define pin 8,9 to drive the tap_motor_sensor1
  55. //dampermotor tap_motor_sensor4(D10,11,tap_solenoid_timeout); // define pin 10,11 to drive the tap_motor_sensor1
  56.  
  57. void printHTTPServerInfo()
  58. {
  59.  Serial.print(F("Site web http: "));
  60.  Serial.print(WiFi.localIP());
  61.  if (HTTPPort != 80)
  62.  {
  63.   Serial.print(F(":"));
  64.   Serial.print(HTTPPort);
  65.  }
  66.  Serial.println();
  67. }
  68.  
  69. #include "config_json_file.h" // include the function for saving and loading config.json
  70. #include "spiffs_page_display.h" // include the function to display web page
  71. #include "waterControl.h" // include the irrigation system
  72.  
  73. /**
  74. * Read Datas from all Sensors
  75. */
  76. int read_Sensors_Values(int EnableSoilPin)
  77. {
  78.  int ReadSensor;
  79.  digitalWrite(EnableSoilPin,HIGH);
  80.  for(int SensorReading = 0;SensorReading < 5;SensorReading++)
  81.  {
  82.   ReadSensor = analogRead(SoilSensor);
  83.  }
  84.  digitalWrite(EnableSoilPin,LOW);
  85.  return ReadSensor;
  86. }
  87.  
  88. /**
  89. * Assign the correct Soil Target for the Plant Sensor connected after user choice from the WebForm
  90. */
  91. int SoilSensorWebUser(int WebSoilSensor)
  92. {
  93.  switch(WebSoilSensor)
  94.  {
  95.   case 0:
  96.     return(0);
  97.   break;
  98.   case 1:
  99.     return(MintSoilMoisture);
  100.   break;
  101.   case 2:
  102.     return(ParsleySoilMoisture);
  103.   break;
  104.   case 3:
  105.     return(BasilSoilMoisture);
  106.   break;
  107.   case 4:
  108.     return(CorianderSoilMoisture);
  109.   break;
  110.   case 5:
  111.     return(DillSoilMoisture);
  112.   break;
  113.   case 6:
  114.     return(SageSoilMoisture);
  115.   break;
  116.   case 7:
  117.     return(rosemarySoilMoisture);
  118.   break;
  119.  }
  120. }
  121.  
  122. void setup()
  123. {
  124.  Serial.begin(115200);
  125.  /*mcp.begin(); // use default address 0
  126.  mcp.pinMode(0, INPUT);
  127.  mcp.pinMode(tap_solenoid1, OUTPUT);
  128.  mcp.pinMode(tap_solenoid2, OUTPUT);
  129.  mcp.pinMode(tap_solenoid3, OUTPUT);
  130.  mcp.pinMode(tap_solenoid4, OUTPUT);*/
  131.  
  132.  Serial.println("\n\nMounting FS...\n");
  133.  // on démarre le SPIFSS
  134.  if (!SPIFFS.begin())
  135.  {
  136.   Serial.println("erreur SPIFFS");
  137.   while (true); // on ne va pas plus loin
  138.  }
  139.  
  140.  loadConfig();
  141.  /*if (!loadConfig())
  142.  {Serial.println("Failed to load config");}
  143.  else
  144.  {Serial.println("Config loaded");}*/
  145.  
  146.  WiFi.begin(ssid, password);
  147.  Serial.println();
  148.  while (WiFi.status() != WL_CONNECTED)
  149.  {
  150.   delay(500);
  151.   Serial.write('.');
  152.  }
  153.  Serial.println();
  154.  // on démarre le serveur
  155.  serveurWeb.begin();
  156.  printHTTPServerInfo();
  157. }
  158.  
  159. void loop()
  160. {
  161.  testRequeteWeb();
  162.  // --- read sensors value Every 5sec for 1sec --- //
  163.  if (currentlyOn && millis()>startTime+onTime) // Switch resistor off
  164.  {
  165.   currentlyOn = false;
  166.   startTime=millis(); // Reset timer
  167.  }
  168.  if (!currentlyOn && millis()>startTime+offTime) // Switch resistor on
  169.  {
  170.   read_s1_value = read_Sensors_Values(SoilS1EnablePin1);
  171.   read_s2_value = read_Sensors_Values(SoilS1EnablePin2);
  172.   read_s3_value = read_Sensors_Values(SoilS1EnablePin3);
  173.   read_s4_value = read_Sensors_Values(SoilS1EnablePin4);
  174.   currentlyOn = true;
  175.   startTime=millis(); // Reset timer
  176.  }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement