Buenas noches, tengo error -301
3 views (last 30 days)
Show older comments
Espero puedan ayudarme por favor, explico mi problema, en este momento estoy manejando una ESP32 mediante Arduino, que conectado con un sensor de movimiento, envíe notificaciones a Telegram y a su vez, necesito que envíe datos a ThingSpeak, he hecho todo lo posible, pero cuando va a enviar la información a ThingSpeak, aparece el error -301 en el monitor serial, dejaré el código para ver si me pueden ayudar, agradezco su ayuda. Quisiera clarar que no es problema de red ya que lo he intentado con diferentes redes locales, gracias.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <ThingSpeak.h>
const char* ssid = "******";
const char* password = "*******";
#define BOTtoken "*********" // Tu Bot Token (Obtener de Botfather)
#define CHAT_ID "*******"
unsigned long channelID = ********;
const char * writeAPIKey = "*********";
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
const int buzPin = 25;
const int motionSensor = 27;
bool motionDetected = false;
// Indica cuando se detecta movimiento
void IRAM_ATTR detectsMovement() {
motionDetected = true;
}
void handleNewMessages(int numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Usuario No Autorizado", "");
continue;
}
String text = bot.messages[i].text;
if (text == "/alarma_on") {
bot.sendMessage(chat_id, "Alarma activada", "");
digitalWrite(buzPin, HIGH);
}
if (text == "/alarma_off") {
bot.sendMessage(chat_id, "Alarma desactivada", "");
digitalWrite(buzPin, LOW);
}
}
}
void setup() {
Serial.begin(115200);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
pinMode(buzPin, OUTPUT);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay(5000);
Serial.println("Inicializando ThingSpeak...");
ThingSpeak.begin(client);
Serial.println("ThingSpeak inicializado correctamente");
bot.sendMessage(CHAT_ID, "Bot iniciado", "");
}
void loop() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages) {
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if(motionDetected){
bot.sendMessage(CHAT_ID, "Movimiento detectado!!", "");
motionDetected = false;
delay(5000);
int writeResult = ThingSpeak.writeField(channelID, 1,1, writeAPIKey);
if (writeResult == 200) {
Serial.println("Datos enviados correctamente a ThingSpeak");
} else {
Serial.print("Error al enviar datos a ThingSpeak. Código de error: ");
Serial.println(writeResult);
}
}
}
0 Comments
Answers (1)
Christopher Stapels
on 14 May 2024
The 301 error means the library is not able to find a connection that works. Either your conenction parameters are incorrect, or you are blocked from sending data on that network.
Perhaps try sending data from a different device connected to the same network (i.e you phone perhaps) using the browser window and the write api call.
0 Comments
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on Read Data from Channel in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!