To control the ESP8266 using Telegram

Code tips and tricks for beginners
Post Reply
MarioL
Posts: 20
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 250 times
Been thanked: 33 times

To control the ESP8266 using Telegram

Post by MarioL »

Hi people of annex,
this example show how to control the ESP8266 GPIOs from anywhere using Telegram.
You just to send a message to your Telegram Bot to obtain a list of commands to set four GPIOs outputs at logic 1 (ON) or logic 0 (OFF).
In this example is not implemented limited access for autorized account, therefore anyone that knows your bot username can interact with ESP.

Salve gente di annex,
questo esempio mostra come controllare le uscite dell'ESP8266 da qualsiasi luogo utilizzando Telegram.
Devi solo inviare un messaggio al tuo Bot di Telegram per ottenere l'elenco dei comandi per impostare lo stato delle quattro uscite GPIO allo stato logico 1 (ON) o logico 0 (OFF).
In questo esempio non è implementato la limitazione d'accesso all'account autorizzato, quindi chiunque conosca il nome del tuo bot può interagire con l'ESP8266.

TGscreen.jpg

Code: [Local Link Removed for Guests]

'ESP12F Dev Kit    Annex WiFi 1.44.2 

' define the variables that are used for the output pins
out1 = 1  'set initial logic value of check boxes and output pin
out2 = 1  'set initial logic value of check boxes and output pin
out3 = 1  'set initial logic value of check boxes and output pin
out4 = 1  'set initial logic value of check boxes and output pin
OutPin1 = 16 'assign GPIO
OutPin2 = 14 'assign GPIO
OutPin3 = 5  'assign GPIO
OutPin4 = 4  'assign GPIO
PIN.MODE OutPin1, OUTPUT  'define pin mode
PIN.MODE OutPin2, OUTPUT  'define pin mode
PIN.MODE OutPin3, OUTPUT  'define pin mode
PIN.MODE OutPin4, OUTPUT  'define pin mode
gosub lblPinOutSet 'set pin out at initial valuue
RcvdTxt$ = ""   'Received text extract from receivet telegram message
MsgToTLGM$ = ""  'Message to transmit to Telegram
token$ = "54321123:aiaiaiaiaia-oioioioiiiiooo"  'BOT token keep confidential
c$ = "" 'chat id
telegram.settoken token$  'set telegram token ID
telegram.setwait 10   'set the max time for answer from TG server
wlog "-telegram.getMe ----", time$
wlog telegram.getMe$ 'get the user’s informations
wlog "--------------------"

timer0 5000, lblGetTGmessage  'Receive the messages from telegram at 5s interval
wait



lblGetTGmessage: 'Receive the messages from telegram
r$ = telegram.getUpdates$ 'get the new messages
wlog "-getUpdate -----------------", time$
wlog r$
wlog "--------------------------------------"
RcvdTxt$ = json$(r$, "text") 'get text field
if (RcvdTxt$ = "not found") then RETURN
 'c$ = json$(r$, "from.id")   'get the chat_id
  c$ = json$(r$, "from.chat.id")   'get the chat_id correct
'  c= val(c$)      'problemi di conversione numeri >32bit
  'wlog telegram.sendmessage$(c,"echo : " + RcvdTxt$) 
wlog "-received text field -----------------"
wlog RcvdTxt$
wlog "--------------------------------------"


'================= MANAGE RECEIVED COMMANDS FROM TELEGRAM ====================
lblManageTGCommands:
if RcvdTxt$ = "/update" then  goto lblReadPinStatus  'jump, return  output pin logical level and create response to telegram
if RcvdTxt$ = "/Toggle_OUT1" then  out1 = 1-out1 : gosub lblPinOutSet  'toggle out1  'set/update pin out at variables value
if RcvdTxt$ = "/Toggle_OUT2" then  out2 = 1-out2 : gosub lblPinOutSet  'toggle out2  'set/update pin out at variables value
if RcvdTxt$ = "/Toggle_OUT3" then  out3 = 1-out3 : gosub lblPinOutSet  'toggle out3  'set/update pin out at variables value
if RcvdTxt$ = "/Toggle_OUT4" then  out4 = 1-out4 : gosub lblPinOutSet  'toggle out4  'set/update pin out at variables value
if RcvdTxt$ = "/TurON_all_otputs" then  out1=1:out2=1:out3=1:out4=1 : gosub lblPinOutSet  'all out set 1 'set/update pin out at variables value
if RcvdTxt$ = "/TurOFF_all_otputs" then  out1=0:out2=0:out3=0:out4=0 : gosub lblPinOutSet  'all out set 1 'set/update pin out at variables value

