Hello all,
I’m looking to eventually query my Photon from my Android phone without requiring the cloud connection to be operational.
I’m attempting to use a TCP connection to communicate between my Android phone and the Photon (where the phone is a wireless hotspot not connected to the internet).
Whenever I attempt to get them to connect to one another, I can get the client (Android app) to connect to the server (Photon), but I can’t get the Photon to find that client.available() == true, but have already confirmed that client.connected() == true, previously. The TCPClient helper class also seems to get stuck after the initial message is sent to the Photon.
This is my Photon firmware:
SYSTEM_MODE(MANUAL);
TCPServer server = TCPServer(23);
TCPClient client;
void setup() {
WiFi.on();
WiFi.setCredentials("AndroidAP", "password");
WiFi.connect();
// Make sure your Serial Terminal app is closed before powering your device
Serial.begin(9600);
// Now open your Serial Terminal, and hit any key to continue!
// start listening for clients
server.begin();
}
void loop() {
if (client.connected()) {
// echo all available bytes back to the client
while (client.available() > 0) {
server.write("200");
Serial.println("200");
Serial.println(WiFi.SSID());
Serial.println(WiFi.localIP());
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
}
This is my MainActivity.java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private TCPClient mTcpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.send_button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// connect to the server
new connectTask().execute("message1");
}
});
}
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
//we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
Log.d("Message", message);
}
});
mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.d("values", values[0]);
}
}
}
and my TCPClient helper class:
import android.util.Log;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClient {
private String serverMessage;
public static final String SERVERIP = "192.168.43.157"; //your Photon (formerly computer) IP address
public static final int SERVERPORT = 23;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
PrintWriter out;
BufferedReader in;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public TCPClient(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
* @param message text entered by client
*/
public void sendMessage(String message){
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
}
public void stopClient(){
mRun = false;
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
//send the message to the server
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
// Code never seems to be able to execute past this point...
//receive the message which the server sends back
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the server
while (mRun) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
}
serverMessage = null;
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
The TCPClient seems to get stuck waiting for the response from the Photon.
Has anyone run into these problems before, or got a potential solution? Or maybe even have a past project that they would be willing to share?
Many thanks!