Stop Motion Movie System using Raspberry Pi 4

Thank you for joining us for yet another session of this series on Raspberry Pi programming. In the previous tutorial, we built a motion sensor-based security system with an alarm. Additionally, we discovered how to use Twilio to notify the administrator whenever an alarm is triggered. However, in this tutorial, we'll learn how to build a stop motion film system using raspberry pi 4.

Where To Buy?
No.ComponentsDistributorLink To Buy
1BreadboardAmazonBuy Now
2Jumper WiresAmazonBuy Now
3Raspberry Pi 4AmazonBuy Now

What you will make

With a Raspberry Pi, Py, and a pi-camera module to capture images, you can create a stop-motion animated video. In addition, we'll learn about the various kinds of stop motion systems and their advantages and disadvantages.

The possibilities are endless when it comes to using LEGO to create animations!

What will you learn?

Using your RPi to build a stop motion machine, you'll discover:

  • How to install and utilize the picamera module on the RPi

  • This article explains how to take photos with the Picamera library.

  • RPi GPIO Pushbutton Connection

  • Operate the picamera by pressing the GPIO pushbutton

  • How to use avconv to create a video clip from the command prompt

Prerequisites

Hardware

  • Raspberry Pi 4

  • Breadboard

  • Jumper wires

  • Button

Software

It is recommended that FFmpeg comes preconfigured on the most recent release of Raspbian. If you don't have it, launch the terminal then type:

sudo apt-get update

sudo apt-get upgrade

sudo apt install FFmpeg

What is stop-motion?

Inanimate things are given life through the use of a sequence of still images in the stop-motion cinematography technique. Items inside the frame are shifted slightly between every picture to create the illusion of movement when stitched together.

You don't need expensive gadgets or Graphics to get started in stop motion. That, in my opinion, is the most intriguing aspect of it.

If you've ever wanted to learn how to make a stop-motion video, you've come to the right place. 

Types of stop-motion

  1. Object-Motion

Product Animation can also be referred to as the frame-by-frame movement of things. You're free to use any items around you to tell stories in this environment.

  1. Claymation

Changing clay items in each frame is a key part of the claymation process. We've seen a lot of clever and artistic figures on the big screen thanks to wires and clay.

  1. Pixilation Stop Motion

Making folks move! It is rarely utilized. For an artist to relocate just a little each frame, and the number of images you would need, you'll need a lot of patience and possibly a lot of money, if you're hiring them to do so.

The degree of freedom and precision with which they can move is also an important consideration. However, if done correctly, this kind can seem cool, but it can also make you feel a little dizzy at times.

  1. Cutout Animation

One can do so much with cuts in cutout motion because of this. two-dimensional scraps of paper may appear lifeless, yet you may color & slice them to show a depth of detail.

It's a lot of fun to play about with a cartoon style, but it also gives you a lot more control over the final product because you can add your graphics and details. However, what about the obvious drawback? I find the task of slicing and dicing hundreds of pieces daunting.

  1. Puppet Animation

Having puppets can be a fun and creative way to tell stories, but they can also be a pain in the neck if you're dealing with a lot of cords. However, this may be a challenge for professional stop motion filmmakers who are not the greatest choice to work with at first. These puppets are of a more traditional design.

When animators use the term "puppet" to describe their wire-based clay character, they are referring to claymation as a whole. Puppets based on the marionette style are becoming less popular.

  1. Silhouette Stop Motion

Position the items or performers behind a white sheet and light their shadows on the sheet with a backlight. Simple, low-cost methods exist for creating eye-catching animations of silhouettes.

How long does it take to make a stop-motion video?

The duration takes to create a stop-motion video is entirely dependent on the scale and nature of your project. Testing out 15- and 30-second movies should only take an hour or two. Because of the complexity of the scenes and the usage of claymation, stop-motion projects can take days to complete.

Connect the camera to the raspberry pi.

You must first attach the camera to the Pi before it can begin rebooting.

Next to Ethernet, find the camera port. Take a look at the top.

The blue side of the strip should face the Ethernet port when it is inserted into the connector. Push that tab downward while keeping the ribbon in place.

Try out the camera

Use the app menu to bring up a command prompt. The following command should be typed into the terminal:

libcamera-hello

If all goes well, you'll see a sneak peek of what's to come. What matters is that it's not upside-down; you can fix it afterward. To close the preview, hit Ctrl + C.

For storing an image on your computer, run the command below:

libcamera-jpeg -o test.jpg

To examine what files are in your root folder, type ls in the command line and you'll see test.jpg among the results.

Files and folders will be displayed in the taskbar's file manager icon. Preview the image by double-clicking test.jpg.

There is no default way to make Python Picamera work with Raspbian newest version.

To make use of the camera module, one must activate the camera's legacy mode.

The command below must be entered into a command window:

sudo raspi-config

When you get to Interface Options, hit 'Enter' on your keyboard to save your changes.

Ensure that the 'Legacy Camera option is selected then tap the 'Return' key.

Select Yes using the pointer keys and hit the 'Return' key.

Repeat the process of pressing 'Return' to verify.

Click on Finish with your mouse cursor buttons.

To restart, simply press the 'Return' key.

Py IDLE can be accessed from the menu bar.

While in the menu, click Files and then New Window to launch a Python code editor.

Paste the code below paying attention to the capitalization with care into the newly opened window.

from picamera import PiCamera

from time import sleep

camera = PiCamera()

camera.start_preview()

sleep(3)

camera.capture('/home/pi/Desktop/image.jpg')

camera.stop_preview()

Using the File menu, choose Save Animated film.

Use the F5 key to start your program.

You should be able to locate image.jpg on your desktop. It's as simple as clicking it twice to bring up a larger version of the image.

It's possible to fix an upside-down photo by either repositioning your picamera with a camera stand or by telling Python to turn the picture. Adding the following lines will accomplish this.

camera.rotation = 180

Once the camera is set to PiCamera(), the following is the result:

from picamera import PiCamera

from time import sleep

camera = PiCamera()

camera.rotation = 180

camera.start_preview()

sleep(3)

camera.capture('/home/pi/Desktop/image.jpg')

camera.stop_preview()

A fresh photo with the proper orientation will be created when the file is re-run. Do not remove these lines of code from your program when making the subsequent modifications.

Connect a physical button to a raspberry pi

Hook the Raspberry Pi to the pushbutton as illustrated in the following diagram with a breadboard and jumper wires:

Pushbutton may be imported at the beginning of the program, attached to pin17, and the sleep line can be changed to use the pushbutton as a trigger in the following way:

from picamera import PiCamera

from time import sleep

from gpiozero import Button

button = Button(17)

camera = PiCamera()

camera.start_preview()

button.wait_for_press()

camera.capture('/home/pi/image.jpg')

camera.stop_preview()

It's time to get to work!

Soon as the new preview has begun, press the pushbutton on the Pi to take a picture.

If you go back to the folder, you will find your image.jpg there now. Double-click to see the image once more.

Take a picture with Raspberry Pi 4

For a self-portrait, you'll need to include a delay so that you can get into position before the camera board takes a picture of you. Modifying your code is one way to accomplish this.

Before taking a picture, put in a line of code that tells the program to take a little snooze.

camera.start_preview()

button.wait_for_press()

sleep(3)

camera.capture('/home/pi/Desktop/image.jpg')

camera.stop_preview()

It's time to get to work.

Try taking a selfie by pressing the button. Keep the camera steady at all times! It's best if it's already mounted somewhere.

Inspect the photo in the folder once more if necessary. You can snap a second selfie by running the application again.

Things to consider for making a stop motion animation

  1. You must have a steady pi-camera!

This is made easier with the aid of a well-designed setup.  To avoid blurry photos due to camera shaking, you will most likely want to use a tripod or place your camera on a flat surface.

  1. Keep your hands away from the pi-camera

If you don't press the push button every time, your stop-motion movie will appear the best. To get the camera to snap a picture, use a wireless trigger.

  1. Shoot manually

Maintain your shutter speed, ISO, aperture, and white balance same for every photo you shoot. There are no "auto" settings here. You have the option of selecting and locking the app's configurations first. As long as your preferences remain consistent throughout all of your photos, you're good to go. The configurations will adapt automatically as you keep moving the items, which may cause flickering from image to image if you leave them on auto.

  1. Make sure you have proper lighting.

It's ideal to shoot indoors because it's easier to regulate and shields us from the ever-changing light. Remember to keep an eye out for windows if you're getting more involved. Try using a basic lighting setup, where you can easily see your items and the light isn't moving too much. In some cases, some flickering can be visible when you're outside of the frame. Other times the flickering works well with animation, but only if it does so in a way that doesn't disrupt the flow of the project.

  1. Frame Rate

You do not get extremely technical with this in the beginning, but you'll need to understand how many frames you'll have to shoot to achieve the series you desire. One sec of the film is typically made up of 12 images or frames. If your video is longer than a few secs, you risk seeming like a stop motion animation.

  1. Audio

When you're filming your muted stop motion movie, you can come up with creative ways to incorporate your sound later. 

Stop-motion video

The next step is to experiment with creating a stop motion video using a collection of still photos that you've captured with the picamera. Note that stills must be saved in their folder. Type "mkdir animation" in the command line.

When the button is pushed, add a loop to your program so that photographs are taken continuously.

camera.start_preview()

frame = 1

while True:

    try:

        button.wait_for_press()

        camera.capture('/home/pi/animation/frame%03d.jpg' % frame)

        frame += 1

    except KeyboardInterrupt:

        camera.stop_preview()

        break

Since True can last indefinitely, you must be able to gently end it. If you use Ctrl + C to force it to end, the picamera preview will collapse and the loop will be terminated because it is using try-except.

Files stored as "frame" with a three-digit number preceded by a leading zero (009,005.) are known as "frame" files because of the % 03d format. This makes it simple to arrange them in the proper sequence for the video.

To capture each following frame, simply push the button a second time once you've finished rearranging the animation's main element.

To kill the program, use Ctrl + C when all the images have been saved.

Your image collection can be viewed in the folder by opening the animation directory.

Create the video

To initiate the process of creating the movie, go to the terminal.

Start the movie rendering process by running the following command:

FFmpeg -r 10 -i animation/frame%03d.jpg -qscale 2 animation.mp4

Because FFmpeg and Py recognize the percent 03d formatting, the photographs are sent to the movie in the correct sequence.

Use vlc to see your movie.

vlc animation.mp4

The renderer command can be edited to change the refresh rates. Try adjusting -r 10 to a different value.

Modify the title of the rendered videos to prevent them from being overwritten. Modify animation.h264 to a different file to accomplish this.

What's the point of making stop motion?

Corporations benefit greatly from high-quality stop motion films, despite the effort and time it takes to produce them. One of these benefits is that consumers enjoy sharing these movies with friends, and their inspiring content can be associated with a company.  Adding this to a company's marketing strategy can help make its product extra popular and remembered.

When it comes to spreading awareness and educating the public, stop motion films are widely posted on social media. It's important to come up with an original idea for your stop motion movie before you start looking for experienced animators.

Stop Motion Movie's Advantages

In the early days of filmmaking, stop motion was mostly employed to give animated characters the appearance of mobility. The cameras would be constantly started and stopped, and the multiple images would all be put together to tell a gripping story.

It's not uncommon to see films employ this time-honored method as a tribute to the origins of animations. There's more, though. 

  1. Innovation

In the recent resurgence of stop motion animations, strange and amazing props and procedures have been used to create these videos. Filmmakers have gone from generating stop motion with a large sheet of drawings, to constructing them with plasticine figures that need to be manually manipulated millimeters at a time, and to more esoteric props such as foodstuffs, domestic objects, and creatures.

Using this technique, you can animate any object, even one that isn't capable of moving by itself. A stop-motion movie may be made with anything, thus the options are practically limitless.

  1. Animated Tutorials

A wide range of material genres, from educational films to comedic commercials, is now being explored with stop motion animation.

When it comes to creating marketing and instructional videos, stop motion animations is a popular choice due to their adaptability. An individual video can be created. 

Although the film is about five minutes long, viewers are likely to stick with it because of its originality.  The sophisticated tactics employed captivate the audience. Once you start viewing this stop motion video, it's impossible to put it down till the finish.

  1. Improve the perception of your brand

It's easy to remember simple but innovative animations like these. These movies can assist a company's image and later recall be more positive. Stop motion video can provoke thought and awe in viewers, prompting them to spread the creative message to their social networks and professional contacts.

It is becoming increasingly common for organizations of all kinds to include stop-motion animations in their advertisements. 

  1. In education 

Stop-motion films can have a positive impact on both education and business. Employees, customers, and students all benefit from using them to learn difficult concepts and methods more enjoyably. Stop motion filmmaking can liven up any subject matter, and pupils are more likely to retain what they've learned when it's done this way.

Some subjects can be studied more effectively in this way as well. Using stop motion films, for instance, learners can see the entire course of an experiment involving a slow-occurring reaction in a short amount of time.

Learners are given a stop motion assignment to work on as a group project in the classroom. Fast stop motion animation production requires a lot of teamwork, which improves interpersonal skills. Some learners would work on the models, while others might work on the backdrops and voiceovers, while yet others might concentrate on filming the scenes and directing the actors.

  1. Engage Customers and Employees

The usage of stop motion movies can be utilized to explain product uses rapidly, even though the application of the device and the output may take a while. You can speed up the timeline as much as you want in stop motion animations!

For safety and health demonstrations or original sales demonstrations, stop motion instructional films may also be utilized to effectively express complex concepts. Because of the videos' originality, viewers are more likely to pay attention and retain the content.

  1. Music Video

Some incredibly creative music videos have lately been created using stop motion animations, which has recently seen a resurgence in popularity.  Even the human body could be a character in this film.

Stop-motion animations have the potential to be extremely motivating. Sometimes, it's possible to achieve it by presenting things in a novel way, such as by stacking vegetables to appear like moving creatures. The sky's the limit when it comes to what you can dream up.

  1. Reaction-Inducing Video

When it comes to creating a stop motion movie, it doesn't have to be complicated. If you don't have any of these things in your possession, you'll need to get them before you can begin filming. However, if you want to create a professional-level stop motion film, you'll need to enlist the help of an animation company.

As a marketing tool, animated videos may be highly effective when they are created by a professional team. 

  1. Create an Intriguing idea

The story of a motion-capture movie is crucial in attracting the attention of audiences, so it should be carefully planned out before production begins. It should be appropriate for the video's intended audience, brand image, and message. If you need assistance with this, consider working with an animation studio.

Disadvantages

But there are several drawbacks to the overall process of stop motion filmmaking, which are difficult to overcome. The time it takes to create even a min of footage is the most remarkable. The time it takes to get this film might range from a few days to many weeks, depending on the approach used.

Additionally, the amount of time and work that is required to make a stop-motion movie might be enormous. This may necessitate the involvement of a large team. Although this is dependent on the sort of video, stop motion animating is now a fairly broad area of filmmaking, which can require many different talents and approaches.

Conclusion

Using the Raspberry Pi 4, you were able to create a stop-motion movie system. Various stop motion technologies were also covered, along with their advantages and disadvantages. After completing the system's basic functions and integrating additional components of your choice, you're ready to go on to the next phase of programming. Using raspberry pi 4 in the next article, we will build an LED cube.

Bitwise Logic Operators in PLC Ladder Logic Programming

Hello friends, all of us know that PLCs are nothing but the smartest migration from relay logic control to programmable logic control. Also, you know clearly that, logic is the heart of any programming language, and the same is applied to ladder logic programming. Bitwise operators represent the logical operations including the basic logical operations like AND, OR, and NOT and the derived logical operations like NAND, NOR, and XOR. in most cases, for each bitwise operator, there are inputs based on which the output can be decided. Some of these bitwise operators have two inputs and some have only one input.  In this article, we are going to present how we can use these bitwise logical operators and their instructions with examples and practice using the PLC simulator.

Logic Gates basics and concepts

Despite we have talked about these, basics and concepts in one of our articles, we have seen it’s good to remind you briefly that basic is the ground for your foot to stand coding logical operations in ladder logic programming. for defining a logical operation, there is a truth table that shows the combinations of the inputs and the resulting output. For example, the bitwise operator that has only one input like “NOT”, has only two possible value for input which is high or low, True or False. And it has one output which is the negate of the input. So it is either TRUE or FALSE. To sum up, for every bitwise operator instruction, we are going to discuss its truth table showing the states of the possible combinations of its inputs and the resulting output as well. In addition, one example using ladder logic programming shows the operation practically. We come to see it is important to list the bitwise operators in table 1 below shows the inputs, output, and the symbol of the logical gate that is equivalent to that bitwise operator.

Table 1: the bitwise operator list 

The bitwise operator

Inputs

Output

AND

2

1

OR

2

1

NOT

1

1

NAND

2

1

NOR

2

1

XOR

2

1

AND bitwise operator

Guys, we can see the truth table of the AND instruction as tabulated in table 2. It shows the operator has two operands input A, and input B, and one output. The inputs can be switches, sensors i.e. limit switch for example while the output is a digital output to switch one actuator i.e. motor.

Input A

Input B

Output

False

False

False

True 

False

False

False

True

False

True

True

True

Also, Fig. 1 shows the symbol of the AND gate which is equivalent to AND bitwise operator. It shows two inputs A and B and only one output. Let us see that practically in ladder logic programming showing how to implement this with example.

