How to send data throught Wi-Fi with Kotlin to an Arduino with ESP8266-01S

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật