You are here

How to control your arduino yun using iPhone or Android phone

About:

Write and read all digital pins, PWM pins & read all analog pins in Arduino Yun.

 

Applications:

– Smart Home.

– Automation.

– Hoppy Projects.

– Schools & university projects.

– Remote control.

 

Check this YouTube to see in real world:

http://goo.gl/ZGKCJ2

 

Requirements:

– Arduino Yun Board.

– Arduino Sketch for iPhone App:

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
 
#include <EEPROM.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
 
// will forward there all the HTTP requests you send
YunServer server;
 
void setup() {
  // Bridge startup
  Bridge.begin();
  for (int i = 2; i <= 13; i++) {
    pinMode(i, OUTPUT);
  }
  /*
  int eevalwrite;
  for (int i = 2; i <= 13; i++) {
    eevalwrite = EEPROM.read(i);
    digitalWrite(i, eevalwrite);
    }
    */
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
 
}
 
void loop() {
  /*
  int address = 2;
  for (int i = 2; i <= 13; i++) {
    byte eevalread = digitalRead(i);
    EEPROM.write(i, eevalread);
    address++;
  }*/
 
 
  // Get clients coming from server
  YunClient client = server.accept();
 
  // There is a new client?
  if (client) {
    // Process request
    process(client);
 
    // Close connection and free resources.
    client.stop();
  }
 
  delay(50); // Poll every 50ms
}
 
void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');
 
  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }
 
  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }
 
  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
 
  if (command == "allonoff") {
    allonoff(client);
  }
 
  if (command == "allstatus") {
    allstatus(client);
  }
 
 
}
 
void digitalCommand(YunClient client) {
  int pin, value;
 
  // Read pin number
  pin = client.parseInt();
 
  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
 
  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}
 
void analogCommand(YunClient client) {
  int pin, value;
 
  // Read pin number
  pin = client.parseInt();
 
  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);
    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
 
}
 
void modeCommand(YunClient client) {
  int pin;
 
  // Read pin number
  pin = client.parseInt();
 
  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }
 
  String mode = client.readStringUntil('\r');
 
  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }
 
  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }
 
  client.print(F("error: invalid mode "));
  client.print(mode);
}
 
 
 
void allonoff(YunClient client) {
  int pin, value, realvalue;
  // Read pin number
  value = client.parseInt();
  for (int i = 2; i <= 13; i++) {
    digitalWrite(i, value);
  }
  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
 
}
 
 
void allstatus(YunClient client) {
  int r;
  int x = 2;
  //init JSON data
  client.println("Status:200");
  client.println("content-type:application/json");
  client.println();
  client.println("{");
 
  /*
  loop through the analog pins and for each pin,
  make a JSON line, like:
  "A0" : 450
  */
  for (int i = 0; i <= 5; i++) {
    client.print("\"A");
    client.print(i);
    client.print("\":");
    if (i == 5) {
      client.print(analogRead(i));
      client.println(",");
    }
    else {
      client.print(analogRead(i));
      client.println(",");
    }
  }
  for (int i = 2; i <= 13; i++) {
    r = digitalRead(i);
 
    client.print("\"D");
    client.print(i);
    client.print("\":");
 
    if (r == 1) {
      x++;
    }
    if (i == 13) {
      client.print(r);
      if (x == 14) {
        client.println(",");
        client.print("\"alldigital");
        client.print("\":");
        client.print(r);
      }
      else {
        client.println(",");
        client.print("\"alldigital");
        client.print("\":");
        client.print(0);
      }
 
    }
    else {
      client.print(r);
      client.println(",");
    }
  }
 
  //close
  client.println(",");
  client.print("\"boardstatus");
  client.print("\":");
  client.println(1);
  client.println("}");
}

 

– Arduino Sketch for Android App:

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
 
#include <EEPROM.h>
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
 
// will forward there all the HTTP requests you send
YunServer server;
 
void setup() {
  // Bridge startup
  Bridge.begin();
  for (int i = 2; i <= 13; i++) {
    pinMode(i, OUTPUT);
  }
  /*
  int eevalwrite;
  for (int i = 2; i <= 13; i++) {
    eevalwrite = EEPROM.read(i);
    digitalWrite(i, eevalwrite);
    }
    */
  // Listen for incoming connection only from localhost
  // (no one from the external network could connect)
  server.listenOnLocalhost();
  server.begin();
 
}
 
