I had used kotlin to connect with wifi to an ESP32. The ESP32 is a web-server with a local IP, mobile app is connected to the same local network. So on the mobile app we create a json message to post it to the ESP32 IPS, something like this:
ui.button.setOnClickListener {
ui.visorTexto.setText("")
//val url = "http://192.168.100.235"
val url = ui.urlTexto.text.toString()
val jsonBody = JSONObject()
//val valorPin = ui.pinTexto.text.toString().toIntOrNull()
//val valorEstado = ui.estadoTexto.text.toString().toIntOrNull()
val valorPin = 1
val valorEstado = 1000
if (valorPin != null) {
if(valorPin in 0..13){
if (valorEstado != null) {
if(valorEstado >= 0 && valorEstado <= 2000){
jsonBody.put("opcion", valorPin.toString())
jsonBody.put("tiempo", valorEstado.toString())
val jsonObjectRequest = JsonObjectRequest(Request.Method.POST, url, jsonBody,
{ response -> ui.visorTexto.setText(response.toString())}
) { error -> ui.visorTexto.setText(error.printStackTrace().toString()) }
jsonObjectRequest.retryPolicy = DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
val cache = DiskBasedCache(cacheDir, 1024 * 1024) // 1MB cap
val network = BasicNetwork(HurlStack())
var requestQueue = RequestQueue(cache, network).apply {
start()
}
requestQueue.add(jsonObjectRequest)
} else{
ui.visorTexto.setText("Error el estado es incorrecto: ${valorEstado}")
}
}
else{
ui.visorTexto.setText("Error2")
}
} else{
ui.visorTexto.setText("Error el rango de pin es incorrecto")
}
}
else{
ui.visorTexto.setText("Error1 ${valorPin}")
}
}
So every time I press the button, the listener will make a json message with the contents of two lineEdits, it will then packed the message and post it to a html conection to the esp32's IPs.