Changeset 224 for rtos_arduino/trunk/arduino_lib/libraries/Ciao/examples
- Timestamp:
- Apr 30, 2016, 11:29:25 PM (8 years ago)
- Location:
- rtos_arduino/trunk/arduino_lib/libraries/Ciao/examples
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
rtos_arduino/trunk/arduino_lib/libraries/Ciao/examples/CiaoRestClient/CiaoRestClient.ino
r175 r224 3 3 4 4 #define CONNECTOR "rest" 5 #define SERVER_ADDR "192.168. 0.100" // change ip address with your server ip address5 #define SERVER_ADDR "192.168.1.1" // change ip address with your server ip address 6 6 7 int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed8 int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on7 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 9 long lastDebounceTime = 0; // the last time the output pin was toggled 10 10 long debounceDelay = 50; // the debounce time; increase if the output flickers 11 String command = "/arduino/mode/13/output"; 12 int previous_value = LOW; 11 13 12 14 void setup() { 13 15 Ciao.begin(); 16 Ciao.write(CONNECTOR, SERVER_ADDR, command); 14 17 pinMode(2, INPUT); 15 18 … … 20 23 //sample the state of the button - is it pressed or not? 21 24 buttonState = digitalRead(2); 22 25 23 26 //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 27 if ( (buttonState == HIGH) && (previous_value == LOW) && (millis() - lastDebounceTime) > debounceDelay ) { 28 if (ledState == HIGH){ 29 command = "/arduino/digital/13/0"; 30 ledState = LOW; 32 31 } 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 32 else{ 33 command = "/arduino/digital/13/1"; 34 ledState = HIGH; 47 35 } 48 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 49 47 } 50 48 49 previous_value = buttonState; 50 51 51 } -
rtos_arduino/trunk/arduino_lib/libraries/Ciao/examples/CiaoRestServer/CiaoRestServer.ino
r175 r224 1 1 /* 2 2 3 This sketch uses the xmpp connector to receive command for the MCU from a xmpp client. 3 This sketch uses the restserver connector receive rest calls. It allows access to 4 the analog and digital pin of the board via rest calls. 5 6 supported boards: Yun,Tian. 4 7 5 8 Possible commands to send from the xmpp client: 6 9 7 * "digital/PIN" -> to read a digital PIN 8 * "digital/PIN/VALUE" -> to write a digital PIN (VALUE: 1/0) 9 * "analog/PIN/VALUE" -> to write in a PWM PIN(VALUE range: 0 - 255); 10 * "analog/PIN" -> to read a analog PIN 11 * "servo/PIN/VALUE" -> to write angle in a SERVO PIN(VALUE range: 0 - 180); 12 * "mode/PIN/VALUE" -> to set the PIN mode (VALUE: input / output) 13 * "ledon" -> turn on led 13 14 * "ledoff" -> turn off led 13 15 * "ciao" -> random answers in 5 different languages 16 17 NOTE: be sure to activate and configure xmpp connector on Linino OS 18 http://labs.arduino.org/Ciao 19 10 * "/arduino/digital/PIN" -> to read a digital PIN 11 * "/arduino/digital/PIN/VALUE" -> to write a digital PIN (VALUE: 1/0) 12 * "/arduino/analog/PIN/VALUE" -> to write in a PWM PIN(VALUE range: 0 - 255); 13 * "/arduino/analog/PIN" -> to read a analog PIN 14 * "/arduino/servo/PIN/VALUE" -> to write angle in a SERVO PIN(VALUE range: 0 - 180); 15 * "/arduino/mode/PIN/VALUE" -> to set the PIN mode (VALUE: input / output) 16 17 Example: 18 "/arduino/mode/13/output" -> pinMode(13, OUTPUT) 19 "/arduino/digital/13/1" -> digitalWrite(13, HIGH) 20 21 22 NOTE: be sure to activate and configure restserver connector on Linino OS 23 http://labs.arduino.org/Ciao 24 20 25 created September 2015 21 26 by andrea[at]arduino[dot]org 22 27 23 28 */ 24 29 25 30 #include <Ciao.h> 26 31 #include <Servo.h> … … 35 40 void loop() { 36 41 37 CiaoData data = Ciao.read("restserver"); 38 if(!data.isEmpty()){ 42 CiaoData data = Ciao.read("restserver"); 43 if(!data.isEmpty()){ 39 44 String id = data.get(0); 40 45 String sender = data.get(1); 41 46 String message = data.get(2); 42 47 43 48 message.toUpperCase(); 44 49 45 50 String command[3]; 46 51 47 52 splitString(message,"/",command,3); 48 53 execute(command,id); … … 71 76 void servoCommand(String cmd[], String id){ 72 77 int pin, value; 73 78 74 79 pin = (cmd[1]).toInt(); 75 80 76 81 if (cmd[2] != "-1") { 77 82 value = (cmd[2]).toInt(); … … 85 90 } 86 91 else 87 Ciao.writeResponse("restserver",id,"Invalid command"); 92 Ciao.writeResponse("restserver",id,"Invalid command"); 88 93 } 89 94 90 95 void digitalCommand(String cmd[], String id) { 91 96 int pin, value; 92 97 93 98 pin = (cmd[1]).toInt(); 94 99 95 100 if (cmd[2] != "-1") { 96 101 value = (cmd[2]).toInt(); … … 99 104 Ciao.writeResponse("restserver",id,"Pin D"+String(pin)+" ON"); 100 105 else if(value == 0) 101 Ciao.writeResponse("restserver",id,"Pin D"+String(pin)+" OFF"); 106 Ciao.writeResponse("restserver",id,"Pin D"+String(pin)+" OFF"); 102 107 } 103 108 else if (cmd[2] == "-1") { … … 111 116 112 117 pin = (cmd[1]).toInt(); 113 118 114 119 if (cmd[2] != "-1") { 115 120 value =(cmd[2]).toInt(); … … 127 132 128 133 pin = (cmd[1]).toInt(); 129 134 130 135 if (cmd[2] == "INPUT") { 131 136 pinMode(pin, INPUT);
Note:
See TracChangeset
for help on using the changeset viewer.