Arduino wifi with android

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

ayoubElhoucine/Connect-Android-to-Arduino-via-Wifi

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Android wifi is a demo aplication that shows you how to connect and send messages to Arduino via wifi communication , the demo android app is also provided in google play under the link https://play.google.com/store/apps/details?id=com.tofaha.Android_wifi . the communication system based on sever/client communication , in which android app act like client and the arduino program as server.

in the android side the steps to communicate to arduino is very easy , the socket client is used for that , the socket client needs two parameters to find the tareget wifi module the first parameter is the wifi module IP address , and the seconde parameter is the port number , well the first thing you need to do is to open a socket connection , the server side (arduino with wifi module) has to be launched first and then the client (android device) will reach the server by the ip address and port number , you should be aware of where you instanciate the socket because it has to be instanciated in diffrent thread then the UI thread , if you do it in UI thread the app will crash , here sample how to open socket connection inside asyncTask:

class OpenConnection(private val ipAddress: String, private val portNumber: Int) : AsyncTask() < override fun doInBackground(vararg voids: Void): Void? < try < MyData.socket = Socket(ipAddress, portNumber) System.out.println("connection opened") >catch (e: IOException) < e.printStackTrace() >return null > > 

and to send messages to server , you will need printWriter object to write in socket ibject that you already created and the socket will do the rest , here how you can do it :

Читайте также:  Фуджита зум смарт вай фай

class SendMessages(msg: String) : AsyncTask()

internal var msg = "" init < this.msg = msg >override fun doInBackground(vararg voids: Void): Void? < try < val out = PrintWriter(BufferedWriter(OutputStreamWriter(MyData.socket .getOutputStream())), true) out.println(msg) println("message send") >catch (e: IOException) < e.printStackTrace() >return null > 

and to recieve messages from server , all you need to do is create an instance of bufferReader and pass the socket to it as parameter , and of course you need to do that inside loop with time out as optional , so whenever the server send message the socket object will receive it and bufferdReader will read the message from socket and you write it to the UI thread , here is the code (I used anko library in this code):

to close the connection that what you do : socket.close() method :

class CLoseConnection : AsyncTask()

override fun doInBackground(vararg params: Void?): Void? < try < MyData.socket?.close() System.out.println("connection closed") >catch (e: IOException) < e.printStackTrace() >return null > 

Server Side (Arduino with wifi module)

in the server side I use arduino with wifi module(ESP8266) , well the first thing you need to consider is how to communicate arduino with the module , the communication will be done by the serial communication(Rx/Tx) , arduino board contains UART(Universal Asynchronious Receiver Transmmiter) , and the number of UART that arduino board contain is depend on the type of arduino like arduino uno has only one UART , so we are going to use the library of the serialSoftwar in arduino and by this library we can write and read to serial buffer (Rx/Tx) , after that we attach the Rx and Tx pin to Tx and Rx pin of the ESP8266 , this is how to communicate arduino with ESP8266 :

Communicate arduino with esp module

#include SoftwareSerial ESP8266(2,3); //(Rx/Tx) that means you have to attach pin 2 with esp Tx and pin 3 with esp Rx 

ESP configuration and setup with AT commands

after the communication is established you now need to configure ESP as you need it to work , and that will be done by the ATcommand , in arduino IDE we will type the ATcommand and write it to the serialSoftware :

void sendESP8266Cmdln(String cmd, int waitTime) < ESP8266.println(cmd); delay(waitTime); clearESP8266SerialBuffer(); >void setup() < Serial.begin(9600); ESP8266.begin(115200); // change this value to your wifi module (esp8266) baud rate do< ESP8266.println("AT"); delay(1000); if(ESP8266.find((char*)"OK")) < Serial.println("Module is ready"); delay(1000); clearESP8266SerialBuffer(); //configure ESP as station sendESP8266Cmdln("AT+CWMODE=1",1000); //Join Wifi network sendESP8266Cmdln("AT+CWJAP="+ssid+","+pass,6500); //Get and display my IP sendESP8266Cmdln("AT+CIFSR", 1000); //Set multi connections sendESP8266Cmdln("AT+CIPMUX=1", 1000); //Setup web server on port 80 sendESP8266Cmdln("AT+CIPSERVER=1,3333",1000); Serial.println("Server setup finish"); FAIL_8266 = false; >else < Serial.println("Module have no response."); delay(500); FAIL_8266 = true; >>while(FAIL_8266); ESP8266.setTimeout(100); > 

