1
+ /*
2
+
3
+ This sketch uses the xmpp connector to receive command for the MCU from a xmpp client.
4
+
5
+ Possible commands to send from the xmpp client:
6
+
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
+ * "led on" -> turn on led 13
14
+ * "led off" -> 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
+
20
+ created September 2015
21
+ by andrea[at]arduino[dot]org
22
+
23
+ */
24
+
25
+ #include < Ciao.h>
26
+ #include < Servo.h>
27
+
28
+ Servo servo;
29
+
30
+ void setup () {
31
+
32
+ Ciao.begin ();
33
+ }
34
+
35
+ void loop () {
36
+
37
+ CiaoData data = Ciao.read (" xmpp" );
38
+ if (!data.isEmpty ()){
39
+ String id = data.get (0 );
40
+ String sender = data.get (1 );
41
+ String message = data.get (2 );
42
+
43
+ message.toUpperCase ();
44
+
45
+ if (message == " LED ON" ){
46
+ digitalWrite (13 ,HIGH);
47
+ Ciao.writeResponse (" xmpp" ,id," Led D13 ON" );
48
+ }
49
+
50
+ else if (message == " LED OFF" ){
51
+ digitalWrite (13 ,LOW);
52
+ Ciao.writeResponse (" xmpp" ,id," Led D13 OFF" );
53
+ }
54
+ else {
55
+ String command[3 ];
56
+
57
+ splitString (message," /" ,command,3 );
58
+ execute (command,id);
59
+ }
60
+ }
61
+ }
62
+
63
+ void execute (String cmd[], String id) {
64
+
65
+ if (cmd[0 ] == " DIGITAL" ) {
66
+ digitalCommand (cmd,id);
67
+ }
68
+ else if (cmd[0 ] == " ANALOG" ) {
69
+ analogCommand (cmd,id);
70
+ }
71
+ else if (cmd[0 ] == " SERVO" ) {
72
+ servoCommand (cmd,id);
73
+ }
74
+ else if (cmd[0 ] == " MODE" ) {
75
+ setMode (cmd,id);
76
+ }
77
+ else
78
+ Ciao.writeResponse (" xmpp" ,id," sorry, i don't understand :(" );
79
+ }
80
+
81
+ void servoCommand (String cmd[], String id){
82
+ int pin, value;
83
+
84
+ pin = (cmd[1 ]).toInt ();
85
+
86
+ if (cmd[2 ] != " -1" ) {
87
+ value = (cmd[2 ]).toInt ();
88
+ if (value <= 180 && value >=0 ){
89
+ servo.attach (pin);
90
+ servo.write (value);
91
+ Ciao.writeResponse (" xmpp" ,id," Servo D" +String (pin)+" set to " +String (value)+" degrees" );
92
+ }
93
+ else
94
+ Ciao.writeResponse (" xmpp" ,id," Invalid angle value" );
95
+ }
96
+ else
97
+ Ciao.writeResponse (" xmpp" ,id," Invalid command" );
98
+ }
99
+
100
+ void digitalCommand (String cmd[], String id) {
101
+ int pin, value;
102
+
103
+ pin = (cmd[1 ]).toInt ();
104
+
105
+ if (cmd[2 ] != " -1" ) {
106
+ value = (cmd[2 ]).toInt ();
107
+ digitalWrite (pin, value);
108
+ if (value == 1 )
109
+ Ciao.writeResponse (" xmpp" ,id," Pin D" +String (pin)+" ON" );
110
+ else if (value == 0 )
111
+ Ciao.writeResponse (" xmpp" ,id," Pin D" +String (pin)+" OFF" );
112
+ }
113
+ else if (cmd[2 ] == " -1" ) {
114
+ value = digitalRead (pin);
115
+ Ciao.writeResponse (" xmpp" ,id," D" +String (pin)+" value = " +String (value));
116
+ }
117
+ }
118
+
119
+ void analogCommand (String cmd[], String id) {
120
+ int pin, value;
121
+
122
+ pin = (cmd[1 ]).toInt ();
123
+
124
+ if (cmd[2 ] != " -1" ) {
125
+ value =(cmd[2 ]).toInt ();
126
+ analogWrite (pin, value);
127
+ Ciao.writeResponse (" xmpp" ,id," D" +String (pin)+" set to analog" );
128
+ }
129
+ else if (cmd[2 ] == " -1" ) {
130
+ value = analogRead (pin);
131
+ Ciao.writeResponse (" xmpp" ,id," A" +String (pin)+" value = " +String (value));
132
+ }
133
+ }
134
+
135
+ void setMode (String cmd[], String id) {
136
+ int pin;
137
+
138
+ pin = (cmd[1 ]).toInt ();
139
+
140
+ if (cmd[2 ] == " INPUT" ) {
141
+ pinMode (pin, INPUT);
142
+ Ciao.writeResponse (" xmpp" ,id," pin D" +String (pin)+" set in INPUT mode" );
143
+ return ;
144
+ }
145
+
146
+ if (cmd[2 ] == " OUTPUT" ) {
147
+ pinMode (pin, OUTPUT);
148
+ Ciao.writeResponse (" xmpp" ,id," pin D" +String (pin)+" set in OUTPUT mode" );
149
+ return ;
150
+ }
151
+ Ciao.writeResponse (" xmpp" ,id," invalid mode" );
152
+ }
0 commit comments