-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathETHEL-LIDAR-Version.ino
276 lines (238 loc) · 8.14 KB
/
ETHEL-LIDAR-Version.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* ETHEL -
* Ultrasonic Sensor HC-SR04 and Arduino to detect stopped vehicle
* Mini ENC28J60
* TFMini
*/
/* Post a message to Twitter */
#include <SPI.h>
//#include <Ethernet.h>
//#include <UIPEthernet.h>
//#include <EtherCard.h>
//#include <Twitter.h>
#include <TimeLib.h>
//#include <time.h>
#include <SoftwareSerial.h>
#include <TFMini.h>
#define CONGESTING_VEHICLE_LANE 40 //Distance to congesting vehicle lane
#define CONGESTING_VEHICLE_FAR_CURB 50 //Distance to congesting vehicle lane far curb
#define VERY_LONG_DELAY_MS 900000 // 15 minutes
#define LONG_DELAY_MS 30000 // 30 seconds
#define SHORT_DELAY_MS 5000 // 5 seconds
static byte myip[] = {192,168,2,100};
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte mygateway[] = {192,168,2,1};
static byte mydns[] = {8,8,8,8};
static byte mymask[] = {255,255,255,0};
static byte session;
//byte Ethernet::buffer[700];
#define TOKEN ""
// Twitter twitter("");
const char website[] PROGMEM = "arduino-tweet.appspot.com";
// Variable for Twitter message
char msg[50];
// Setup software serial port
SoftwareSerial mySerial(3, 7); // Uno RX (TFMINI TX Green), Uno TX (TFMINI RX White)
TFMini tfmini;
// Defines variables
long duration;
long interval = 5000; // Initially set interval to 30 seconds
int congestion_count = 0;
int free_flowing_count = 0;
String interval_code_ran = "false";
String congested = "unknown";
uint16_t distance;
unsigned long previousMillis = 0; // will store last time LED was updated
//byte Ethernet::buffer[700];
//Stash stash;
void setup()
{
Serial.begin(115200); // Starts the serial communication
/*
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("My IP: ", ether.myip);
// ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("Server: ", ether.hisip);
*/
mySerial.begin(TFMINI_BAUDRATE); // Initialize the data rate for the SoftwareSerial port
tfmini.begin(&mySerial); // Initialize the TF Mini sensor
//tfmini.setSingleScanMode(); // Initialize single measurement mode with external trigger
settime();
//ether.staticSetup(myip,mygateway,mydns,mymask);
//ether.dhcpSetup();
//Ethernet.begin(mac, ip);
sprintf(msg, "%d:%d:%d:%d:%d:%d Booting...", hour(now()),minute(now()),second(now()),day(now()),month(now()),year(now()));
Serial.println(msg);
//tweet();
}
void loop()
{
//check_response();
String while_ran = "false";
while (while_ran != "true")
{
check_distance();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you ran
previousMillis = currentMillis;
//check_distance();
while_ran = "true";
}
//check_distance();
}
Serial.println("Still running. Going to check congestion...");
check_congestion();
}
void check_distance()
{
//check_response();
distance = tfmini.getDistance(); // Take one TF Mini distance measurement
//Serial.println(distance);
}
void check_congestion()
{
if (congested != "true" && distance >= CONGESTING_VEHICLE_LANE && distance <= CONGESTING_VEHICLE_FAR_CURB)
{
Serial.println("Possible congesting vehicle.");
congestion_count++;
interval = SHORT_DELAY_MS;
String break_out = "false";
for (int i=0; (i < 5 && break_out != "true"); i++)
{
String while_ran = "false";
while (while_ran != "true" && break_out != "true")
{
check_distance();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you ran
previousMillis = currentMillis;
//check_distance();
if (distance >= CONGESTING_VEHICLE_LANE && distance <= CONGESTING_VEHICLE_FAR_CURB)
{
Serial.println("Possible congesting vehicle.");
congestion_count++;
}
else
{
Serial.println("False alarm");
congestion_count = 0;
interval = LONG_DELAY_MS;
break_out = "true";
//break;
}
while_ran = "true";
}
}
}
if (congestion_count == 6)
{
Serial.println("Confirmed congesting vehicle.");
congested = "true";
congestion_count = 0;
//delay(VERY_LONG_DELAY_MS); //Assume it takes 15 minutes to reopen
interval = LONG_DELAY_MS;
sprintf(msg, "%d:%d:%d:%d:%d:%d Congested.", hour(now()),minute(now()),second(now()),day(now()),month(now()),year(now()));
//tweet();
}
}
if (congested != "false" && distance >= CONGESTING_VEHICLE_FAR_CURB)
{
Serial.println("Possible free flowing traffic.");
free_flowing_count++;
interval = SHORT_DELAY_MS;
String break_out = "false";
for (int i=0; (i < 5 && break_out != "true"); i++)
{
String while_ran = "false";
while (while_ran != "true" && break_out != "true")
{
check_distance();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
// save the last time you ran
previousMillis = currentMillis;
//check_distance();
if (distance >= CONGESTING_VEHICLE_FAR_CURB)
{
Serial.println("Possible free flowing traffic.");
free_flowing_count++;
}
else
{
Serial.println("False alarm");
free_flowing_count = 0;
interval = LONG_DELAY_MS;
break_out = "true";
//break;
}
while_ran = "true";
}
//check_distance();
}
}
if (free_flowing_count == 6)
{
Serial.println("Confirmed free flowing traffic.");
congested = "false";
free_flowing_count = 0;
//delay(VERY_LONG_DELAY_MS); //Assume it takes 15 minutes to reopen
interval = LONG_DELAY_MS;
sprintf(msg, "%d:%d:%d:%d:%d:%d Free flowing.", hour(now()),minute(now()),second(now()),day(now()),month(now()),year(now()));
//tweet();
}
}
}
/*
void tweet()
{
Serial.println("Sending tweet...");
byte sd = stash.create();
//const char tweet[] = "message";
stash.print("token=");
stash.print(TOKEN);
stash.print("&status=");
stash.println(msg);
stash.save();
int stash_size = stash.size();
// Compose the http POST request, taking the headers below and appending
// previously created stash in the sd holder.
Stash::prepare(PSTR("POST http://$F/update HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website, website, stash_size, sd);
// send the packet - this also releases all stash buffers once done
// Save the session ID so we can watch for it in the main loop.
session = ether.tcpSend();
}
*/
void settime()
{
//timeStatus();
setTime(1530375099);
}
/*
void check_response()
{
ether.packetLoop(ether.packetReceive());
const char* reply = ether.tcpReply(session);
if (reply != 0)
{
Serial.println("Got a response!");
Serial.println(reply);
}
}
*/