Setup Arduino Development Environment - Internet of Things - ثاني ثانوي
Part 1
1. IoT Fundamentals
2. The IoT in Our Lives
3. Building IoT applications with Arduino
4. Building an IoT cloud application
Part 2
5. IoT Advanced Applications
6. ++IoT Programming With C
7. IoT messaging
8. IoT Wireless Sensor Network Simulation
116 4. Building an IoT Cloud Application In this unit, you will create an loT application that monitors an environment and sends data through a web service to a cloud platform. You will build a real circuit using an Arduino microcontroller that you will program using Python. Learning Objectives In this unit, you will learn to: > Use Python to program an Arduino microcontroller with PyFirmata. > Design a circuit for an IoT application using an Arduino microcontroller. > Monitor an environment and collect sensor data in real time. > Use a web service to send the collected data to a cloud platform. > Monitor a remote environment with data provided in a cloud platform. > Understand how data from sensors and cloud datasets are used for data-driven decision making. > Understand how loT applications can be scaled to evolve into complex solutions. Tools › Arevino IDE > JetBrains PyCharm > Binary loT Cloud
In this unit, you will create an IoT application that monitors an environment
Learning Objectives
Tools
Lesson 1 Setup Arduino Development Environment Using Python with Arduino The official programming language for the Arduino microcontroller is C++ but you can use Python through the Firmata protocol. Python is a high level programming language and its greatest strength is the wide range of libraries that can support any scenario. The Firmata protocol allows for a connection between a microcontroller and the commands provided by another programming language. In our case, we will use Python with the PyFirmata library which is an interface for the Firmata protocol. حرارة التعليم 3125-185 Connect the Arduino controller te your USB port. Link to diva lesson www.len edu Figure 4.1 Arduino connected to laptop USB port 117
Using Python with Arduino
The Arduino Integrated Development Environment (Arduino IDE) is a text editor specifically designed for Arduino microcontrollers. It comes with additional tools for interacting with an Arduino and contains pre-built programs for specific Arduino tasks. To install the Arduino IDE go to the website: https://www.arduino.cc/en/software and download the latest version. Run the installer to complete the installation process. You will now open the Arduino IDE in order to load the Standard Firmata sketch to set up communication between the Arduino and your Python code. Make sure that the Arduino IDE version you are downloading is compatible with your Arduino microcontroller device. Let's start by finding and running the Arduino IDE from your computer's search button. To load the StandardFirmata sketch: > Click the Windows search button and type Arduino IDE. > Open the Arduino IDE > Load the StandardFirmata sketch into the Arduino IDE by clicking on File > Examples > Firmata > StandardFirmata. > Upload the sketch to your Arduino. Ann Document th More Arduino IDE 3 لفة مقالة | محلي التصمد | kide download Nourtside download windows 10 amicides online 20 antum de software HTH 900 ()( // put your awtup code here, to run once: אס ם א Pas de language // pat jou bain mode here, to run repeatedly: A 2 1
The Arduino Integrated Development Environment (Arduino IDE)
Loading the StandardFirmata sketch:
وتسلية | وكس امتتعالى Fille Jen Touth Comp Open- Open Recent 04 Comm С Wal-W Seve Qui-S Пле Tag Sp Pr Trace Co GEM Он C-U M SAI Serve 4 EEPROM SVT Www Firmata is a protocol that enables software on a computer to communicate with microcontrollers. The protocol may be implemented on any microcontroller architecture. وزارة التعليم 2173-1475 us ence: Алауберна Exting Simp Sund SWIF 5 Figure 4.2 Loading the firmata sketch
Firmata is a protocol that enables so�ware on a computer to communicate with microcontrollers.
You can get the communication port from your PC to Arduino by clicking on Tools > Port > Serial ports as demonstrated below. In this example, the communication port is COM4, on your PC it might be different, e.g. COM3, COMS. Make a note of the communication port because you will use it in your Python code to communicate with Arduino. StandFirmata Archao 1.29 FEdi Skich Too Hal A Form The Encoding & Reload Sea Mina C-T Gesin un chunde сом Figure 4 Viewing the Arduino communication port Whenever you control an Arduino microcontroller with Python, you need to keep the StandardFirmsta Puiker running so that the Python code that you write can communicate with Arduino. 2173-175 120 Arduino Uno is connected to COM4. If you do not connect the Arduino device to a USB port in a device, then the port will be inactive.
You can get the communication port from your PC to Arduino by clicking on Tools > Port > Serial ports as demonstrated below.
Now that you have loaded Standard Firmata onto the Arduino microcontroller, you need to follow these steps for each Python project: Open PyCharm and install the pyfirmata Python package through pip. In PyCharm open the terminal in your working directory and enter the following command: pip install pyfirmata Create a new Python file and at the beginning of your code import the pyfirmata package with the line: import pyfirmata Create a variable called communication port which will store the name of the communication port of your PC where the Arduino is connected: communication port = "COM4" Use the following commands to set up the connection between your Python program and your Arduino, and to access the pins of the Arduino: # Set Lie Arduino port to read from board = pyfirmata. Arduino (communication_port) # Set up access to the inputs of the circuit it = pyfirmata.util.Iterator(board) it.start() For the commands that you give the Arduino, you need an infinite loop so that they will be executed continuously. x pyfirmata PC PyCharm Figure 4.4 Coding and Arduino with Python through Airfirmita while True: وزارة التعليم here Although the connection is through a USB cable, Windows is using a traditional serial port connection to exchange data with the Arduino device. The OS takes care of the virtual serial port. 125
Now that you have loaded StandardFirmata onto the Arduino microcontroller, you need to follow these steps for each Python project:
The code for every Python program for Arduino starts like this: import pyfirmata communication_port = "COM4" # Set the Arduino port to read from board = pyfirmata. Arduino(communication_port) # Set up pyfirmata to access the status of the inputs of the circuit it pyfirmata.util.Iterator(board) il.start() while True: #udit your code hear The code that you implement is run on your own PC, not on the Arduino, which means that you can access functions that are not available on the Arduino microcontroller. وزارة التعليم 2123-1445 132 Figure 4.5 Controlling an Arduino microcontroller with Python code ARDUING
The code for every Python program for Arduino starts like this:
Working with PyFirmata The main functionalities that we need to implement in Python with the use of PyFirmata are the reading and the writing of values to both the analog and digital pins of the Arduino microcontroller. In pyfirmata the above action is done by the board.get_pin() method which takes 3 character arguments separated by a colon. The first argument is 'a' or 'd' which means analog or digital pin.. The second argument is the number of the pin that you want. ■ Digital pins are numbered 0-12. Analog pins are numbered AO-AS. The third argument is the type of interaction with the Arduino microcontroller. ■ For digital pins it is 'i' or 'o' for input or output. ⚫ For analog pins it is 'i' or 'p' for input or PWM (Pulse Width Modulation). Pulse Width Modulation (PWM) Pulse Width Modulation (PWM) is a type of modulation that uses a digital output to make an analog signal with varying power. Dv Dv 1 Period Pulse Width Modulation 1 Period 0% Duty Cycle analogWrite(0) 75% 75% 7-M 25% Duty Cycle analogWrite(64) 50 W 50 D Smy 500 50M DV Period 1 Period Periodi 750 15% 5 DUA LOW TIME 51 50% 10% 50% Duty Cycle analogWrite(127) 75 710 29 75 M 25% DU Sv وزارة التعليم 2123-1465 25 75M TW 25 TEM 75% Duty Cycle analogWrite(191) ADDING ILOX 100% 100% 100% 100% 100% Duty Cycle-onologWrite(255) Figure 4.6, Polse Width Modulation
Working with PyFirmata
Interacting with Digital Pins Reading a value from a digital pin (digital pin 10). = digital pin board. get_pin("d;10:1") pin value digital pin.read() Writing a value to a digital pin (digital pin 10). digital pinboard.get_pin("d: 10:0") digital pin.write(1) digital pin.write(0) Interacting with Analog Pins Reading a value from an analog pin (analog pin 4). analog pin board.get_pin("a:4:1") pin_value = analog_pin.read() Writing a value to an analog pin (analog pin 4). analog_pin = board.get_pin("a:4:p") analog pin.write(0.75) حرية التعليم 2103-1475 724 Values Analog 75 pin AND Each sensor or actuator require different values to work properly. Values Digital pin HIGH Figure 4.7 The difference between reading data from digital and analog pins
Interacting with Digital Pins
Interacting with Analog Pins
A Simple Arduino Project with PyFirmata To demonstrate the use of the PyFirmata library, we will create a simple Arduino project using an external LED light and the integrated LED pin of an Arduino. We will use Python to program each LED light to blink alternately. First we will simulate the circuit in Tinkercad Circuits. You will need the following components: ⚫ 1 Arduino Uno R3 1 Breadboard Small 1 LED ⚫ 1 Resistor Components that you will use in this project وزارة التعليم 127-173 ∞ UNO 18 ARDUINO Arduino Uno R3 Breadboard Small LED Resistor Figure 4.8 Project components 15
A Simple Arduino Project with PyFirmata
Let's start by putting the required components into the Tinkercad Circuits workplane. To load the components: > Find the Arduino Uno R3 from the components library and drag and drop it into the workplane. > Find the Breadboard Small from the components library and drag and drop it into the workplane. IoT Unit 4.mson. وزارة التعليم OO UND Figure 49 Loading the components 2 1 0 = Code
Let's start by putting the required components into the Tinkercad Circuits workplane.
Now you will complete the wiring to connect the Arduino to the external LED. To wire the Arduino: > Connect the Arduino Uno R3 GND to the negative column of the Breadboard Small and change the wire color to black. > Find the Resistor from the components library, place it on the Breadboard Small, connect the Digital pin 12 to Terminal 1 of the Resistor and change the wire color to red. Wing LEDS وزارة التعليم 2 1 3 O LIND Figure 4.10 Wiring the Arduino
Now you will complete the wiring to connect the Arduino to the external LED.
To connect the LED: > Find the LED from the components library, place it on the Breadboard Small, > Connect Terminal 2 of the Resistor to the Anode of the LED. > Connect the Cathode of the LED to the negative column of the Breadboard Small. Complete circuit وزارة التعليم 2 10. 3 MBLIA FINM OO UNO Fieure 4.11 Connecting the external LED 8 Figure 4 12 Complete circuit in Tinkercad 0
Connecti ng the LED:
Physical Circuit This photo represents the physical circuit's look. وزارة التعليم 123-1985 8 ARDUINO UNO R3 ANALOG ENT Figure 13 Photo of the physical circul External LED Internal LED
Physical Circuit
170 Programming the Arduino to Blink We will program the two LEDs to blink one after the other with a time difference of one second. The built-in LED on the Arduino is connected to digital pin 13, and the external LED is connected to the digital pin 12. Inside the infinite loop, we will send a HIGH (1) signal to the LED that we want to shine and a LOW (0) signal to the other LED. After one second, we will reverse the signals. D12 Open PyCharm, create a new Python file and import the required libraries. import pyfirmata import time Set up the communication port. communication_port = 'COM4 Set up the communication between PyFirmata and the board. board pyfirmata.Arduino(communication_port) it pyfirmata.util.Iterator(board) it.start() Get the pins for the external LED and the internal LED. external led board. get pin("d: 12:0") internal led board.get_pin("d: 13:0") AREF GND 22 13 12 -11 وزارة التعليم 2173-1485 ARDILLINO D13 d TX ARD RX gure Connected pins to components
Programming the Arduino to Blink
Write the logic to make the lights blink. 123-1445 while True: external led.write(1) internal led.write(0) time.sleep(1) external led.write(0) internal led.write(1) time.sleep(1) D12 LOW D12 HIGH ARDUINO Figure 4:15 Arduino sending digital signal to gifs
Write the logic to make the lights blink.
712 Complete Code import pyfirmata import time communication_port 'COM4' board pyfirmata. Arduino(communication_port) itpyfirmata.util.Iterator(board) it.start() external led board.get_pin("d:12:0") internal led = board. get pin("d:13:0") DIGITAL IPWM- OO UNO ARDUINO while True: external led.write(1) internal_Led.write(0) Lime.sleep(1) external Led.write(0) internal led.write(1) Lime.sleep(1) وزارة التعليم 15 - כקו U 6 20 27. DIGITAL IPWM- XO UNO ARDUIND Figure 4 16 LEDs Binting interchangeably
Complete Code
Exercises 1 Read the sentences and tick True or False. 1. An Arduino microcontroller can be programmed only with C++. 2. Firmata is a cryptography protocol. 3. PyFirmata is a library used only with Python. 4. To open communication between an Arduino and a PC, you need to upload the ServoFirmata sketch. 5. The PyFirmata code automatically recognizes the communication port the Arduino uses. 5 Analog pins use Pulse Width Modulation Instead of standard output. 7. External LED lights can emit light with both digital and analog signals. 8. The internal LED light of the Arduino is connected to the digital 10 pin. 9, The Python code with PyFirmata runs on the Arduino microcontroller. 10. The main bedefit of using a real breadboard is that if the components are placed incorrectly then they can simply be moved to the correct location on the breadboard. True False
Read the sentences and tick True or False.
2 Describe the process of setting up a development environment for Arduino and mention what are the software and hardware environments. 3 What are the advantages of programming an Arduino with Python? Justify your answer. 4 What is the disadvantage of working with the Firmata protocol and the PyFirmata library? وزارة التعليم
Describe the process of setting up a development environment for Arduino and mention what are the software and hardware environments.
What are the advantages of programming an Arduino with Python? Justify your answer.
What is the disadvantage of working with the Firmata protocol and the PyFirmata library?
5 What do the following lines of code do? pinboard.get_pin("a:4:p") pin.write(0.75) 6 Give two examples of a sensor or an actuator that work more effectively with digital signals and two more examples of sensors or actuators which work better with analog signals? Consider the sensors and actuators you have been taught about. pullohg