You are here

Remote control Arduino Yun over Internet with “RemoteYun”

2

3

 

Arduino Yun offers a lot of ways to remote control pin value and mode, but most ways need programming skills, or need a local server running on Arduino Yun, and in most cases you need a static IP to access the Yun from Internet, or access is limited to local network.


With “RemoteYun” you can easily control pin value and mode from browsers and a dedicated Android app (, simply loading a sketch on your Arduino.
You don’t need to change any network configuration in your home, you just have to connect Arduino Yun to your router/switch (Via WiFi or Ethernet) and the board will automatically connect to the remote app server.

The first step for remote control is the registration at the website “remoteyun.com“, so you can download your custom sketch.
The second step is login at the website “remoteyun.com“, and then from the download section you have to download the sketch, compile and load it on the Yun with Arduino IDE.
After that an important step is connect Arduino Yun to Internet, you can do this in the classic way, using Ethernet or Wifi interface for connecting to your domestic router/switch.
At this point Arduino Yun is connected to the remote server and you can see directly from the web site or from the android app the pin value and mode and change it.


For analog pins the server automatically saves the last 1000 values acquired, and you can also set an amplification/attenuatin factor and a measure unit for this pins.
When your Arduino Yun is not conncted to Internet you can anyway access to application for see historical pin values, but you can not change pin mode or value.

 

RemoteYun is based on a client part running on your Arduino Yun, and a server side part running on a single centralized server. The client part is written in C and is the sketch that you load on Arduino and is open source, the server side instead is based on node.js technology for best asynchronous comunications, but is not visible to the user.
This is the sketch you have to load on the Yun (N.B: you have to replace the ID in the sketch with the one received in the subscription email at the website remoteyun.com, or download the sketch directly from the site when you are logged in).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
 
#include 
 
#include 
 
 
 
#define host "remoteyun.com"
 
#define port 999
 
#define conn_delay 15000
 
#define loop_delay 30
 
#define MAX_ARGS 100
 
#define MAX_ARGS_LEN 15
 
#define PIN_NUMBER 24
 
 
 
String id = "Insert here your ID";
 
YunClient client;
 
char r =' ';
 
String readed =String("");
 
int i=0;
 
int pin=0;
 
unsigned long ltime;
 
char* pins[]={"o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o"};
 
 
 
void setup() {
 
  Bridge.begin();
 
  ltime=millis();
 
}
 
 
 
void loop() {
 
  if(client.connected() && client.available())  readloop();
 
  if(!client.connected())  connect_server();
 
  if((millis()-ltime)<0) ltime=millis(); if((millis()-ltime)>conn_delay) client.stop();
 
  delay(loop_delay);
 
}
 
 
 
void readloop(){
 
  readed="";
 
  while (client.available()) {
 
    r = client.read();
 
    if(r=='!') break;
 
    readed +=  r;      
 
  }
 
  check_command(readed);
 
  return;
 
}
 
 
 
void connect_server(){
 
    client.stop();
 
    ltime=millis();
 
    if(client.connect(host, port)) {    
 
      client.write((uint8_t*)&id[0], id.length());
 
    }
 
    else delay(conn_delay);
 
    return;    
 
} 
 
 
 
void check_command(String com){
 
    String data[MAX_ARGS];
 
    int numArgs=0;
 
    int beginIdx=0;
 
    int idx=com.indexOf(",");
 
    String arg;
 
    char charBuffer[MAX_ARGS_LEN];
 
    while (idx != -1)
 
    {
 
      arg=com.substring(beginIdx,idx);
 
      arg.toCharArray(charBuffer,MAX_ARGS_LEN);
 
      data[numArgs++]=charBuffer;
 
      beginIdx=idx+1;
 
      idx=com.indexOf(",", beginIdx);
 
    }
 
    data[numArgs++] = com.substring(beginIdx);
 
    if(data[0]=="dig"){
 
      pin = data[1].toInt();
 
      digitalWrite(pin, data[2].toInt());
 
      com ="11,"+com;
 
      client.write((uint8_t*)&com[0],com.length());
 
    }    
 
    else if(data[0]=="pwm"){ 
 
      analogWrite(data[1].toInt(), data[2].toInt()); 
 
      com="11,"+com;
 
      client.write((uint8_t*)&com[0],com.length());
 
    }  
 
    else if(data[0]=="ana") send_analog();
 
    else if(data[0]=="stp"){ 
 
      i=1;  
 
      while(true){    
 
        pin = data[i].toInt();
 
        if(pin==666) break; 
 
        String mode = data[i+1];
 
        int val = data[i+2].toInt();
 
        if(mode=="d"){
 
          pinMode(pin,OUTPUT);           
 
          digitalWrite(pin,val);
 
          pins[pin]="o";
 
        }
 
        else if(mode=="p") {
 
          pinMode(pin,OUTPUT);
 
          analogWrite(pin,val);
 
          pins[pin]="o";
 
        }
 
        else if(mode=="a") {
 
          pinMode(pin,INPUT); 
 
          pins[pin]="a";
 
        }
 
        else pins[pin]="o";
 
        i+=3;
 
      }
 
    }
 
    else if(data[0]=="cng"){
 
      pin=data[1].toInt();
 
      if(data[2]=="a") {
 
        pinMode(pin,INPUT);
 
        pins[pin]="a";  
 
      }  
 
      else {
 
        pins[pin]="o";
 
        pinMode(pin,OUTPUT);
 
        digitalWrite(pin,0);
 
      }
 
    }
 
    return;
 
}
 
 
 
void send_analog(){
 
     ltime=millis();  
 
     String a = ("AN,");     
 
     for(int x=0; x<PIN_NUMBER;x++)  if(pins[x]=="a")  a += (String) read_analog_val(x) + "," + (String) x + ",";     
 
     a = a.substring(0, a.length()-1); 
 
     client.write((uint8_t*)&a[0],a.length());
 
     return;
 
}
 
 
 
int read_analog_val(int p){
 
  int v=0;
 
  switch (p) {
 
    case 4:
 
      v=analogRead(6);
 
      break;
 
    case 6:
 
      v=analogRead(7);
 
      break;
 
    case 8:
 
      v=analogRead(8);
 
      break;
 
    case 9:
 
      v=analogRead(9);
 
      break;
 
    case 10:
 
      v=analogRead(10);
 
      break;
 
    case 12:
 
      v=analogRead(11);
 
      break;
 
    case 18:
 
      v=analogRead(0);
 
      break;
 
    case 19:
 
      v=analogRead(1);
 
      break;        
 
    case 20:
 
      v=analogRead(2);
 
      break;
 
    case 21:
 
      v=analogRead(3);
 
      break;
 
    case 22:
 
      v=analogRead(4);
 
      break;
 
    case 23:
 
      v=analogRead(5);
 
      break;
 
    default:
 
      v=0;
 
      break;  
 
  }
 
  return v;
 
}
This is a video with an eaxample of application:
Remote control of 5 leds
https://www.youtube.com/watch?v=Cw29lf7SqZw

(You can see other examples on the website remoteyun.com)
Web interfaces for controlling pin value and mode:
 2
3
Changin pin label:
5
6
Changing pin mode:
4
Ludovico Gialanella
Ludovico Gialanella is an electronic engineer with a great passion for web and information technologies. In his spare time he enjoys programming and developing web projects, snowboarding, climbing, driving motard. He was born in Avellino, a small town near Neaples, in the South Italy, and he lives in Trento where he works as electronic and informatic high school teacher. He works with Arduino from over 5 years, developing several private or professional projects.

Leave a Reply

Top