-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot maj code.txt
118 lines (100 loc) · 3.07 KB
/
iot maj code.txt
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
#include <SoftwareSerial.h>
#define RX 10
#define TX 11
String AP = "GNU/Linux Users' Group";
String PASS = "nitdgplug18";
String API = "0VQ1KLYUH7P0AC19";
String HOST = "api.thingspeak.com";
String PORT = "80";
String field[] = {"field1", "field2"};
int countTrueCommand;
int countTimeCommand;
boolean found = false;
float valSensor = 1;
SoftwareSerial esp8266(RX,TX);
/* sensor var */
int no_of_bins = 2;
int trigPin[2] = {6,8};
int echoPin[2] = {7,9};
float duration;
int speed_of_sound = 350; //Speed of sound in m/s
float distance; //Variable to store distance in meters
int height_of_bin = 21;
float percentage;
void setup() {
Serial.begin(115200);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
int i;
for(i = 0; i < no_of_bins; i++)
{
pinMode(trigPin[i], OUTPUT);
pinMode(echoPin[i], INPUT);
}
}
void loop() {
int i;
String getData = "GET /update?api_key="+ API;
for(i = 0; i < no_of_bins; i++)
{
valSensor = getSensorData(i);
getData = getData + "&" + field[i] + "=" + String(valSensor);
}
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
float getSensorData(int bin_no){
digitalWrite(trigPin[bin_no], LOW);
delayMicroseconds(2000);
digitalWrite(trigPin[bin_no], HIGH);
delayMicroseconds(15);
digitalWrite(trigPin[bin_no], LOW);
duration = pulseIn(echoPin[bin_no], HIGH);
duration = duration/1000000.0; //Convert duration from microseconds to seconds
/*Serial.print("Duration = ");
Serial.println(duration);*/
distance = duration * speed_of_sound;
distance = distance*100; //Convert distance from meters to centimeters
distance = distance/2;
Serial.print("Distance measured by sensor ");
Serial.print(bin_no+1);
Serial.print(" in centimeters = ");
Serial.println(distance);
percentage = (distance/height_of_bin)*100;
delay(1000);
return percentage;
}
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("Success");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}