void loop() {
  /*
  int address = 2;
  for (int i = 2; i <= 13; i++) {
    byte eevalread = digitalRead(i);
    EEPROM.write(i, eevalread);
    address++;
  }*/
 
 
  // Get clients coming from server
  YunClient client = server.accept();
 
  // There is a new client?
  if (client) {
    // Process request
    process(client);
 
    // Close connection and free resources.
    client.stop();
  }
 
  delay(50); // Poll every 50ms
}
 
void process(YunClient client) {
  // read the command
  String command = client.readStringUntil('/');
 
  // is "digital" command?
  if (command == "digital") {
    digitalCommand(client);
  }
 
  // is "analog" command?
  if (command == "analog") {
    analogCommand(client);
  }
 
  // is "mode" command?
  if (command == "mode") {
    modeCommand(client);
  }
 
  if (command == "allonoff") {
    allonoff(client);
  }
 
  if (command == "allstatus") {
    allstatus(client);
  }
 
 
}
 
void digitalCommand(YunClient client) {
  int pin, value;
 
  // Read pin number
  pin = client.parseInt();
 
  // If the next character is a '/' it means we have an URL
  // with a value like: "/digital/13/1"
  if (client.read() == '/') {
    value = client.parseInt();
    digitalWrite(pin, value);
  }
 
  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
}
 
void analogCommand(YunClient client) {
  int pin, value;
 
  // Read pin number
  pin = client.parseInt();
 
  // If the next character is a '/' it means we have an URL
  // with a value like: "/analog/5/120"
  if (client.read() == '/') {
    // Read value and execute command
    value = client.parseInt();
    analogWrite(pin, value);
    // Update datastore key with the current pin value
    String key = "D";
    key += pin;
    Bridge.put(key, String(value));
  }
 
}
 
void modeCommand(YunClient client) {
  int pin;
 
  // Read pin number
  pin = client.parseInt();
 
  // If the next character is not a '/' we have a malformed URL
  if (client.read() != '/') {
    client.println(F("error"));
    return;
  }
 
  String mode = client.readStringUntil('\r');
 
  if (mode == "input") {
    pinMode(pin, INPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as INPUT!"));
    return;
  }
 
  if (mode == "output") {
    pinMode(pin, OUTPUT);
    // Send feedback to client
    client.print(F("Pin D"));
    client.print(pin);
    client.print(F(" configured as OUTPUT!"));
    return;
  }
 
  client.print(F("error: invalid mode "));
  client.print(mode);
}
 
 
 
void allonoff(YunClient client) {
  int pin, value, realvalue;
  // Read pin number
  value = client.parseInt();
  for (int i = 2; i <= 13; i++) {
    digitalWrite(i, value);
  }
  // Update datastore key with the current pin value
  String key = "D";
  key += pin;
  Bridge.put(key, String(value));
 
}
 
 
void allstatus(YunClient client) {
  int r;
  int x = 2;
  //init JSON data
  client.println("Status:200");
  client.println("content-type:application/json");
  client.println();
  client.println("{");
 
  /*
  loop through the analog pins and for each pin,
  make a JSON line, like:
  "A0" : 450
  */
  for (int i = 0; i <= 5; i++) {
    client.print("\"A");
    client.print(i);
    client.print("\":");
    if (i == 5) {
      client.print(analogRead(i));
      client.println(",");
    }
    else {
      client.print(analogRead(i));
      client.println(",");
    }
  }
  for (int i = 2; i <= 13; i++) {
    r = digitalRead(i);
 
    client.print("\"D");
    client.print(i);
    client.print("\":");
 
    if (r == 1) {
      x++;
    }
    if (i == 13) {
      client.print(r);
      if (x == 14) {
        client.println(",");
        client.print("\"alldigital");
        client.print("\":");
        client.print(r);
      }
      else {
        client.println(",");
        client.print("\"alldigital");
        client.print("\":");
        client.print(0);
      }
 
    }
    else {
      client.print(r);
      client.println(",");
    }
  }
 
  //close
  client.println(",");
  client.print("\"boardstatus");
  client.print("\":");
  client.println(1);
  client.println("}");
}

 

Features:

– Totally new and easy user interface.

– Now when you open the app it will remember the last status of the pins.

– Ability to change the mode of the pin (Output, PWM, Input).

– Now you will get notified whenever digital input equal High.

– Automatically login to Arduino Yun.

 

iPhone app link:

https://itunes.apple.com/sa/app/arduino-yun-kit/id1079396851?mt=8

Android app link:

https://play.google.com/store/apps/details?id=com.arduino.yunkit

 

 

Rabee Alhattawi
Major: Bsc in Electrical/ Electronics Engineering Professional in: Arduino, PIC, Aviation & Wether Systems Own a company: TATCO Inc
http://tatco.cc

Leave a Reply

Top