Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Irrigation controller for each plant individually based to their soil moisture sensor
- * I am using an extended GPIO port MCP23017 because I don't have enough GPIO port on the ESP
- * Each plant have a soil moisture sensor and a tap solenoid
- * Main pump is controlled if the tap solenoids are open or closed
- * Read saving data by User from the JSON file from the SPIFFS
- * in this program I use 7 soil sensor and tap solenoid, the max is 10
- */
- #include <ESP8266WiFi.h>
- #include "FS.h"
- #include <ArduinoJson.h>
- #include <Wire.h>
- #include "Adafruit_MCP23017.h"
- const char* CONFIG_HTML = "/html/config.html";
- const char* UPDATE_HTML = "/html/Update.html";
- const char* DISPLAY_DATA_HTML = "/html/display_data.html";
- const char* ssid = "ASUS";
- const char* password = "Famillejacobi";
- const uint16_t HTTPPort = 80;
- WiFiServer serveurWeb(HTTPPort); // crée un serveur sur le port HTTP standard // create server on http port
- const byte maxHTTPLine = 100;
- char httpLine[maxHTTPLine + 1]; //+1 pour avoir la place du '\0' // +1 stay space for the '\0'
- const byte maxURL = 50;
- char urlRequest[maxURL + 1]; //+1 pour avoir la place du '\0' // +1 stay space for the '\0'
- Adafruit_MCP23017 mcp;
- #define ReadSoilSensor A0 //define analog Pin A0 read sensors data
- #define SoilS1EnablePin1 D3 //Connect Sens1 to Pin D3 on the ESP
- #define tap_solenoid1 D4 //Connect tap1 for the Sens1 to D6
- #define SoilS1EnablePin2 D5 //Connect Sens2 to Pin D5 on the ESP
- #define tap_solenoid2 D6 //Connect tap2 for the Sens2 to D6
- #define SoilS1EnablePin3 D7 //Connect Sens3 to pin D7 on the ESP
- #define tap_solenoid3 D8 //Connect tap3 for Sens3 to ESP
- #define SoilS1EnablePin4 0 //Connect Sens4 to Pin GPA0 on the mcp
- #define tap_solenoid4 1 //Connect tap4 for Sens4 to MCP GPA1
- #define SoilS1EnablePin5 2 //Connect Sens5 to MCP GPA2
- #define tap_solenoid5 3 //Connect tap5 for Sens5 to MCP GPA3
- #define SoilS1EnablePin6 4 //Connect Sensor 6 to MCP GPA4
- #define tap_solenoid6 5 //Connect tap6 for Sens6 to MCP GPA5
- #define SoilS1EnablePin7 6 //Connect Sensor 7 to MCP GPA6
- #define tap_solenoid7 7 //Connect tap7 for SENS 777789mcp GPA7
- #define PumpPin D0 //Connect pump pin to ESP D0
- //Configure the soil moisture
- const int MintSoilMoisture = 250;//Menth
- const int ParsleySoilMoisture = 230;//Persil
- const int BasilSoilMoisture = 260;//Basilic
- const int CorianderSoilMoisture = 300;//Coriande, Cousbara
- const int DillSoilMoisture = 2700;//Aneth, shamir
- const int SageSoilMoisture = 310;//Sauge, Marva
- const int rosemarySoilMoisture = 200;//Romarin
- 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
- int PlantS1,PlantS2,PlantS3,PlantS4,PlantS5,PlantS6,PlantS7; //Keep Selected Plant by User from WebForm
- 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
- const int onTime = 1000; // in ms
- const int offTime = 5000; // in ms
- boolean currentlyOn = false;
- unsigned long startTime;
- void printHTTPServerInfo()
- {
- Serial.print(F("Site web http: "));
- Serial.print(WiFi.localIP());
- if (HTTPPort != 80)
- {
- Serial.print(F(":"));
- Serial.print(HTTPPort);
- }
- Serial.println();
- }
- #include "config_json_file.h" // include the function for saving and loading config.json
- #include "spiffs_page_display.h" // include the function to display web page
- #include "waterControl.h" // include the irrigation system
- /**
- * Read Datas Sensors Pins connected to ESP
- */
- int read_Sensors_Values(int EnableSoilPin)
- {
- int ReadSensor;
- digitalWrite(EnableSoilPin,HIGH);
- for(int SensorReading = 0;SensorReading < 5;SensorReading++)
- {
- ReadSensor = analogRead(ReadSoilSensor);
- }
- digitalWrite(EnableSoilPin,LOW);
- return ReadSensor;
- }
- /**
- * Read Datas Sensors Pins connected to MCP
- */
- int read_mcp_sensor_values(int EnableSoilPin)
- {
- int ReadSensor;
- mcp.digitalWrite(EnableSoilPin,HIGH);
- for(int SensorReading = 0;SensorReading < 5;SensorReading++)
- {
- ReadSensor = analogRead(ReadSoilSensor);
- }
- mcp.digitalWrite(EnableSoilPin,LOW);
- return ReadSensor;
- }
- /**
- * Assign the correct Soil Target for the Plant Sensor connected after user choice from the WebForm
- */
- int SoilSensorWebUser(int WebSoilSensor)
- {
- switch(WebSoilSensor)
- {
- case 0:
- return(0);
- break;
- case 1:
- return(MintSoilMoisture);
- break;
- case 2:
- return(ParsleySoilMoisture);
- break;
- case 3:
- return(BasilSoilMoisture);
- break;
- case 4:
- return(CorianderSoilMoisture);
- break;
- case 5:
- return(DillSoilMoisture);
- break;
- case 6:
- return(SageSoilMoisture);
- break;
- case 7:
- return(rosemarySoilMoisture);
- break;
- }
- }
- //Define GPIO for Input/Output
- void PinsConfiguration()
- {
- pinMode(SoilS1EnablePin1,OUTPUT);
- pinMode(tap_solenoid1,OUTPUT);
- pinMode(SoilS1EnablePin2,OUTPUT);
- pinMode(tap_solenoid2,OUTPUT);
- pinMode(SoilS1EnablePin3,OUTPUT);
- pinMode(tap_solenoid3,OUTPUT);
- mcp.pinMode(SoilS1EnablePin4, OUTPUT);
- mcp.pinMode(tap_solenoid4, OUTPUT);
- mcp.pinMode(SoilS1EnablePin5, OUTPUT);
- mcp.pinMode(tap_solenoid5, OUTPUT);
- mcp.pinMode(SoilS1EnablePin6, OUTPUT);
- mcp.pinMode(tap_solenoid6, OUTPUT);
- mcp.pinMode(SoilS1EnablePin7, OUTPUT);
- mcp.pinMode(tap_solenoid7, OUTPUT);
- pinMode(PumpPin,OUTPUT);
- }
- void setup()
- {
- Serial.begin(115200);
- mcp.begin(); // use default address 0
- PinsConfiguration(); //Define GPIO for Input/Output
- Serial.println("\n\nMounting FS...\n");
- // on démarre le SPIFSS
- if (!SPIFFS.begin())
- {
- Serial.println("erreur SPIFFS");
- while (true); // on ne va pas plus loin
- }
- loadConfig();
- /*if (!loadConfig())
- {Serial.println("Failed to load config");}
- else
- {Serial.println("Config loaded");}*/
- WiFi.begin(ssid, password);
- Serial.println();
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.write('.');
- }
- Serial.println();
- // on démarre le serveur
- serveurWeb.begin();
- printHTTPServerInfo();
- }
- void loop()
- {
- testRequeteWeb();
- // --- read sensors value Every 5sec for 1sec --- //
- if (currentlyOn && millis()>startTime+onTime) // Switch off
- {
- currentlyOn = false;
- startTime=millis(); // Reset timer
- }
- if (!currentlyOn && millis()>startTime+offTime) // Switch on
- {
- //Enable Pin Sensor connected to ESP
- read_s1_value = read_Sensors_Values(SoilS1EnablePin1);
- read_s2_value = read_Sensors_Values(SoilS1EnablePin2);
- read_s3_value = read_Sensors_Values(SoilS1EnablePin3);
- //Enable Pin Sensor connected to MCP
- read_s4_value = read_mcp_sensor_values(SoilS1EnablePin4);
- read_s5_value = read_mcp_sensor_values(SoilS1EnablePin5);
- read_s6_value = read_mcp_sensor_values(SoilS1EnablePin6);
- read_s7_value = read_mcp_sensor_values(SoilS1EnablePin7);
- currentlyOn = true;
- startTime=millis(); // Reset timer
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement