-
Notifications
You must be signed in to change notification settings - Fork 1
/
wqm.ino
147 lines (115 loc) · 3.44 KB
/
wqm.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <String.h>
#include<LiquidCrystal.h>
SoftwareSerial mySerial(9, 10);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define ONE_WIRE_BUS 12 //digital D12 pin for temperature value
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float temp=0.0;
float turbid = 0.0;
int sms_count = 0;
void setup()
{
sensors.begin();
mySerial.begin(9600); // the GPRS baud rate
Serial.begin(9600); // the GPRS baud rate
lcd.begin(16,2); // lcd begin
delay(1000);
}
void loop()
{
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);
turbid = analogRead(A1);// read the input on analog pin 0:
turbid = turbid * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
lcd.setCursor(0,0);
lcd.print("Temp in C= "); //display temp
lcd.print(temp);
lcd.setCursor(0,1);
lcd.print("Turbidity = ");//display turbidity
lcd.print(turbid);
delay(2);
if(temp>40||turbid<1.5) //conditions for temp and turbidity alert
{
SetAlert(); // Function to send SMS Alerts
}
Send_data();
if (mySerial.available())
Serial.write(mySerial.read());
}
void Send_data() //to send data to thingspeak channel
{
mySerial.println("AT");
delay(1000);
mySerial.println("AT+CPIN?");
delay(1000);
mySerial.println("AT+CREG?");
delay(1000);
mySerial.println("AT+CGATT?");
delay(1000);
mySerial.println("AT+CIPSHUT");
delay(1000);
mySerial.println("AT+CIPSTATUS");
delay(2000);
mySerial.println("AT+CIPMUX=0");
delay(2000);
ShowSerialData();
mySerial.println("AT+CSTT=\"www\"");//start task and setting the APN,
delay(1000);
ShowSerialData();
mySerial.println("AT+CIICR");//bring up wireless connection
delay(3000);
ShowSerialData();
mySerial.println("AT+CIFSR");//get local IP adress
delay(2000);
ShowSerialData();
mySerial.println("AT+CIPSPRT=0");
delay(3000);
ShowSerialData();
mySerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
delay(6000);
ShowSerialData();
mySerial.println("AT+CIPSEND");//begin send data to remote server
delay(4000);
ShowSerialData();
String str="GET https://api.thingspeak.com/update?api_key=SALKQM2665XVB256&field1=" + String(temp)+"&field2=" + String(turbid);
mySerial.println(str);//begin send data to remote server
delay(4000);
ShowSerialData();
mySerial.println((char)26);//sending
delay(5000);//waitting for reply, the time is base on the condition of internet
mySerial.println();
ShowSerialData();
mySerial.println("AT+CIPSHUT");//close the connection
delay(100);
ShowSerialData();
}
void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}
void SetAlert()
{
while(sms_count<3) //Number of SMS Alerts to be sent
{
SendTextMessage(); // Function to send AT Commands to GSM module
}
}
void SendTextMessage()
{
mySerial.println("AT+CMGF=1"); //To send SMS in Text Mode
delay(2000);
mySerial.println("AT+CMGS=\"+91XXXXXXXXXX\"\r"); // change to the phone number you using
delay(2000);
mySerial.println("Alert for Temperature and Turbidity! ");//the content of the message
mySerial.println(temp);
mySerial.println(turbid);
delay(200);
mySerial.println((char)26);//the stopping character
delay(5000);
sms_count++;
}