Salve a tutti, sono due giorni che sto cercando di venire a capo di questo dilemma senza riuscirci. In pratica ho collegato il NodeMCU v3 a telegram da cui posso accendere un led, comandare un relay e conoscere la temperatura. Tutto funziona perfettamente fin qui, poi decisi di collegare anche un sensore pir che mi facesse da antifurto. Il problema è che una volta attivato il sensore vorrei ricevere una notifica se rileva qualche movimento; ho provato diverse combinazioni, fatto ricerche su google ma non riesco a risolvere questo problema. Spero qualcuno mi aiuti. Vi allego il codice di seguito. Grazie mille
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include "DHT.h"
// Initialize Wifi connection to the router
#define WIFI_SSID "xxxxxx"
#define WIFI_PASSWORD "xxxxx"
#define BOTtoken "xxx"
#define LED_PIN D3
#define RELAY_PIN D6
#define DHT_PIN D5
#define DHTTYPE DHT22
#define pir D7
#define BOT_SCAN_MESSAGE_INTERVAL 1000
long lastTimeScan;
bool ledStatus = false;
bool relayStatus = false;
bool statePir = false;
byte intruso = 0;
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
DHT dht(DHT_PIN, DHTTYPE);
void handleNewMessages(int numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Convidado";
if(text == "/pirON"){
digitalWrite(pir, HIGH);
statePir = true;
bot.sendMessage(chat_id, "Antifurto acceso", "");
}
if(intruso = digitalRead(pir)){
if(intruso == 1){
bot.sendMessage(chat_id, "ATTENZIONE, INTRUSO", "");
}
}
if(text == "/pirOFF"){
statePir = false;
digitalWrite(pir, LOW);
bot.sendMessage(chat_id, "Antifurto spento", "");
}
if (text == "/ledON") {
digitalWrite(LED_PIN, HIGH); // turn the LED on (HIGH is the voltage level)
ledStatus = true;
bot.sendMessage(chat_id, "Led acceso", "");
}
if (text == "/ledOFF") {
ledStatus = false;
digitalWrite(LED_PIN, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Led spento", "");
}
if (text == "/relayON") {
digitalWrite(RELAY_PIN, HIGH);
relayStatus = true;
bot.sendMessage(chat_id, "Relè attivo", "");
}
if (text == "/relayOFF") {
relayStatus = false;
digitalWrite(RELAY_PIN, LOW);
bot.sendMessage(chat_id, "Relè non attivo", "");
}
if (text == "/status") {
String message = "Led ";
if(ledStatus){
message += "acceso";
}else{
message += "spento";
}
message += ". \n";
message += "Relè ";
if(relayStatus){
message += "attivo";
}else{
message += "non attivo";
}
message += ". \n";
message += "Antifurto ";
if(statePir){
message += "acceso";
}else{
message += "spento";
}
message += ". \n";
bot.sendMessage(chat_id, message, "Markdown");
}
if( text == "/temp") {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
String message = "La temperatura é di " + String(temperature, 2) + " °C.\n";
message += "L' umidità dell'aria é del " + String(humidity, 2)+ "%.\n\n";
bot.sendMessage(chat_id, message, "Markdown");
}
// Cria teclado com as opções de comando
if (text == "/options") {
String keyboardJson = "[[\"/ledON\", \"/ledOFF\"],[\"/relayON\", \"/relayOFF\"],[\"/pirON\", \"/pirOFF\"],[\"/temp\",\"/status\"]]";
bot.sendMessageWithReplyKeyboard(chat_id, "Scegli una opzione", "", keyboardJson, true);
}
// Comando de inicio de conversa no telegram
if (text == "/start") {
String welcome = from_name + ", Benvenuto.\n";
welcome += "Usa uno dei seguenti comandi per svolgere una funzione.\n\n";
welcome += "/ledON : per accendere il Led \n";
welcome += "/ledOFF : per spegnere il Led \n";
welcome += "/relayON : per attivare il Relè \n";
welcome += "/relayOFF : per disattivare il Relè \n";
welcome += "/pirON : per accendere l'antifurto \n";
welcome += "/pirOFF : per spegnere l'antifurto \n";
welcome += "/temp : per conoscere temperatura e umidità dell' ambiente \n";
welcome += "/status : per conoscere lo stato dei sensori \n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setupWifi(){
// attempt to connect to Wifi network:
// Serial.print("Connecting Wifi: ");
// Serial.println(ssid);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
// Serial.print(".");
delay(500);
}
}
void setupPins(){
pinMode(LED_PIN, OUTPUT);
pinMode(pir, INPUT);
pinMode(RELAY_PIN, OUTPUT);
delay(10);
digitalWrite(LED_PIN, LOW);
digitalWrite(RELAY_PIN, LOW);
dht.begin();
}
void setup() {
// Serial.begin(115200);
setupWifi();
setupPins();
lastTimeScan = millis();
}
void loop() {
if (millis() > lastTimeScan + BOT_SCAN_MESSAGE_INTERVAL) {
// Serial.print("Checking messages - ");
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
// Serial.println(numNewMessages);
while(numNewMessages) {
// Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
lastTimeScan = millis();
}
yield();
delay(10);
}