├── .gitignore
├── LICENSE
├── README.md
├── android
└── WiFiDemo
│ ├── .gitignore
│ ├── .idea
│ ├── .name
│ ├── compiler.xml
│ ├── copyright
│ │ └── profiles_settings.xml
│ ├── dictionaries
│ │ └── hayk.xml
│ ├── encodings.xml
│ ├── gradle.xml
│ ├── inspectionProfiles
│ │ ├── Project_Default.xml
│ │ └── profiles_settings.xml
│ ├── misc.xml
│ ├── modules.xml
│ ├── scopes
│ │ └── scope_settings.xml
│ └── vcs.xml
│ ├── WiFiDemo.iml
│ ├── app
│ ├── .gitignore
│ ├── app.iml
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src
│ │ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ └── edu
│ │ │ └── stanford
│ │ │ └── me202
│ │ │ └── wifidemo
│ │ │ └── WiFiActivity.java
│ │ └── res
│ │ ├── drawable-hdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ └── ic_launcher.png
│ │ ├── drawable-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── layout
│ │ └── activity_wi_fi.xml
│ │ ├── menu
│ │ └── wi_fi.xml
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ ├── build.gradle
│ ├── gradle.properties
│ ├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
│ ├── gradlew
│ ├── gradlew.bat
│ └── settings.gradle
├── arduino
└── wifi_demo
│ ├── Credentials.h
│ └── wifi_demo.ino
└── presentation.pdf
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # Log Files
26 | *.log
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Hayk Martirosyan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | android-arduino-wifi
2 | ====================
3 |
4 | Demo of a clear and robust way of creating a serial communication
5 | link between an Arduino and an Android device using WiFi.
6 |
7 | **Features:**
8 |
9 | * WiFi server using the [WiFly Shield](https://www.sparkfun.com/products/9954)
10 | and associated library on Arduino
11 | * WiFi client socket implemented using AsyncTask in Android
12 | * UI for connecting, disconnecting, reading, and writing
13 | * Exception handling and error management to prevent crashes
14 | * Parsing of complete newline-delimited messages from the stream
15 | * Timeout detection using a ping system
16 | * Lots of comments
17 |
18 | **Requirements:**
19 |
20 | * Android device with WiFi enabled
21 | * Arduino with a WiFly shield
22 | * Android Studio and an Arduino IDE
23 | * A WiFi network to connect to and a not-blocked port
24 |
25 | **Usage:**
26 |
27 | * Arduino - set your WiFi network name and password in `Credentials.h`.
28 | * Arduino - set your desired port number in `wifi_demo.ino`.
29 | * Flash the Arduino, then turn it on and open the serial monitor at 115200bps.
30 | * Wait for the WiFly to get an IP address.
31 | * Open the Android app, enter the address and port, and hit connect.
32 | * You should now be able to send messages and have them echoed back.
33 | * You can also type messages into the Aruino serial monitor to send them
34 | to the Android device (select newline endings).
35 |
36 | **Important files:**
37 |
38 | * `WiFiActivity.java`
39 | * `activity_wi_fi.xml`
40 | * `wifi_demo.ino`
41 |
42 | **Issues:**
43 |
44 | * WiFly takes a long time (~20s) to connect to a network on startup (watch the LEDs).
45 | * After disconnecting from a client, the WiFly takes about 6 seconds before
46 | it can connect to another client (again, watch the LEDs).
47 | * I had an issue with a version of the WiFly library where it would not
48 | initialize until it heard a `*READY*` message from the chip, which never came.
49 | If you get stuck at WiFly.begin(), look into this.
50 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | /local.properties
3 | /.idea/workspace.xml
4 | /.idea/libraries
5 | .DS_Store
6 | /build
7 | *.iws
8 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/.name:
--------------------------------------------------------------------------------
1 | WiFi Demo
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/dictionaries/hayk.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/inspectionProfiles/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/scopes/scope_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/android/WiFiDemo/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/android/WiFiDemo/WiFiDemo.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/app.iml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 20
5 | buildToolsVersion "20.0.0"
6 |
7 | defaultConfig {
8 | applicationId "edu.stanford.me202.wifidemo"
9 | minSdkVersion 15
10 | targetSdkVersion 20
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | runProguard false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | }
25 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /usr/local/android-studio/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/java/edu/stanford/me202/wifidemo/WiFiActivity.java:
--------------------------------------------------------------------------------
1 | package edu.stanford.me202.wifidemo;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.util.Log;
6 | import android.view.View;
7 | import android.widget.Button;
8 | import android.widget.EditText;
9 | import android.widget.TextView;
10 |
11 | import android.os.AsyncTask;
12 | import java.net.InetSocketAddress;
13 | import java.net.Socket;
14 | import java.io.BufferedReader;
15 | import java.io.InputStreamReader;
16 | import java.io.OutputStream;
17 |
18 | /**
19 | * Simple UI demonstrating how to open a serial communication link to a
20 | * remote host over WiFi, send and receive messages, and update the display.
21 | *
22 | * Author: Hayk Martirosyan
23 | */
24 | public class WiFiActivity extends Activity {
25 |
26 | // Tag for logging
27 | private final String TAG = getClass().getSimpleName();
28 |
29 | // AsyncTask object that manages the connection in a separate thread
30 | WiFiSocketTask wifiTask = null;
31 |
32 | // UI elements
33 | TextView textStatus, textRX, textTX;
34 | EditText editTextAddress, editTextPort, editSend;
35 | Button buttonConnect, buttonSend;
36 |
37 | @Override
38 | protected void onCreate(Bundle savedInstanceState) {
39 |
40 | super.onCreate(savedInstanceState);
41 | setContentView(R.layout.activity_wi_fi);
42 |
43 | // Save references to UI elements
44 | textStatus = (TextView)findViewById(R.id.textStatus);
45 | textRX = (TextView)findViewById(R.id.textRX);
46 | textTX = (TextView)findViewById(R.id.textTX);
47 | editTextAddress = (EditText)findViewById(R.id.address);
48 | editTextPort = (EditText)findViewById(R.id.port);
49 | editSend = (EditText)findViewById(R.id.editSend);
50 | buttonConnect = (Button)findViewById(R.id.connect);
51 | buttonSend = (Button)findViewById(R.id.buttonSend);
52 |
53 | // Disable send button until a connection is made
54 | buttonSend.setEnabled(false);
55 | }
56 |
57 | /**
58 | * Helper function, print a status to both the UI and program log.
59 | */
60 | void setStatus(String s) {
61 | Log.v(TAG, s);
62 | textStatus.setText(s);
63 | }
64 |
65 | /**
66 | * Try to start a connection with the specified remote host.
67 | */
68 | public void connectButtonPressed(View v) {
69 |
70 | if(wifiTask != null) {
71 | setStatus("Already connected!");
72 | return;
73 | }
74 |
75 | try {
76 | // Get the remote host from the UI and start the thread
77 | String host = editTextAddress.getText().toString();
78 | int port = Integer.parseInt(editTextPort.getText().toString());
79 |
80 | // Start the asyncronous task thread
81 | setStatus("Attempting to connect...");
82 | wifiTask = new WiFiSocketTask(host, port);
83 | wifiTask.execute();
84 |
85 | } catch (Exception e) {
86 | e.printStackTrace();
87 | setStatus("Invalid address/port!");
88 | }
89 | }
90 |
91 | /**
92 | * Disconnect from the connection.
93 | */
94 | public void disconnectButtonPressed(View v) {
95 |
96 | if(wifiTask == null) {
97 | setStatus("Already disconnected!");
98 | return;
99 | }
100 |
101 | wifiTask.disconnect();
102 | setStatus("Disconnecting...");
103 | }
104 |
105 | /**
106 | * Invoked by the AsyncTask when the connection is successfully established.
107 | */
108 | private void connected() {
109 | setStatus("Connected.");
110 | buttonSend.setEnabled(true);
111 | }
112 |
113 | /**
114 | * Invoked by the AsyncTask when the connection ends..
115 | */
116 | private void disconnected() {
117 | setStatus("Disconnected.");
118 | buttonSend.setEnabled(false);
119 | textRX.setText("");
120 | textTX.setText("");
121 | wifiTask = null;
122 | }
123 |
124 | /**
125 | * Invoked by the AsyncTask when a newline-delimited message is received.
126 | */
127 | private void gotMessage(String msg) {
128 | textRX.setText(msg);
129 | Log.v(TAG, "[RX] " + msg);
130 | }
131 |
132 | /**
133 | * Send the message typed in the input field using the AsyncTask.
134 | */
135 | public void sendButtonPressed(View v) {
136 |
137 | if(wifiTask == null) return;
138 |
139 | String msg = editSend.getText().toString();
140 | if(msg.length() == 0) return;
141 |
142 | wifiTask.sendMessage(msg);
143 | editSend.setText("");
144 |
145 | textTX.setText(msg);
146 | Log.v(TAG, "[TX] " + msg);
147 | }
148 |
149 | /**
150 | * AsyncTask that connects to a remote host over WiFi and reads/writes the connection
151 | * using a socket. The read loop of the AsyncTask happens in a separate thread, so the
152 | * main UI thread is not blocked. However, the AsyncTask has a way of sending data back
153 | * to the UI thread. Under the hood, it is using Threads and Handlers.
154 | */
155 | public class WiFiSocketTask extends AsyncTask {
156 |
157 | // Location of the remote host
158 | String address;
159 | int port;
160 |
161 | // Special messages denoting connection status
162 | private static final String PING_MSG = "SOCKET_PING";
163 | private static final String CONNECTED_MSG = "SOCKET_CONNECTED";
164 | private static final String DISCONNECTED_MSG = "SOCKET_DISCONNECTED";
165 |
166 | Socket socket = null;
167 | BufferedReader inStream = null;
168 | OutputStream outStream = null;
169 |
170 | // Signal to disconnect from the socket
171 | private boolean disconnectSignal = false;
172 |
173 | // Socket timeout - close if no messages received (ms)
174 | private int timeout = 5000;
175 |
176 | // Constructor
177 | WiFiSocketTask(String address, int port) {
178 | this.address = address;
179 | this.port = port;
180 | }
181 |
182 | /**
183 | * Main method of AsyncTask, opens a socket and continuously reads from it
184 | */
185 | @Override
186 | protected Void doInBackground(Void... arg) {
187 |
188 | try {
189 |
190 | // Open the socket and connect to it
191 | socket = new Socket();
192 | socket.connect(new InetSocketAddress(address, port), timeout);
193 |
194 | // Get the input and output streams
195 | inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
196 | outStream = socket.getOutputStream();
197 |
198 | // Confirm that the socket opened
199 | if(socket.isConnected()) {
200 |
201 | // Make sure the input stream becomes ready, or timeout
202 | long start = System.currentTimeMillis();
203 | while(!inStream.ready()) {
204 | long now = System.currentTimeMillis();
205 | if(now - start > timeout) {
206 | Log.e(TAG, "Input stream timeout, disconnecting!");
207 | disconnectSignal = true;
208 | break;
209 | }
210 | }
211 | } else {
212 | Log.e(TAG, "Socket did not connect!");
213 | disconnectSignal = true;
214 | }
215 |
216 | // Read messages in a loop until disconnected
217 | while(!disconnectSignal) {
218 |
219 | // Parse a message with a newline character
220 | String msg = inStream.readLine();
221 |
222 | // Send it to the UI thread
223 | publishProgress(msg);
224 | }
225 |
226 | } catch (Exception e) {
227 | e.printStackTrace();
228 | Log.e(TAG, "Error in socket thread!");
229 | }
230 |
231 | // Send a disconnect message
232 | publishProgress(DISCONNECTED_MSG);
233 |
234 | // Once disconnected, try to close the streams
235 | try {
236 | if (socket != null) socket.close();
237 | if (inStream != null) inStream.close();
238 | if (outStream != null) outStream.close();
239 | } catch (Exception e) {
240 | e.printStackTrace();
241 | }
242 |
243 | return null;
244 | }
245 |
246 | /**
247 | * This function runs in the UI thread but receives data from the
248 | * doInBackground() function running in a separate thread when
249 | * publishProgress() is called.
250 | */
251 | @Override
252 | protected void onProgressUpdate(String... values) {
253 |
254 | String msg = values[0];
255 | if(msg == null) return;
256 |
257 | // Handle meta-messages
258 | if(msg.equals(CONNECTED_MSG)) {
259 | connected();
260 | } else if(msg.equals(DISCONNECTED_MSG))
261 | disconnected();
262 | else if(msg.equals(PING_MSG))
263 | {}
264 |
265 | // Invoke the gotMessage callback for all other messages
266 | else
267 | gotMessage(msg);
268 |
269 | super.onProgressUpdate(values);
270 | }
271 |
272 | /**
273 | * Write a message to the connection. Runs in UI thread.
274 | */
275 | public void sendMessage(String data) {
276 |
277 | try {
278 | outStream.write(data.getBytes());
279 | outStream.write('\n');
280 | } catch (Exception e) {
281 | e.printStackTrace();
282 | }
283 | }
284 |
285 | /**
286 | * Set a flag to disconnect from the socket.
287 | */
288 | public void disconnect() {
289 | disconnectSignal = true;
290 | }
291 | }
292 | }
293 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hmartiro/android-arduino-wifi/61a68dee9226d32354d7177de8a64faab9bd40b5/android/WiFiDemo/app/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hmartiro/android-arduino-wifi/61a68dee9226d32354d7177de8a64faab9bd40b5/android/WiFiDemo/app/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hmartiro/android-arduino-wifi/61a68dee9226d32354d7177de8a64faab9bd40b5/android/WiFiDemo/app/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hmartiro/android-arduino-wifi/61a68dee9226d32354d7177de8a64faab9bd40b5/android/WiFiDemo/app/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/layout/activity_wi_fi.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
19 |
20 |
25 |
26 |
32 |
33 |
39 |
40 |
46 |
47 |
48 |
53 |
54 |
61 |
62 |
69 |
70 |
71 |
72 |
78 |
79 |
85 |
86 |
92 |
93 |
94 |
99 |
100 |
106 |
107 |
113 |
114 |
115 |
121 |
122 |
128 |
129 |
136 |
137 |
138 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/menu/wi_fi.xml:
--------------------------------------------------------------------------------
1 |
4 |
8 |
9 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WiFi Demo
5 | Hello world!
6 | Settings
7 |
8 |
9 |
--------------------------------------------------------------------------------
/android/WiFiDemo/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/android/WiFiDemo/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:0.12.2'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/android/WiFiDemo/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Settings specified in this file will override any Gradle settings
5 | # configured through the IDE.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/android/WiFiDemo/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hmartiro/android-arduino-wifi/61a68dee9226d32354d7177de8a64faab9bd40b5/android/WiFiDemo/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/android/WiFiDemo/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
7 |
--------------------------------------------------------------------------------
/android/WiFiDemo/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/android/WiFiDemo/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/android/WiFiDemo/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/arduino/wifi_demo/Credentials.h:
--------------------------------------------------------------------------------
1 | #ifndef __CREDENTIALS_H__
2 | #define __CREDENTIALS_H__
3 |
4 | // Access point parameters
5 | char ssid[] = "mynetworkname";
6 | char passphrase[] = "mypassword";
7 |
8 | #endif
9 |
10 |
--------------------------------------------------------------------------------
/arduino/wifi_demo/wifi_demo.ino:
--------------------------------------------------------------------------------
1 | /**
2 | * WiFi communication demo using the WiFly module.
3 | *
4 | * Author: Hayk Martirosyan
5 | */
6 |
7 | #include "WiFly.h"
8 | #include "Credentials.h"
9 |
10 | // How often to ping (milliseconds)
11 | static const int PING_TIME = 1000;
12 |
13 | // Special messages
14 | static const String PING_MSG = "SOCKET_PING";
15 | static const String CONNECTED_MSG = "SOCKET_CONNECTED";
16 |
17 | // Server object, created with the desired port
18 | int port = 80;
19 | Server server(port);
20 |
21 | void setup() {
22 |
23 | Serial.begin(115200);
24 |
25 | Serial.println("Initiating WiFly...");
26 | WiFly.begin();
27 |
28 | Serial.println("Joining network " + String(ssid) + "...");
29 | if (!WiFly.join(ssid, passphrase, true)) {
30 |
31 | Serial.println("ERROR: Failed to connect!");
32 | while (1) {}
33 | }
34 |
35 | String ip = String(WiFly.ip());
36 | Serial.println("IP: " + ip + ", Port: " + String(port));
37 |
38 | server.begin();
39 | Serial.println("WiFi server initialized!");
40 | }
41 |
42 | /**
43 | * Called whenever a newline-delimited message is received.
44 | */
45 | void got_message(String msg) {
46 | Serial.println("[RX] " + msg);
47 | }
48 |
49 | /**
50 | * Send a message to the given client.
51 | */
52 | void send_message(Client client, String msg) {
53 | client.println(msg);
54 | Serial.println("[TX] " + msg);
55 | }
56 |
57 | void loop() {
58 |
59 | // Looks for a connected client
60 | Client client = server.available();
61 |
62 | if (client) {
63 |
64 | // Buffer to store data
65 | String rx_buffer = "";
66 | String tx_buffer = "";
67 |
68 | // Last time a ping was sent
69 | int senttime = millis();
70 |
71 | Serial.println("Connected to client.");
72 | client.println(CONNECTED_MSG);
73 |
74 | while (client.connected()) {
75 |
76 | // Read data from the client, and when we hit a newline,
77 | // invoke the message_received callback and echo the message
78 | // back to the client.
79 | if(client.available()) {
80 | while(client.available()) {
81 |
82 | char c = client.read();
83 |
84 | if(c == '\n') {
85 | got_message(rx_buffer);
86 | send_message(client, rx_buffer);
87 | rx_buffer = "";
88 |
89 | } else {
90 | rx_buffer += c;
91 | }
92 | }
93 | }
94 |
95 | // Send messages from Serial to the connected client
96 | // Delimited by newlines
97 | if(Serial.available()) {
98 | while(Serial.available()) {
99 |
100 | char c = Serial.read();
101 |
102 | if(c == '\n') {
103 | send_message(client, tx_buffer);
104 | tx_buffer = "";
105 | } else tx_buffer += c;
106 | }
107 | }
108 |
109 | // Send regular ping messages to keep the connection open
110 | int now = millis();
111 | if (now - senttime > PING_TIME) {
112 | client.println(PING_MSG);
113 | senttime = now;
114 | }
115 | }
116 |
117 | // Give the client time to receive data before closing
118 | Serial.println("Disconnecting from client.");
119 | delay(100);
120 | client.flush();
121 | client.stop();
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/presentation.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hmartiro/android-arduino-wifi/61a68dee9226d32354d7177de8a64faab9bd40b5/presentation.pdf
--------------------------------------------------------------------------------