我正在用Arduino Ide (v1.8.13)开发一个程序,它获取亮度值并将它们发送到ESP32 Web Server。另外,我还想增加通过电报查看数据的可能性。目前,我只想向Telegram机器人和Arduino程序发送“/start”,并以欢迎消息作为回答。然而,机器人没有收到我的消息。我读到这是ArduinoJson,通用电报机器人,Arduino Ide和ESP32核心版本之间的版本问题。谁能告诉我哪些版本可以在ESP32中正常工作?我都快疯了。
我添加了我的实际代码。
以下是我使用的版本: Arduino Ide:1.8.13,Arduino Json:6.18,Universal Telegram Bot:1.3.0,Wifi Client Secure:1.0,ESP32:1.04。
提前感谢:)
// Import required libraries
#include <WiFi.h>
#include <Wire.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <TimeAlarms.h>
#include <Time.h>
#include <TimeLib.h>
const char* ssid_STA = "XXXXXX";
const char* password_STA = "XXXXXX";
IPAddress ip(192,168,1,44);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
#define BOTtoken "XXXXXX"
#define CHAT_ID "XXXXXXX"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
time_t fecha;
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
Serial.println("------------------------------------");
Serial.print ("Gain: "); Serial.println("Auto");
Serial.print ("Timing: "); Serial.println("13 ms");
Serial.println("------------------------------------");
}
String readLuz() {
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
Serial.print(event.light); Serial.println(" lux");
return String(event.light);
}
void Informacion(){
Serial.println("voy a enviar");
sensors_event_t event;
tsl.getEvent(&event);
//bot.sendMessage(chat_ID, "La luminosidad en tu jardín es de " ,"");
Serial.println("enviado");
}
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
Serial.println(chat_id);
if (chat_id != CHAT_ID){
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}
//COger texto
String text = bot.messages[i].text;
Serial.println(text);
//Coger nombre
String from_name = bot.messages[i].from_name;
//if (text == "/get_info") {
//}
if (text == "/start") {
String welcome = "Bienvenida "+from_name+"\n";
welcome += "Properties.\n\n";
welcome += "/get_info : Obtener información actual del jardín\n";
bot.sendMessage(chat_id, welcome, "");
}
}
}
/**************************************************************************/
/*
Arduino setup function (automatically called at startup)
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(115200);
// client.setInsecure();
/* Initialise the sensor */
//use tsl.begin() to default to Wire,
//tsl.begin(&Wire2) directs api to use Wire2, etc.
if(!tsl.begin())
{
/* There was a problem detecting the TSL2561 ... check your connections */
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on this sensor and Setup the sensor gain and integration time */
//displaySensorDetails();
configureSensor();
///EJEMPLO SREVER
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
;
// Connect to Wi-Fi
//WiFi.softAP(ssid_AP, password_AP);
WiFi.mode(WIFI_STA);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid_STA, password_STA);
while (WiFi.status() != WL_CONNECTED) {
delay(3000);
Serial.println("Connecting to WiFi..");
}
// Print ESP32 Local IP Address
Serial.println("IP");
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html");
});
server.on("/luz", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readLuz().c_str());
});
// Start server
server.begin();
/*
//ALARMS
//Coger la hora real al reiniciar
//fecha = now();
//Serial.print(hour(fecha)); Serial.print(minute(fecha)); Serial.print(second(fecha)); Serial.print(day(fecha)); Serial.print(month(fecha)); Serial.println(year(fecha));
//setTime(hour(fecha), minute(fecha),second(fecha), day(fecha), month(fecha),year(fecha));
setTime(12, 42, 00, 16, 6, 21);
// Crear las alarmas y configurar las funciones correspondientes a cada una
Alarm.alarmRepeat(8, 0, 0, Informacion); // Evento a las 14:00 diario
Alarm.alarmRepeat(12,43, 0, Informacion); // Evento a las 14:05 diario
Alarm.alarmRepeat(21, 0, 0, Informacion); // Evento a las 14:10 diario
*/
}
/**************************************************************************/
/*
Arduino loop function, called once 'setup' is complete (your own code
should go here)
*/
/**************************************************************************/
void loop(void){
if (millis() > lastTimeBotRan + botRequestDelay) {
Serial.println("loop");
Serial.println(bot.last_message_received);
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeBotRan= millis();
}
// Mostrar el reloj en el monitor serial
//digitalClockDisplay();
// Esperar 1 segundo y procesar las Alarmas mientras tanto...
//Alarm.delay(1000);
}
/*
void digitalClockDisplay() {
Serial.print(hour());
printDigits(minute());
//printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
*/
发布于 2021-09-17 06:55:07
我想你没有听懂这句话
// Add root certificate for api.telegram.org
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
https://stackoverflow.com/questions/68020536
复制相似问题