Saludos, estoy tratando de enviar un mensaje a la API de telegram desde mi placa arduino uno con una placa Ethernet shield pero no logro hacerlo, me bota un error, este es el codigo que he aplicado:
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
// Configurar la dirección IP, máscara de subred y dirección IP del gateway
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192, 168, 0, 177);
IPAddress subnet(255, 255, 255, 0);
IPAddress gateway(192, 168, 0, 1);
IPAddress myDns(192, 110, 40, 105);
// Configurar el token de acceso del bot de Telegram
String token = "{token goes here}";
// Configurar el chat ID del destinatario del mensaje
String chat_id = "{chat ID goes here}";
void setup() {
Serial.begin(9600);
while (!Serial) {}
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to configure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
}
// Inicializar la conexión Ethernet
//Ethernet.begin(mac, ip, gateway, subnet);
// Enviar el mensaje a Telegram
sendMessageToTelegram("Hola desde Arduino!");
}
void loop() {
// No hay nada más que hacer en el bucle principal
}
void sendMessageToTelegram(String message) {
// Crear un objeto JSON que contenga el mensaje que se desea enviar
DynamicJsonDocument doc(200);
doc["chat_id"] = chat_id;
doc["text"] = message;
// Convertir el objeto JSON a una cadena de caracteres
String json;
serializeJson(doc, json);
// Establecer la conexión con la API de Telegram
EthernetClient client;
if (!client.connect("api.telegram.org", 443)) {
Serial.println("Error al conectar con la API de Telegram");
return;
}
// Enviar la solicitud HTTP POST a la API de Telegram
client.println("POST /bot" + token + "/sendMessage HTTP/1.1");
client.println("Host: api.telegram.org");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println("Content-Length: " + String(json.length()));
client.println();
client.println(json);
// Leer la respuesta de la API de Telegram
String response;
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
while (client.available()) {
char c = client.read();
response += c;
}
Serial.println(response);
}
Pero el monitor serial me bota el siguiente error:
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
Entiendo que es por el tema de Https, saben si existe alguna manera de poder enviar este mensaje con el hardware que tengo?
Moderator edit: sensitive material redacted