[175] | 1 | #include <Wire.h>
|
---|
| 2 | #include <Ciao.h>
|
---|
| 3 |
|
---|
| 4 | #define CONNECTOR "rest"
|
---|
[224] | 5 | #define SERVER_ADDR "192.168.1.1" // change ip address with your server ip address
|
---|
[175] | 6 |
|
---|
[224] | 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
|
---|
[175] | 9 | long lastDebounceTime = 0; // the last time the output pin was toggled
|
---|
| 10 | long debounceDelay = 50; // the debounce time; increase if the output flickers
|
---|
[224] | 11 | String command = "/arduino/mode/13/output";
|
---|
| 12 | int previous_value = LOW;
|
---|
[175] | 13 |
|
---|
| 14 | void setup() {
|
---|
| 15 | Ciao.begin();
|
---|
[224] | 16 | Ciao.write(CONNECTOR, SERVER_ADDR, command);
|
---|
[175] | 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);
|
---|
[224] | 25 |
|
---|
[175] | 26 | //filter out any noise by setting a time buffer
|
---|
[224] | 27 | if ( (buttonState == HIGH) && (previous_value == LOW) && (millis() - lastDebounceTime) > debounceDelay ) {
|
---|
| 28 | if (ledState == HIGH){
|
---|
| 29 | command = "/arduino/digital/13/0";
|
---|
| 30 | ledState = LOW;
|
---|
[175] | 31 | }
|
---|
[224] | 32 | else{
|
---|
| 33 | command = "/arduino/digital/13/1";
|
---|
| 34 | ledState = HIGH;
|
---|
[175] | 35 | }
|
---|
[224] | 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 |
|
---|
[175] | 47 | }
|
---|
| 48 |
|
---|
[224] | 49 | previous_value = buttonState;
|
---|
| 50 |
|
---|
[175] | 51 | }
|
---|