Sending data to ESP8266 Wi-Fi chip from Android device - java

I have a ESP8266 chip which is connected to the microcircuit. When chip gets value "200" the light is starting to blink 4 times and than it returns "100" value. I need to make an Android app using Java which will connect to the ESP8266 chip, send data to it and will get value "100". I don't know what library I should use to deal with it. Please, help me, how can I do that? I think it is not the most hard question here.

For your Controller you dont need any Libary. You just can use the serial AT Commands: http://www.electrodragon.com/w/ESP8266
After setting up your ESP like this:
In your App you should deal with TCP-Sockets: https://de.wikibooks.org/wiki/Googles_Android/_TCP-Sockets
Try something like this in an async task:
socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), Connect_Timeout);
DataOutputStream DataOut = new DataOutputStream(socket.getOutputStream());
DataOut.writeBytes(message);
DataOut.flush();
socket.close();
So your ESP is the Server and the App the Client. This should work without problems.

Related

Android - Connect to a Bluetooth module

I have to develop an Android app to connect to a Bluetooth module that is connected to a board. My goal is to send and receive data to this board.
I'm currently able to enable the Bluetooth on my phone, to pair to the Bluetooth module but I don't know how to connect and send/receive data to this module.
Most of examples explain how to create a server and a client to communicate via sockets. Is it the good way for me? As described here : https://developer.android.com/guide/topics/connectivity/bluetooth.html#java
Following is a method:
create service class which communicate every time when it is
connected to that device.
register that service into your main activity through broadcast update.
then scan you Bluetooth devices(after validating permissions) and connect it,
note that connection code must be in your serviceclass(all communication with device is through service class).
after that you can sent data to and from the Bluetooth device.
Here attaching an example for you for working with BLE,Created by Nordic Semiconductor
Click Here
Do it like in the Example: https://developer.android.com/guide/topics/connectivity/bluetooth.html#example_1
Note that you will probably need to know what kind of service/profile the module provides. Often, generic modules/devices use the Serial Port Profile (SPP).
You use createInsecureRfcommSocketToServiceRecord() or createRfcommSocketToServiceRecord() to connect.
Which UUID you need depends on the actual service the module provides. For SPP see e.g. How to find the UUID of serial port Bluetooth device?:
The short, 16-bit UUID for SPP is
0x1101
the full UUID is
"00001101-0000-1000-8000-00805f9b34fb"
So, on Android, you'd use
final UUID SPP_SERVICE_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
final BluetoothSocket socket = device.createRfcommSocketToServiceRecord( SPP_SERVICE_UUID );
socket.connect();
final InputStream is = socket.getInputStream();
final OutputStream os = socket.getOutputStream();
// Send data to output stream and/or receive data from input stream
// ...
socket.close(); // Disconnect

Use Java to connect ethernet device

I had a board connect to the PC using LAN cable(RJ45). I need to write the Java code to connect the board and get some data from it. How can I do it?
Actually I got a code from C++, it used CAsyncSocket class to do it. The C++ code is like this:
CAsyncSocket.Create();
CAsyncSocket.connect(IP, PORT);
Now, I would like to convert it into Java. Actually, I'm not so familiar with Java. Can someone show the code to me?
Example: My board IP is 192.168.2.10 and PORT is 2000. How can I connect it using Java?
see here (for example):
Socket socket = new Socket("192.168.2.10", 2000);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Input: " + input.readLine());
socket.close();
http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html
Check out the socket tutorial Lesson: All About Sockets:
URLs and URLConnections provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application.
See the [http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html](Reading from and Writing to a Socket) example:
Let's look at a simple example that illustrates how a program can establish a connection to a server program using the Socket class and then, how the client can send data to and receive data from the server through the socket.

Socket communication between Android app and web browser

