forked from CobaltBlue-Team7/IoTProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trash_bin.ino
169 lines (144 loc) · 4.66 KB
/
trash_bin.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
//Time interval between measuring distance.
int interval = 15000; // One Minute
// Data regarding sensor
//int sonarPin = A0;
//int sensorValue;
const int trigPin = D1;
const int echoPin = D2;
// defines variables
long duration;
int distance;
int first_mea;
int temp_mea;
int height_bin = -1;
// Information needed to connect to Wifi
const char* ssid = "AS712a";
const char* password = "dcclab(!)";
// Information needed to connect to ThingSpeak
const char* host = "api.thingspeak.com";
String url = "/update?api_key=LUIAXBEYKEWW2B6R";
const int httpPort = 80;
// Information needed to connect to the server(Ubuntu VM @ Sogang Univ)
const char* private_server = "163.239.78.89";
const int serverPort = 5000;
const int serverPort2 = 8000;
// Get distance data from sensor
String working(int trigger, int echo) {
/*sensorValue=analogRead(sonarPin);
delay(50);
Serial.println(sensorValue);
delay(100);
return String(sensorValue);*/
// Clears the trigPin
digitalWrite(trigger, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
Serial.print("check height_bin :");
Serial.println(height_bin);
if(height_bin == -1 && distance <= 100) { // 쓰레기통 높이 결정
height_bin = distance;
Serial.println("height_bin : ");
Serial.print(height_bin);
return String(0.0);
}
if(distance <= height_bin) {
Serial.println("Distance : ");
Serial.print(distance);
return String(((float)height_bin-(float)distance) / (float)height_bin * 100.0);
}
distance = -1;
return String(-1);
}
// Send the sensor data to
void delivering(String payload, int bin_num) {
String getheader;
WiFiClient client;
Serial.print("\nconnecting to Host: ");
Serial.println(host);
//Connect to ThingSpeak Server
if (client.connect(host, httpPort)) {
//http call to server by using GET Method.
getheader = "GET "+ String(url) +"&field1="+ String(payload) +" HTTP/1.1";
client.println(getheader);
client.println("User-Agent: ESP8266");
client.println("Host: " + String(host));
client.println("Connection: close");
client.println();
Serial.println(getheader);//To Check
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
else{
Serial.print("connection failed to ");
Serial.println(host);
}
Serial.print("\nconnecting to Host: ");
Serial.println(private_server);
//Connect to the server
if(client.connect(private_server,serverPort)){
//http call to server by using GET Method.
String getheader = "GET /?temp="+ String(payload) +" HTTP/1.1";
client.println(getheader);
client.println("User-Agent: ESP8266");
client.println("Host: " + String(private_server));
client.println("Connection: close");
client.println();
Serial.println(getheader);//To Check
while (client.connected()) {
String line = client.readStringUntil('\n');
Serial.println(line);
}
}
else{
Serial.print("connection failed to ");
Serial.println(private_server);
}
Serial.println("Done cycle.");
}
//Connect to WiFi
void connect_ap() {
Serial.println();
Serial.print("connecting to WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("\n Got WiFi, IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
//pinMode(sonarPin,INPUT);
//Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
connect_ap(); // connect to WiFi
Serial.println();
Serial.println("Arduino: Measure The Amount of Trash");
//sensors.begin();
}
unsigned long mark = 0;
void loop() {
if (millis() > mark ) {
mark = millis() + interval;
String payload = working(trigPin, echoPin); // get sensor data
if(distance != -1)
delivering(payload, 1); // deliver it to Thingspeak and Linux server via WiFi
}
}