#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <EEPROM.h>
#include "BluetoothSerial.h"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "6493133820:AAHGUYhkdyfWjlDbJPFtGmS8kavYNW2ruik"
BluetoothSerial SerialBT;
// Wifi network station credentials
char WIFI_SSID[33]="CespyNet";
char WIFI_PASSWORD[64]="st3f@ni@02";
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
byte comando = 0;
const int ledPin = 2;
char IDabilitati [5][10];
int ledStatus = 0;
struct Wifi_Struct {
char sid[33];
char pwd[64];
} credenziali;
void handleNewMessages(int numNewMessages)
{
Serial.print("handleNewMessages ");
Serial.println(numNewMessages);
for (int i = 0; i < numNewMessages; i++)
{
char chat_id [11];
bot.messages[i].chat_id.toCharArray(chat_id,bot.messages[i].chat_id.length() +1);
// if (Inlista(chat_id)){ //Controlla se l'ID arrivato è nella lista dei 5 abilitati
Serial.print("Abilitato?: ");
if(Inlista(chat_id)){
Serial.println("Si");
}else{
Serial.println("No");
}
bool msgvalido = false;
char testo[15];
char command[6];
char argomento[11];
String text = bot.messages[i].text;
bot.messages[i].text.toCharArray(testo,14);
bot.messages[i].text.toCharArray(command,6);
for(int s=5;s<16;s++)argomento[s-5]=testo[s];
argomento[10]=0;
String from_name = bot.messages[i].from_name;
if (from_name == "")
from_name = "Ospite";
Serial.print("comando: ");
Serial.println(command);
Serial.print("argomento: ");
Serial.println(argomento);
if (command == "/Apri")
{
digitalWrite(ledPin, LOW); // turn the LED on (HIGH is the voltage level)
ledStatus = 1;
String welcome = "Ti ho aperto, " + from_name + ".";
bot.sendMessage(chat_id, welcome, "");
msgvalido = true;
}
if (strcmp(command,"/List")==0)
{
EEPROM.get(100,IDabilitati);
Lista(chat_id);
msgvalido = true;
}
if (strcmp(command,"/Agg.")==0)
{
Aggiungi(argomento);
msgvalido = true;
}
if (strcmp(command,"/Canc")==0)
{
Riempi();
msgvalido = true;
}
if (text == "/start")
{
String welcome = "Benvenuto nel bot apertura cancello, " + from_name + ".\n";
welcome += "Utilizza /Apri per aprire il cancello.\n";
welcome += "/List per la lista degli ID abilitati.\n";
welcome += "/Agg. seguito dal numero ID per aggiungere un ID.\n";
welcome += "/Canc seguito dal numero ID per cancellare un ID.\n";
bot.sendMessage(chat_id, welcome, "Markdown");
msgvalido = true;
}
if (!msgvalido)
{
bot.sendMessage(chat_id, "Testo non valido", "");
}
// }else{
// bot.sendMessage(chat_id, "Utente non abilitato", "");
// }
}
}
void setup()
{
Serial.begin(115200);
EEPROM.begin(160);
EEPROM.get(0, credenziali);
strcpy(WIFI_SSID,credenziali.sid);
strcpy(WIFI_PASSWORD,credenziali.pwd);
SerialBT.begin("Lambiase_gate"); //Bluetooth device name
Serial.println();
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
delay(10);
digitalWrite(ledPin, HIGH); // initialize pin as off (active LOW)
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
//secured_client.setInsecure();
while (WiFi.status() != WL_CONNECTED)
{
if (SerialBT.available()) {
comando= SerialBT.read();
delay(40);
if (comando == 69){ //lettera E (Hex 45)segue un treno di byte per SSID poi un byte=0 come separatore, poi un treno di byte per la password
ScriviWifi();
ESP.restart();
}
}
Serial.print(".");
delay(500);
}
SerialBT.end();
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
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);
Serial.print("Numeri nuovi messaggi: ");
Serial.println(numNewMessages);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
}
void ScriviWifi(){
strcpy(WIFI_SSID,"");
strcpy(WIFI_PASSWORD,"");
boolean flag= false;
int x = 0;
while (SerialBT.available()) {
comando= SerialBT.read();
if (comando != 0){
if (!flag){
WIFI_SSID[x]= comando;
}else{
WIFI_PASSWORD[x]= comando;
}
x+=1;
}else{
flag= true;
WIFI_SSID[x]= '\0';
x = 0;
}
}
WIFI_PASSWORD[x]= '\0';
strcpy(credenziali.sid,WIFI_SSID);
strcpy(credenziali.pwd,WIFI_PASSWORD);
//Serial.println(credenziali.sid);
//Serial.println(credenziali.pwd);
EEPROM.put(0, credenziali);
EEPROM.commit();
}
void Riempi(){
for (int riga=0;riga<5;riga++){
for(int colo=0;colo<10;colo++){
IDabilitati[riga][colo]= 0;
}
}
EEPROM.put(100,IDabilitati);
EEPROM.commit();
}
void Lista(char chat_id[11]){
String messaggio;
for (int riga=0;riga<5;riga++){
for(int colo=0;colo<10;colo++){
if (colo == 0){
messaggio += "ID " + String(riga) + " : ";
Serial.print("Riga ");
Serial.print(riga);
Serial.print(" : ");
}
if (IDabilitati[riga][colo] == 0){
messaggio +="0";
}else{
messaggio +=IDabilitati[riga][colo];
}
Serial.print(IDabilitati[riga][colo]);
if (colo == 9){
messaggio += ".\n";
Serial.println(" ");
}
}
}
bot.sendMessage(chat_id, messaggio, "Markdown");
}
bool Inlista(char chat_id[11]){
char id[11];
Serial.println("FUNZIONE");
id[10] = '\0';
for (int riga=0;riga<5;riga++){
for(int colo=0;colo<10;colo++){
id[colo] = IDabilitati[riga][colo];
}
if (strcmp(chat_id, id)== 0){
return true;
}
}
return false;
}
void Aggiungi(char chat_id[11]){
if (strlen(chat_id) == 10){
for (int riga=0;riga<5;riga++){
if(IDabilitati[riga][0]==0){ //trovata riga libera
for(int colo=0;colo<10;colo++){
if(chat_id[colo] >= 48 && chat_id[colo] <=57){
IDabilitati[riga][colo]= chat_id[colo];
}else{
for (int z=0;z<10;z++) IDabilitati[riga][z]=0;
break;
}
}
break;
}
}
}
}