One of the problems that we often face is that during online meetings, we cannot get up from the webcam and do things like closing the room door or turning on and off the light or air conditioner. My idea is to be able to close the door of our room through the application in which the meeting is being held. Or turn off the light behind us without getting up. what's the solution? One of the popular apps for holding online meetings is Skype. Skype provides an API to third parties, through which developers can build a bot for Skype. Other popular applications such as Slack and Zoom have also provide API to do such things. But a platform like Google Meet does not provide users with such freedom of action. Next, we will write a Skype bot and control a hardware through this bot. This hardware can be any microcontroller that supports Wi-Fi, such as ESP32. I used Raspberry Pi Pico W microcontroller in this project.
To enter the coding discussion, we need to install the skpy module in Python. We do this very easily with the pip command.
pip install skpy
Using the following script, we extract the Chat id of each conversation. Each of our conversations (whether private or group) in Skype has a unique chat id. In the output of this chat id script, our conversations are displayed in order of date and time. The first chat id displayed corresponds to our most recent and recent conversation on Skype. Also, the user_name and password parameters are related to our Skype account, with which we log in to Skype.
from skpy import Skype, SkypeChats
user_mname = ""
password = ""
sk = Skype(user_mname, password) # Connect to Skype
skc = SkypeChats(sk)
chats = skc.recent() # List recent chats
for chat in chats:
# Check if 'chat' is an instance of a chat object
if hasattr(chat, 'id'):
print(chat.id) # Print the chat ID
else:
print(f"Item is not a chat object: {chat}")
After extracting the chat id of the desired conversation, we must enter our bot into that conversation. Of course, this is not scientifically true. Basically, we send messages through our own Skype account by writing the following programs. In other words, we have automated our Skype account. The bot that we write here does not work like Telegram and Discord bots and does not have a separate user account and sends messages through our own user account. Using the following Python script, we can send a message through this bot in a specific conversation.
from skpy import Skype, SkypeChats
user_mname = ""
password = ""
sk = Skype(user_mname, password) # Connect to Skype
skc = SkypeChats(sk)
chat_id = '19:315fd69cedec4b6c9a6d33a2086e6e3f@thread.skype' # Replace with your actual group chat ID
ch = sk.chats[chat_id]
ch.sendMsg('hello from bot')
So far, we've gotten to know the Skype bot in general and understood how the skpy module works. Before writing the main Skype bot program, we must talk about socket programming. At the beginning, we said that the purpose of making this Skype bot is to control a piece of hardware. Like a room door or a lamp. The mastermind behind our hardware control circuit is the Raspberry Pi Pico W. How does this microcontroller receive commands from the Skype bot? By means of programming sockets and network protocols, we can send control commands to Raspberry Pi Pico W via Skype Bot. The following simple script easily demonstrates the concept of socket programming.
import socket
from time import sleep
# Set up the client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.1.234', 80)) # Connect to the server
while True:
# Send a string to the server
client_socket.sendall(b'Hello, Pico W!')
sleep(2)
# Close the connection
client_socket.close()
In the above program, the function client_socket.connect(('192.168.1.234', 80)) requires two inputs. A port and an IP address. Our port can be any number. We just have to choose one of the free computer ports. But the IP address must be the IP address of the Raspberry Pi Pico microcontroller so that we can send commands to it.
now we come to the main program of Skype bot. This program detects keywords in the chat screen. For example, if we send the message "turn on the light" on the dialog page. This Skype bot sends an "on" string in bytes over the network to the Raspberry Pi Pico. And the Raspberry Pi Pico turns on the lamp. Or if we send the phrase "open the door" in the dialogue page, the Skype bot will send the "open" string through the network to the Raspberry Pi Pico and the door of the room will be opened. Of course, here our control circuit can only control the lamp. In the future, I will definitely open and close the room door through a hydraulic arm. The final Skype bot app can be seen below.
from skpy import SkypeEventLoop, SkypeNewMessageEvent
user_mname = ""
password = ""
import socket
from time import sleep
# Set up the client socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('192.168.1.234', 80)) # Connect to the server
class SkypeBot(SkypeEventLoop):
def onEvent(self, event):
if isinstance(event, SkypeNewMessageEvent): # Check for new message events
message_content = event.msg.content # Get the content of the message
if 'close the door' in message_content.lower(): # Check for a specific keyword
event.msg.chat.sendMsg('door is close!!!!') # Send a specific answer
client_socket.sendall(b'close')
print("door is close")
elif "open the door" in message_content.lower():
event.msg.chat.sendMsg("door is open!!!!")
client_socket.sendall(b"open")
print("door is open")
elif "turn on the light" in message_content.lower():
event.msg.chat.sendMsg("light is on!!!")
client_socket.sendall(b"on")
print("light is on")
elif "turn off the light" in message_content.lower():
event.msg.chat.sendMsg("light is off!!!")
client_socket.sendall(b"off")
print("light is off")
else:
pass
bot = SkypeBot(user_mname, password)
bot.loop()
The part related to the Skype bot programming is finished and we have only two steps to the final implementation of the project. Before programming Raspberry Pi Pico, we take a look at the hardware circuit. This circuit has practically no special point and you can see it below.
Finally, we upload the following program that I wrote using MicroPython on Raspberry Pi Pico W. This program receives the commands sent by the Skype bot over the network and in byte format by means of the socket programming. And it performs a specific action according to each received command. According to our current facilities, this circuit currently controls one lamp.
import socket
import network
from time import sleep
from machine import Pin
ssid = 'Mrsh77'
password = '1m77n2299215r77#'
door_pin = Pin(14,Pin.OUT)
light_pin = Pin(15,Pin.OUT)
door_pin.value(0)
light_pin.value(0)
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def open_socket(ip):
address = (ip,80)
connection = socket.socket()
connection.bind(address)
connection.listen(1)
#print(connection)
return connection
try:
ip = connect()
connection = open_socket(ip)
while True:
# Accept a connection from a client
client, addr = connection.accept()
print(f'Connected to {addr}')
while True:
# Receive data from the client
data = client.recv(1024)
if data:
# Print the data to the console
print(data)
if data == b"on":
light_pin.value(1)
print("light is on")
elif data == b"off":
light_pin.value(0)
print("light is off")
elif data == b"open":
door_pin.value(1)
print("door is open")
elif data == b"close":
door_pin.value(0)
print("door is close")
else:
pass
except KeyboardInterrupt:
# Close the server socket
connection.close()
Finally, you can watch the practical video of the project.
Comments
Please log in or sign up to comment.