I’m working on a project where I send information via Wi-Fi between an app and an Arduino Nano. I need to send simple text to an ESP8266-01S Wi-Fi module, which then communicates with the Arduino. The text will represent values such as temperature or light, and I also need to receive this text back and translate it to actual values.
Currently, I have both the phone and the Arduino connected to the same network, and I send data to the private IP of the ESP8266.
I’m new to Kotlin, and I’m using the following code to send information. Later, I will try to receive information from the Arduino.
import android.app.Activity
import android.os.Bundle
import android.widget.Button
import com.example.proyectoarduino.R
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
class WifiActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_inicio)
val btnPrenderLuces = findViewById<Button>(R.id.BotonPrenderLuces)
val btnApagarLuces = findViewById<Button>(R.id.BotonApagarLuces)
val ipArduino = "192.168.136.13"
btnPrenderLuces.setOnClickListener {
enviarComandoAlArduino(ipArduino, "/LED=ON")
}
btnApagarLuces.setOnClickListener {
enviarComandoAlArduino(ipArduino, "/LED=OFF")
}
}
private fun enviarComandoAlArduino(ipArduino: String, comando: String) {
Thread {
try {
val url = URL("https://$ipArduino$comando")
val connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.connect()
val responseCode = connection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
val response = connection.inputStream.bufferedReader().use { it.readText() }
println("Respuesta del servidor: $response")
} else {
println("Error en la conexión: $responseCode")
}
connection.disconnect()
} catch (e: Exception) {
e.printStackTrace()
}
}.start()
}
}
Also this is the code of the ESP8266
#include <ESP8266WiFi.h>
const char* ssid = "MySSID";
const char* password = "MyPassword";
WiFiServer server(80);
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
while(!client.available()){
delay(1);
}
String request = client.readStringUntil('r');
client.flush();
if (request.indexOf("/LED=ON") != -1) {
Serial.println("ON");
}
if (request.indexOf("/LED=OFF") != -1) {
Serial.println("OFF");
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>Control de LED</title></head>");
client.println("<body>");
client.println("<h1>Control de LED</h1>");
client.println("<p><a href="/LED=ON"><button>Encender LED</button></a></p>");
client.println("<p><a href="/LED=OFF"><button>Apagar LED</button></a></p>");
client.println("</body>");
client.println("</html>");
}
There are no problems on the physical side of this project, as I can search for the IP address of the ESP on my phone and successfully turn the lights on and off.
Feel free to ask any questions or request more code. Sorry if my English is a bit off.