1 | #include <Wire.h>
|
---|
2 | #include <Ciao.h>
|
---|
3 |
|
---|
4 | #define CONNECTOR "rest"
|
---|
5 | #define SERVER_ADDR "192.168.0.100" // change ip address with your server ip address
|
---|
6 |
|
---|
7 | int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed
|
---|
8 | int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
|
---|
9 | long lastDebounceTime = 0; // the last time the output pin was toggled
|
---|
10 | long debounceDelay = 50; // the debounce time; increase if the output flickers
|
---|
11 |
|
---|
12 | void setup() {
|
---|
13 | Ciao.begin();
|
---|
14 | pinMode(2, INPUT);
|
---|
15 |
|
---|
16 | }
|
---|
17 |
|
---|
18 | void loop() {
|
---|
19 |
|
---|
20 | //sample the state of the button - is it pressed or not?
|
---|
21 | buttonState = digitalRead(2);
|
---|
22 |
|
---|
23 | //filter out any noise by setting a time buffer
|
---|
24 | if ( (millis() - lastDebounceTime) > debounceDelay) {
|
---|
25 |
|
---|
26 | //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
|
---|
27 | if ( (buttonState == HIGH) && (ledState < 0) ) {
|
---|
28 |
|
---|
29 | CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/12/1"); //turn LED on
|
---|
30 | ledState = -ledState; //now the LED is on, we need to change the state
|
---|
31 | lastDebounceTime = millis(); //set the current time
|
---|
32 | }
|
---|
33 | else if ( (buttonState == HIGH) && (ledState > 0) ) {
|
---|
34 |
|
---|
35 | CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, "/arduino/digital/12/0"); //turn LED off
|
---|
36 | ledState = -ledState; //now the LED is off, we need to change the state
|
---|
37 | lastDebounceTime = millis(); //set the current time
|
---|
38 |
|
---|
39 | if (!data.isEmpty()){
|
---|
40 | Ciao.println( "State: " + String (data.get(1)) );
|
---|
41 | Ciao.println( "Response: " + String (data.get(2)) );
|
---|
42 | }
|
---|
43 | else{
|
---|
44 | Ciao.println ("Write Error");
|
---|
45 | }
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | }
|
---|