-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalertlight.ino
More file actions
97 lines (72 loc) · 2.47 KB
/
alertlight.ino
File metadata and controls
97 lines (72 loc) · 2.47 KB
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
#include <math.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "******";
const char* password = "******";
const int redPin = 5;
const int greenPin = 4;
const int bluePin = 0;
float duration, distance, OldDistance, AverageVelocity;
int trigPin = 14;
int echoPin = 12;
void setup()
{
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print("Connecting..");
}
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//Kijk of old_distance al is gedefineerd
if (OldDistance != 0)
{
if (distance < 200)
{
//Berekening snelheid.
AverageVelocity = ((OldDistance - distance) / 0.200) * 3.6;
if (AverageVelocity > 130 && AverageVelocity < 250)
{
int TooHardVelocity = round(AverageVelocity);
Serial.print(TooHardVelocity);
Serial.println(" km/h");
digitalWrite(redPin,255);
digitalWrite(greenPin, 0);
digitalWrite(bluePin, 0);
//POST REQUEST
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
HTTPClient http; //Declare an object of class HTTPClient
http.begin("http://api.alertlight.nl/api/speed/new/1/"+String((TooHardVelocity / 2))); //Specify request destination
int httpCode = http.GET(); //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
}
} else
{
digitalWrite(redPin, 255);
digitalWrite(greenPin, 255);
digitalWrite(bluePin, 255);
}
}
}
OldDistance = distance;
delay(200);
}