I want to send a message from android app to NodeMCU. Here is the codes :
Android Studio (MainActivity.java) :
package com.send.client.socket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.IOException;
import java.io.PrintWriter;
import java.Socket;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnSendOnClick(View v) {
Thread send = new Thread() {
@Override
public void run() {
try {
Socket socket = new Socket("192.168.1.1", 4444);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("Hellow World");
} catch (IOException e) {
e.printStackTrace();
}
}
};
send.start();
}
}
NodeMCU (Arduino.ino) :
#include <ESP8266WiFi.h>
const char ssid[] = "NodeMCU" ;
const char pass[] = "12345678";
char ch;
String msg;
IPAddress ip(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(4444);
WiFiClient client;
void setup()
{
Serial.begin(9600);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, pass);
delay(100);
WiFi.softAPConfig(ip, ip, subnet);
server.begin();
}
void loop()
{
client = server.available();
if (client.available() > 0)
{
ch = NULL;
msg = "";
while (client.available() > 0)
{
ch = client.read();
msg += ch;
}
Serial.print(msg);
}
}
The codes works to some extent but it is unstable, I have to tap the send button of app a lot of times (10 times or so) in order to send one message! I am sure about NodeMCU side because by using another C# socket programming I can send the message instantly.
I want to send a message from android app to NodeMCU. Here is the codes :
Android Studio (MainActivity.java) :
package com.send.client.socket;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnSendOnClick(View v) {
Thread send = new Thread() {
@Override
public void run() {
try {
Socket socket = new Socket("192.168.1.1", 4444);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("Hellow World");
} catch (IOException e) {
e.printStackTrace();
}
}
};
send.start();
}
}
NodeMCU (Arduino.ino) :
#include <ESP8266WiFi.h>
const char ssid[] = "NodeMCU" ;
const char pass[] = "12345678";
char ch;
String msg;
IPAddress ip(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(4444);
WiFiClient client;
void setup()
{
Serial.begin(9600);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, pass);
delay(100);
WiFi.softAPConfig(ip, ip, subnet);
server.begin();
}
void loop()
{
client = server.available();
if (client.available() > 0)
{
ch = NULL;
msg = "";
while (client.available() > 0)
{
ch = client.read();
msg += ch;
}
Serial.print(msg);
}
}
The codes works to some extent but it is unstable, I have to tap the send button of app a lot of times (10 times or so) in order to send one message! I am sure about NodeMCU side because by using another C# socket programming I can send the message instantly.
You should make sure that network requests are asynchronous, and performing network operations on the main thread can easily cause the interface to get stuck or unresponsive
public void btnSendOnClick(View v) {
executorService.execute(new Runnable() {
@Override
public void run() {
Socket socket = null;
PrintWriter writer = null;
try {
socket = new Socket("192.168.1.1", 4444);
writer = new PrintWriter(socket.getOutputStream(), true);
writer.println("Hello World");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
Then close at the end
@Override
protected void onDestroy() {
super.onDestroy();
executorService.shutdown();
}
You can also try using Kotlin's coroutine, which is very convenient