This is the solution of the previous activities or activity number eight, where you have to control
Arduino from Telegram so you can send some text on the LCD screen and you can also choose the color
for the RGB with just one command.
So we have a few things to do.
Let's start, as always, with the Arduino side and let's okay, let's initialize here
with the baud rate while not serial.
Okay, that's pretty fast to do.
And then we will need to initialize the LCD and the RGB.
So let's create the defines, define RGB rate pin, which is 11 define.
RGB green pin, which is ten.
Let's not make a mistake this time on the blue.
Blue pin, which is nine and not 19 like I did before.
And then define LCD s pin.
I'm going to put the number just after.
Define LCD e pin.
Define LCD four.
So we have d4 d5 d6 d7 five.
Six seven with A4 a5 two, three, four and five.
Okay.
You make sure they are correctly configured.
We include the liquid crystal, but.
With a Y here H.
And then we create the liquid crystal here.
So liquid.
Crystal LCD.
So the initialization is always the same.
You can even copy paste the code from the previous.
Blessings if you want to go rcd for.
And then.
D5 d6.
And.
D7.
Okay, let's do.
LCD dot begin 16 by two.
And let's also do the Pinmode with RGB red.
To output.
And then the same for the green.
And the same for the blue.
And we are done with initialization of the Arduino component.
Now in the void loop.
So I'm going to remove that and I'm going to do this again.
Okay.
Explaining it step by step.
So in the void loop, we don't have any other thing to do than checking the sale.
So if cell dot available greater strictly than zero, we are going to read the command is equal to sale
dot read string until backslash n.
And then we are going to check if command is equal to.
Okay.
Let's start with the LCD.
What command do we receive here and what command do we send here?
So this command will contain first something that we can recognize, for example, print text, but
then it's going to contain something that we don't know yet from the Arduino side.
Okay.
So instead of just checking the equality for the comment, I'm going to do, if Command Dot starts with
and let's say that the command starts with print, text, colon, and then we have the text.
So if it starts with this, we are going to do.
Comment dots remove.
So we remove the first comment.
So 1234, five, six, seven, eight, nine, ten, 11.
So we remove from 0 to 11.
And we are left with the text to print and let's do LCD dot print command.
And before we print the command, we could also, for example, do LCD dot clear.
Okay.
And LCD dot set cursor.
Zero zero.
We clear?
We come back to the beginning.
We print the text.
Okay, Let's go to the next.
So else if.
Command starts with.
Now we have the set led.
Okay.
So we check if it's a print text command or a set led command.
If this is the case.
Okay, before I forget, I'm just going to add else where we do nothing for now and well, I will add
a best practice in the final project.
Okay.
For this else that you're going to see later, one thing at a time.
So if we have the command that starts with set, then one, two, three, four, five, six, seven,
eight.
We do command dot remove zero eight and then we get each color.
So int red is equal to command dot sub string from 0 to 3 and dot to int.
Okay.
You can put this as a help if you want to count.
Let's say we have this.
So zero one, two, three and then four, five, six, seven.
So we do int green.
Of course it's important that when you send the command, you respect the order.
Okay.
Red, green, blue command dot substring.
From 4 to 7.
So we just ignore the comma to int.
INT blue is equal to comment dot
substring.
Okay.
If this is seven, then this is eight.
We put eight and we don't need to provide the end if we just want to go until the end.
Okay.
So we just read the three last characters to inch to inch because we have a string we need to convert
to int so we can use in analog.
Right.
Analog, right with the red first.
And we give red color and let's do the same for the green and the blue.
Green with green.
And blue
with the blue we just got.
Okay, let's remove that.
And I think our program is complete.
Let's just try it.
Okay, Let's upload the code.
Activity eight.
And let's try.
Okay.
We first have an error here.
Yes, because I forgot.
So this is quite a sneaky error here.
I forgot.
The parentheses because we open parentheses, we open one, so we have to close two.
And of course, the same here.
Now.
Okay.
The compiling works now.
The uploading should work as well.
And well, let's try something here.
So I'm going to open the serial monitor and let's send some commands.
For example, we want to print something.
So print text with let's put a space here, for example.
Hello world.
You can see we have Hello World on the screen here.
And we have well, we have actually one space before because you can see we have added one space.
So everything that we have after print text, colon is going to be printed.
Let's change the string with Abcd, for example.
You can see now everything has been cleared.
We start back at the beginning and we print Abcd if I do.
GB No, actually it's set led with let's put 200 and then zero and then 180.
Okay.
We have a purple, a purple color, so it's correctly working.
Okay.
So now that we know the Arduino code is working, let's write the code so that we can actually control
this from Telegram.
Okay.
So on the Raspberry Pi, let's first connect to Serial.
And I'm going to here I'm not going to write the code entirely.
I'm going to use the code we did previously with the retry.
Okay.
So like this.
And then.
This.
So we connect.
We try to connect to.
And then we wait for three seconds.
Okay.
And we have this infinite loop running inside a try except.
Block And now I am going to add, so I'm just going to leave it there.
I'm just going to comment that this is a comment and I'm going to add the Telegram functionality.
So from Telegram dot txt, we are going to import this time updater and comment handler.
I'm going to get the token like we did before, so I'm just going to get the token.
Like this.
So, for example, here and then we are going to create not a bot object, but an update operator is
equal to update.
We've token equals.
Telegram, Token.
Okay, so we do that after we have correctly initialized the cell here we add a dispatcher, which is
a data dot dispatcher, and then we are going to add some callbacks.
So let's add the callbacks right here before this code.
So def let's say we have a start handler.
Okay, let's always create a start handler update context and in the start handler we do context.
Dot, dot, dot, send message with chat ID is equal to update dot effective chat.id and text is equal
to let's say hello.
Now, maybe just welcome.
Okay, We have the start handler.
Then we create a let's name it lxd handler.
Okay.
A date context.
And what do we do when we receive a comment from the LCD?
We are going to send that command to the Raspberry Pi.
So how to get the text?
The text is update dot message, dot text.
I'm going to remove that now.
And now.
The string to send.
So the command is going to be equal to print text.
Okay.
Because we need to send print text here.
And then plus text and then plus backslash.
N and we do say dot.
Right command dot encode with UTF eight.
Okay.
And also what we could do is just send back a message to our bot to confirm that we have done the operation.
Okay.
Context dot, dot, dot send.
So actually that's going to be the same as here.
And the text.
Let's just send.
Okay.
All right, now we have diff RGB handler, so you just name it as you want.
That's just a function update context.
But as you can see, I usually put handler.
Okay.
I put the name of the command and then handler.
So that's it's easier to navigate the code.
Okay, we have the text which is also update dot message dot text.
We create the command is equal to.
So the command this time is set.
With Colon and we expect the user from Telegram to give us the correct format.
Okay.
For the command we're going to send.
So this I'm just going to leave it like that.
But as an improvement, what you could do is to validate the data here also and then try to find another
way to send data with a less strict format plus backslash n at the end, let's not forget that.
And then we just do the same thing here.
We write the comment and we send back a message to the chat.
Okay, now let's not forget to add those handlers.
So.
Dispatcher Dot add Handler.
Command handler and we have so the keyword start is going to go with start handler.
And then let's add another two of those.
We will have the command LCD, which is going to go with the LCD handler.
So you don't necessarily have to put the same command here and the same name for the function.
It doesn't.
It is not linked.
Okay.
Just use it for simplicity and Argb command for the RGB handler.
Okay.
After we have all the commands that are ready to be dispatched, we do a data dot, start polling.
And the last thing is that when we actually close the program, we do closing sale and we then do print.
Stopping telegram data.
Okay.
Where?
We have to wait a few seconds and we do a data dot.
Stop.
So we start and we stop.
All right, let's try this code.
So we have written more than 50 lines of code, and let's just try.
Let's see if it works.
Activity.
Eight.
So let's see what we get here.
Successfully connected to soil.
Okay.
Now let's open telegram and let's just send some comments.
So LCD Hello.
Let's see what we get.
Okay.
You can see on the screen we have not just hello, but we have slash LCD space.
Hello.
So we need to come back to our program and well, here we haven't tested.
Okay, So it was kind of luck if it was going to work or not.
But when you see that we get update dot message dot text, we get the complete text, okay.
Even with the command and the space.
So we need to remove that before we send it, of course.
So what we can do is actually something similar that we did here in the on the Arduino is to remove
the first N characters.
Okay, let's do that.
It's going to be simpler.
So here we receive.
So we received slash LCD space and then comment.
So we have one, two, three, four, five characters to remove.
So what we can do is just do five and colon and bracket.
So a string is also an array of characters.
Okay, So if you do that, you're going to remove the first five characters.
Okay, let's run again.
And once it is connected, let's send.
The same.
LCD.
Hello.
And you can see now we have hello on the screen.
And so I'm going to come back to the program to change, of course, here for the RGB handler.
So for the RGB handler we have.
Slash RGB and then comment.
So we have also one, two, three, four, five characters to remove.
So we do this five colon bracket to take everything after the fifth character.
Let's run that.
Say, ah, okay.
And let's try with Slash.
Let's give the same color as before 200 zero 180 and let's see what we get.
You can see now the RGB is purple.
Okay, let's bring some text LCD, It works and well, it correctly works.
We can control the Arduino components from Telegram Chat.
All right.
And this is the end of this activity.