Bitwise Operator AND in Ladder Logic

Figure 2 shows our first simple rung that codes the logic of AND bitwise logic. It shows two contacts A and B are connected in series to decide output AND-RES based on AND logic shown in truth table 2. So let us go .simulating this thing we just coded and see the logic in the application.

Simulating basic AND in ladder logic

We typically have 4 cases according to the truth table. Figure 3 shows the case when inputs A and B are low. The output shows low as shown by the coil AND-RES.

Now let us switch on input A  and keep the second operand B false as shown in fig. 4, output AND-RES output still off.

So let us try to switch the other input, B ON, and set input A off, as shown in fig. 5 output is off as represented by coil M0.2, tag AND-RES.

At last, when both inputs A and B are ON as in Fig. 6. Now only the output comes to turn ON.

Guys!, you know for sure have got to know that, with AND logic, for having the output turned high both inputs A and B must be high. 

Ladder logic in Siemens and most other brands offer the facility to perform AND between byte, word, and Double word memory space as shown in Fig. 7 shows the AND block.

Simulate AND block in ladder logic

Figure 8 shows the usage of AND block for byte data type and it is possible to do the same with a word or double word data type. The instruction block applies to byte, word, and double word data types. However, for showing one example, Fig. 8. Shows the process of byte datatype. It shows the inputs A and B represented by memory bytes MB1 and MB2 while the output is represented by MB3. The operation shown in Fig, 8, can show the AND logic between two bytes that hold values “10001110” and “00001111” so the output represented by MB3 shows the value “00001110”.

OR bitwise operator

This logic gate has two inputs and one output like the “AND” gate. Like its name, the output comes true when either input A or input B comes true as shown in Fig. 4.

Table 3 lists the truth table of the “OR” gate. It lists all possible combinations of inputs and the output status as well. It shows that the output comes to true when input A or input B comes to true.

Input A

Input B

Output

False

False

False

True 

False

True

False

True

True

True

True

True

OR bitwise in ladder logic

Figure 10 depicts the very simple rung of ladder logic that represents OR bitwise operation. It shows two inputs A and B connected in two parallel branches to give the output. So let us simulate that very OR code and apply the cases listed in the truth table in table  3.

Simulating  OR bitwise operation in ladder logic

Typically as listed in table 3, the output is low when both inputs are OFF as shown in Fig. 11.

Ohh, you can see output comes ON when input A is ON and input B is OFF as shown in Fig. 12.

Also, output comes ON when input B is ON and input A is OFF as shown in Fig. 13.

Also, you can see output comes ON when both inputs A and B are ON as shown in Fig. 14.

That concludes two things guys, in OR bitwise logic, for having output ON, there must be at least one of the inputs High. 

OR block in ladder logic

Ladder logic in Siemens and most other brands offer the facility to perform OR between byte, word, and Double word memory space as shown in Fig. 15 shows the AND block.

Simulate OR block in ladder logic

Figure 16 shows the usage of OR block for byte data type and it is possible to do the same with a word or double word data type. The instruction block applies to byte, word, and double word data types. However, for showing one example, Fig. 16. Show the process of byte datatype. It shows the inputs A and B represented by memory bytes MB1 and MB2 while the output is represented by MB3. The operation shown in Fig, 15, can show the OR logic between two bytes that hold values “00001111” and “11110000” so the output represented by MB3 shows the value “11111111”.

The bitwise “NOT”

This logic gate has only one input and one output. In a very simple language, the output is the inverted logic of the input. So when the input is true, the output would come to false and vice versa as shown in  Fig. 17.

Table 4 lists the truth table rows of all possible combinations of input and output.

Input

Output

True 

False

False

True


Figure 18 depicts a very simple example of a ladder logic rung that shows the output “NOT-A” is the negate logic of the input A.  

Simulating NOT bitwise in ladder logic

Now let's simulate the two cases listed in table 4 when inputs A is high and when it is low as well. Figure 19  and 20 show the output is the negate of the input using the NOT bitwise logic.

Now Guys we have gone through the basic bitwise logic. So how about the other bitwise logic that is formed by combining these basic bitwise operators like XNOR? let us simulate XNOR.

XNOR in ladder logic

XNOR is the negative logic of OR bitwise or you can name it as NOT-OR. Table 5 lists the combination of its two inputs and its output. It shows clearly that, the output becomes true when inputs are equal i.e. both inputs are true or both are false. Yes! Exactly, that’s why this bitwise operation is used when we need to compare two inputs if they are equal or not.

Input A

Input B

Output

0

0

1

0

1

0

1

0

0

1

1

1

Figure 21 shows the symbol of the “XNOR” logic gate showing inputs and output of the bitwise operation.

On the other hand, Fig. 22 shows a sample ladder logic of an “XNOR” logic gate implementation. It shows that there are only two ways to have the output in a TRUE state which are by set both inputs TRUE or setting both FALSE. So let us apply this to our simulator and test cases listed in table 5.

Simulating XNOR bitwise operator in ladder logic

Figure 23 shows the test of simulating XNOR when both inputs are low. You can notice friends that output comes to high.

The same thing when both inputs are high, outputs show high as shown in Fig. 24.

Ohh, you can see when one input is not matched with the other that leads to low logic of the output. That concludes the XNOR gives a high logic output when only both inputs have the same logic; otherwise, it gives a low if they are different.

What next???

Before naming the next tutorial, I would like to let you know that there are many other bitwise operators like XOR, NAND, NOR, etc. this bitwise logic can be programmed in the same scenario we demonstrated above. Now, let me tell you that the next tutorial will be about one of the most important instructions ever in the PLC that will ease the data transfer between memory locations including all data types bytes, word, Dword, etc. it is the MOVE instruction in ladder logic. So be there to learn and practice that good one.

Sequencer Output Instruction in PLC Ladder Logic Programming

Hi friends, today we are going to learn a good technique to run multi outputs in sequence. In another word, when we have some output that is repeatedly run in sequence. In the normal or conventional technique of programming we deal with them individually or one by one which takes more effort in programming and much space of memory. So instead we can use a new technique to trigger these outputs in sequence using one instruction which will save the effort of programming and space of memory. In this article, we are going to introduce how to implement sequencer output instruction. And practice some examples with the simulator as usual. Before starting the article, we need to mention that, some controllers like Allen Bradley have sequencer output instruction and some has not like Siemens. So we are going to give one example for each case showing how to code the equivalent to the sequencer output instruction in the PLCs that does not support this instruction.

Sequencer output instruction 

Figure one shows the block diagram of the process. The instruction takes the input data from the file, array, and data block and sequentially relays it to the outputs to trigger them sequentially.

Figure 2 shows the block of the sequencer output instruction showing input and output parameters. The file parameter is the first input parameter showing the address of the reference sequencer file. In addition, the mask input is to receive the address of the mask or the data block of which the instruction will move the data sequentially before relaying it to the output. Furthermore, the dest parameter is an output parameter that shows the address of the output to which the sequence bits will be applied. And the control parameter is the storage words for the status of the instruction, the file length, and the position of the pointer in the data file or array. Also, the length parameter holds the number of steps to move in the data file to the output destination sequentially. And position parameter holds the location of the pointer in the data file.


Block description and example of the sequencer output instruction

Figure 3 shows an instance of sequencer output instruction QSO. The QSO instruction steply moves through the input data file or array or data block and transfers sequentially the data bits from the data file to the output (destination word) through the mask word. The status of the instruction can be shown in the DONE (DN) bit. You should notice my friends that after the data transition is done the position is reset to the initial position or the first step.

Ladder Logic Example

Now guys, let us move to the ladder logic coding. So how does that sequencer output instruction work in ladder logic? Well!  Figure 4 shows a complete example of QSO instruction that is used in Allen Bradley to handle the sequencer output process, it shows one rung that has a start and stops push buttons from left to right at address I:0/0 and I:0/1 respectively to control the starting and stopping of the sequencer output processing. Next, you can see input I:0/2 which is used as a sequencer process flag to switch on or off the sequencer process. So, if the start PB is requested when no emergency stop and the sequencer on input is ON, the QSO is enabled and the data at address #B3:0 will be moved to dest at address O0:0 though the mask word at address 000Fh starting from position 0 with length 4.

Figure 5 shows the data file that the QSO uses to transfer sequence data bits to output. It shows the bits B3:0, B3:1, B3:2 & B3:3 are set to 1 for reference. So, when the sequencer ON input is set to high (I:0/2). The output Q:0/1 will be turned on based on the data in the data file shown in fig. 5. In that case, the length is 4 and the position is 1.

And when the sequencer flag I:0/2 is switched on next time, output O:0/2 will be switched ON. In that case, the length is 4 and the position is 2 as shown in Fig. 6.

In the third time, the sequencer flag is turned ON, the output O:0/3 will be turned ON and the length and position are updated to 4 and 3 respectively as shown in Fig. 7.

When it comes to the fourth time of switching the sequencer flag I:o/2, the output O:0/4 will be turned high and the position will be at 4 and length is 4 as shown in fig.8. At that time, the process is reset and position reset to 1.

The previous example shows how it is simple to control a bunch of outputs that are required to run in sequence with only one rung of the ladder program and using only one instruction which is QSO in Allen Bradley. This merit helps to save the memory space and time and efforts of programming and troubleshooting as well because the program will be shorter and more readable. However, still, some brands have not supported such instructions even the big names like siemens. That can not be counted as limitations but they are banking on there being a way to implement such logic. So, it is very beneficial for you guys to go implement together a piece of code (ladder logic) that is equivalent to such instruction for performing the function of sequencer output instruction in Siemens S7-1200 using our simulator. 

Ladder logic code for SQ0

As you guys see the sequencer output instruction is nothing but shifting the height value from right to left bit or right to left or even rotated shift for continuous operation. That drives our thinking to use the shift instructions in Siemens to perform this sequencer output instruction. 

Figure 9 shows the rungs of a ladder PLC simple program that implements the sequencer output process. See guys how lengthy the logic we have to code to do the same function of single instruction QSO in Allen Bradley. Again, that is a drawback or limitation thing but the program is more lengthy and takes more effort and also memory it is consuming that way. Moving to the logic listed in Fig. 9, the first thing is using a rotated shift instruction that shifts through the data block bit by bit and applies to the output QW0. At the same time increment instruction is used to move through the data. Also, one on delay timer is used to do some delay to show up the sequencing process of activating the output sequentially. And the end, a comparison instruction has been used to check if the pointer or the position reached the last output coil to reset to the first position and so on.

Simulating the sequencer output ladder code

Figure 10 shows the simulation of the sequencer output ladder code before activating the processes by using M0.0, it shows the position is at 1 and the output QW0 is all zeros. So let us activate the sequencer output process by set M0.0 to high and see the output.

Figure 11 shows the process after activating the sequencer program and starting to switch on outputs sequentially. The figure shows the process reached the sixth output coil and the position set to 7 to point at the next output. The process continues to tell reach the last one and then the position set the first step.

What’s Next???

I am glad to have you guys following tell that point and I hope you see the importance of the sequencer output technique in reducing the effort of programming and saving memory. Next time will take a tour with the bitwise logic operator and how they are utilized and how they work in the ladder program with given examples and for sure simulation to practice their usage. So let’s meet then and be ready.


Introduction to Fluid Mechanics

Fluid mechanics is considered to be one of the essential branches of Mechanical Engineering. Fluid Mechanics comprises two words, fluid, and mechanics, with different meanings and research criteria. In this article, I will extensively introduce fluid mechanics and its importance in daily life. So without wasting any time, let us start:

What is Fluid?

We remember in the early classes, we used to study three states of matter, and afterward, they became four named:

  • Solid.
  • Liquid.
  • Gas.
  • Plasma.

The definition of fluid is the state of matter that can be liquid or solid. We might have noticed that whenever the matter is in any stage, the criteria to know its state is to understand how much stress it can bear, and we name that stress as shear stress that changes its shape. But this situation mostly happens in solid or liquid cases. When the stress is applied, the substantial changes shape and reform into a new one. But up to a limit that cannot destroy its ultimate form. The stress applied to a solid is directly proportional to the strain, whereas, in liquids, the stress is directly proportional to the strain rate. 

What is Mechanics?

As mentioned that fluid mechanics comprises of two terms, so now I will define what mechanics is and how much it is essential in our today’s life. 

  • Mechanics is a branch of physical science that deals with bodies in motion or statics with the effect of force.

Here are two essential terminologies:

  • Bodies at rest.
  • Motion under force. 

Statics

The branch of mechanics in which bodies are at rest is called statics. It is a vast study of internal and external forces in a structure

  • Example:

The best example of statics is when you are standing on a plane on the rigid ground. The force of gravity and the reaction force as the reaction of gravitational force, both these forces act as statics and help in maintaining the state of rest.

Dynamics

The branch of mechanics which studies the bodies in motion is called dynamics. The study is all related to the movement and what is the cause of it. 

  • Example:

The example of moving the body and dealing with all the forces that are acting on and their effect is categorized in dynamics.

What is Fluid Mechanics?

Fluid Mechanics is the sub-category defining the fluid’s nature at rest or in motion.

The types of fluid mechanics are as follows:

