Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* New with JSON */
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- #include <DNSServer.h>
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h>
- #include <Wire.h>
- #include "Adafruit_MCP23017.h"
- Adafruit_MCP23017 mcp;
- #define S0 D0 /* Assign Multiplexer pin S0 connect to pin D0 of NodeMCU */
- #define S3 D3 /* Assign Multiplexer pin S3 connect to pin D3 of NodeMCU */
- #define S1 D7 /* Assign Multiplexer pin S1 connect to pin D1 of NodeMCU */
- #define S2 D8 /* Assign Multiplexer pin S2 connect to pin D2 of NodeMCU */
- #define SIG A0 /* Assign SIG pin Analog output for all Multiplexer's channels to A0 of NodeMCU */
- #define SoilHumidity 430 /* Assign the level of the soil humidity to irrigate the plant */
- #define PUMP D6
- #define solenoid0 0 //Connect Solenoid0 for Sens0 GPA0 on mcp
- #define solenoid1 1 //Connect Solenoid1 for Sens1 GPA1 on mcp
- #define solenoid2 2 //Connect Solenoid2 for Sens2 GPA2 on mcp
- #define solenoid3 3 //Connect Solenoid3 for Sens3 GPA3 on mcp
- #define solenoid4 4 //Connect Solenoid4 for Sens4 GPA4 on mcp
- #define solenoid5 5 //Connect Solenoid5 for Sens5 GPA5 on mcp
- #define solenoid6 6 //Connect Solenoid6 for Sens6 GPA6 on mcp
- #define solenoid7 7 //Connect Solenoid7 for Sens7 GPB0 on mcp
- #define solenoid8 8 //Connect Solenoid8 for Sens8 GPB1 on mcp
- #define solenoid9 9 //Connect Solenoid9 for Sens9 GPB2 on mcp
- #define solenoid10 10 //Connect Solenoid10 for Sens10 GPB3 on mcp
- #define solenoid11 11 //Connect Solenoid11 for Sens11 GPB4 on mcp
- #define solenoid12 12 //Connect Solenoid11 for Sens11 GPB5 on mcp
- #define solenoid13 13 //Connect Solenoid11 for Sens11 GPB6 on mcp
- #define solenoid14 14 //Connect Solenoid11 for Sens11 GPB7 on mcp
- #define solenoid15 15 //Connect Solenoid11 for Sens11 GPB8 on mcp
- // int sensors[15];
- int sensor0; /* Assign the name "sensor0" as analog output value from Channel C0 */
- int sensor1; /* Assign the name "sensor1" as analog output value from Channel C1 */
- int sensor2; /* Assign the name "sensor2" as analog output value from Channel C2 */
- int sensor3; /* Assign the name "sensor3" as analog output value from Channel C3 */
- int sensor4; /* Assign the name "sensor4" as analog output value from Channel C4 */
- int sensor5; /* Assign the name "sensor5" as analog output value from Channel C5 */
- int sensor6; /* Assign the name "sensor6" as analog output value from Channel C6 */
- int sensor7; /* Assign the name "sensor7" as analog output value from Channel C7 */
- int sensor8; /* Assign the name "sensor8" as analog output value from Channel C8 */
- int sensor9; /* Assign the name "sensor9" as analog output value from Channel C9 */
- int sensor10; /* Assign the name "sensor10" as analog output value from Channel C10 */
- int sensor11; /* Assign the name "sensor11" as analog output value from Channel C11 */
- int sensor12; /* Assign the name "sensor12" as analog output value from Channel C12 */
- int sensor13; /* Assign the name "sensor13" as analog output value from Channel C13 */
- int sensor14; /* Assign the name "sensor14" as analog output value from Channel C14 */
- int sensor15; /* Assign the name "sensor15" as analog output value from Channel C15 */
- // int SoilHumidity = 525;
- String pumpstate = "OFF";
- String solenoidState0 = "OFF";
- String solenoidState1 = "OFF";
- String solenoidState2 = "OFF";
- String solenoidState3 = "OFF";
- String solenoidState4 = "OFF";
- String solenoidState5 = "OFF";
- String solenoidState6 = "OFF";
- String solenoidState7 = "OFF";
- String solenoidState8 = "OFF";
- String solenoidState9 = "OFF";
- String solenoidState10 = "OFF";
- String solenoidState11 = "OFF";
- String solenoidState12 = "OFF";
- String solenoidState13 = "OFF";
- String solenoidState14 = "OFF";
- String solenoidState15 = "OFF";
- ESP8266WebServer server(80);
- static const char PROGMEM INDEX_HTML[] = R"rawliteral(
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content=meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
- <title>DHT Monitor</title>
- <style type="text/css">
- body{
- padding: 35px;
- background-color: #222222;
- }
- h1{
- color: #FFFFFF;
- font-family: sans-serif;
- }
- p{
- color: #FFFFFF;
- font-family: sans-serif;
- font-size: 18px;
- }
- span{
- padding-left: 80px;
- }
- </style>
- </head>
- <body>
- <h1>Irrigation System Viewer</h1>
- <p id="pumpState"></p>
- <p id="t0"><span id="s0"></span></p>
- <p id="t1"><span id="s0"></span></p>
- <p id="t2"><span id="s2"></span></p>
- <p id="t3"><span id="s3"></span></p>
- <p id="t4"><span id="s4"></span></p>
- <body>
- <script type="text/javascript">
- refresh();
- setInterval(refresh, 1000);
- function refresh(){
- refreshSoilHumidity();
- refreshSolenoid();
- refreshPump();
- }
- function refreshSoilHumidity(){
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == XMLHttpRequest.DONE && xmlhttp.status == 200){
- const parsedData = JSON.parse(xmlhttp.responseText);
- document.getElementById("t0").innerHTML = "SoilHumidity Sensor 0: " + parsedData.t0 + "%";
- document.getElementById("t1").innerHTML = "SoilHumidity Sensor 1: " + parsedData.t1 + "%";
- document.getElementById("t2").innerHTML = "SoilHumidity Sensor 2: " + parsedData.t2 + "%";
- document.getElementById("t3").innerHTML = "SoilHumidity Sensor 3: " + parsedData.t3 + "%";
- document.getElementById("t4").innerHTML = "SoilHumidity Sensor 4: " + parsedData.t4 + "%";
- }
- };
- xmlhttp.open("GET", "/SoilHumidity", true);
- xmlhttp.send();
- }
- function refreshSolenoid(){
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == XMLHttpRequest.DONE && xmlhttp.status == 200){
- const parsedData = JSON.parse(xmlhttp.responseText);
- document.getElementById("s0").innerHTML = "Solenoid0 State 0: " + parsedData.s0;
- document.getElementById("s1").innerHTML = "Solenoid1 State 1: " + parsedData.s1;
- document.getElementById("s2").innerHTML = "Solenoid2 State 2: " + parsedData.s2;
- document.getElementById("s3").innerHTML = "Solenoid3 State 3: " + parsedData.s3;
- document.getElementById("s4").innerHTML = "Solenoid4 State 4: " + parsedData.s4;
- }
- };
- xmlhttp.open("GET", "/Solenoid", true);
- xmlhttp.send();
- }
- function refreshPump(){
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == XMLHttpRequest.DONE && xmlhttp.status == 200){
- document.getElementById("pumpState").innerHTML = "Pump State: " + this.responseText;
- }
- };
- xmlhttp.open("GET", "/pump", true);
- xmlhttp.send();
- }
- </script>
- </html>
- )rawliteral";
- void handleRoot(){
- server.send_P(200, "text/html", INDEX_HTML);
- }
- void handleNotFound() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i = 0; i < server.args(); i++) {
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- }
- // read soil humiduty and display in html
- void getSoilHumidity() {
- // Channel 0 (C0 pin - binary output 0,0,0,0)
- digitalWrite(S0,LOW); digitalWrite(S1,LOW); digitalWrite(S2,LOW); digitalWrite(S3,LOW);
- sensor0 = analogRead(SIG);
- // Channel 1 (C1 pin - binary output 1,0,0,0)
- digitalWrite(S0,HIGH); digitalWrite(S1,LOW); digitalWrite(S2,LOW); digitalWrite(S3,LOW);
- sensor1 = analogRead(SIG);
- // Channel 2 (C2 pin - binary output 0,1,0,0)
- digitalWrite(S0,LOW); digitalWrite(S1,HIGH); digitalWrite(S2,LOW); digitalWrite(S3,LOW);
- sensor2 = analogRead(SIG);
- // Channel 3 (C3 pin - binary output 1,1,0,0)
- digitalWrite(S0,HIGH); digitalWrite(S1,HIGH); digitalWrite(S2,LOW); digitalWrite(S3,LOW);
- sensor3 = analogRead(SIG);
- // Channel 4 (C4 pin - binary output 0,0,1,0)
- digitalWrite(S0,LOW); digitalWrite(S1,LOW); digitalWrite(S2,HIGH); digitalWrite(S3,LOW);
- sensor4 = analogRead(SIG);
- // Channel 5 (C5 pin - binary output 1,0,1,0)
- digitalWrite(S0,HIGH); digitalWrite(S1,LOW); digitalWrite(S2,HIGH); digitalWrite(S3,LOW);
- sensor5 = analogRead(SIG);
- // Channel 6 (C6 pin - binary output 0,1,1,0)
- digitalWrite(S0,LOW); digitalWrite(S1,HIGH); digitalWrite(S2,HIGH); digitalWrite(S3,LOW);
- sensor6 = analogRead(SIG);
- // Channel 7 (C7 pin - binary output 1,1,1,0)
- digitalWrite(S0,HIGH); digitalWrite(S1,HIGH); digitalWrite(S2,HIGH); digitalWrite(S3,LOW);
- sensor7 = analogRead(SIG);
- // Channel 8 (C8 pin - binary output 0,0,0,1)
- digitalWrite(S0,LOW); digitalWrite(S1,LOW); digitalWrite(S2,LOW); digitalWrite(S3,HIGH);
- sensor8 = analogRead(SIG);
- // Channel 9 (C9 pin - binary output 1,0,0,1)
- digitalWrite(S0,HIGH); digitalWrite(S1,LOW); digitalWrite(S2,LOW); digitalWrite(S3,HIGH);
- sensor9 = analogRead(SIG);
- // Channel 10 (C10 pin - binary output 0,1,0,1)
- digitalWrite(S0,LOW); digitalWrite(S1,HIGH); digitalWrite(S2,LOW); digitalWrite(S3,HIGH);
- sensor10 = analogRead(SIG);
- // Channel 11 (C11 pin - binary output 1,1,0,1)
- digitalWrite(S0,HIGH); digitalWrite(S1,HIGH); digitalWrite(S2,LOW); digitalWrite(S3,HIGH);
- sensor11 = analogRead(SIG);
- // Channel 12 (C12 pin - binary output 0,0,1,1)
- digitalWrite(S0,LOW); digitalWrite(S1,LOW); digitalWrite(S2,HIGH); digitalWrite(S3,HIGH);
- sensor12 = analogRead(SIG);
- // Channel 13 (C13 pin - binary output 1,0,1,1)
- digitalWrite(S0,HIGH); digitalWrite(S1,LOW); digitalWrite(S2,HIGH); digitalWrite(S3,HIGH);
- sensor13 = analogRead(SIG);
- // Channel 14 (C14 pin - binary output 0,1,1,1)
- digitalWrite(S0,LOW); digitalWrite(S1,HIGH); digitalWrite(S2,HIGH); digitalWrite(S3,HIGH);
- sensor14 = analogRead(SIG);
- // Channel 15 (C15 pin - binary output 1,1,1,1)
- digitalWrite(S0,HIGH); digitalWrite(S1,HIGH); digitalWrite(S2,HIGH); digitalWrite(S3,HIGH);
- sensor15 = analogRead(SIG);
- sensor1 = 532;
- sensor2 = 345;
- sensor3 = 555;
- sensor4 = 269;
- // Serial.println(sensor0);
- String json = "{\"t0\":" + String(sensor0) + ",\"t1\":" + String(sensor1)+ ",\"t2\":" + String(sensor2)+ ",\"t3\":" + String(sensor3)+ ",\"t4\":" + String(sensor4)+ "}";
- server.send (200, "application/json", json);
- }
- // Read Solenoid status and display status in html
- void driveSolenoid() {
- solenoidState0 = solneoidStatus(sensor0, solenoid0);
- solenoidState1 = solneoidStatus(sensor1, solenoid1);
- solenoidState2 = solneoidStatus(sensor2, solenoid2);
- solenoidState3 = solneoidStatus(sensor3, solenoid3);
- solenoidState4 = solneoidStatus(sensor4, solenoid4);
- solenoidState5 = solneoidStatus(sensor5, solenoid5);
- solenoidState6 = solneoidStatus(sensor6, solenoid6);
- solenoidState7 = solneoidStatus(sensor7, solenoid7);
- solenoidState8 = solneoidStatus(sensor8, solenoid8);
- solenoidState9 = solneoidStatus(sensor9, solenoid9);
- solenoidState12 = solneoidStatus(sensor10, solenoid10);
- solenoidState11 = solneoidStatus(sensor11, solenoid11);
- solenoidState12 = solneoidStatus(sensor12, solenoid12);
- solenoidState13 = solneoidStatus(sensor13, solenoid13);
- solenoidState14 = solneoidStatus(sensor14, solenoid14);
- solenoidState15 = solneoidStatus(sensor15, solenoid15);
- String json1 = "{\"s0\":" + String(solenoidState0) + ",\"s1\":" + String(solenoidState1)+ ",\"s2\":" + String(solenoidState2)+ ",\"s3\":" + String(solenoidState3)+ ",\"s4\":" + String(solenoidState4)+ "}";
- server.send (200, "application/json", json1);
- }
- // Drive the Solenoid
- String solneoidStatus(int sensor, int solenoidPin) {
- String solenoidState;
- if(sensor <= SoilHumidity) {
- solenoidState = "OFF";
- mcp.digitalWrite(solenoidPin,LOW);
- }
- else {
- solenoidState = "ON";
- mcp.digitalWrite(solenoidPin,HIGH);
- }
- return(solenoidState);
- }
- // Drive the pump and display status in html
- void drivePump() {
- if(solenoidState0 == "OFF" && solenoidState1 == "OFF" && solenoidState2 == "OFF" && solenoidState3 == "OFF" &&
- solenoidState4 == "OFF" && solenoidState5 == "OFF" && solenoidState6 == "OFF" && solenoidState7 == "OFF" &&
- solenoidState8 == "OFF" && solenoidState9 == "OFF" && solenoidState10 == "OFF" && solenoidState11 == "OFF" &&
- solenoidState12 == "OFF" && solenoidState13 == "OFF" && solenoidState14 == "OFF" && solenoidState15 == "OFF") {
- if(pumpstate == "ON") {
- pumpstate = "OFF";
- digitalWrite(PUMP,LOW);
- }
- }
- else {
- if(pumpstate == "OFF") {
- pumpstate = "ON";
- digitalWrite(PUMP,HIGH);
- }
- }
- server.send(200, "text/plain", pumpstate);
- }
- void setup() {
- Serial.begin(115200);
- mcp.begin();
- pinMode(S0,OUTPUT); /* Define digital pin as output to the Multiplexer pin SO */
- pinMode(S1,OUTPUT); /* Define digital pin as output to the Multiplexer pin S1 */
- pinMode(S2,OUTPUT); /* Define digital pin as output to the Multiplexer pin S2 */
- pinMode(S3,OUTPUT); /* Define digital pin as output to the Multiplexer pin S3 */
- pinMode(SIG, INPUT); /* Define analog pin as input from the Multiplexer pin SIG */
- pinMode(PUMP,OUTPUT); /* Define digital pin os output to control pump */
- mcp.pinMode(solenoid0, OUTPUT);
- mcp.pinMode(solenoid1, OUTPUT);
- mcp.pinMode(solenoid2, OUTPUT);
- mcp.pinMode(solenoid3, OUTPUT);
- mcp.pinMode(solenoid4, OUTPUT);
- mcp.pinMode(solenoid5, OUTPUT);
- mcp.pinMode(solenoid6, OUTPUT);
- mcp.pinMode(solenoid7, OUTPUT);
- mcp.pinMode(solenoid8, OUTPUT);
- mcp.pinMode(solenoid9, OUTPUT);
- mcp.pinMode(solenoid10, OUTPUT);
- mcp.pinMode(solenoid11, OUTPUT);
- mcp.pinMode(solenoid12, OUTPUT);
- mcp.pinMode(solenoid13, OUTPUT);
- mcp.pinMode(solenoid14, OUTPUT);
- mcp.pinMode(solenoid15, OUTPUT);
- digitalWrite(PUMP,LOW);
- mcp.digitalWrite(solenoid0,LOW);
- mcp.digitalWrite(solenoid1,LOW);
- mcp.digitalWrite(solenoid2,LOW);
- mcp.digitalWrite(solenoid3,LOW);
- mcp.digitalWrite(solenoid4,LOW);
- mcp.digitalWrite(solenoid5,LOW);
- mcp.digitalWrite(solenoid6,LOW);
- mcp.digitalWrite(solenoid7,LOW);
- mcp.digitalWrite(solenoid8,LOW);
- mcp.digitalWrite(solenoid9,LOW);
- mcp.digitalWrite(solenoid10,LOW);
- mcp.digitalWrite(solenoid11,LOW);
- mcp.digitalWrite(solenoid12,LOW);
- mcp.digitalWrite(solenoid13,LOW);
- mcp.digitalWrite(solenoid14,LOW);
- mcp.digitalWrite(solenoid15,LOW);
- WiFiManager wifiManager;
- wifiManager.autoConnect("AutoConnectAP");
- Serial.println("Yourconnected...yeey :)");
- server.on("/", handleRoot);
- server.on("/SoilHumidity", HTTP_GET, getSoilHumidity);
- server.on("/Solenoid", HTTP_GET, driveSolenoid);
- server.on("/pump", HTTP_GET, drivePump);
- server.onNotFound(handleNotFound); /* page not found */
- server.begin();
- Serial.println("HTTP server started");
- Serial.println(WiFi.localIP());
- }
- void loop()
- {
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement