-
Notifications
You must be signed in to change notification settings - Fork 0
/
j_areat_control.ino
97 lines (71 loc) · 2.2 KB
/
j_areat_control.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
/*
This a simple example of the aREST Library for Arduino (Uno/Mega/Due/Teensy)
using the Ethernet library (for example to be used with the Ethernet shield).
See the README file for more details.
Written in 2014 by Marco Schwartz under a GPL license.
*/
// Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <aREST.h>
#include <avr/wdt.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Enter a MAC address for your controller below.
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xFE, 0x40 };
// IP address in case DHCP fails
IPAddress ip(192,168,1,2);
// Ethernet server
EthernetServer server(80);
// Create aREST instance
aREST rest = aREST();
// Variables to be exposed to the API
//int temperature;
//int humidity;
void setup(void)
{
// Start Serial
Serial.begin(115200);
sensors.begin();
// Init variables and expose them to REST API
//temperature = 24;
//humidity = 40;
//rest.variable("temperature",&temperature);
//rest.variable("humidity",&humidity);
// Function to be exposed
rest.function("temp",getTemp);
// Give name and ID to device
rest.set_id("008");
rest.set_name("dapper_drake");
// Start the Ethernet connection and the server
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
// Start watchdog
wdt_enable(WDTO_4S);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
rest.handle(client);
wdt_reset();
}
// Custom function accessible by the API
int getTemp(String command) {
sensors.requestTemperatures();
int temp = sensors.getTempCByIndex(0);
Serial.println(temp);
return temp;
}