Receive messages from client

after the configuration and setup of the ESP now you are ready to send and recieve data from client : to recieve data from client is very easy step , first you need to prepare the server and that will be done in the setup part , after that you write this code inside the loop method :

Читайте также:  Все открытые сети wifi

to send data to client , well in this case to establish the client you need communication id , so if there are for exemple 5 client , so you need 5 communication id to send to each client , and in the case there is only one client you can use 0 as the value of the communication id :

connectionId = 0 ; void sendStringResponse(int connectionId, String content) < sendCIPData(connectionId,content); >void loop() < if ( Serial.available() )< String s = Serial.readString(); sendStringResponse(connectionId , s + "\r\n"); >> 

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Demo of a clear and simple way to interface Android and Arduino over a WiFi connection

License

hmartiro/android-arduino-wifi

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Demo of a clear and robust way of creating a serial communication link between an Arduino and an Android device using WiFi.

  • WiFi server using the WiFly Shield and associated library on Arduino
  • WiFi client socket implemented using AsyncTask in Android
  • UI for connecting, disconnecting, reading, and writing
  • Exception handling and error management to prevent crashes
  • Parsing of complete newline-delimited messages from the stream
  • Timeout detection using a ping system
  • Lots of comments

Requirements:

  • Android device with WiFi enabled
  • Arduino with a WiFly shield
  • Android Studio and an Arduino IDE
  • A WiFi network to connect to and a not-blocked port
  • Arduino — set your WiFi network name and password in Credentials.h .
  • Arduino — set your desired port number in wifi_demo.ino .
  • Flash the Arduino, then turn it on and open the serial monitor at 115200bps.
  • Wait for the WiFly to get an IP address.
  • Open the Android app, enter the address and port, and hit connect.
  • You should now be able to send messages and have them echoed back.
  • You can also type messages into the Aruino serial monitor to send them to the Android device (select newline endings).
Читайте также:  Ноутбук самсунг раздает wifi

Important files:

  • WiFly takes a long time (~20s) to connect to a network on startup (watch the LEDs).
  • After disconnecting from a client, the WiFly takes about 6 seconds before it can connect to another client (again, watch the LEDs).
  • I had an issue with a version of the WiFly library where it would not initialize until it heard a *READY* message from the chip, which never came. If you get stuck at WiFly.begin(), look into this.

About

Demo of a clear and simple way to interface Android and Arduino over a WiFi connection

Источник

Can I connect an Arduino wifi module directly to my android phone?

I would like to use my arduino even where there’s no wifi router. I’ve only found people saying that it needs to connect to a router, but isn’t the phone like a router when it’s in tethering mode? Do I still need a mobile internet connection or can I do it directly?

2 Answers 2

A phone running in wifi tethering (mobile hotspot) mode is no different to a wifi access point or router. So yes, you can use that.

Chances are though that you will need a mobile internet connection in order to activate mobile hotspot mode in the first place.

My Galaxy Mini 2 lets me turn mobile router while not connected to mobile connection. The matter’s that the example sketches that I’ve seen by now are all something like this: connect to a router, get the ip, and then from android you put the ip and connect to the arduino. I’d like to skip the ip part: arduino connects to the mobile router, and (since when you connect to a router you need to send data back and forth from router to user and vice versa) they exchange data even if the mobile phone has no connection. Can I do this?

You need the IP. Without that you don’t know where to send data. You may be able to find it in the tethering settings under the list of connected devices depending on the version of Android.

So I always need an internet connection. I thought it would have worked like bluetooth, since router and user need to talk to each other, but what you’re saying suggests me I can’t. Unless I can use the «internal IP» (don’t know if it is its name), the one that is always 192.something. If I can use that, isn’t it given before the connection to the internet starts?

An IP is an IP. Every device connected on a network needs one. The phone can hand one to the Arduino through DHCP. You don’t need an internet connection for that, not if you can turn on mobile hotspot without one. It sounds like you need a crash course in how networks work. Browse around wikipedia a bit and learn the basics first.

Источник

Оцените статью
Adblock
detector