1 | #include <Wire.h>
|
---|
2 | #include <Ciao.h>
|
---|
3 |
|
---|
4 | #define CONNECTOR "rest"
|
---|
5 | #define SERVER_ADDR "192.168.1.1" // change ip address with your server ip address
|
---|
6 |
|
---|
7 | int buttonState; //this variable tracks the state of the button, low if not pressed, high if pressed
|
---|
8 | int ledState = HIGH; //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 | String command = "/arduino/mode/13/output";
|
---|
12 | int previous_value = LOW;
|
---|
13 |
|
---|
14 | void setup() {
|
---|
15 | Ciao.begin();
|
---|
16 | Ciao.write(CONNECTOR, SERVER_ADDR, command);
|
---|
17 | pinMode(2, INPUT);
|
---|
18 |
|
---|
19 | }
|
---|
20 |
|
---|
21 | void loop() {
|
---|
22 |
|
---|
23 | //sample the state of the button - is it pressed or not?
|
---|
24 | buttonState = digitalRead(2);
|
---|
25 |
|
---|
26 | //filter out any noise by setting a time buffer
|
---|
27 | if ( (buttonState == HIGH) && (previous_value == LOW) && (millis() - lastDebounceTime) > debounceDelay ) {
|
---|
28 | if (ledState == HIGH){
|
---|
29 | command = "/arduino/digital/13/0";
|
---|
30 | ledState = LOW;
|
---|
31 | }
|
---|
32 | else{
|
---|
33 | command = "/arduino/digital/13/1";
|
---|
34 | ledState = HIGH;
|
---|
35 | }
|
---|
36 |
|
---|
37 | lastDebounceTime = millis(); //set the current time
|
---|
38 | CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, command);
|
---|
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 | previous_value = buttonState;
|
---|
50 |
|
---|
51 | }
|
---|