-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmov_ethernet.ino
87 lines (62 loc) · 1.62 KB
/
mov_ethernet.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
#include <Ethernet.h>
#include <SPI.h>
#include <ArduinoJson.h>
#include "RestClient.h"
const int PIR = 22;
int pir_lectura = 0;
const int LED = 34;
int buzzer = 8;
#define IP "192.168.0.100"
#define PORT 3000
//String responseMovimiento = "On"; ---- 1
//String responseMovimiento; ---- 2
RestClient client = RestClient(IP, PORT);
void setup()
{
Serial.begin(9600);
Serial.println("connect to network");
client.dhcp();
Serial.print("IP de Placa Ethernet Shield ");
Serial.println(Ethernet.localIP());
pinMode(LED, OUTPUT);
pinMode(PIR, INPUT);
Serial.println("Listo :D!");
}
void loop()
{
pir_lectura = digitalRead(PIR);
String responseMovimiento = "On";
digitalRead(PIR);
if (pir_lectura == HIGH)
{
digitalWrite(LED, HIGH);
Serial.println("Movimiento detectado");
postMov(responseMovimiento);
delay(1000);
}
else
{
digitalWrite(LED, LOW);
Serial.println("No hay movimiento");
delay(500);
}
}
void postMov(String responseMovimiento)
{
//responseMovimiento = "On";
client.setHeader("Content-Type: application/json");
StaticJsonBuffer<200> jsonBuffer;
char json[256];
JsonObject& root = jsonBuffer.createObject();
root["mov"] = responseMovimiento;
Serial.println();
root.printTo(json, sizeof(json));
Serial.println(json);
Serial.println();
int statusCode = client.post("http://localhost:3000/api/movimiento/", json, &responseMovimiento);
Serial.println("Status code from server: ");
Serial.println(statusCode);
Serial.println("responseMovimiento body from server: ");
Serial.println(responseMovimiento);
delay(3000);
}