1) Fluid Dynamics

  • Fluid dynamics is the study of the movement of liquids and gases. 

    Example:

    It involves the mass flow rate of oil through the pipeline, study of the pattern weather forecast, and blood circulation.

    2) Fluid Statics

    • Fluid statics is the study of the behavior of fluids at rest. 

      Example:

      The best example of fluid statics involves drinking with a straw. The mechanism happening inside is that when we reduce the pressure at the top of the straw, inside the liquid the atmosphere pushes the liquid up to the mouth.

      Sub Branches of Fluid Mechanics

      Fluid statics and dynamics are divided into compressible and incompressible fluid as well as real and ideal fluids. So real is divided into the laminar and turbulent flow, and this goes on. 

      Classification of Fluid Flows

      As I discussed earlier, fluid flow is classified, and they vary from type to type.

      Laminar and Turbulent Flow

      By reading their names, you get an idea of what a laminar and turbulent flow is. So laminar flow is one in which fluid flows smoothly without any turbulence. Usually, highly viscous fluid with low viscosity is characterized as laminar flow. Whereas turbulent flow is the one in which fluid flow is not smooth. And they have high velocities.

      Compressible and Incompressible Flow

      The compressible and incompressible flows depend upon one of the significant factors density. The flow is incompressible when the density is constant or nearly constant throughout the flow. Incompressible flows characterize most liquids. The compressible flows are opposite to the incompressible ones; they don’t have constant throughout. One of the best examples of compressible flow is gases.

      Viscous and Inviscid Flow

      In these types of flow, viscosity is one of the essential vital elements. Every fluid has some viscosity value. So the flows with a significant amount of frictional effect are said to be viscous. Inviscid flows are where viscosity is neglected or to some extent.

      Internal and External Flow

      As the name shows, the flow covered with a solid boundary is considered internal flow. The liquid is flowing in a pipe or a wire. At the same time, the external flow is defined as an unbounded flow. The fluid flowing over the pipe or the fluid over the ball is exemplified as external flow. And the flow inside a pipe covered is said to be internal flow.

      Steady and Unsteady Flow

      The names are enough to define the nature of the flows. So steady or uniform flows are said to be steady flows. And the unsteady one is opposite to the steady one that does not have any uniformity.

      Natural (Unforced) and Forced Flow

      Natural flows are the one that flows naturally. But the theoretical example will be the one that flows due to the buoyancy effect. The forced fluids are the ones that are forced to flow with the help of external means. An example of forced flow is a fan or pump.

      Types of Fluids

      The nature of the fluid varies from type to type. The following are some vital types of fluids.

      Real Fluid

      Real fluids possess viscosity. Viscosity is defined as resistance or opposition. Eliminating the ideal cases, all the fluids are examples of real fluids.

      Ideal Fluids

      The ideal fluids are the one that has no viscosity at all. As I have mentioned just now that all the fluids have viscosity. So ideal fluids are just an ideal case study.

      Newtonian and Non-Newtonian Fluid

      Both of them have two different properties. The Newtonian fluids are the ones in which the shear stress is directly proportional to the shear strain, and in non-Newtonian fluids, they are not proportional to each other.

      Ideal Plastic Fluid

      The ideal plastic fluids are the ones in which shear stress is directly proportional to the shear strain. The shear stress value is also more than the yield value. These fluids are velocity gradient ones and have significant importance.

      Fundamental Laws applied in Fluid Mechanics.

      Fluid mechanics is considered one of the vast branches of mechanical engineering that covers all the fundamental laws of physics. It is not wrong to say those fluid mechanics depend on these laws, and they are named as follows:

      • Second Law of Thermodynamics

      • Conservation of mass

      • Conservation of linear momentum

      • Conservation of energy 

      • Conservation of angular momentum

      Graphical Presentation of Fluid Types 

      As I have briefly discussed all the types of fluids, the following is their graphical presentation.

      Properties of Fluid

      The properties are one of the significant features of everything. The fluids also have some properties. The following are some essential properties of the fluid. 

      Viscosity

      The word viscosity means thickness. According to the definition, viscosity is defined as the fluid’s property related to friction and resistance.

      When one layer moves adjacently to the other, some friction exists, which we named viscosity. The layers are moving at some distance and are named dy. The velocities of the fluids are u and u+du, respectively. 

      The graphical presentation of the layer velocity versus the distance is shown below.

      The graph will explain the trends of velocity and distance. As mentioned, two layers are moving adjacently to each other, so the layer that is on top imposes shear stress on the lower layer, and the lower layer, in response, causes shear stress on the upper one. 

      Density

      According to physics, density is defined as the mass to volume ratio. So the fluid mass to fluid volume ratio is the density of the fluid. In liquids, the density is constant, but in gases, it’s variable.

      Specific Weight 

      The specific weight is defined as the ratio between the weight of the fluid and volume. Thus the weight density is defined as the weight per unit volume of fluid and is denoted by w.

      Mathematically,

      w=Weight of FluidVolume of Fluid

      w=Mass of Fluid×Acceleration due to cycleVol. of fluid

      w=Mass of Fluid×gVol. of Fluid

      w=ρg

      Specific Volume

      The specific volume is defined as the volume of a fluid by a unit mass or volume. This property applies to gases.

      Mathematically,

      Specific Volume = Vol. of fluidMass of fluid

      Specific Volume=1Mass of FluidVol.

      Specific Volume=1

      Thermodynamics Properties

      The thermodynamic property is the salient feature of gas and liquid. We know that when liquids are compressed, they form gas, so thermodynamics is one of the critical features of gases.

      The equation below shows a connection between the pressure, specific volume, absolute temperature, and gas constant.

      p=RT

      System and Control Volume

      The definition of a system is as follows:

      The system is the quantity of matter or a specific region specified for research or study.

      As you can see from the diagram, an imaginary or real wall or a surface separates the system from the surroundings.  So a system can be open, close, or isolated (special case). So following is a brief explanation of all the types of systems.

      • Open System

      In the open system, the volume is controlled and the energy and mass can easily pass through the boundary of the control volume.

      • Close System

      In the closed system, the mass is controlled and cannot cross the boundary. The energy can cross the boundary easily and volume is also not fixed.

      • Isolated System

      In the isolated system, the energy cannot cross the boundary.

      Units and Dimensions

      The definition of dimensions is as follows:

      • “The physical quantities are mentioned by names called Dimensions.”

      The Definition of units is as follows:

      • “The magnitudes are assigned dimensions that are called units.”

      There are two types of units explained below briefly.

      Primary or Fundamental Units

      Some basic dimensions are given the names and they are as follows:

      Dimensions

      Units


      Mass

      m

      Temperature

      T

      Length

      L

      Time

      t


      Secondary or Derived Units

      Some dimensions are assigned names in terms of primary dimensions and they are as follows:

      Dimensions

      Units


      Volume 

      V

      Energy

      E

      Velocity

      V


      Two kinds of units are commonly used in today’s world and that is;

      English System (United States Customary System USCS)

      The English system does not have an apparent systematic and numerical base. It is considered to be one of the most difficult systems to memorize. In almost every country metric SI units are widely used but the United States is the only country that has not fully opted for the metric system rather they use the English system in many states.

      Example

      • 12 in =1 ft

      • 1 mile =5280 ft

      • 4 qt =1 gal


      SI units (International System)

      It is one of the most commonly used and feasible units. The metric SI units are widely used in industries and countries like England. There are seven basic fundamental dimensions introduced and their units in SI are as follows:

      Dimension

      Unit


      Length

      meter (m)

      Mass

      Kilogram (kg)

      Time

      Second (s)

      Temperature

      Kelvin (K)

      Electric Current

      Ampere  (A)

      Amount of light

      Candela (cd)

      Amount of Matter

      Mole (mol)


      Application of Fluid Mechanics 

      There are numerous examples of fluid mechanics in our daily life. The following examples are some crucial parameters that cover fluid mechanics.

      Human Body

      Our heart is an integral part of the human body that pumps blood to all body parts through arteries and veins. In this modern era of science and technology, many scientists have designed artificial hearts that work on the working principle of fluid dynamics and transmit blood and pumps like the original heart. 

      Homes 

      Our homes are one of the best examples of fluid mechanics. The piping, sewage, hot and cold water pipes, natural gas, and LPG work on fluid mechanics principles. Moreover, our refrigerator, air conditioning, heating, cooling, and insulating system are all examples of fluid mechanics.

      Automobile Industry

      We find various examples in our cars, planes, buses, and ships. Fluid mechanics covers all the fields associated with fuel transportation, from the fuel tanks to the cylinders, fuel pumps, carburetors, etc. It covers all the cooling heating systems of automobiles, lubrication systems, power steering, and radiator cooling. 

      Medical Field

      Fluid mechanics is used in many medical devices such as glucose monitors, heart assistance devices, etc.

      Environment

      It is beneficial for eliminating pollution from the atmosphere, cleaning water, cleaning sewage systems, and controlling floods.

      How to Optimize a PCB Panel Layout

      Hi Guys! Hope you’re well today. I welcome you on board. In this post, I’ll walk you through How to Optimize a PCB Panel Layout.

      PCB panels are used in the manufacturing process to produce PCBs in large numbers. This not only reduces the overall cost but also makes the manufacturing process more efficient and reliable. PCB panelization is a manufacturing technique where multiple PCB designs are replicated on a single large board called a PCB panel. Then these individual boards are removed and depanelized from the panel to install them in the final product. 

      The number of panels is directly related to the overall manufacturing cost. To produce more panels, more cost will be required. However, it also depends on the shape of the board. If you require more boards of the same shape and size, it will reduce the panel cost since they all can be replicated and manufactured on a single panel. 

      Read on to find out how we can optimize the PCB panel layout to save both cost and time.

      Let’s jump right in.

      How to Optimize a PCB Panel Layout

      PCB panel is a large board that contains multiple instances of a small PCB. Know that the PCB panel is made up of the same material as the board and the panel size depends on the number of small boards you aim to produce. It is required to best use the panel space to produce PCBs in large numbers and to keep the unused panel space minimum. Since the more boards cover the overall panel space, the more efficient the manufacturing process will be.

      PCB Panelization Layout Tips

      You can pick the panel size as per your requirement. However, the most commonly used panel size is 18 x 24 inches. Normally, the boards are 0.100 inches apart in one panel. Panel designers need to be very careful while putting the PCB designs in the panel. 

      1: Use PCB Design Tools Efficiently

      It is important to use your PCB design tools properly to avoid any hassle in the manufacturing process. Decisions made earlier during the PCB design process can go a long way and keep you from redesigning the entire board from scratch. These design tools can help you select the layer stack up configuration and PCB material. Additionally, they can help you transfer design data using open standard formats. Using this format manufacturer can exchange the design information with the PCB designers and can compile it in one file format.

      2: Collaborate with PCB Designer

      To make the manufacturing process efficient, it’s better that PCB designers are in contact with the PCB panel designers. This way PCB designers can make some tweaks in the design to slightly alter its shape so multiple boards can effortlessly find a place in the panel to maximize the panel space. 

      Following points that every PCB designer should consider:

      • Hanging Components

      Extra clearance is required for some components that overhang the board. It is created around the outline of the board placed in the panel. Creating extra room for clearance may slightly change the design of the panel. So it is better that designers contact in advance with the panel designers to figure out all the options. 

      • Adding Features 

      The panel designer will add features to the panel like edge marks and tooling holes. PCB designer needs to make sure the placement of PCB designs doesn’t cause any problems. 

      • Weight of Components

      PCB panels carry some strength though, but they are not solid enough to withstand a large number of components. Since components come in various shapes and sizes. They vary from small to large size. The concentration of components may cause some problems and create a slight bend in the panel. Collaborate with the panel designers to explore other PCB layout options to handle a large number of components.  

      • Width of the Board

      The panel made up of thick board doesn’t cause a problem. It can deal with all manufacturing techniques applied on the array of the board. Trouble arises when thin boards are to be made for the final product. Thin boards produce a bend on the panel and can cause the solder to appear on the top of the boards. Consult with your panel designer if you want thin boards for your product. They may use a pallet and come up with another PCB layout option to deal with the thin boards. 

      3: Consider Board Edge Clearance

      Board edge clearance is another aspect to take into consideration while making the panel. It works as a shield for the board components and the copper and keeps them from being damaged. 

      • Clearance in Breakout Tabs

      PCBs are depanelized in two ways: by breakout tabs where small tabs are produced between the PCB designs. These tabs come with spacing between them on the panel. Both the copper and the components should have 0.125 inches clearance from the tab. 

      • Clearance in V-grooves 

      Another method of depanelization is by cutting the V-grooves that are pre-scored V-shaped marks placed alongside the board edges. In V-grooves, the copper should come with 0.02 inches clearance and the components should exhibit 0.05 inches clearance. 

      4: Panels For Flexible Boards

      FPC flexible boards are produced using three-panel methods namely:

      • Backward Panel

      • Conventional Panel 

      • Oblique Panel

      Panels are created to save material during the manufacturing process. Keep the distance between the boards minimum to effectively use the entire space of the panel. To accelerate the manufacturing process and to keep the entire panel process in check, a few things are included in the panel, like plate size, necessary instructions, and etching characters. 

      Each corner of the entire panel is drilled with a positioning hole to keep the board in place during the production process. For flexible boards, the panel width and the length should be 250mm. Since the larger board will lead to low production accuracy and eventually product failure. 

      Where to Buy PCB Panels?

      An online world is flooded with scores of PCB fabrication houses. It’s difficult to find a diamond in the rough when all they claim to be the best in what they do. What we are going to share is our personal experience with the company called PCBWAY. They have an expert team who thoroughly hears your demand and guides you along the process to make an educated decision. The products are no less than quality. If you think what we say doesn’t really matter, then go and try it yourself, you’ll find the answer. 

      Apart from PCB prototype, they also offer PCBA (printed circuit board assembly) service so you don’t have to place components on the board. They come on board ready-made from the fabrication house. 

      If you want PCBs in large quantities, PCBWAY also offers PCB panel service. When you order a PCB panel, you can see a significant cost difference compared to if you want one PCB to be manufactured. 

      How to Order PCB Panel

      When you visit the called PCBWAY Fabrication House website, you see the following image with the option “Instant Quote”.

      After writing the parameters in the given space, when you click the “Quote now” option you will come across the following page. 

      You can the option “Board Type” which is further divided into three categories with two options for PCB Panel. Either you can select the PCB panel by selecting the “Panel by Customer” option or you can ask PCBWAY to make the panel by selecting “Panel by PCBWAY”

      You can select the size, quantity, number of layers, material, thickness and much more. Additionally, you can write down your further requirements in the “other special request” option. The primary aim is to give clear instructions so the final product exactly matches what you ordered in the first place. 

      Order Status

      Once you submit the order, it exhibits full detail on how your order is going to be processed, including order status, address, past orders, invoice details and the total time it’ll take to complete the order. There are no hidden charges, which means you’ll be charged exactly the price you’ll see in the order status. Plus, if you find any difficulty in placing the order or in the selection of the material, they will guide you to make the final decision. 

      Live Chat Option

      The website comes with a live chat option through which you can communicate with the agent for any query. Though their English is not impeccable, but it still good enough to understand your questions and answer them properly. You can contact them any time from Monday to Friday, however, if you want to contact on the weekend, you can submit your email address and leave a message and they will get back to you within one business day. 

      Quality Assurance

      Once the boards are manufactured, they go through a rigorous inspection test to ensure the quality of the product. This includes if the holes are properly drilled and aligned, the uniformity of the traces all the way through the board, and a thorough comparison with the design document to ensure that all the requirements are met. The inspection tools include an X-ray inspection machine, a flying probe tester, and an automated inspection machine. With 50 engineers on board, rest assured the final product gets the proper treatment it deserves. 

      Final Thoughts

      As mentioned earlier, panels are used to produce PCBs in bulk. Moreover, they also refrain the boards from vibration and shock during the assembly process. 

      A simple coordination between the PCB designer and Panel designer can help build the PCB panels with accuracy and efficiency.

      If you’re a newbie and just starting out, it’s better to get your PCB manufactured by the professionals in the PCB fabrication house. This will save you both time and money and you’ll learn many things along the process. 

      That’s all for today. Hope you find this article helpful. If you have any questions, you can ask me in the section below. I’d love to help you the best way I can. Thank you for reading the article.

      How to Help a Remote Team Work Together

      There is no doubt that the traditional workplace has changed in a major way in the last few years.  About half of companies now have remote workers. This means that managing a team looks different from what it ever did before.  Facilitating the best of what a team has to offer, the synergies, the camaraderie, the collaboration, looks and feels different.  It is sometimes difficult. 

      Those who manage remote teams are learning how to keep teams engaged and motivated, even as they work in isolation.  Here are some of the techniques they are employing to keep their employees on track.

      Regular Touch-Base Meetings

      Working in the office made it easy and natural to casually ask questions, double-check information, and get feedback from colleagues.  That ease made collaboration and the sharing of ideas more convenient.  Managers who want to keep the teamwork going need to create situations in which employees have the chance to talk informally about work. 

      Scheduling a daily touch-base meeting , set up not to accomplish a specific task, but rather just to get aligned on the day, is vital.  These daily meetings should be short and predictable.  Every team member should know that this meeting is where they will be briefed or reminded about the big picture for the day and have the opportunity to make comments or ask questions about things the team is working on.  To be clear, this is not a time to get into the details about how to accomplish a project, but rather a time to discuss teamwork in general.

      Practice Great Communication

      In-person, co-workers can hear each other’s voice inflections, see body language, and generally understand more of the intent behind what someone is saying.  Communicating through a computer screen takes away all those context clues.  It’s really easy to misinterpret someone’s tone when you read a text or email.

      The solution is to take zero shortcuts when it comes to communication .  Don’t rush the email.  Write in complete sentences.  Avoid shorthand and abbreviations.  Make it clear that if anyone has any questions, they are welcome and encouraged to ask.  Thank employees who take the time to verify and clarify instructions.

      Communicate Parameters

      Working from home might feel, to some employees, like they are always at work or that there are no boundaries for when to send emails.  Being connected 24/7 should not make the team feel obligated to be available for work 24/7.  Clearly communicate what the expectations are for when employees should be sending messages and also the timing of when they should be responding.  For example, employees should respond within 3 hours of receiving a message between 9 a.m. and 5 p.m., but have no obligation to respond outside of those hours.

      These rules will help the people communicating information and receiving information.  They have the added benefit of building trust among team members and of making employees feel appreciated by the company.

      Give Them the Right Tools

      Technology tools that help the team work as a team are the most important investment a company can make when it has remote employees.  It’s not a place to scrimp.  Companies are wise to look into software hosting services that allow any computer from any location to share desktop features, access to software, and the ability to work on shared documents.  A good software hosting company will also provide security from hacking, cyber-attacks, and guard logins. 

      Having the right technology is so crucial that it should be a regular topic of conversation among teams and managers.  Managers should regularly poll employees about how their current technology is meeting or not meeting their needs. 

      Focus on Big Picture

      Even when everyone worked in the same office, managers understood that employees all have their own personalities, challenges, and styles.  The era of working from home only adds to those differences.  Not only do workers come to the job with their own personal uniqueness, but they now also bring their home lives to work, literally. 

      Some employees may live in areas with inconsistent internet connections.  They may have pets, relatives, roommates, or alternative living arrangements. Their living space may not have the capacity for a dedicated workspace.  They may live somewhere where getting some quiet or privacy is a struggle.   

      The way managers can combat these special needs is to shift the focus of work towards goals and deadlines, rather than pacing.  Managers need to be more flexible.  It’s not even possible to micromanage remote teams, so why try?  Does it really matter if an employee is going to be distracted by his kids getting off the bus every afternoon as long as he puts in the time and effort to get his work done on time? 

      Remind the team why they are doing what they are doing.  Understanding the purpose of the work is a huge motivator and will drive better performance.

      Running ZeroTier On the Raspberry Pi 4

      Our next step in the Raspberry Pi training program is to get zero tiers up and run on a Raspberry Pi 4. How to utilize a Raspberry Pi to measure internet speed and store the results in Grafana or Onedrive was the topic of the last piece. During the project, you will discover how to install ZeroTier on a Raspberry Pi and get it up and running. We will also learn how to set up a firewall to secure our network.

      Where To Buy?
      No.ComponentsDistributorLink To Buy
      1Raspberry Pi 4AmazonBuy Now

      Components

      • Raspberry pi 4

      • Power supply

      • Ethernet or wifi

      What is zeroTier

      ZeroTier is a software that provides a streamlined web-based interface for constructing virtual networks connecting various gadgets. Somewhat akin to configuring a virtual private network on a Raspberry Pi, these networks exist only in cyberspace. The process of provisioning, however, is much easier, especially when dealing with several devices.

      Using a decentralized system, all of your gadgets will connect. The end-to-end encryption used to protect these connections helps ensure that your data remains private at all times.

      ZeroTier can be used on various platforms, from computers to mobile phones. Its cross-platform compatibility with Unix, Microsoft, and macintosh means you can set up a virtual connection without worrying about whether or not your hardware will be able to connect to it.

      The ZeroTier business model is "freemium." Using our free plan, you can connect up to 50 approved devices to the virtual network.

      Retrieving your ZeroTier Network ID

      You need to create an account on the ZeroTier website before you can use the program on your Raspberry Pi. This is because virtual network administration is performed through their website.

      You may manage your entire virtual network from one central web-based console, including assigning permanent IP addresses to individual devices.

      Registration on the ZeroTier hub website is required before a network ID can be generated. Access your virtual networks with this web-based interface. Go to ZeroTier Central on whichever browser you like. When you go to the site, look for the "Register" button so you can start the account creation process.

      Sign up for ZeroTier by providing the necessary information on the supplied registration form. After you've finished filling out the form, hit the "Register" button at the bottom.

      Your account won't be active until you confirm your email address after signing up for it. 

      The following window will appear once you've created an account and logged into the web interface. Hit the "Create A Network" button in the screen's center to get started.

      When you initially visit ZeroTier and click the button, your first network will automatically generate. The network identification number is listed here. We require this identifier to link your Raspberry Pi to the ZeroTier virtual network. Keep this ID in a safe place; we'll need it soon. Select the entry to change the network's settings, such as the name.

      Listed below are the default configuration options for your ZeroTier system. You can modify the network's name, provide a description, and adjust its security level using these options. In addition to the IP subnet, ZeroTier gives you control over many other virtual network features.

      Installing ZeroTier to the Raspberry Pi

      We can move on now that you've joined ZeroTier and have your network ID. In this part, you'll learn how to download and install ZeroTier on your pi device.

      First, let's check that the software on your pi Device is up to date.

      To be up-to-date, we need to run the following two instructions for the item list and all installed modules.

      sudo apt update

      sudo apt upgrade

      After adding the GPG key, we can install ZeroTier via their installation repository on our pi Device. With this key, we can ensure that the tools we're installing are directly from ZeroTier and don't include any malicious code. To obtain the GPG key via their repo, type the following code and store the contents of the "de-armored" file in the "/usr/share/keyrings/" folder.

      curl https://raw.githubusercontent.com/zerotier/ZeroTierOne/master/doc/contact%40zerotier.com.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/zerotierone-archive-keyring.gpg >/dev/null

      Now that the GPG key has been inserted, a source list containing the ZeroTier repository must be compiled. First, we need to create a shell variable named "RELEASE" and assign it the operating system's internal codename. To construct the proper URLs for the ZeroTier repo in the subsequent steps, we will execute the following command.

      RELEASE=$(lsb_release -cs)

      Once we have the shell variable configured, we can utilize it to construct the relevant ZeroTier repo Urls for the Operating system. We finally save this string in the "/etc/apt/sources.list.d/" folder under the name "zerotier.list."

      echo "deb [signed-by=/usr/share/keyrings/zerotierone-archive-keyring.gpg] http://download.zerotier.com/debian/$RELEASE $RELEASE main" | sudo tee /etc/apt/sources.list.d/zerotier.list

      The next time you refresh the Raspberry Pi's packages lists, it will pull ZeroTier directly from this location.

      Since we have modified the Rpi's source code, we must revise the list of installed packages. Using the command line, you could change your system's component list.

      sudo apt update

      After updating, we can use the command beforehand to download the ZeroTier package onto our RPi.

      sudo apt install -y zerotier-one

      ZeroTier can be set up to automatically launch on system startup as part of the setup procedure.

      Running ZeroTier on Raspberry Pi 4

      Having finished the ZeroTier installation on our RPi, we can now link to the networking we created in the introduction. First, make sure you get the network's identifier handy.

      To connect the RPi to the network, we must use the ZeroTier Command line. You can utilize the following code to accomplish this. As a first step, swap out "[NETWORKID]" for the ID you gathered previously in this tutorial.

      sudo zerotier-cli join [NETWORKID]

      So after this message, your RPi should've just joined the ZeroTier channel.

      Joining the ZeroTier channel doesn't make your machine an official part of the network until you verify it. To accomplish this, you must go back to a ZeroTier Main dashboard and change your network configuration. You can also access the site via the following Link, where you should substitute your network Address for "[NETWORKID].

      https://my.zerotier.com/network/[NETWORKID]

      The "Members" portion is located toward the bottom of the managerial section for the ZeroTier system on the RPi.

      1. You'll need to select the "Auth" box to tick here after identifying the machine you added. As a result, your RPi can communicate with other gadgets on the same network.

      2. A machine through your ZeroTier channel can be located using the information in the "Address" column. The "sudo zerotier-cli status" prompt will cause the RPi to display this data.

      3. The Name/Description field can be used to assign a memorable label to this innovative gadget for future reference.

      4. Lastly, take a peek at the "Managed IPs" section.

      If an IP address has been assigned to the gadget, it will appear in this column. These IP addresses will allow you to gain access to that machine. This column can specify which device will receive the IP address. If you're trying to get an Internet address for a newly approved source, be patient; it could take a few minutes.

      As soon as your RPi executes the command below, you will know it is linked to the ZeroTier channel. Using this prompt, you can see a complete list of the tracks to which the ZeroTier configuration is currently connected, along with information about the health of those connections.

      sudo zerotier-cli listnetworks

      Whenever your RPi successfully connects to the ZeroTier networks, you must see something similar to what is shown below. The last number is Pi's Internet protocol address within the VPN connection.

      Connecting your RPi to your ZeroTier network must now be complete.

      Connecting to other gadgets on the VPN connection is now possible. Having the device's Internet protocol is all that's required. The ZeroTier management console is the quickest way to learn which IP addresses are assigned to particular gadgets. 

      Protecting your Raspberry Pi with UFW

      Syncthing is a piece of software developed specifically for real-time file synchronization between several devices like the RPi. TLS is used to synchronize data among nodes. Thanks to encryption, the data will be safe and private in transit, making it more challenging to steal. Every node must prove its identity with a rock-solid cryptographic certificate. A connection will be denied to a node if it does not present a valid certificate, preventing unauthorized access. Files on your Microsoft, Macintosh, or Unix computer can be synchronized with your Raspberry Pi via Syncthing. Simple Syncthing apps are accessible for all of these platforms.

      Setting Up Syncthing on a Raspberry Pi

      Here you can find detailed instructions for setting up your RPi with the Syncthing program. For the program to be installed, we must first add the program's PGP keys and the package repo as possible sources.

      We need to upgrade our Rpi before downloading the files synchronization program. We can use the following two commands to keep our RPi up to date.

      sudo apt update

      sudo apt full-upgrade

      Following this, check that the apt-transport-HTTP package has been successfully installed. When using the installer, you can now access sources that utilize the secure Secure protocols, thanks to this package's inclusion. It's not possible to do this by default. This is included by default in most modern operating systems, but it may be missing from lightweight distributions like Raspberry Pi OS Lite. Executing the line below will install the necessary package.

      sudo apt install apt-transport-HTTPS

      Finally, the Syncthing credentials may be added to our keyrings folder. The purpose of these keys is to verify the authenticity and integrity of the packages we install before trusting them. To obtain the credentials, execute the command that follows on the RPi.

      curl -s https://syncthing.net/release-key.txt | gpg --dearmor | sudo tee /usr/share/keyrings/syncthing-archive-keyring.gpg >/dev/null

      Since the key has been included, the repo itself may be included. The RPi project will use the Syncthing program, namely the stable release. Use the following command to include the repo in the list of sources.

      echo "deb [signed-by=/usr/share/keyrings/syncthing-archive-keyring.gpg] https://apt.syncthing.net/ syncthing stable" | sudo tee /etc/apt/sources.list.d/syncthing.list

      We have to refresh the installation list before installing Syncthing from the repo. We must revise the list for the package manager to use our different sources. To update your RPI, type the following command into your device's terminal.

      sudo apt update

      Let's finish setting up our RPi by installing the Syncthing app. Now that the package repository has been added, the program can be installed with a single command.

      sudo apt install syncthing

      Permitting Third-Party Access to the Syncthing Graphical Interface

      The Syncthing web application will only be accessible while close to the device. Those using a Raspberry Pi without a monitor or keyboard would have a very frustrating time if this were the case, but we can change the setup to allow external access.

      The first order of business is to discover the RPi's actual local network address. Before proceeding, please ensure that your Rpi has been assigned a permanent IP address. This command lets you find your Pi's local IP address.

      hostname –I

      To move on, a single iteration of Syncthing must be run to create initial configuration files. The RPI user will be used solely in this tutorial to launch Syncthing.

      Syncthing

      Press CTRL + C to exit the program after the first launch.

      The necessary configurations for Syncthing will be generated after the first execution. The Syncthing program must be launched in the context of the pi user for this configuration file to take effect. With nano editor, start editing the necessary configuration file with the line below.

      nano ~/.config/syncthing/config.xml

      Locate the following code in this script with the searching key CTRL + W to quickly locate this sentence.

      127.0.0.1:8384

      This line needs to have the local Internet protocol of our Pi substituted for the default local Internet address (127.0.0.1). For instance, with our Pi's IP address, this code would become something like this.

      192.168.0.193:8384

      We are limiting our access to people in the same local area network by use of the local Internet address. Alternatively, you can use the internet address "0.0.0.0" to grant access to every IP. Following the successful IP address change, save changes to the script.

      Installing Syncthing on a Raspberry Pi as a Web Service

      One final step is necessary now that the Syncthing us may be accessed from devices other than the RPi. This responsibility includes developing and launching a system for the program. The Service will enable Syncthing to launch automatically at system boot and be halted and started quickly.

      Once again, we'll use nano to make the necessary changes to the Service's configuration file. The Syncthing authorized GitHub is the source for the application we will be developing. To start adding content to the file in "/lib/systemd/system," run the following command.

      sudo nano /lib/systemd/system/syncthing.service

      copy lines below and paste them to this file.

      [Unit]

      Description=Syncthing - Open Source Continuous File Synchronization

      Documentation=man:syncthing(1)

      After=network.target

      [Service]

      User=pi

      ExecStart=/usr/bin/syncthing -no-browser -no-restart -logflags=0

      Restart=on-failure

      RestartSec=5

      SuccessExitStatus=3 4

      RestartForceExitStatus=3 4

      # Hardening

      ProtectSystem=full

      PrivateTmp=true

      SystemCallArchitectures=native

      MemoryDenyWriteExecute=true

      NoNewPrivileges=true

      [Install]

      WantedBy=multi-user.target

      Those lines specify how our Rpi's OS must deal with Syncthing. When you're done adding lines, save the file. We could now set up our Service to automatically launch at system startup. Enter this command and hit enter.

      Sudo systemctl enable syncthing

      Let's run the Service to use the Syncthing internet UI. Once again, the systemctl tool will need to be used to kick off the Service.

      sudo systemctl start syncthing

      The Syncthing program on the RPi should be checked to ensure it has begun. Using the below program, we can make sure of that.

      sudo systemctl status syncthing

      The notification should read as follows if the Service was successfully started and is now active.

      Web-based Syncthing management through RPi

      If everything goes smoothly, you should utilize the Syncthing program on the RPi. Now that the hardware has been added, we can move on to configure the program and synchronize our data. We'll break this up into chunks for easy reading. The web-based user interface makes installing and linking devices a breeze.

      Establishing a Link to the Syncthing Online Control Panel

      You'll need to launch the web-based interface in your preferred internet browser to begin using it. The Internet address of the RPi is required to use the web-based interface. Using the Url, navigate to the following location in your preferred internet browser.

      http://[PIIPADDRESS]:8384

      Since the Syncthing program only listens on port 8384, you mustn't remove it from the end of the string.

      After creating login details, you will be prompted to sign in before proceeding to the next step.

      Safeguarding Syncthing's User Interface

      There is no predetermined login information for Syncthing, meaning anyone with access to the UI can change your preferences. Login credentials can be set up to prevent unauthorized users from wreaking havoc.

      You will be warned of the potential risks if you have never specified the login details. The "Settings" button on this caution will take us directly to the configuration page.

      The configurations page can also be accessed by choosing the "Actions" menu box in the upper right corner and then choosing "Configurations" if this notice isn't shown.

      Navigate to the "GUI" tab on the popup settings page. Access this window's account by selecting "GUI" from the window's header.

      Login credentials are required to access this page. Passwords should be firm and difficult to guess. Use a mix of alphabetic characters, numeric digits, and special characters. After inputting each, hit the "Save" option to keep your modifications.

      After resetting your password, this website will log you out. You'll need to sign in with your new credentials each time you access Syncthing's graphical interface.

      How to Get the Syncthing ID of a Device

      For Syncthing to function, it must create a random identifier for each connected device. Adding the other device's ID to your own is necessary for sharing information between devices. The RPi Syncthing installation's unique identifier can be located via the web interface.

      To return to the main page of the web interface, select "Actions" from the toggle menu in the top right. Select "Show ID" from the selection menu to open the desired dialogue box.

      The identification string and corresponding QR code are displayed below. The ideal identifier length is between 50 and 56 characters and may incorporate digits, letters, and hyphens. System-wise, the hyphens are disregarded, but they improve readability. If you want to connect your Raspberry Pi to additional devices, you'll need to give each of them the unique ID assigned to your Pi. You must also include their identification number. Syncthing's mechanism for linking many gadgets to a single pool requires the ID.

      Incorporating a New Device into Your Raspberry Pi's Syncthing Network

      We've covered how to get your gadget id Number, so now we'll cover adding a new one. Keep in mind that the identifier for your RPi must be entered into whatever gadget you are installing. If not, communication between the devices will be impossible.

      The "Add Remote Device" button may be in the lower right corner of the Syncthing UI. When we click this option, we'll be taken to a dialogue where we can add a gadget to our Syncthing collection.

      You can add any unique Id to your pool from this dialogue box. Enter the identifier for the gadget you wish to link to the top textbox. Despite its length, the ID is easily copied and pasted. After that, you can connect the device by clicking the "Save" option.

      Incorporating each other's Syncthing server Identifiers should result in a successful connection.

      Replicating a Directory Across All of Your Devices

      Now that we have a device linked to the RPi Syncthing, you can test directory sharing. In this particular chunk, the default directory will suffice. Here, we keep our sync files in a folder called "/home/pi/sync" on our RPi.

      Select the "Edit" button next to a directory to change its share settings. We can access the folder's sharing settings dialog by clicking this option and making the necessary changes.

      We must navigate to the Share tab under the file settings dialogue. Select the "Share" tab to switch to this view.

      The dialog box lets you pick which gadgets will access the shared folder. As soon as you've decided which devices you wish to sync with, choose the "Save" option.

      When syncing with the other gadget, keep in mind that you'll need to accept the shared folder on that gadget before synchronizing can begin.

      Your directory should have started syncing immediately. When the syncing process is complete, the guide and the gadget should be labeled "Up to current."

      Conclusion

      Having ZeroTier Syncthing installed on your RPi and linked to a VPN, you may now sync data across machines. If you're looking for a basic virtual network solution, ZeroTier is it. And the best part is that it offers an ideally enough free plan for most people's fundamental needs. Additionally, Syncthing is a user-friendly software that enables you to synchronize folders across several gadgets. The program is among the best methods for allowing many computers to maintain directory consistency in real time. No longer will you have to trust a remote service like Cloud Servers to keep your data safe.

      Internet Speed Monitor Using Raspberry Pi 4

      Following up on our Raspberry Pi programming course is the next lesson. In the previous post, we learned how to construct an FM radio using a Raspberry Pi. Analog FM broadcasting's circuit construction was also studied in detail. How to use a Raspberry Pi as an internet speed meter and save the data in Grafana or Google Drive is the subject of this article.

      You can use this article if you want to keep track of how your downloads, uploads, and ping speeds change over time, and it's easy to use. In addition, you can use this to determine when your internet is at its busiest or if your internet speed has deteriorated. We'll demonstrate how to use Ookla's Internet speed test command-line interface in conjunction with Python code to create an internet speed meter.

      The connection speed monitor will employ the Internet speed Command line interface to keep tabs on your connectivity.

      Components

      • Raspberry pi 4

      • Micro SD card

      • USB drive

      • Ethernet cable or Wi-Fi

      Installing the Speed test CLI

      The first step in configuring the RPi to monitoring system the Internet's performance is to ensure the Raspberry is updated. There is an easy way for this using the command line:

      sudo apt-get update

      sudo apt-get upgrade

      To add a repo for the Internet speed Command line software, we have to download a few additional packages. apt-transport-https, dirmngr, & gnupg1 may all be installed on your RPi by running the commands listed below.

      sudo apt install apt-transport-https gnupg1 dirmngr

      The apt software may now use the HTTPS secure protocols thanks to the apt-transport-HTTPS module. Apt will fail to connect to Ookla's software repository if it doesn't have it. Our Speedtest.net services and your RPi must communicate securely, therefore we'll also set up gnupg1.

      Lastly, the dirmngr software is installed. This software is used to add the package repositories to the Rpi's source list. Now that we've installed the necessary tools, we can import the GPG keys for Ookla's Performance test repository into our keychain and start running tests. The performance test CLI interface cannot be downloaded to our RPi without this passcode.

      curl -L https://packagecloud.io/ookla/speedtest-cli/gpgkey | gpg --dearmor | sudo tee /usr/share/keyrings/speedtestcli-archive-keyring.gpg >/dev/null

      The Ookla repo must be added to our list of sources next. The Performance test CLI cannot be installed on our RPi without the repo being added. The command to add this repo is as follows.

      echo "deb [signed-by=/usr/share/keyrings/speedtestcli-archive-keyring.gpg] https://packagecloud.io/ookla/speedtest-cli/debian/ $(lsb_release -cs) main" | sudo tee  /etc/apt/sources.list.d/speedtest.list

      You'll see that "$(LSB release -cs)" is used in the command. Input the title of the RPi Operating system release using this string of text in the prompt. We have to upgrade our packages list because we have a new module repository. Simply use the following command to update the list of installed packages.

      sudo apt update

      Our RPi is now equipped with the official Ookla Connection speed CLI. Installing the software on your device is as simple as running the command below.

      sudo apt install speed test

      We may now run a speed test on your Raspberry Pi to ensure that we have successfully installed the program. To begin the speed test, enter the following command into your terminal.

      Speedtest

      There are a few terms of service you must agree to while using the speed test app on your Raspberry Pi. Simply hit "YES" accompanied by the Return key to go past this warning.

      Writing our Speed Test Python Script

      On our RPi, we can now begin writing our Program code that will actively check the speed of our downloads and uploads. The command prompt will get us started on writing our Program code to check the connection speed on the RPi.

      cd ~

      nano speedtest.py

      Type the code below in this file. We'll walk you through each component of the program, so you can get a sense of how it all works.

      import os

      import re

      import subprocess

      import time

      This script will use all of the packages listed in these four lines. We'll discuss exactly each of the modules that will be put to use in the following paragraphs.

      Import os: 

      The script uses the operating system package to interface with the os. This package will be used to see if a file already exists as part of this program.

      Import re: 

      This repackage provides a library for managing pattern searching so that we may simply perform regular expressions. The Speed test command line provides us with all the information we need to find our desired values.

      Import subprocess: 

      To run another python code, this script needs the subprocess package. To use the subprocess module, we will be able to launch the Internet speed Command line software and receive the results.

      Import time: 

      We make use of the time package to keep track of the dates and times of all Speed test Command line calls. We will be able to keep track of the performance over time thanks to this package.

      response = subprocess.Popen('/usr/bin/speedtest --accept-license --accept-gdpr', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')

      Subprocess is used to initiate a request to the Internet speed command line and instruct it to route the output of the speed test to stdout in this section of code. stdout.read is used to output data (). Finally, we decode('UTF-8') our reply variables to make it usable as a Py object after the call to the Speed test Command line.

      ping = re.search('Latency:\s+(.*?)\s', response, re.MULTILINE)

      download = re.search('Download:\s+(.*?)\s', response, re.MULTILINE)

      upload = re.search('Upload:\s+(.*?)\s', response, re.MULTILINE)

      jitter = re.search('\((.*?)\s.+jitter\)\s', response, re.MULTILINE)

      Each of these 3 pieces of code accomplishes the same task. Every text fragment has a unique number adjacent to it, which they can deduce by running a mathematical equation on it using the re library. A ping lookup for "Latency: 47.943 ms" returns "Latency: 47.943 ms," with only the value between the characters.

      ping = ping.group(1)

      download = download.group(1)

      upload = upload.group(1)

      jitter = jitter.group(1)

      To retrieve the right numbers, we must utilize the ".group()" function. The CSV file will be able to contain the results of the Speed test Command line software output, thanks to this method.

      try:

          f = open('/home/pi/speedtest/speedtest.csv', 'a+')

          if os.stat('/home/pi/speed test/speedtest.csv').st_size == 0:

                  f.write('Date,Time,Ping (ms),Jitter (ms),Download (Mbps),Upload (Mbps)\r\n')

      except:

          pass

      This is a simple piece of code. The program is contained within a try statement, which ensures that the program will continue to run even if an error occurs. First, we retrieve our speedtest.csv document in the try block.

      If indeed the document does not already exist, "a+" in the parameters tells it that we wish to generate it and add any new content to what exists already. After that, we use the operating system package to determine the real size of our speedtest.csv documents. If indeed the file's contents are equal to zero, we can proceed. No action is required on our part if the document does not exist.

      f.write('{},{},{},{},{},{}\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M'), ping, jitter, download, upload))

      There are commas to differentiate each record's information. When formatting a string, we utilize the time strftime() method to include the time and current date. Our pings, downloads, and uploads will follow. Example output.

      This is what your program should look like when you are done writing it down.

      import os

      import re

      import subprocess

      import time

      response = subprocess.Popen('/usr/bin/speedtest --accept-license --accept-gdpr', shell=True, stdout=subprocess.PIPE).stdout.read().decode('UTF-8')

      ping = re.search('Latency:\s+(.*?)\s', response, re.MULTILINE)

      download = re.search('Download:\s+(.*?)\s', response, re.MULTILINE)

      upload = re.search('Upload:\s+(.*?)\s', response, re.MULTILINE)

      jitter = re.search('\((.*?)\s.+jitter\)\s', response, re.MULTILINE)

      ping = ping.group(1)

      download = download.group(1)

      upload = upload.group(1)

      jitter = jitter.group(1)

      try:

          f = open('/home/pi/speedtest/speedtest.csv', 'a+')

          if os.stat('/home/pi/speed test/speedtest.csv').st_size == 0:

                  f.write('Date,Time,Ping (ms),Jitter (ms),Download (Mbps),Upload (Mbps)\r\n')

      except:

          pass

      f.write('{},{},{},{},{},{}\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M'), ping, jitter, download, upload))

      you can save the script. Once our script is complete, we will create a directory in which to keep the speedtest.csv data. Make this directory by typing the command below.

      mkdir ~/speedtest

      After we have created the necessary directory, we can execute the program. The command below can be used to run our program and see if it works as expected.

      python3 ~/speedtest.py

      Open the newly generated speedtest.csv file to see the results of the script's execution.  Let's see whether we can open this document on the RPi with the command below.

      nano ~/speedtest/speedtest.csv

      You should be able to find anything similar to this in that file. A few rows of records and the column headings.

      It is possible to use this guide in two ways. Either utilize Influx Database and Grafana or use google Drive to synchronize the data to Google Account using Influx Database and Grafana.

      How to View Your Speedtest Results in Grafana?

      We'll teach you to easily plot your performance test data using Grafana throughout this section. To conduct data analytics, load up metrics that make some sense of the immense amount of data, and track our applications with the aid of cool configurable panels, we use Grafana, a free software solution that is free and open source.  In addition to the fact that Grafana is an open-source platform, we may create our plugins to integrate with a variety of data sources.

      Technically known as time series analytics, the technology aids in the study, analysis, and monitoring of data across time. By giving relative data, it aids us in tracking user activity, app behavior patterns, error rate, error kind, and contextual circumstances in operation or a pre-production scenario.

      Organizations that are concerned about security or other factors do not have to use the vendor cloud because the project can be implemented on-premise. Over the years, this framework has become an industry standard and is used by companies like PayPal, eBay, Intel, and many more. In a moment, I'll go over some real-world examples from the industry.

      Grafana Platform & Enterprise are 2 extra services provided by the Grafana developers for companies in addition to the free software core product. What do they do? The remainder of this post will go into greater detail regarding this. In the meantime, how about we take a closer look at the tool's capabilities and architecture flow, starting with an explanation of what a panel is? & How does it all work? '

      Grafana Panel: What Is It?

      They use sources of data like Graphite and Prometheus as well as Influx database and Elastic Search to populate the panels. Grafana has built-in compatibility for a wide range of data sources, including these.

      All of the common charts and graphs that a business needs to analyze data can be found in the panel's visualization options, including heat maps, scatterplots, and more. On the grid of a dashboard, there are several discrete panels. There are a variety of features available on each panel.

      What are Grafana's Capabilities?

      Let's have a look at the fully accessible panel framework's capabilities. Our application's metrics are handled via an open platform. This data can be analyzed through the use of metrics in a variety of ways.

      The panel is well-equipped to generate a sense of complicated data, and it is constantly changing. Geo-mapping, heat maps, scatterplots, and more can be displayed with graphs in a variety of ways. Our business needs can be met by a wide range of data presentation possibilities provided by the software.

      As soon as a predetermined event occurs, an alert is set up and triggered. Slack or any other communication tool used by the monitoring team might be alerted to these events. Grafana is pre-installed with support for about a dozen different types of databases. And there is a slew of more, all made possible thanks to plugins.

      It can be hosted on-premises or in the cloud. Custom data can be retrieved using built-in Graphite support and expressions such as "add," "filter," "average," "minimum," and "maximum" functions. Graphite is a chemical element. Later, I'll address that. Influx database, Prometheus, Elastic Search, and Cloud Monitoring are also included. Up front, I'll cover it all.

      What is the purpose of Grafana Cloud?

      A cloud-native, highly accessible, quick, and completely open SaaS metric framework, Grafana Cloud As a result, individuals who don't want to host the solution on their own and prefer to avoid the headache of managing their deployment infrastructure may find this useful. It's a Kubernetes-based service. Prometheus and Graphite back end is supported. This gives us two options: either use Grafana on-premises or both.

      Setting up the Influx Database for our Connection Speed Monitoring

      Installing Influx Database on your RPi is a prerequisite for this stage of the internet speed monitoring guide. Our connection speed monitoring system sends data to this location, thus we'll be storing it here.

      Influx database

      Designed by Influx Intelligence, Influx Database is a free and open-source time series system built in Go. Time series data, such as that collected from sensors and IoT devices, may be accessed quickly and reliably with this system because of its focus on high-availability extraction and retention. As a Time Series Database, Influx Database is capable of storing up to several hundred thousand points each second. A SQL-like query language for time series data, the Influx Database was designed expressly for this purpose.

      Why use Influx DB?

      • Shorter duration

      • Extensive research and analysis

      • Retention, ingestion, querying, and visualization are now all available through a single application programming interface in Influx Database.

      • Templates that are simple to create and distribute, thanks to the influx of DB templates

      First, we'll fire up the Influx Database CLI tool by typing the command below. Using this application, we will be creating an online repository for our data.

      There is no need to enter the passcode and username for Influx Database if you haven't set login. Establish a database with the name "internet speed" in it immediately. After typing CREATE DATABASE, the DB name, and pressing enter, the DB is ready to use.

      CREATE DATABASE internet speed

      Creating a user named "speed monitor" will be the next phase in working with the database. The passcode "pass" should be replaced by a more secure one. Privileges are not a concern at this time, as we shall take care of them in the following stage.

      CREATE USER "speed monitor" WITH PASSWORD 'pass' Now assign the new "speed monitor" user all privileges to our "internet speed" database.

      GRANT ALL ON "internet speed" to "speed monitor"

      To shut off the application, type the command below.

      Quit

      Installing the Python package required to communicate with the Influx DB is the final step.

      sudo apt install python3-influxdb

      Saving our Speed Monitor Data to our Influx DB

      Create a new Script file to start populating our Influx database now that it has been set up. If you've already read through the previous script, you won't have to go over anything new here.

      rm ~/speedtest.py

      nano ~/speedtest.py

      To get started, we have to include all of the Python packages that we will be using in this file.

      import re

      import subprocess

      from influxdb import InfluxDBClient

      operating system and time have been eliminated, as seen. We no longer have to communicate with records, and the Influx database automatically timestamps data, therefore these two libraries are no longer required. After importing the "InfluxDBClient" for our Influx database server, we are ready to use it. The next phase is to launch the Speedtest Command line interface and process the results. Upon completion of this code snippet, we'll have all the information we need.

      response = subprocess.Popen('/usr/bin/speedtest --accept-license --accept-gdpr',

                                  shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')

      ping = re.search('Latency:\s+(.*?)\s', response, re.MULTILINE)

      download = re.search('Download:\s+(.*?)\s', response, re.MULTILINE)

      upload = re.search('Upload:\s+(.*?)\s', response, re.MULTILINE)

      jitter = re.search('\((.*?)\s.+jitter\)\s', response, re.MULTILINE)

      ping = ping.group(1)

      download = download.group(1)

      upload = upload.group(1)

      jitter = jitter.group(1)

      Now everything gets a little more complicated. This data must be converted to a Py dictionary for us to use it. Because the library wants the information to be presented in a JSON-like form, this is an explanation.

      speed_data = [

          {

              "measurement" : "internet_speed",

              "tags" : {

                  "host": "Raspberrytheengineeringprojects"

              },

              "fields" : {

                  "download": float(download),

                  "upload": float(upload),

                  "ping": float(ping),

                  "jitter": float(jitter)

              }

          }

      ]

      In this section, we established our dictionaries by the Influx database data model. "internet speed" is the title we assigned the metric. The tag "host" was also added so that if we were to manage numerous devices within the same DB, we could segregate them. After that, we enter the data we obtained in the preceding line of code, including the download speed, upload speed, and pings.

      To make them into numbers, we use the float () method to turn our download, uploads, and pings parameters into strings. Grafana will read these as characters if we don't utilize the float () method. Now that we have all the information we need, we can begin using Influx Database. It is necessary to create an InfluxDBClient object and provide the network information.

      Only the hostname, port number, user id, passcode, and DB name are passed to this method. You can refer to the official Python manual for Influx Database if you wish to know what information can be set.

      client = InfluxDBClient('localhost', 8086, 'speedmonitor', 'pass', 'internetspeed')

      "localhost" should be replaced with the Internet address of your Influx database server if it is hosted elsewhere. Change "pass" to the passcode you created earlier in this article. To send data to our Influx database server, we need to add a block of code like the one below to our existing codebase.

      client.write_points(speed_data)

      To send data to Influx Database, we only need to do that. Assuming you've entered every bit of code in the document, this should look something like this.

      import re

      import subprocess

      from influxdb import InfluxDBClient

      response = subprocess.Popen('/usr/bin/speedtest --accept-license --accept-gdpr',

                                  shell=True, stdout=subprocess.PIPE).stdout.read().decode('UTF-8')

      ping = re.search('Latency:\s+(.*?)\s', response, re.MULTILINE)

      download = re.search('Download:\s+(.*?)\s', response, re.MULTILINE)

      upload = re.search('Upload:\s+(.*?)\s', response, re.MULTILINE)

      jitter = re.search('\((.*?)\s.+jitter\)\s', response, re.MULTILINE)

      ping = ping.group(1)

      download = download.group(1)

      upload = upload.group(1)

      jitter = jitter.group(1)

      speed_data = [

          {

              "measurement" : "internet_speed",

              "tags" : {

                  "host": "Raspberrytheengineeringprojects"

              },

              "fields" : {

                  "download": float(download),

                  "upload": float(upload),

                  "ping": float(ping),

                  "jitter": float(jitter)

              }

          }

      ]

      client = InfluxDBClient('localhost', 8086, 'speed monitor', pass, 'internet speed')

      client.write_points(speed_data)

      Save the document to your computer.

      Using Grafana to see our Connection Speed Stats

      The database needs to be displayed in Grafana. All the information will be graphed and shown by using the Grafana application.

      Grafana installation on the Raspberry Pi.

      It's a fully accessible metric monitoring and data presentation package for people who aren't familiar with it. The purpose of this software is to aid in the visual representation of time-based information. To speed things up, Grafana entrusts most of the heavy lifting to the client, such as generating graphs. Since there are minimal data to analyze, the software can concentrate on giving information that can be used to create graphs.

      Many various types of data sources are supported by Grafana, and you can set up warning rules for key metrics to be warned immediately if anything isn't quite right. The famous Influx Database is one of the available Grafana data capture options. Quick and "time series" oriented, Influx Database is a famous system for use with Grafana. Each register contains a timestamp.

      Grafana is frequently used to keep tabs on system metrics like the temperatures of the equipment and how much of it is being used. In addition, it can be used to graph data, for example, the weather, across time. Grafana is an excellent tool for instantly presenting data from your Raspberry Pi.

      Setting up a Raspberry Pi with Grafana

      It's a good idea to double-check that all of the packages on your RPi are updated before beginning the Grafana installation. The 2 techniques listed below can be used to do this. The packages list will be updated, and all installed applications will be upgraded to the most recent versions using these instructions.

      sudo apt update

      sudo apt upgrade

      The Grafana source repo must be added to the RPi before Grafana can be installed. As a prerequisite, we must add an APT password. Using the APT password, you can confirm that the modules you're installing originated from the Grafana packages service and are properly signed. The instruction to include the Grafana APT password to your RPi's keychain is as follows.

      curl https://packages.grafana.com/gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/grafana-archive-keyrings.gpg >/dev/null

      Once we've uploaded the password to our Raspberry, we're good to go with the Grafana repo as a resource for our software. Include this repo to the source list by running the command below on your RPi.

      echo "deb [signed-by=/usr/share/keyrings/grafana-archive-keyrings.gpg] https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list

      The RPi will automatically check the Grafana repo for new packages whenever you launch and upgrade them. An update is necessary because we've added new packages to our list. When using apt to perform an update, the most up-to-date package list is obtained from all available sources. To accomplish this, run the command below in the console of your Raspberry.

      sudo apt update

      Please keep in mind that Grafana can be installed on your RPI. Run the command below to install the newest release of Grafana on your computer.

      sudo apt install grafana

      Getting Grafana to start automatically at startup is the next step we need to take. Grafana includes a systemd service file, which is a godsend for those of us using it on Linux systems. All we have to do is execute the command below to make Grafana start automatically at system startup.

      sudo systemctl enable grafana-server

      The "grafana-server.service" services record will be enabled by this instruction to a network's service management. The Grafana server's service management will utilize this file as a reference guide. In the console of the Raspberry Pi, enter the following command to begin using Grafana's webserver.

      sudo systemctl start grafana-server

      Setting up the Grafana dashboard on your Pi 4 

      Now that we've installed Grafana on your Pi 4, we can use its web interface to monitor your data. If you have a Raspberry Pi, the first thing we'll need to do is get its Internet address. Grafana on your local area network can be accessed remotely via this Internet protocol. The IP address of your Raspberry Pi may be found by typing the following code.

      hostname –I

      Static IPs are a good idea if you frequently need to connect to your Raspberry Pi. Make sure you have your Internet Protocol (IP) address available before visiting this URL. A web application for the Grafana dashboard can be found on line 3000 of the Rasp Internet address. "IPADDRESS>" should be replaced with your Internet address from earlier.

      When you initially open Grafana, you'll get a login page. When you initially installed Grafana on the RPi, you were given the option of logging in with the default administrator account. The username and passcode are "admin" and "admin," respectively, for this account (1.). However, even though the passphrase is incredibly insecure, we'll be able to alter it right after this one. Grafana's "Login" tab can be clicked once the userid and passcode have been entered.

      Grafana's online UI will prompt you to update the user's passcode upon the first login. You can skip this step if you like, but we don't encourage it. You must change the passcode as soon as possible due to its extreme insecurity. After entering a new pass, select the "Save" option to continue to the Grafana homepage.

      The below screen will appear after you have signed in and updated the passcode. Now that you've reached this screen, you're ready to begin configuring the Grafana interface on your RPI.

      Now that Grafana is installed on the RPi, you can access the online interface. To continue with this article, access the Grafana graphical interface once it has been installed. If you're not sure where it is, type http://localhost:3000 into your browser's address bar.

      IPADDRESS:3000

      A new information source must be added to Grafana's web app. ' The "Data Sources" menu selection can be accessed by clicking on the wheel on the left (1.). 

      To continue, click "Add source of data" in the following menu.

      Navigate to the "Add data" webpage then click the "Select" tab when you discover Influx Database.

      We must now fill in the data regarding the Influx Database configuration. Enter the Influx Database Hyperlink first (1.). In our scenario, the RPi which we are using to execute the program is where this is located. Use the following Link if you're following suit.

      http://localhost:8086

      The credentials for our DB must then be entered (2.). The Db must be set to "internetspeed" if you closely followed our instructions. Last but not least, the passcode must be the one we mentioned; if you utilize our examples, it is "theengineeringprojects". The Username should be "speedmonitor,". After you've entered all the necessary data, select the "Save & Test" tab (3.)

      The following step is to build a panel to show this information. Select the "Dashboard" option by hovering your cursor over the add (+) button in the navigation bar.

      Select "Add Query" from the menu that appears over "New Panel."

      Graph reading from our DB can now be configured. The metric name we want to use is "internetspeed," so click "choose measurement" (1.) after that (2.)

      We must now establish the data collected that we want to examine after we have the metric set. The "value" wording adjacent to the field can be clicked (1.). Click "downloads," "uploads," or "ping" from the drop-down menu. We'll begin with "download" in this tutorial (2.).

      After that, we'd like Grafana to consider each outcome as a separate entity. In the picking row, click on the add (+) tab to accomplish this (1.). In the pop-up, select "distinct" from "Aggregations" (2.).

      If you want to comprehend this data better, it's time to give it a new name. Assign an alias to each field that accurately reflects what it contains (1.). "Download Pace," for example, is the name of the speed at which data is downloaded. It's time to repeat these procedures 7–9 till all three variables (downloads, uploads, and pings) have been included in the graph.

      Add every field by clicking the wrench and wheel symbols on the navigation bar, and then click "Save".

      Using the "Title" box (1.) allows you to enter the name of the graph's title. As a title, "Network Speed " was utilized in this case. The panel can be saved by tapping on the save option (2.)

      Give your panel a name (1.). You have a complete creative license with this one. Our network bandwidth monitor was given the name "Network Speed Monitor" by us. Afterward, you may save this panel by selecting "Save" (2.).

      Your statistics should now be visible on a visually appealing graph, assuming all went according to plan.

      Grafana used to automate your Speed Monitor scripts

      Making your program run on a regular schedule is as simple as automation.  The crontab is the simplest approach to schedule your script to execute regularly. On your RPi, you can change the crontab by typing the command below.

      crontab –e

      When asked which editor should use, we suggest nano because it's the simplest to learn and the most intuitive. The following cronjob should be added at the bottom of this file. Cronjobs are scheduled to run each half an hour by default. We advise using our Crontab generator if you'd like to experiment with alternative timings.

      Exactly what does it mean to have a "Cron Job?"

      Jobs are scheduled using Cron, which is built into Unix-like systems like Linux and its numerous variants. It is a time-based mechanism. Using the cron is a common approach to run instructions or bash scripts regularly. "Cron Jobs" refers to tasks that are scheduled using the "cron" utility. While using Unix-based systems like Raspbian, you'll quickly become dependent on cron jobs.

      Using Google Drive to store your Internet Speed Readings

      gDrive Installation and Compilation

      It's easy to use gDrive, a cli program, to transmit to Google Account. Once you've got it established on the smartphone, it's a breeze to use. This instruction will explain to you how to use your personal Google accounts to develop the gDrive program on the RPi. The same procedures can be used to create gDrive for any os, even if this instruction concentrates on the RPi. 

      Getting the Go Compiler installed

      The Go engine must be installed on our device before we can assemble the gDrive program. Download the appropriate drivers from the official website whether you're working on a PC or Mac.

      If you're working with a Linux distribution like Raspbian, the process becomes a little more complicated. Using a Linux terminal, type one of these commands. 

      The Raspberry Pi can be used with this.

      wget https://dl.google.com/go/go1.13.7.linux-armv6l.tar.gz -O go.tar.gz

      a 64-bit version of Linux

      wget https://dl.google.com/go/go1.13.7.linux-amd64.tar.gz -O go.tar.gz

      After downloading the Go libraries, we must now unpack them to the root directory. 

      sudo tar -C /usr/local -xzf go.tar.gz

      Next, we'll see whether we can get the console to talk to Go. If we alter the shell aliases script, we can accomplish this goal. Shell will run automatically the script and pull in our updated path names.

      nano ~/.bashrc

      The following lines should be added to the end of this file. With these lines, we may execute the compiler instantly from the cli, without having to specify the directory to the engine.

      export GOPATH=$HOME/go

      export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin

      Now you may save the script.

      source ~/.bashrc

      Retrieving your Google Drive API Credentials

      We require your Google Cloud Apis details before we can start with the gDrive program compilation. " Your project's name can be found on this webpage (1.). "gDrive-theengineeringprojects" shall be the name of our example.

      For our project, we'll need to activate the Google Cloud Application programming interface first. In the Cloud Services panel, we can access the Google Cloud Application programming interface page. Select ENABLE from the drop-down menu.

      When you get to the next screen, select the "Credentials" tab from the navigation bar (1.). "Config Permission SCREEN" needs to be clicked next to finish the process (2.).

      You'll be prompted to select a "User Type" at this point. "External" is the only option available if you are not running a business (1.). Once you've made your choice, click "Start" to begin building your app (2.).

      You'll need to give your app a name on this screen (1.). In this case, we'll use the name "gDrive theengineeringprojects" for the program.

      To save the document, you simply need to type in a title for your program.

      Return to the "Credentials" page, and then we'll be done.

      The navigation bar makes it simple to return to the previous page.

      Add credentials by clicking on this tab: "Create Credentials" (1.). When the pop-up displays, select the "OAuth user ID" option (2.).

      Selecting an app type is what we need to do next. We chose "Other" since none of the other options were appropriate for the API's intended use. Once we've done that, we'll need to give this program a name. We'll just call it "gDrive theengineeringprojects" for the sake of simplicity. Once all of the information has been input, click the "Create" tab to begin the process.

      You should now have both the client id and client secret.

      In this article, you'll need each of these numbers at some point.

      Setting up your API Keys for gDrive

      We'll need to use git to download gDrive's source code before we can compile it. Before we can proceed, we need to install the git client on our computer. To install Git on a Debian-based operating system like Linux or Raspbian, you may either go to the main Git webpage or use the procedures below.

      sudo apt update

      Just type the command below and we'll be done in no time.

      sudo apt install git

      now clone git

      git clone https://github.com/gdrive-org/gdrive

      The next step is to update the program to reflect the new client password and session id. Make a copy of the "handlers meta.go" file in the cloned subdirectory and edit it with nano.

      cd gdrive

      nano handlers_drive.go

      Change the collected details in the following statement of this file. Both your user id and password should be in your possession.

      const ClientId = "367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com"

      Substitute your login Credential here.

      const ClientId = "YOURCLIENTID"

      const ClientSecret = "1qsNodXNaWq1mQuBjUjmvhoO"

      You can use your user password instead

      const ClientSecret = "YOURCLIENTSECRET"

      Save all the changes. Now it's time to execute the following code to get the additional modules needed to compile our updated version of gDrive using the Go engine.

      go get github.com/prasmussen/gdrive

      To get gDrive working on our device, simply enter the command shown below into your terminal.

      go build -ldflags '-w -s'

      It's time to get this thing working on the command line, so let's get started! We need to relocate the file to the root directory to use the gdrive inside the cli. To relocate the executables, type the command below.

      sudo mv gdrive /usr/local/bin/gdrive

      The final step is to provide the gdrive file with the ability to run.

      sudo chmod a+x /usr/local/bin/gdrive

      It's time to play around with gDrive

      Now that your Google account is linked to the app, we can test the program gDrive. The gdrive instruction and the "list" parameter are required to get things started.

      gdrive list

      Following gDrive's list statement, you will be informed that authorization is necessary. There needs to be a Hyperlink at the bottom of the message. Using your Google acc, users must visit this Address and sign in. You'll get a security code if you perform the next few steps on the internet browser. Enter the verification code that you just copied into the terminal.

      GDrive has been successfully installed onto your device if a listing of files is displayed. To see the ids for each of your directories, you can use this command. Using the IDs listed above, you can sync a specific folder.  The command below can be used to test syncing a folder. You can replace Folder> with the path to your synchronized folders.

      The identification of a directory that you obtained with the grdive listing commands must be substituted for GOOGLEFOLDERID>.

      Uploading Speed Test Data to Google Drive

      Now that gDrive is installed on the RPi, we're ready to collect some speed test results. Using gDrive, establish a new directory on the Google drive account for our speedtest.csv record. This will be our starting point. This next terminal command will allow us to accomplish this.

      gdrive mkdir speedtest

      A notification stating that the subdirectory has been established will be displayed as a result of running this command. This mail will also provide you with your identification number. Write this Identification down someplace safe; we'll need it in a few stages. We may now utilize the subdirectories Identification to add a file to it, as the directory has been created. The speedtest.csv record will be used in this experiment. Be careful to substitute YOUR FOLDER ID with the identification you received in the previous phase before running the command below.

      gdrive sync upload ~/speedtest YOUR_FOLDER_ID

      The command prompt should display something like the one below during the first sync. Messages such as this one inform you that document has been successfully transferred to your Onedrive.

      In the meantime, we'll be able to automate the entire process such that we submit the speedtest.csv whenever we make changes to it.

      Using Google Drive to automate the Rpi's connection speed monitoring

      Automating your Raspberry Connection Speed Monitoring is the following main task related to it. We'll be building a shell script to automate the process. Crontab will use this script to run it regularly. Use the following Unix commands on the RPi to get started developing the shell script.

      nano ~/speedtest.sh

      The following lines are what we'd like to include in this document. Your Google storage subdirectories unique ID must be replaced by YOUR FOLDER Identification.

      #!/bin/bash

      python3 /home/pi/speedtest.py

      /usr/local/bin/gdrive sync upload  /home/pi/speedtest YOUR_FOLDER_ID

      Save the script. Our shell script needs to be granted permission to run before we can set up a crontab in which to run it. By entering the command below into the prompt, we can accomplish our goal!

      sudo chmod +x /home/pi/speedtest.sh

      We're now ready to set up the crontab now that everything is finished. Start by executing the command below on the RPi to begin modifying the crontab. When prompted, choose Nano as your editor of choice.

      crontab –e

      At the end of the document, paste the following code. This command tells crontab to execute our shell scripts once every hour, which it will do. Our Crontab generator can help you come up with new values for the crontab if you'd like.

      0 * * * * /home/pi/speedtest.sh

      Conclusion

      We learned how to set up a pi 4 internet connection test monitoring in this article. We also learned how to set up the internet monitoring system's influx database and grafana application. Now you can experiment with other servers to see if you can enhance the speed test's precision and performance. We're going to use our Raspberry Pi 4 to develop a Wi-Fi gateway in the next tutorial.

      Security System with Image Capturing in Raspberry Pi 4

      Thank you for joining us for yet another session of this series on Raspberry Pi programming. In the preceding tutorial, we constructed a personal Twitter bot using Tweepy, a Py framework for querying the Twitter application programming interface. We also constructed a Response to robot mentions that would post a response to everybody's tweet mentioning it with a certain keyword. However, in this tutorial, we will implement a security system using a motion sensor with an alarm.

      This is what it looks like:

      PIR Motion Sensors can be implemented with RPi by understanding how it is connected to a Raspberry Pi. Whenever the motion sensor detects human movement, an alarm is triggered in this project and the LEDs blink. You may create a simple motion-detection alarm using this interface.

      Overview

      Infrared Motion Detectors or PIR Sensors are Motion Sensors that use Infrared Radiation to detect movement.

      Infrared rays are emitted by anything with a temperature higher than absolute zero, be it life or non-living. Humans are unable to see infrared radiation because its wavelength is longer than the wavelength of visible light.

      That's why PIR Sensors are designed to pick up on those infrared rays. Due to their wide range of uses, such as motion sensors for security systems and intruder alert devices

      "Passive" in motion sensor refers to the fact that it doesn't produce any radiant rays of its own, but rather detects it when other things emit infrared radiation. This is in contrast to active detectors, which perform both the generation of infrared waves and the detection of these waves simultaneously.

      An Overview of Motion Detectors

      For this project, we used a motion detector that included an infrared sensor, a BISS0001 integrated circuit, and other parts.

      The 3 pins on the motion sensor are used for power, data, and ground. There are two potentiometers on the Motion Sensor that may be used to modify both the sensor's sensitivity and the period it remains high on sensing a body movement.

      A key role in directing infrared rays onto the sensor is played by the Fresnel lens overlaying the Pyroelectric Sensor. This lens allows the PIR Sensor to detect things at an angle of 1200 degrees. The sensor has an 8-meter detection range, meaning it can pick up on human movement within that distance.

      PIR Sensor Adjustments

      Two potentiometers are provided for fine-tuning the sensor and output timing, as previously described.

      With the aid of a potentiometer, you may modify the sensor's sensitivity. The distance can be changed between 3m and eight meters. To increase the detecting distance, spin the Potentiometer in a clockwise motion and to reduce, rotate it in the opposite direction.

      The second potentiometer allows you to choose how long the motion sensor's output remains HIGH. Anywhere from 0.3s to 600s can be used. Turn the POT clockwise to raise the time and the opposite turn to decrease it.

      PIR Motion Sensor with Raspberry Pi 4

      A Motion Sensor based on RPi and Python language has been the goal of this project since the beginning, as stated in the intro.

      I have an Infrared Motion Sensor Component in numerous different projects like Automated Lighting using Raspberry and Various Sensors, Automated Door Opening with Arduino and a motion sensor, and GSM Home Automation Security with Pi.

      The key advantage of the Infrared Motion Sensor utilizing RPi over the above-described projects is that RPi can be readily connected to the Web and allows Internet of things implementation of the project.

      Circuit Diagram

      The following figure illustrates the interfaces concerning the Infrared Motion Detector using RPi.

      Components Required

      • Raspberry Pi 4

      • PIR Sensor

      • Speaker

      • Jumper Wires

      • Breadboard

      • Power Supply

      • Computer

      Circuit Design

      Link the Motion Sensor's Vin and GND connectors to the RPi's 5 volts and GND pins. Use pin11 to attach the Infrared Sensor's DATA Input.

      Gnd and pin 3 are where you'll want to connect the led. As soon as the sensor is triggered, these LEDs will come on and go off.

      Code

      Python is used for the programming portion of the project. The Python program for RPi's infrared Motion Sensor is provided below. Insert the program into a new file called motion.py.

      import RPi.GPIO as GPIO

      import time

      GPIO.setwarnings(False)

      GPIO.setmode(GPIO.BOARD)

      GPIO.setup(11, GPIO.IN) #Read output from PIR motion sensor

      GPIO.setup(3, GPIO.OUT) #LED output pin

      while True:

      i=GPIO.input(11)

      if i==0: #When output from motion sensor is LOW

      print("No intruders",i)

      GPIO.output(3, 0) #Turn OFF LED

      time.sleep(0.1)

      elif i==1: #When output from motion sensor is HIGH

      print("Intruder detected",i)

      GPIO.output(3, 1) #Turn ON LED

      time.sleep(0.1)

      How it works

      The operation of the Infrared Motion Sensor with Raspberry Pi is pretty straightforward. If the Infrared sensor senses some body motion, it sets the Data Input to HIGH.

      RPI on identifying a 1 on the associated input gpio, will trigger the alarm.

      If the PIR sensor is not working:

      When you purchase a new sensor, it doesn't work. The Trim port is in the default setting, so it's not a sensor issue. Sensitivity of the sensor and trigger duration port if you modify these settings. It's going to start working as planned. Make sure the trigger duration port's knob is on the left as a low trigger duration and the sensitivity port is in the middle.

      Applications

      Infrared Motion Sensor with Raspberry Pi has already been discussed. They include:

      • Automated house lights

      • Motion sensing

      • Intruders notice

      • Automated door open

      • Home security systems

      When motion is detected by the PIR sensor on the raspberry pi, we will look into how to record video and transmit it to Whatsapp as an alarm. So that we can tell who's in your room right away thanks to the photo.

      Capture an image with the pi camera

      Enable the camera by going to the Preferences menu and selecting the Raspberry Pi configuration option.

      Activating the camera and saving the image will allow us to identify who or what triggered the alarm.

      Python code

      import picamera

      from time import sleep

      camera = picamera.PiCamera()

      camera.capture('image.jpg')

      When we run our software, the preceding code will take a picture and put it inside the root directory of the script. This image will be used to identify the intruder that has been detected.

      Sound alarm

      When an alarm system is triggered, there is an alert that must sound. We'll use a loudspeaker instead of a buzzer for our alarm system in this scenario. When the motion sensor is activated, we will play an alarm sound.

      Code:

      import pygame

      pygame.mixer.init()

      pygame.mixer.music.load("alarm.mp3")

      pygame.mixer.music.play()

      while pygame.mixer.music.get_busy() == True:

      continue

      As a bridge python software for video game design, Pygame is an excellent choice. Additionally, it provides sights, sounds, and visualizations that can improve the game that is being created.

      Graphics for video games can be generated using a variety of libraries that deal with visuals and sounds. It streamlines the entire game workflow and makes it easier for newcomers who wish to create games.

      Copy the code above and save it to a file named alarm.py then run it in the terminal.

      python alarm.py

      Send an image to Whatsapp using Twilio

      Any internet or mobile app's compatibility with several platforms was a major hurdle to overcome when designing it. It used to be possible to build a link between two pieces of software using Bandwidth or Podium or Telnyx or Zipwhip or similar before Twilio was invented. In recent years, though, Twilio has dominated the competition. Twilio has become the preferred communication API for programmers. Twilio will become clearer to you if you stick around for a time.

      What is Twilio

      Developers can use Twilio's API to communicate with each other in a modern way.

      When it comes to creating the ideal client experience, developers have a wealth of tools at their disposal in the form of Twilio's APIs, which the company describes as "a set of building blocks."

      It is possible to utilize Twilio to communicate with customers via text message (SMS), WhatsApp, voice, video, and email. Your software only needs to be integrated with the API.

      What does Twilio do?

      Twilio is a provider of end-to-end solutions for integrating voice and data communication. Twilio is already used by over a billion developers and some of the world's most well-known businesses. The Twilio Communication application programming interface enables web and mobile app developers to integrate voice, message, and video conferencing capabilities. This makes it easier for app developers to communicate with one another.

      The API provided by Twilio makes it simple and accessible to communicate across the web. Mobile and web applications can use this service to make phone calls as well as send text messages and multimedia messages (MMS).

      How Does Twilio Work?

      You might want to learn more about Twilio and how it works. As a result, Twilio allows enterprises to better understand their customers than any other service. Twilio's primary concept is to acquire clients, get to know them, provide for their needs, and keep them coming back.

      Twilio has a worldwide operations center that keeps an eye on carrier networks around the clock to ensure that they are operating at peak efficiency. To keep up with the ever-changing traffic patterns, Twilio's skilled communications engineers are on the job all the time.

      They employ real-time feedback from several provider services to make smarter routing decisions based on real-time data on the availability of handsets. The key distinction between Twilio and other application programming interface integration networks is that Twilio's data-centric strategy provides customer engagement service.

      Key Areas Of Twilio

      Contact Center

      Managing a contact center in today's business environment is critical to the success of the company. Businesses can use Twilio to manage their interactions with clients and consumers through a central contact center platform.

      Messaging

      Before Twilight, sending mass SMS was a difficult task. Now, the Twilio Message application programming interface is widely used to transmit and receive messages, MMS, and OTT communications worldwide. Users can verify whether or not messages have been delivered using the intelligence tracking services.

      Videos

      For healthcare, virtual classrooms, recruiting, and other uses, Twilio's WebRTC and cloud infrastructure components make it easy for developers to create secure, video, and HD audio applications.

      Marketing Campaigns

      Twilio's ability to run and manage marketing campaigns is another noteworthy but still-evolving feature. Users can examine performance numbers, run campaigns, and view design concepts.

      Voice

      As a result of this trend, Twilio has also seen an increase in voice traffic. Any app, website, or service can use Twilio to make phone calls over the PSTN or SIP. It's easy to use Twilio Programmable Voice to make and manage digital calls for any campaign.

      Email

      The Twilio SendGrip application programming interface eliminates the issue of emails that never make it to their intended recipient's inbox. Customers and clients will receive your emails with Twilio, so you won't have any worries about them not getting them.

      User Verification

      You'll never have to worry about online scams or fraud again using Twilio's verify feature. It is continuously validated by SMS, Voice, email, and push alerts continuously.

      Connectivity

      Advancing solutions and services provided by Twilio allow for global connectivity. As a result of this connectedness, your company can grow with ease.

      Obtain Twilio credentials

      The Twilio WhatsApp sandbox

      Developing and testing your app is made simple using Twilio's WhatsApp Sandbox. Your Twilio mobile number must be approved by WhatsApp before you can seek production access.

      You'll learn how to connect your phone to the environment in this section. Select Messaging in the Twilio Console and then Take a look at the WhatsApp section by clicking on it. On the webpage, you'll find the information you need to join our sandbox.

      The word "join" will be the first character in the code, followed by a two-word phrase chosen at random.

      As soon as Twilio receives your message, you should be able to send and receive text messages on your cell phone without any issues.

      Please repeat the sandbox application process for each additional mobile device that you wish to use to test the application

      Configuration

      Set up a new Python project in the following section.

      mkdir python-whatsapp-pic

      cd python-whatsapp-pic

      We'll need a virtual space for this project because we'll be installing several Python packages.

      Open a terminal on your RPI machine and type:

      python -m venv venv

      source venv/bin/activate

      (venv) $ pip3 install twilio

      When using a PC running Windows, execute these commands from a command line.

      python -m venv venv

      source venv\bin\activate

      (venv) $ pip3 install twilio

      Python's Twilio library will be used to deliver messages via Twilio.

      Authenticate against Twilio services

      To authenticate with the Twilio service, we must safely store a few critical credentials. To use Twilio we need to register for an account at the official Twilio website. Create a new account with your email and password. They will send a confirmation message to your email inbox for you to confirm the registration. Go ahead and confirm it. You will also have to verify your WhatsApp phone number to proceed.

      Setting environment variables can be done by entering the code below into your terminal:

      ssh auth token

      export TWILIO_ACCOUNT_SID="your account sid"

      export TWILIO_AUTH_TOKEN= "your auth token"

      after we have exported the credentials in our environment, the next step is to activate the WhatsApp sandbox to receive messages. Go to the develop mode, then select messaging and send a Whatsapp message.

      You will see a message directing you to deliver a text to your phone and if Whatsapp is connected to the computer, it will be easier to click on the link that will be provided below to send the message. Send the message that will be displayed on the chat box on your Whatsapp application.

      If it works you will see a message shown below:

      This number that will be displayed here is the “from” number that we will use in our code and the “to” number is your Whatsapp number.

      How to send a photo message, using the Twilio service

      Copy the following code into your python file.

      import os

      from twilio.rest import Client

      account_sid = os.environ['TWILIO_ACCOUNT_SID']

      auth_token = os.environ['TWILIO_AUTH_TOKEN']

      client = Client(account_sid, auth_token)

      from_whatsapp_number = 'whatsapp:+14155238886'

      to_whatsapp_number = 'whatsapp:+254706911425'

      message = client.messages.create(body='The engineering project sent your this image!',

      media_url='https://www.theengineeringprojects.com/wp-content/uploads/2022/04/TEP-Logo.png',

      from_=from_whatsapp_number,

      to=to_whatsapp_number)


      print(message.sid)

      With this now all we have to do is run our app.py program on the terminal.

      python app.py

      Putting it all together:

      So far we have written our motion sensor code, pi camera code, and sound system code, but how can we integrate all these different scripts into one project? Well, let us see how we can do it. As a quick recap, we wanted to detect motion, get the intruder's image and save then send the image to the admin's Whatsapp to alert the presence of an intruder in this project. To do this we have to create another file named main.py and write all the code for those functions in the main file. Copy the code below and paste it into the main file:

      import pygame

      import RPi.GPIO as GPIO

      import time

      import picamera

      camera = picamera.PiCamera()


      GPIO.setwarnings(False)

      GPIO.setmode(GPIO.BOARD)

      GPIO.setup(11, GPIO.IN) #Read output from PIR motion sensor

      GPIO.setup(3, GPIO.OUT) #LED output pin

      pygame.mixer.init()

      pygame.mixer.music.load("alarm.mp3")

      import os

      from twilio.rest import Client

      account_sid = os.environ['TWILIO_ACCOUNT_SID']

      auth_token = os.environ['TWILIO_AUTH_TOKEN']

      client = Client(account_sid, auth_token)

      from_whatsapp_number = 'whatsapp:+14155238886'

      to_whatsapp_number = 'whatsapp:+254706911425'

      while True:

      i=GPIO.input(11)

      if i==0: #When output from motion sensor is LOW

      print("No intruders",i)

      GPIO.output(3, 0) #Turn OFF LED

      pygame.mixer.music.stop()

      time.sleep(0.2)

      elif i==1: #When output from motion sensor is HIGH

      print("Intruder detected",i)

      GPIO.output(3, 1) #Turn ON LED

      pygame.mixer.music.play()

      capture image

      camera.capture('intruder.jpeg')

      #send image to whatsapp

      message = client.messages.create(body='The engineering projects program has detected and intruder!',

      media_url='https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse4.mm.bing.net%2Fth%3Fid%3DOIP.q1z1XWRn_WAV4oM-Qr2M2gHaGb%26pid%3DApi&f=1',

      from_=from_whatsapp_number,

      to=to_whatsapp_number)


      print(message.sid)

      time.sleep(0.2)

      GPIO.cleanup()

      break


      Captured image

      Conclusion

      In this article, you learned to build a security system using a motion detector and raspberry pi. We also learned how to set up Twilio to send and receive Whatsapp messages using the Twilio API. This project can be implemented in so many areas therefore it is a good idea for you it plays around with the code and implements some extra features. In the next tutorial, we are going to build a led cube in raspberry pi 4.

      Build a Twitter bot in Raspberry pi 4

      Thank you for joining us for yet another session of this series on Raspberry Pi programming. In the preceding tutorial, we integrated a real-time clock with our raspberry pi four and used it to build a digital clock. However, In this tutorial, we will construct your personal Twitter bot using Tweepy, a Py framework for querying the Twitter application programming interface.

      You will construct a Response to mentions robot that will post a response to everybody's tweet mentioning it with a certain keyword.

      The response will be a photo we will make and put any text over it. This message is a quote you will acquire from a 3rd application programming interface. Finally, we will look at the benefits and drawbacks of bots.

      This is what it looks like:

      Where To Buy?
      No.ComponentsDistributorLink To Buy
      1Raspberry Pi 4AmazonBuy Now

      Prerequisites

      To continue through this guide, you'll need to have the following items ready:

      An AWS account

      Ensure you've joined up for Aws Beanstalk before deploying the finished project.

      Twitter application programming interface auth credentials

      To connect your robot to Twitter, you must create a developer account and build an app that Twitter provides you access to. 

      Python 3

      Python 3.9 is the current version, although it is usually recommended to use an edition that is one point behind the latest version to avoid compatibility problems with 3rd party modules. 

      You have these Python packages installed in your local environment.

      • Tweepy — Twitter's API can be used to communicate with the service.

      • Pillow — The process of creating an image and then adding words to it

      • Requests — Use the Randomized Quote Generation API by sending HTTP queries.

      • APScheduler — Regularly arrange your work schedule

      • Flask — Develop a web app for the Elastic Beanstalk deployment.

      The other modules you will see are already included in Python, so there's no need to download and install them separately.

      Twitter application programming interface auth credentials

      OAuth authentication is required for all requests to the official Twitter API. As a result, to use the API, you must first create the necessary credentials. The following are my qualifications:

      • consumer keys

      • consumers secret

      • access tokens

      • access secrets

      Once you've signed up for Twitter, you'll need to complete the following steps to generate your user ID and password:

      Step 1: Fill out an Application for a Developers Twitter Account

      The Twitter developer’s platform is where you may apply to become a Twitter developer.

      When you sign up for a developer account, Twitter will inquire about the intended purpose of the account. Consequently, the use case of your application must be specified.

      To expedite the approval process and increase your chances of success, be as precise as possible about the intended usage of your product.

      Step 2: Build an App

      The verification will arrive in a week. Build an application on Twitter's developers portal dashboard after Twitter's developers account access has been granted.

      Apps can only use authentication details; thus, you must go through this process. Twitter's application programming interface can be used to define an app. Information regarding your project is required:

      • Your project's name serves as its identifier.

      • Your project's category should be selected here. Choose "Creating a bot" in this scenario.

      • Your project's purpose or how users will interact with your app should be described in this section. 

      • The app's name: Finally, give your app a name by typing it in the box provided.

      Step 3: The User Credentials should be created

      To begin, navigate to Twitter's apps section of your account and create your user credentials. When you click on this tab, you'll be taken to a new page on which you can create your credentials.

      The details you generate should be saved to your computer so they may be used in your program later. A new script called credentials.py should be created in your project's folder and contains the following four key-value pairs:

      access_token="XXXXXXX"

      access_token_secret="XXXXXXXX"

      API_key="XXXXXXX"

      API_secret_key="XXXXXXXX"

      You can also test the login details to see if everything is functioning as intended using:

      import tweepy

      # Authenticate to Twitter

      auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")

      auth.set_access_token("ACCESS_TOKEN", "ACCESS_SECRET")

      api = tweepy.API(auth)

      try:

          api.verify_credentials()

          print("Authentication Successful")

      except:

          print("Authentication Error")

      Authorization should be successful if everything is set up correctly.

      Understand Tweepy

      Tweepy is a Python module for interacting with the Twitter application programming interface that is freely available and simple. It provides a way for you to interact with the Application programming interface of your program.

      Tweepy's newest release can be installed by using the following command:

      pip install tweepy

      Installing from the git repo is also an option.

      pip install git+https://github.com/tweepy/tweepy.git

      Here are a few of its most important features:

      OAuth

      As part of Tweepy, OAuthHandler class handles the authentication process required by Twitter. As you can see from the code above, Tweepy's OAuth implementation is depicted below.

      Twitter application programming interface wrapper

      If you'd want to use the RESTful application programming functions, Tweepy provides an application programming interface class that you can use. You'll find a rundown of some of the more popular approaches in the sections that follow:

      • Function for tweet

      • Function for user

      • Function for user timeline

      • Function for trend

      • Function for like

      Models

      Tweepy model class instances are returned when any of the application programming interface functions listed above are invoked. The Twitter response will be contained here. How about this?

      user = api.get_user('apoorv__tyagi')

      When you use this method, you'll get a User model with the requested data. For instance:

      python print(user.screen_name) #User Name print(user.followers_count) #User Follower Count

      Fetch the Quote

      You're now ready to begin the process of setting up your bot. Whenever somebody mentions the robot, it will respond with a picture with a quotation on it.

      So, to get the quote, you'll need to use an application programming interface for a random quotation generator. If you want to do this, you'll need to establish a new function in the tweetreply.py script and send a hypertext transfer protocol request to the application programming interface endpoint. Python's requests library can be used to accomplish this.

      Using Python's request library, you can send hypertext transfer protocol requests. As a result, you could only fixate on the software's interactions with services and data consumption rather than dealing with the complex making of requests.

      def get_quote():

          URL = "https://api.quotable.io/random"

          try:

              response = requests.get(URL)

          except:

              print("Error while calling API...")

      This is how they responded:

      The JSON module can parse the reply from the application programming interface. You can use import JSON to add JSON to your program because it is part of the standard libraries.

      As a result, your method returns the contents and author alone, which you will use. As you can see, here's how the whole thing will work.

      def get_quote():

          URL = "https://api.quotable.io/random"

          try:

              response = requests.get(URL)

          except:

              print("Error while calling API...")

          res = json.loads(response.text)

          return res['content'] + "-" + res['author']

      Generate Image

      You have your text in hand. You'll now need to take a picture and overlay it with the text you just typed.

      The Pillow module should always be your first port of call when working with images in Python. The Python Pillow imaging module provides image analysis and filetypes support, providing the interpreter with a strong image processing capacity.

      Wallpaper.py should be created with a new function that accepts a quote as the argument.

      def get_image(quote):

          image = Image.new('RGB', (800, 500), color=(0, 0, 0))

          font = ImageFont.truetype("Arial.ttf", 40)

          text_color = (200, 200, 200)

          text_start_height = 100

          write_text_on_image(image, quote, font, text_color, text_start_height)

          image.save('created_image.png')

      Let's take a closer look at this feature.

      • Image.new() A new photo is created using the given mode and size. The first thing to consider is the style used to generate the new photo. There are a couple of possibilities here: RGB or RGBA. Size is indeed the second factor to consider. The width and height of an image are given as tuples in pixels. The color of the background image is the final option (black is the default color).

      • ImageFont.TrueType() font object is created by this method. It creates a font object with the desired font size using the provided font file. While "Arial" is used here, you are free to use any other font if you so like. Font files should be saved in the project root folder with a TrueType font file extension, such as font.ttf.

      • In other words, the text's color and height at which it begins are specified by these variables. RGB(200,200,200) works well over dark images.

      • Image. Save () created png image will be saved in the root directory due to this process. It will overwrite any existing image with the same name that already exists.

      def write_text_on_image(image, text, font, text_color, text_start_height):

          draw = ImageDraw.Draw(image)

          image_width, image_height = image.size

          y_text = text_start_height

          lines = textwrap.wrap(text, width=40)

          for line in lines:

              line_width, line_height = font.getsize(line)

              draw.text(((image_width - line_width) / 2, y_text),line, font=font, fill=text_color)

              y_text += line_height

      A message will be added to the image using the following method in the same script, Wallpaper.py. Let's take a closer look at how this feature works:

      • Create two-dimensional picture objects with the ImageDraw package.

      • A solitary paragraph is wrapped in texts using text wrap. Wrap () ensures that each line is no more than 40 characters in length. Output lines are returned in a tally form.

      • Draw. Text () will draw a text at the provided location. 

      Use parameter:

      • XY — The text's upper-left corner.

      • Text — The text to be illustrated.

      • Fill — The text should be in this color.

      • font — One of ImageFont's instances

      This is what Wallpaper.py look like after the process:

      from PIL import Image, ImageDraw, ImageFont

      import text wrap

      def get_wallpaper(quote):

          # image_width

          image = Image.new('RGB', (800, 400), color=(0, 0, 0))

          font = ImageFont.truetype("Arial.ttf", 40)

          text1 = quote

          text_color = (200, 200, 200)

          text_start_height = 100

          draw_text_on_image(image, text1, font, text_color, text_start_height)

          image.save('created_image.png')

      def draw_text_on_image(image, text, font, text_color, text_start_height):

          draw = ImageDraw.Draw(image)

          image_width, image_height = image.size

          y_text = text_start_height

          lines = textwrap.wrap(text, width=40)

          for line in lines:

              line_width, line_height = font.getsize(line)

              draw.text(((image_width - line_width) / 2, y_text),line, font=font, fill=text_color)

              y_text += line_height

      Responding to Mentions by Keeping an Eye on the Twitter Feed.

      You've got both the quote and an image that incorporates it in one. It's now only a matter of searching for mentions of you in other people's tweets. In this case, in addition to scanning for comments, you will also be searching for a certain term or hashtags.

      When a tweet contains a specific hashtag, you should like and respond to that tweet.

      You can use the hashtag "#qod" as the keyword in this situation.

      Returning to the tweet reply.py code, the following function does what we want it to:

      def respondToTweet(last_id):

          mentions = api.mentions_timeline(last_id, tweet_mode='extended')

          if len(mentions) == 0:

              return

          for mention in reversed(mentions):

              new_id = mention.id

              if '#qod' in mention.full_text.lower():

                  try:

                      tweet = get_quote()

                      Wallpaper.get_wallpaper(tweet)

                      media = api.media_upload("created_image.png")

                      api.create_favorite(mention.id)

                      api.update_status('@' + mention.user.screen_name + " Here's your Quote", 

                            mention.id, media_ids=[media.media_id])

                  except:

                      print("Already replied to {}".format(mention.id))

      • Respond to tweet() The last id is the function's only argument. Using this variable, you can only retrieve mentions produced after the ones you've previously processed. Whenever you initially invoke the method, you will set its value to 0, and then you'll keep updating it with each subsequent call.

      • mentions_timeline() Tweets are retrieved from the Tweepy module using this function. Only tweets with the last id newer than the provided value will be returned using the first parameter. The default is to show the last 20 tweets. When tweet mode='extended' is used, the full uncut content of the Tweet is returned. Text is shortened to 140 characters if the option is set to "compat."

      Create favorite() is used to generate a favorite for every tweet that mentions you in reverse chronological order, starting with the earliest tweet first and working backward from there.

      In your case, you'll use update status() to send a reply to this message, which includes the original tweet writer's Twitter handle, your textual information, the original tweet's identification, and your list of multimedia.

      To Prevent Repetition, Save Your Tweet ID

      There are several things to keep in mind when repeatedly responding to a certain tweet. Simply save the tweet's identification to which you last answered in a text document, tweetID.txt; you'll scan for the newer tweet afterward. The mention timeline() function will take care of this automatically because tweet IDs can be sorted by time.

      Now, you'll pass a document holding this last identification, and the method will retrieve the identification from the document, and the document will be modified with a new one at the end.

      Finally, here is what the method response to tweet() looks like in its final form:

      def respondToTweet(file):

          last_id = get_last_tweet(file)

          mentions = api.mentions_timeline(last_id, tweet_mode='extended')

          if len(mentions) == 0:

              return

          for mention in reversed(mentions):

              new_id = mention.id

              if '#qod' in mention.full_text.lower():

                  try:

                      tweet = get_quote()

                      Wallpaper.get_wallpaper(tweet)

                      media = api.media_upload("created_image.png")

                      api.create_favorite(mention.id)

                      api.update_status('@' + mention.user.screen_name + " Here's your Quote", 

                            mention.id, media_ids=[media.media_id])

                  except:

                      logger.info("Already replied to {}".format(mention.id))

          put_last_tweet(file, new_id)

      You'll notice that two additional utility methods, get the last tweet() and put the last tweet(), have been added to this section ().

      A document name is required for the function to get the last tweet(); the function putlasttweet() requires the document as a parameter, and it will pick the most recent tweet identification and modify the document with the latest identification.

      Here's what the final tweet reply.py should look like after everything has been put together:

      import tweepy

      import json

      import requests

      import logging

      import Wallpaper

      import credentials

      consumer_key = credentials.API_key

      consumer_secret_key = credentials.API_secret_key

      access_token = credentials.access_token

      access_token_secret = credentials.access_token_secret

      auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)

      auth.set_access_token(access_token, access_token_secret)

      api = tweepy.API(auth)

      # For adding logs in application

      logger = logging.getLogger()

      logging.basicConfig(level=logging.INFO)

      logger.setLevel(logging.INFO)

      def get_quote():

          url = "https://api.quotable.io/random"

          try:

              response = requests.get(url)

          except:

              logger.info("Error while calling API...")

          res = json.loads(response.text)

          print(res)

          return res['content'] + "-" + res['author']

      def get_last_tweet(file):

          f = open(file, 'r')

          lastId = int(f.read().strip())

          f.close()

          return lastId

      def put_last_tweet(file, Id):

          f = open(file, 'w')

          f.write(str(Id))

          f.close()

          logger.info("Updated the file with the latest tweet Id")

          return

      def respondToTweet(file='tweet_ID.txt'):

          last_id = get_last_tweet(file)

          mentions = api.mentions_timeline(last_id, tweet_mode='extended')

          if len(mentions) == 0:

              return

          new_id = 0

          logger.info("someone mentioned me...")

          for mention in reversed(mentions):

              logger.info(str(mention.id) + '-' + mention.full_text)

              new_id = mention.id

              if '#qod' in mention.full_text.lower():

                  logger.info("Responding back with QOD to -{}".format(mention.id))

                  try:

                      tweet = get_quote()

                      Wallpaper.get_wallpaper(tweet)

                      media = api.media_upload("created_image.png")

                      logger.info("liking and replying to tweet")

                      api.create_favorite(mention.id)

                      api.update_status('@' + mention.user.screen_name + " Here's your Quote", mention.id,

                                        media_ids=[media.media_id])

                  except:

                      logger.info("Already replied to {}".format(mention.id))

          put_last_tweet(file, new_id)

      if __name__=="__main__":

          respondToTweet()

      Deploy the bot to Server

      In order to complete the process, you will need to upload your program to a server. Python applications can be deployed using AWS Elastic Beanstalk in this area.

      Amazon web service simplifies management while allowing for greater flexibility and control. Your application is automatically provisioned with capacity, load-balanced, scaled and monitored for health using Elastic Beanstalk.

      Here is how it's going to work out:

      • Install Python on the AWS  environment

      • Build a basic Flask app for the bot

      • Connect to AWS and deploy your Flask app

      • Use logs to find and fix bugs

      Set up Elastic Beanstalk environment

      After logging into the Aws services account, type and pick "Elastic Beanstalk," then click "setup a New App."

      You'll be asked to provide the following information:

      • Name of the application; 

      • Application's tags; 

      • Environment;

      • Code of the application

      Each AWS Elastic Beanstalk application resource can have up to 50 tags. Using tags, you may organize your materials. The tags may come in handy if you manage various AWS app resources.

      Platform branches and versions are automatically generated when Python is selected from the selection for the platform.

      Later, you will deploy your app to elastic Beanstalk. Select "sample app" from the drop-down menu and click "new app." For the most part, it should be ready in about a minute or two

      Create a Flask app

      Python is used to create Flask, a website development framework. It's simple to get started and use. Flask has no dependencies, making it a more "beginner-friendly" framework for web applications.

      Flask has several advantages over other frameworks for building online applications, including:

      • Flask comes with a debugger and a development server.

      • It takes advantage of Jinja2's template-based architecture.

      • It complies with the WSGI 1.0 specification.

      • Unit testing is made easier with this tool's built-in support.

      • Flask has a plethora of extensions available for customizing its behavior.

      Flask as a micro-framework

      It is noted for being lightweight and simply providing the needed components. In addition to routing, resource handling, and session management, it includes a limited set of website development tools. The programmer can write a customized module for further features, such as data management. This method eliminates the need for a boilerplate program that isn't even being executed.

      Create a new Python script and call it application.py, then paste the code below into it while AWS creates an environment.

      from flask import Flask

      import tweet_reply

      import atexit

      from apscheduler.schedulers.background import BackgroundScheduler

      application = Flask(__name__)

      @application.route("/")

      def index():

          return "Follow @zeal_quote!"

      def job():

          tweet_reply.respondToTweet('tweet_ID.txt')

          print("Success")

      scheduler = BackgroundScheduler()

      scheduler.add_job(func=job, trigger="interval", seconds=60)

      scheduler.start()

      atexit.register(lambda: scheduler.shutdown())

      if __name__ == "__main__":

          application.run(port=5000, debug=True)

      Use up scheduler and a flask app to execute a single job() function that will ultimately call the main method in the tweet reply.py script on a minute basis.

      As a reminder, the object instance's identifier of the flask app must be "app." For Elastic Beanstalk to work with your application, you must give it the correct name.

      Deploy and set up the app to Amazon Web Services.

      Your online app's code can include Elastic Beanstalk conf files (.ebextensions) for configuring amazon web services resources and the environments.

      The .config script extension is used for YAML files, and these are put in the .ebextensions directory together with the app's code during the deployment process.

      Establish a new directory called .ebextensions inside the code folder and add a new file called Python .config. Add the following code:

      files:

        "/etc/httpd/conf.d/wsgi_custom.conf":

          mode: "000644"

          owner: root

          group: root

          content: WSGIApplicationGroup %{GLOBAL}

      If you want Elastic Beanstalk to tailor its settings to the app's prerequisites, you'll need to include a list of any external libraries inside a requirements.txt script you produce.

      Execute the command below to generate the requirements.txt file using pip freeze

      Finally, package up everything for uploading on Elastic Beanstalk with Elastic Beanstalk. The architecture of your project directory should now look like this:

      Compress all the files and directories listed here together. Open amazon web services again and select Upload Code.

      Once you've selected a zip archive, click "Deploy." When the health indicator becomes green, your app has been successfully launched. "Follow @zeal quote!" if all of the above steps have been followed correctly, they should appear on your website link.

      Procedure for getting an error report in the system

      The following steps will help you access the reports of your app in the event of an error:

      • Logs can be seen under the "Environment" tab in the Dashboard.

      • After choosing "Request Log," you'll be taken to a new page with an options list. The last lines option is for the latest issues, but the "full log" option can be downloaded if you need to troubleshoot an older error.

      • To see the most recent log line, click "Download," A new web page will open.

        The Benefits and Drawbacks of Twitter Autonomy

        Media platforms entrepreneurs benefit greatly from automation, which reduces their workload while increasing their visibility on Twitter and other media platforms. We may use various strategies to ensure that we're always visible on Twitter.

        The benefits of automation are numerous. 

        There is still a need for human intervention with any automated process.

        However, automation should only be a minor element of your total plan. An online presence that is put on autopilot might cause problems for businesses. If your campaign relies on automation, you should be aware of these problems:

        Appearing like a robot

        Engaging others is all about being yourself. The tweet was written by a person who was using a phone to produce it, based on the bad grammar and occasional errors. Those who aren't in the habit of writing their own Twitter tweets on the fly risk seeming robotic when they send out several automated messages. Tweets written in advance and scheduled to post at specific times appear disjointed and formulaic.

        It is possible to appear robotic and dry if you retweet several automated messages. If your goal is to promote user interaction, this is not the best option.

        The solution: Don't automate all of your messages. The platform can also be used for real-time interaction with other people. Whenever feasible, show up as yourself at gatherings.

        Awful Public Relations Fumbles

        When you plan a message to go out at a specific time, you have no idea what will be trending. If a tragic tale is trending, the tweet could be insensitive and out of context. On Twitter, there is a great deal of outrage. Because everyone is rightly concerned about their collective destiny, there is little else to talk about.

        Then, in a few hours, a succession of your tweets surface. Images showing the group having a great time in Hawaii.

        While it's understandable that you'd want to avoid coming across as uncaring or unaware in this day and age of global connectivity and quick accessibility of info from around the globe, it's also not a good look. Of course, you didn't mean it that way, but people's perceptions can be skewed.

        What to do in response to this: Automatic tweets should be paused when there is a major development such as the one above. If you're already informed of the big news, it's feasible, but it may be difficult due to time variations.

        Twitter automation allows your messages to display even if you are not into the service. Your or your company's identity will remain visible to a worldwide audience if you have a global target market.

        If an automatic tweet appears before you can brush up on the latest events in your location, follow it up with a real one to show your sympathy. People find out about breaking news through Twitter, a global platform. Few of us have the luxury of remaining in our small worlds. While it's excellent to be immersed in your company's day-to-day operations, it's also beneficial to keep up with global events and participate in Twitter's wider discussion.

        Absence of Reaction

        People respond to your automatic tweet with congratulations, questions, or pointing out broken links that go unanswered because you aren't the one publishing it; a program is doing it in your stead, not you. Awkward.

        Suppose something occurs in the wee hours of the morning. Another tweet from you will appear in an hour. After seeing the fresh tweet, one wonders if Mr. I-Know-It-All-About-Social-Media has even read his reply.

        What to do in response to this situation: When you next have a chance to log on, read through the comments and answer any that have been left. Delayed responses are better than no responses. Some people don't understand that we're not all connected to our Twitter 24 hours a day.

        Damage to the reputation of your company

        As a means of providing customer support, Twitter has become increasingly popular among businesses. It's expected that social media queries will be answered quickly. Impatience breeds on the social web since it's a real-time medium where slow responses are interpreted as unprofessionalism.

        On the other hand, Automatic tweets offer the idea that businesses are always online, encouraging clients to interact with the company. Customers may think they're being neglected if they don't hear back.

        When dealing with consumer issues, post the exact hours you'll be available.

        Vital Comments Left Unanswered

        As soon as somebody insults you, the business, or even just a tweet, you don't want to let those unpleasant feelings linger for too long. We're not referring to trolls here; we're referring to legitimate criticism that individuals feel they have the right to express.

        What should you do? Even though you may not be able to respond immediately, you should do so as soon as you go back online to limit any further damage.

        Inappropriate actions like Favoriting and DMing might be harmful.

        Individuals and organizations may use IFTTT recipes to do various tasks, like favorite retweets, follow back automatically, and send automated direct messages.

        The unfortunate reality is that automation cannot make decisions on its own. In light of what people may write unpredictably, selecting key phrases and establishing a recipe for a favorite tweet that includes those terms, or even postings published by certain individuals, may lead to awkward situations.

        Spam firms or individuals with shady history should not be automatically followed back. Additionally, Twitter has a cap on the number of followers you can follow at any given time. Spammy or pointless Twitter individuals should not be given your followers.

        What should you do? Make sure you are aware of what others are praising under your name. Discontinue following anyone or any company that does not exude confidence in your abilities. In our opinion, auto-DMs can work if they are personalized and humorous. Please refrain from including anything that can be found on your profile. They haven't signed up for your blog's newsletter; they've just become one of your Twitter followers. Take action as a result!

        Useful Benefits

        Smaller companies and busy people can greatly benefit from Tweet automation. As a result of scheduling Twitter posts, your workload is reduced. A machine programmed only to do certain things is all it is in the end. But be careful not to be lulled into complacency.

        Social media platforms are all about getting people talking. That can’t be replaced by automation. Whether you use automation or not, you must always be on the lookout for suspicious activity on your Twitter account and take action as soon as you notice it.

        Conclusion

        In this article, you learned how to build and publish a Twitter robot in Py.

        Using Tweepy to access Twitter's API and configuring an amazon web service Elastic Beanstalk environment for the deployment of your Python application were also covered in this tutorial. As part of the following tutorial, the Raspberry Pi 4 will be used to build an alarm system with motion sensors.

        Syed Zain Nasir

        I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

        Share
        Published by
        Syed Zain Nasir