Ciao a tutti, sto lavorando a un progetto con Nodemecu V3, e scheda SD Sunfounder, per comandare dei led e ricevere notifiche con Telegram, in poche parole scrivo da pc 3 file, password, ssid, token, e le trasferisco al mio Nodemecu, ho provato il programma in vari step e tutto funziona, ossia, riesco a collegarmi con Telegram, comandare e ricevere stato ingressi, altro programma riesco a leggere i file delle credenziali, nel programma complessivo, ovvero comunicazione con Telegram e scheda SD il programma si pianta nella seguente istruzione:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("?");
delay(500);
}
tutto questo l'ho verificato con il Serial Monitor, e chiedo a voi un aiuto, perché mi accade ciò, di seguito posto i due programmi, vi ringrazio in anticipo per le info
Programma funzionante senza SD
}
if ((digitalRead(imp1, 5) == LOW) && (allarmeimp1_5 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
/******* MANDO ALLARME RIENTRATO ******/
if ((digitalRead(imp1, 0) == HIGH) && (allarmeimp1_0 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 1) == HIGH) && (allarmeimp1_1 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 2) == HIGH) && (allarmeimp1_2 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 3) == HIGH) && (allarmeimp1_3 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 4) == HIGH) && (allarmeimp1_4 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 5) == HIGH) && (allarmeimp1_5 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
}
}
Programma con problemi di connessione
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <pcf8574.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h> // https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
PCF8574 out1(0x24);
PCF8574 imp1(0x20);
/***** FILE NELLA SD *****/
File f_token;
File f_ssid;
File f_passw;
char TOKEN[50] = ""; // Mio Token Telegram scritto su scheda Sd
char SSID[50] = ""; // Mio SSID scritto su scheda Sd
char PASSWORD[50] = ""; // Mia Password scritta su scheda Sd
// Credenziali Wifi
#define WIFI_SSID SSID
#define WIFI_PASSWORD PASSWORD
#define BOT_TOKEN TOKEN // BOT Telegram Token (Get from Botfather)
const unsigned long BOT_MTBS = 1000; // tempo medio tra i messaggi di scansione
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // l'ultima volta che è stata eseguita la scansione dei messaggi
/***** PER TEST STATO USCITE *****/
int impStatus0 = 0;
int impStatus1 = 0;
int impStatus2 = 0;
int impStatus3 = 0;
/***** PER APPOGGIO ALLARMI ******/
int allarmeimp1_0 = 0;
int allarmeimp1_1 = 0;
int allarmeimp1_2 = 0;
int allarmeimp1_3 = 0;
int allarmeimp1_4 = 0;
int allarmeimp1_5 = 0;
void handleNewMessages(int numNewMessages) {
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++) {
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Guest";
/******* TEST INGRESSI PER TRASMETTERE ALLARMI ******/
if ((digitalRead(imp1, 0) == LOW) && (allarmeimp1_0 == 0)) {
bot.sendMessage(chat_id, "ALLARME 1 TASTO PREMUTO", "");
allarmeimp1_0 = 1;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 0) == HIGH) && (allarmeimp1_0 == 1)) {
bot.sendMessage(chat_id, "ALLARME 1 TASTO RIENTRATO", "");
allarmeimp1_0 = 0;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 1) == LOW) && (allarmeimp1_1 == 0)) {
bot.sendMessage(chat_id, "ALLARME 2 TASTO PREMUTO", "");
allarmeimp1_1 = 1;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 1) == HIGH) && (allarmeimp1_1 == 1)) {
bot.sendMessage(chat_id, "ALLARME 2 TASTO RIENTRATO", "");
allarmeimp1_1 = 0;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 2) == LOW) && (allarmeimp1_2 == 0)) {
bot.sendMessage(chat_id, "ALLARME 3 TASTO PREMUTO", "");
allarmeimp1_2 = 1;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 2) == HIGH) && (allarmeimp1_2 == 1)) {
bot.sendMessage(chat_id, "ALLARME 3 TASTO RIENTRATO", "");
allarmeimp1_2 = 0;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 3) == LOW) && (allarmeimp1_3 == 0)) {
bot.sendMessage(chat_id, "ALLARME 4 TASTO PREMUTO", "");
allarmeimp1_3 = 1;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 3) == HIGH) && (allarmeimp1_3 == 1)) {
bot.sendMessage(chat_id, "ALLARME 4 TASTO RIENTRATO", "");
allarmeimp1_3 = 0;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 4) == LOW) && (allarmeimp1_4 == 0)) {
bot.sendMessage(chat_id, "ALLARME 5 TASTO PREMUTO", "");
allarmeimp1_4 = 1;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 4) == HIGH) && (allarmeimp1_4 == 1)) {
bot.sendMessage(chat_id, "ALLARME 5 TASTO RIENTRATO", "");
allarmeimp1_4 = 0;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 5) == LOW) && (allarmeimp1_5 == 0)) {
bot.sendMessage(chat_id, "ALLARME 6 TASTO PREMUTO", "");
allarmeimp1_5 = 1;
i = 0;
text = 0;
}
if ((digitalRead(imp1, 5) == HIGH) && (allarmeimp1_5 == 1)) {
bot.sendMessage(chat_id, "ALLARME 6 TASTO RIENTRATO", "");
allarmeimp1_5 = 0;
i = 0;
text = 0;
}
/******* TEST ACCENSIONE USCITE ******/
if (text == "/ledon") {
digitalWrite(out1, 0, HIGH); // turn the LED on (HIGH is the voltage level)
impStatus3 = 1;
bot.sendMessage(chat_id, "Led is ON", "");
}
if (text == "/ledoff") {
impStatus3 = 0;
digitalWrite(out1, 0, LOW); // turn the LED off (LOW is the voltage level)
bot.sendMessage(chat_id, "Led is OFF", "");
}
/******* TEST STATO IMPIANTO ******/
if (text == "/status") {
if (impStatus3) {
bot.sendMessage(chat_id, "Led is ON", "");
} else {
bot.sendMessage(chat_id, "Led is OFF", "");
}
if (digitalRead(imp1, 0) == LOW) { // (text == "/allarme") // if (digitalRead(imp1, 3) == LOW)
bot.sendMessage(chat_id, "ALLARME 1 TASTO PREMUTO", "");
}
if (digitalRead(imp1, 1) == LOW) { // (text == "/allarme") // if (digitalRead(imp1, 3) == LOW)
bot.sendMessage(chat_id, "ALLARME 2 TASTO PREMUTO", "");
}
if (digitalRead(imp1, 2) == LOW) { // (text == "/allarme") // if (digitalRead(imp1, 3) == LOW)
bot.sendMessage(chat_id, "ALLARME 3 TASTO PREMUTO", "");
}
if (digitalRead(imp1, 3) == LOW) { // (text == "/allarme") // if (digitalRead(imp1, 3) == LOW)
bot.sendMessage(chat_id, "ALLARME 4 TASTO PREMUTO", "");
}
if (digitalRead(imp1, 4) == LOW) { // (text == "/allarme") // if (digitalRead(imp1, 3) == LOW)
bot.sendMessage(chat_id, "ALLARME 5 TASTO PREMUTO", "");
}
if (digitalRead(imp1, 5) == LOW) { // (text == "/allarme") // if (digitalRead(imp1, 3) == LOW)
bot.sendMessage(chat_id, "ALLARME 6 TASTO PREMUTO", "");
}
}
if (text == "/stato") {
String stato = "Comando Centrale con Telegram, " + from_name + ".\n";
stato += "Stato e comando dell'impianto.\n\n";
stato += "la temperatura é: ";
stato += "Variabile Temperatura";
stato += " °C\n";
stato += "Lo stato è: ";
stato += impStatus3;
stato += "\n";
stato += "Allarme 1 ";
stato += allarmeimp1_0;
stato += "\n";
stato += "Allarme 2 ";
stato += allarmeimp1_1;
stato += "\n";
stato += "Allarme 3 ";
stato += allarmeimp1_2;
stato += "\n";
stato += "Allarme 4 ";
stato += allarmeimp1_3;
stato += "\n";
stato += "Allarme 5 ";
stato += allarmeimp1_4;
stato += "\n";
stato += "Allarme 6 ";
stato += allarmeimp1_5;
stato += "\n";
bot.sendMessage(chat_id, stato, "Markdown");
}
}
}
void setup() {
Serial.begin(115200);
Serial.println();
/***** LETTURA DATI DA SD PER CREDENZIALI *****/
Serial.print("Initializing SD card...");
delay(1000);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1)
;
}
Serial.println("initialization done.");
/****** MEMORIZZO TOKEN TELEGRAM *****/
f_token = SD.open("token.txt", FILE_READ);
if (f_token) {
Serial.println("token.txt:");
byte t = 0;
while (f_token.available() && t < 50) //modificato con 32 caratteri
{
char lettot = (f_token.read());
Serial.print(lettot);
TOKEN[t++] = lettot;
}
f_token.close();
} else {
Serial.println("error opening test.txt");
while (1)
;
}
/****** MEMORIZZO SSID WIFI *****/
f_ssid = SD.open("ssid.txt", FILE_READ);
if (f_ssid) {
Serial.println("ssid.txt:");
byte s = 0;
while (f_ssid.available() && s < 50) //modificato con 32 caratteri
{
char lettos = (f_ssid.read());
Serial.print(lettos);
SSID[s++] = lettos;
}
f_ssid.close();
} else {
Serial.println("error opening test.txt");
while (1)
;
}
/****** MEMORIZZO PASSWORD WIFI *****/
f_passw = SD.open("passw.txt", FILE_READ);
if (f_passw) {
Serial.println("passw.txt:");
byte p = 0;
while (f_passw.available() && p < 50) //modificato con 32 caratteri
{
char lettop = (f_passw.read());
Serial.print(lettop);
PASSWORD[p++] = lettop;
}
f_passw.close();
} else {
Serial.println("error opening test.txt");
while (1)
;
}
/******* SETTO INGRESSI E USCITE ******/
pinMode(out1, 0, OUTPUT);
pinMode(out1, 1, OUTPUT);
pinMode(out1, 2, OUTPUT);
pinMode(out1, 3, OUTPUT);
pinMode(out1, 4, OUTPUT);
pinMode(out1, 5, OUTPUT);
pinMode(out1, 6, OUTPUT);
pinMode(out1, 7, OUTPUT);
pinMode(imp1, 0, INPUT_PULLUP);
pinMode(imp1, 1, INPUT_PULLUP);
pinMode(imp1, 2, INPUT_PULLUP);
pinMode(imp1, 3, INPUT_PULLUP);
pinMode(imp1, 4, INPUT_PULLUP);
pinMode(imp1, 5, INPUT_PULLUP);
pinMode(imp1, 6, INPUT_PULLUP);
pinMode(imp1, 7, INPUT_PULLUP);
delay(10);
digitalWrite(out1, 0, LOW);
digitalWrite(out1, 1, LOW);
digitalWrite(out1, 2, LOW);
digitalWrite(out1, 3, LOW);
digitalWrite(out1, 4, LOW);
digitalWrite(out1, 5, LOW);
digitalWrite(out1, 6, LOW);
digitalWrite(out1, 7, LOW);
// ATTESA CONNESSIONE ALLA RETE:
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
Serial.print("Connecting to Wifi SSID ");
Serial.println(WIFI_SSID);
Serial.print("Password: ");
Serial.println(WIFI_PASSWORD);
Serial.print("Token: ");
Serial.println(BOT_TOKEN);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print("?");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
// Check NTP/Time, usually it is instantaneous and you can delete the code below.
Serial.print("Retrieving time: ");
time_t now = time(nullptr);
while (now < 24 * 3600) {
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
}
void loop() {
if (millis() - bot_lasttime > BOT_MTBS) {
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);
}
bot_lasttime = millis();
/******* MANDO ALLARME ATTIVO ******/
if ((digitalRead(imp1, 0) == LOW) && (allarmeimp1_0 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 1) == LOW) && (allarmeimp1_1 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 2) == LOW) && (allarmeimp1_2 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 3) == LOW) && (allarmeimp1_3 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 4) == LOW) && (allarmeimp1_4 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 5) == LOW) && (allarmeimp1_5 == 0)) {
numNewMessages = 1;
Serial.println("Tasto premuto");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
/******* MANDO ALLARME RIENTRATO ******/
if ((digitalRead(imp1, 0) == HIGH) && (allarmeimp1_0 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 1) == HIGH) && (allarmeimp1_1 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 2) == HIGH) && (allarmeimp1_2 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 3) == HIGH) && (allarmeimp1_3 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 4) == HIGH) && (allarmeimp1_4 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
if ((digitalRead(imp1, 5) == HIGH) && (allarmeimp1_5 == 1)) {
numNewMessages = 1;
Serial.println("Allarme rientrato");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
}
Serial.print("TOKEN: ");
Serial.println(TOKEN);
Serial.print("SSID: ");
Serial.println(SSID);
Serial.print("PASSWORD: ");
Serial.println(PASSWORD);
delay(5000);
}
So che si potrebbe fare meglio, purtroppo sono abbastanza ottuso nel capire bene le cose, specialmente alla mia età......