'======== return  output pin logical level and create response to bot =========
lblReadPinStatus:
' %0A means line feed carriage return
' text from character "/" (included) is a command for bot
MsgToTLGM$ = "---   OUTPUT STATUS   ---%0A%0A" 

if pin(OutPin1) = 1 then
 MsgToTLGM$ = MsgToTLGM$ + "Out1=ON  clicK or tap here to /Toggle_OUT1"
else
 MsgToTLGM$ = MsgToTLGM$ + "Out1=OFF clicK or tap here to /Toggle_OUT1"
end if
MsgToTLGM$ = MsgToTLGM$ + "%0A%0A" 'LF CR LF CR

if pin(OutPin2) = 1 then
 MsgToTLGM$ = MsgToTLGM$ + "Out2=ON  clicK or tap here to /Toggle_OUT2"
else
 MsgToTLGM$ = MsgToTLGM$ + "Out2=OFF clicK or tap here to /Toggle_OUT2"
end if
MsgToTLGM$ = MsgToTLGM$ + "%0A%0A" 'LF CR LF CR

if pin(OutPin3) = 1 then
 MsgToTLGM$ = MsgToTLGM$ + "Out3=ON  clicK or tap here to /Toggle_OUT3"
else
 MsgToTLGM$ = MsgToTLGM$ + "Out3=OFF clicK or tap here to /Toggle_OUT3"
end if
MsgToTLGM$ = MsgToTLGM$ + "%0A%0A" 'LF CR LF CR

if pin(OutPin4) = 1 then
 MsgToTLGM$ = MsgToTLGM$ + "Out4=ON  clicK or tap here to /Toggle_OUT4"
else
 MsgToTLGM$ = MsgToTLGM$ + "Out4=OFF clicK or tap here to /Toggle_OUT4"
end if

MsgToTLGM$ = MsgToTLGM$ + "%0A%0A%0A" 'LF CR LF CR LF CR LF CR
MsgToTLGM$ = MsgToTLGM$ + "Click or tap here to /TurON_all_otputs"
MsgToTLGM$ = MsgToTLGM$ + "%0A%0A" 'LF CR LF CR
MsgToTLGM$ = MsgToTLGM$ + "Click or tap here to /TurOFF_all_otputs"
MsgToTLGM$ = MsgToTLGM$ + "%0A%0A" 'LF CR LF CR
MsgToTLGM$ = MsgToTLGM$ + "Click or tap here to /update"

'=============== Send a text message to telegram ======================
lblSendTGmessage:
 wlog "-SendTGmessage -----------------", time$
  if c$ < " " then wlog "c$=nul":return 'return if chat id not valid
 'wlog telegram.sendmessage$(val(c$), "echo : " + text$)  'val(c$) not work properly
 wlog wget$("https://api.telegram.org/bot"+token$+"/sendMessage?chat_id="+c$+"&text="+ MsgToTLGM$)
wlog "---------------------------------"
return


lblPinOutSet:  'set output pin at logical level
pin(OutPin1) = out1
pin(OutPin2) = out2
pin(OutPin3) = out3
pin(OutPin4) = out4
return

You do not have the required permissions to view the files attached to this post.
HPB
Posts: 18
Joined: Mon Mar 01, 2021 9:30 am
Has thanked: 31 times
Been thanked: 2 times

Re: To control the ESP8266 using Telegram

Post by HPB »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Thu Feb 02, 2023 9:59 pm Hi people of annex,
this example show how to control the ESP8266 GPIOs from anywhere using Telegram.
You just to send a message to your Telegram Bot to obtain a list of commands to set four GPIOs outputs at logic 1 (ON) or logic 0 (OFF).
In this example is not implemented limited access for autorized account, therefore anyone that knows your bot username can interact with ESP.

Salve gente di annex,
questo esempio mostra come controllare le uscite dell'ESP8266 da qualsiasi luogo utilizzando Telegram.
Devi solo inviare un messaggio al tuo Bot di Telegram per ottenere l'elenco dei comandi per impostare lo stato delle quattro uscite GPIO allo stato logico 1 (ON) o logico 0 (OFF).
In questo esempio non è implementato la limitazione d'accesso all'account autorizzato, quindi chiunque conosca il nome del tuo bot può interagire con l'ESP8266.

Thanks MarioL,

But a nice menu in Telegram Menu Builder Bot is the way to go :)
Just search for for menu builder bot.
See https://docs.menubuilder.cc/shelves/mat ... in-english

Cheers Hanspeter.
Post Reply