my code is :
#include "DHT.h"
#define DHTPIN 15 // Pin where DHT sensor is connected
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define THINGSPEAK_API_KEY "382U4EOXANOKEW3I"
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>
SoftwareSerial myserial(10, 11); // RX, TX for GSM communication
// Temperature Sensor Setup
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Flow Sensor Setup
#define SENSOR_PIN 2
volatile byte pulseCount = 0;
float flowRate = 0.0;
unsigned int flowMilliLitres = 0;
unsigned long totalMilliLitres = 0;
unsigned long oldTime = 0;
float calibrationFactor = 5.5; // Calibration factor for flow meter
// Turbidity Sensor Setup
int turbiditySensorValue;
float voltage;
// Variables for DHT sensor
float temperatureC;
float temperatureF;
void setup() {
Serial.begin(9600);
myserial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
digitalWrite(SENSOR_PIN, HIGH);
pulseCount = 0;
flowRate = 0.0;
flowMilliLitres = 0;
totalMilliLitres = 0;
oldTime = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), pulseCounter, FALLING);
sensors.begin(); // Initialize temperature sensor
dht.begin(); // Initialize DHT sensor
// GSM Initialization with better error checking
initGSM();
}
void initGSM() {
Serial.println("Initializing GSM...");
// Wait for GSM module to respond
while (!sendATCommand("AT", "OK", 1000)) {
Serial.println("Waiting for GSM module...");
delay(1000);
}
sendATCommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATCommand("AT+SAPBR=3,1,\"APN\",\"your_apn\"", "OK", 2000); // Change APN if needed
sendATCommand("AT+SAPBR=1,1", "OK", 2000);
sendATCommand("AT+SAPBR=2,1", "OK", 2000);
}
bool sendATCommand(const char* command, const char* expected_answer, unsigned int timeout) {
Serial.println("Sending command: " + String(command));
myserial.println(command);
String response = "";
unsigned long previous = millis();
while (millis() - previous < timeout) {
while (myserial.available()) {
char c = myserial.read();
response += c;
}
if (response.indexOf(expected_answer) >= 0) {
Serial.println("Response: " + response); // Print full response
return true;
}
}
Serial.println("Timeout! No response or unexpected response: " + response);
return false;
}
void loop() {
flowMeter();
temperature();
turbidity();
sendToThingSpeak();
delay(15000); // 15 second delay between readings
}
void flowMeter() {
if ((millis() - oldTime) > 1000) {
detachInterrupt(digitalPinToInterrupt(SENSOR_PIN));
flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
oldTime = millis();
flowMilliLitres = (flowRate / 60) * 1000;
totalMilliLitres += flowMilliLitres;
Serial.print("Flow rate: ");
Serial.print(flowRate, 2); // Print with 2 decimal places
Serial.println(" L/min");
pulseCount = 0;
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), pulseCounter, FALLING);
}
}
void pulseCounter() {
pulseCount++;
}
void temperature() {
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
temperatureF = sensors.toFahrenheit(temperatureC);
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println("°C");
}
void turbidity() {
turbiditySensorValue = analogRead(A0);
voltage = turbiditySensorValue * (5.0 / 1024.0);
Serial.print("Turbidity Voltage: ");
Serial.println(voltage, 2); // Print with 2 decimal places
}
void sendToThingSpeak() {
// Check if GSM is connected
if (!sendATCommand("AT+SAPBR=2,1", "OK", 2000)) {
Serial.println("GSM Network Issue! Not sending data.");
return;
}
// Close any existing HTTP connection
sendATCommand("AT+HTTPTERM", "OK", 1000);
delay(1000);
// Initialize HTTP service
if (!sendATCommand("AT+HTTPINIT", "OK", 2000)) {
Serial.println("HTTP init failed");
return;
}
sendATCommand("AT+HTTPPARA=\"CID\",1", "OK", 1000);
// Construct URL properly
String url = "http://api.thingspeak.com/update?api_key=";
url += THINGSPEAK_API_KEY;
url += "&field1=" + String(temperatureC);
url += "&field2=" + String(voltage);
url += "&field3=" + String(flowRate);
Serial.println("Generated URL: " + url); // Print full URL before sending
// Send URL parameter properly
String command = "AT+HTTPPARA=\"URL\",\"" + url + "\"";
if (!sendATCommand(command.c_str(), "OK", 2000)) {
Serial.println("Setting URL failed");
return;
}
// Start HTTP GET request
if (!sendATCommand("AT+HTTPACTION=0", "+HTTPACTION: 0,200", 5000)) {
Serial.println("HTTP GET command failed");
return;
}
delay(5000); // Wait for response
// Read HTTP response
if (!sendATCommand("AT+HTTPREAD", "OK", 5000)) {
Serial.println("Failed to read HTTP response");
return;
}
Serial.println("Data sent successfully!");
// Close HTTP connection
sendATCommand("AT+HTTPTERM", "OK", 1000);
}