I am working on a water optimization project. The details are mentioned below.
I am using
3- Water flow sensors YF-S201
1- Water Pressure sensor(Pressure Gauge Transducer G1/4" Sensor Input 5V Output 0.5-4.5V / 0-5V)
1- Ultrasonic Distance Sensor - HC-SR04
6- Solenoid Valves. controlled by relays
1- Relay to shut down the system in case of emergency
1- ESP8266 WiFi module to send data to the database
Arduino MEGA 2560 as a microcontroller
I have tested the code offline and it is working fine. I am willing to send my sensor's data and relay status to the cloud and want to read my data on my website. I also wish that I could control my relays from my website too.
I am a beginner in Arduino and programming, I have tried too many tutorials, teaching how to send data from Arduino to the database via ESP8266. I was not successful at all in sending my Arduino data to my website. I was wondering if someone could modify my code and help me out to send the data to my website. It would be a great help.
Here, is my code.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD"
// WATER FLOW SENSORS DEFINATION
volatile int flow_frequency20, flow_frequency18, flow_frequency19 ; // Measures flow sensor pulses
unsigned int l_hour20, l_hour18, l_hour19; // Calculated litres/hour
const byte flowsensor20 = 20; // Sensor Input 1
const byte flowsensor18 = 18; // Sensor Input 2
const byte flowsensor19 = 19; // Sensor Input 3
unsigned long currentTime;
unsigned long cloopTime;
//------------------------------------------------------------------
void flow20 () // Interrupt function
{
flow_frequency20++;
}
//------------------------------------------------------------------
void flow18 () // Interrupt function
{
flow_frequency18++;
}
//------------------------------------------------------------------
void flow19 () // Interrupt function
{
flow_frequency19++;
}
//------------------------------------------------------------------
//Water Pressure Sensor Defination
const float OffSet = 0.483 ;
float V, P;
// PIN DEFINED FOR WATER PRESSURE IS ANALAG PIN 0
// -------------------------------------------------
// Water Level Sensor Definition
#define echoPin 9 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 8 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
//--------------------------------------------------------------
// SOLENOID VALVE PINS DEFINATION
int sv_1 = 22;
int sv_2 = 24;
int sv_3 = 26;
int sv_4 = 28;
int sv_5 = 30;
int sv_6 = 32;
//---------------------------------------------------------------
//EMERGENCY SHUTDOWN RELAY DEFINATION
int esr= 50;
//-----------------------------------------------------------------
void setup() {
Serial.begin(115200);
// WATER FLOW SENSORS SETUP
pinMode(flowsensor20, INPUT_PULLUP); // Optional Internal Pull-Up
pinMode(flowsensor18, INPUT_PULLUP); // Optional Internal Pull-Up
pinMode(flowsensor19, INPUT_PULLUP); // Optional Internal Pull-Up
attachInterrupt(digitalPinToInterrupt(flowsensor20), flow20, RISING); // Setup Interrupt
attachInterrupt(digitalPinToInterrupt(flowsensor18), flow18, RISING); // Setup Interrupt
attachInterrupt(digitalPinToInterrupt(flowsensor19), flow19, RISING); // Setup Interrupt
currentTime = millis();
cloopTime = currentTime;
//------------------------------------------------------------------
//Water level sensor Setup
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
// ---------------------------------------------------------------------
//SOLENOID VALVE SETUP
pinMode(sv_1, OUTPUT);
pinMode(sv_2, OUTPUT);
pinMode(sv_3, OUTPUT);
pinMode(sv_4, OUTPUT);
pinMode(sv_5, OUTPUT);
pinMode(sv_6, OUTPUT);
//------------------------------------------------------------------------
//EMERGENCY SHUTDOWN SYSTEM SETUP
pinMode(esr, OUTPUT);
}
//--------------------------------------------------------------------------
void loop() {
// WATER FLOW SENSORS LOOP CODE
currentTime = millis();
// Every second, calculate and print litres/hour
if (currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 23Q, Q is flow rate in L/min.
l_hour20 = ((flow_frequency20 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
flow_frequency20 = 0; // Reset Counter
Serial.print(l_hour20, DEC); // Print litres/hour
Serial.println(" L/hour 20 ");
l_hour18 = ((flow_frequency18 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
flow_frequency18 = 0; // Reset Counter
Serial.print(l_hour18, DEC); // Print litres/hour
Serial.println(" L/hour 18 ");
l_hour19 = ((flow_frequency19 * 60 / 23) * 1.1); // (Pulse frequency x 60 min) / 23Q = flowrate in L/hour
flow_frequency19 = 0; // Reset Counter
Serial.print(l_hour19, DEC); // Print litres/hour
Serial.println(" L/hour 19 ");
Serial.print(l_hour20+l_hour19+l_hour18);
Serial.println(" Total Water Volume Liters/Hour");
//---------------------------------------------------------------------------------
}
// Code for Water Pressure Sensor
V = analogRead(0) * 5.00 / 1024; //Sensor output voltage
P = (V - OffSet) * 250; //Calculate water pressure
Serial.print("Voltage:");
Serial.print(V, 3);
Serial.println("V");
Serial.print(" Pressure:");
Serial.print(P, 1);
Serial.println(" KPa");
Serial.println();
delay(500);
// WATER LEVEL SENSOR LOOP CODE
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delay(500);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delay(1000);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Water Level: ");
Serial.print(distance);
Serial.println(" cm");
//----------------- OPTIMIZATION
if(l_hour20>400){
digitalWrite(sv_1, LOW);
digitalWrite(sv_2, HIGH);
Serial.println(" Water Optimization has started on Line 1");
}
else if (l_hour20<400){
digitalWrite(sv_2, LOW);
digitalWrite(sv_1, HIGH);
Serial.println("Water Flow is within normal range on Line-1 , No optimization is required");
delay(1000);
}
//----------------------------------------------------------------------------------
if(l_hour19>400){
digitalWrite(sv_3, LOW);
digitalWrite(sv_4, HIGH);
Serial.println(" Water Optimization has started on Line 2");
}
else if (l_hour20<400){
digitalWrite(sv_4, LOW);
digitalWrite(sv_3, HIGH);
Serial.println("Water Flow is within normal range on Line-2 , No optimization is required");
delay(1000);
}
//------------------------------------------------------------------------------
if(l_hour18>400){
digitalWrite(sv_5, LOW);
digitalWrite(sv_6, HIGH);
Serial.println(" Water Optimization has started on Line 3");
}
else if(l_hour18<400){
digitalWrite(sv_6, LOW);
digitalWrite(sv_5, HIGH);
Serial.println("Water Flow is within normal range on Line-3 , No optimization is required");
delay(1000);
}
}