My Android device+app is continuously sending data every few ms, and I'd like to receive it on my web browser application that I'm building with JavaScript/HTML.
In the Android/Java app I do the following over socket:
//Initialize, where PORT = local ip of my laptop with web server I guess.
//and I choose an available port on my network, say 8080.
echoSocket = new Socket(HOST, PORT);
out = new PrintWriter(echoSocket.getOutputStream(), true);
//Sending data every few ms:
JSONObject j = new JSONObject();
j.put("x", params[0]);
j.put("y", params[1]);
j.put("z", params[2]);
String jString = j.toString();
out.println(jString);
So I have something like {"x": 1.0023532, "y": 2.454234, "z": 6.234583}.
In other Java applications, I've done this communication by having my receiver application create a ServerSocket on the particular PORT used above. Then as long as I have the right local IP address for my laptop, I can do serverSocket = new ServerSocket(PORT); etc.
Now, how can I accept this data in a web application (JavaScript/HTML)? I've heard of websockets but have no idea how to initialize and use for this purpose - hopefully it's pretty straightforward.
I dont think its possible to send data directly to the browser without a middle man (server). If you want to create a fast and easy server to ping data back and forth I would have some fun with NodeJs. I havent gotten a chance to ever use the stuff but I did have some fun playing with it. It could be something to look into expecially if your just pinging data back and forth between clients.
I watched this video "Introduction to Node.js with Ryan Dahl" a while ago and he showed a basic example that does pretty much what your talking about. Just a thought, plus it would be a fun and fast implementation.
On a side note I do believe Amazon AWS has a instance for Node JS if you want to bring it to a live server. Im pretty sure you can setup a micro instance for no cost.

Java DatagramSocket freezes on initialization

I'm writing a piece of UDP networking program (client - server), and I've run into some trouble.
I want to use streams to I/O data, so I googled "udp inputstream" and found UDPInputStream and UDPOutputStream. When I try to use these, however, the program gets stuck when trying to initialize the UDPOutputStream.
This is the line in my code that freezes:
outStream = new UDPOutputStream(InetAddress.getByName("127.0.0.1"), port);
System.out.println("UDP output stream initialized."); // <-- doesn't get called
I checked out the source of the UDPOutputStream, the code gets stuck on this line:
dsock = new DatagramSocket();
Why does the execution hang up on this line? On the server side, I still use my "old", non-stream version of a simple UDP code, and it works. The socket is initialized the same way and it doesn't hang up. I tried to put a port number to the initialization, but it doesn't solve the problem.
Host machines have more than one network interface (for example, 127.0.0.1 for the loopback interface and some other address for the network card; there may be more than one network card).
If you bind to the loopback address 127.0.0.1 then you'll only be able to receive packets sent locally. If want to receive packets sent over the network from a remote machine you must bind to the local IP address (e.g. 192.168.1.100).
Try following:
InetAddress addr = InetAddress.InetAddress.getLocalHost();
outStream = new UDPOutputStream(addr, port);

Android programming: Change TCP to RTP

I am working on an Android app including client and server that client app sends camera captured data to server for processing, and server sent the result back to client to display. The current version of it uses TCP for transmitting data, so the code includes something like:
Client side:
send(){
Socket socket = new Socket();
DataOutputStream outputStream = new DataoutputStream(socket.getOutPutStream());
outputStream.writeUTF(...)
...
outputStream.write(data);
outputSteram.flush();
...
}
receive(){
DataInputStream dataInputStream = new DataInputStream(...);
...
dataInputStream.readFully(data, 0, length);
...
}
Server side:
...
serverDataInputStream.readFully(data,0,length);
//process data
...
serverDataOutputStream.write(reply)
...
I tried to change this to use UDP, but the data sent from client to server is much larger than the maximum size of a UDP packet, so have to fragment the data in client and reassemble it on server manually. It turns out to be too much troublesome.
I know RTP takes care of data fragmentation, so now I want to change this into RTP. After Googleing for a while, I found the following information:
http://sourceforge.net/projects/jlibrtp/
http://code.google.com/p/sipdroid/source/browse/trunk/src/org/sipdroid/sipua/ui/VideoCamera.java
And these two posts are very similar to what I am doing:
Android Camera RTSP/RTP Stream?
Creating RTP Packets from Android Camera to Send
But the posts just include limited sample code. Can anyone post more details and sample code here? I don't have much experience of Java and Android programming. So detailed examples or step by step instruction will be extremely appreciated!
Thank you!

Categories