Good day everyone!
I'm making my research project a hydroponics sensor that collects air and water temperature, humidity, conductivity, and pH, then sends it through a sim900a GSM module. However, when testing it out, the pH part of the code does not work. When I removed the part of the code that collects pH, the device worked as intended. I got the code from the manufacturer's website, but I'm not sure what to change to make it work. Thank you very much!
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include <GravityTDS.h>
#define ONE_WIRE_BUS 5
#define DHTPIN 7
#define DHTTYPE DHT11
#define TdsSensorPin A1
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
SoftwareSerial mySerial(9, 10);
GravityTDS gravityTds;
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
char msg;
char call;
void setup()
{
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
Serial.println("GSM SIM900A BEGIN");
Serial.println("Enter character for control option:");
Serial.println("h : to disconnect a call");
Serial.println("i : to receive a call");
Serial.println("s : to send information");
Serial.println("c : to make a call");
Serial.println("e : to redial");
Serial.println();
delay(100);
dht.begin();
sensors.begin();
gravityTds.setPin(A1);
gravityTds.setAref(5.0);
gravityTds.setAdcRange(1024);
gravityTds.begin();
pinMode(LED,OUTPUT);
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
case 'c':
MakeCall();
break;
case 'h':
HangupCall();
break;
case 'e':
RedialCall();
break;
case 'i':
ReceiveCall();
break;
}
if (mySerial.available()>0)
Serial.write(mySerial.read());
}
void SendMessage()
{
int h = dht.readHumidity();
float t = dht.readTemperature();
float tdsValue = 0;
float temperature = t;
gravityTds.setTemperature(temperature);
gravityTds.update();
tdsValue = gravityTds.getTdsValue();
sensors.requestTemperatures();
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS=\"+639494145111\"\r"); // Replace x with mobile number
mySerial.print(h);// The SMS text you want to send
mySerial.print("% humidity, ");
mySerial.print(t);
mySerial.print(" degrees Celsius air temperature, ");
mySerial.print(sensors.getTempCByIndex(0));
mySerial.print(" degrees Celsius water temperature, ");
mySerial.print(tdsValue,0);
mySerial.println(" ppm, ");
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
pHValue = 3.5*voltage+Offset;
samplingTime=millis();
}
if(millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
mySerial.print(pHValue,2);
mySerial.print(" pH value");
digitalWrite(LED,digitalRead(LED)^1);
printTime=millis();
}
}
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println("Error number for the array to avraging!/n");
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void ReceiveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to recieve a live SMS
delay(1000);
if (mySerial.available()>0)
{
msg=mySerial.read();
Serial.print(msg);
}
}
void MakeCall()
{
mySerial.println("ATD+639494145111;"); // ATDxxxxxxxxxx; -- watch out here for semicolon at the end!!
Serial.println("Calling "); // print response over serial port
delay(1000);
}
void HangupCall()
{
mySerial.println("ATH");
Serial.println("Hangup Call");
delay(1000);
}
void ReceiveCall()
{
mySerial.println("ATA");
delay(1000);
{
call=mySerial.read();
Serial.print(call);
}
}
void RedialCall()
{
mySerial.println("ATDL");
Serial.println("Redialing");
delay(1000);
}