Introduction to Arduino and Internet of Things Io

  • Slides: 44
Download presentation
Introduction to Arduino and Internet of Things (Io. T) Department of Electrical Engineering Dr

Introduction to Arduino and Internet of Things (Io. T) Department of Electrical Engineering Dr Isaac Fung eeyffung@polyu. edu. hk

Form a group 2/3 students per group

Form a group 2/3 students per group

Introduction What is Internet of Things (Io. T) ◦ is a system of interrelated

Introduction What is Internet of Things (Io. T) ◦ is a system of interrelated computing devices that are provided with unique identifiers and the ability to transfer data over a network without requiring human-to-human or human-to-computer interaction ◦ Network – internet usually via Wi. Fi ◦ Transfer data – remote control and monitoring

Io. T introduction

Io. T introduction

Introduction Example of Io. T applications ◦ Smart home Remote control your Air Conditioner

Introduction Example of Io. T applications ◦ Smart home Remote control your Air Conditioner using an App with your cell phone Monitoring your electric bill Security camera

What are the objectives of this project To develop a simple Io. T system

What are the objectives of this project To develop a simple Io. T system consists of: ◦ ◦ Node. MCU and motor shield A RGB LED A Fan Have Web GUI (graphical user interface) IOT Internet

Components Node. MCU Motor shield RGB LED Fan

Components Node. MCU Motor shield RGB LED Fan

What you will learn Program the Node. MCU device ◦ How to utilize the

What you will learn Program the Node. MCU device ◦ How to utilize the Input/output features of the device ◦ How to utilize the Wi. Fi system of the device ◦ How to develop the Web GUI Note: program is compatible to the Arduino

Demo Use your cell phone and login to the wifi network SSID: testwifi Password:

Demo Use your cell phone and login to the wifi network SSID: testwifi Password: sfgs 8770 Use a web browser to open 192. 168. 43. 247 Try clicking the buttons

Demo

Demo

Node. MCU Features of Node. MCU ◦ ◦ ◦ ESP 8266 Wi. Fi module

Node. MCU Features of Node. MCU ◦ ◦ ◦ ESP 8266 Wi. Fi module Can be programmed with Arduino IDE Can access 16 GPIO (General Purpose Input/Output) Has analog input Other serial input systems: SPI and I 2 C

About Node. MCU

About Node. MCU

Arduino IDE setup

Arduino IDE setup

Concepts of GPIO In a digital system you need ◦ Input devices (most likely

Concepts of GPIO In a digital system you need ◦ Input devices (most likely sensors) to collect data ◦ Based on the data, the processor do some decision making then control some output devices Example: ◦ Input device is a light sensor (input) when the light sensor detected that the light is dark then the processor can turn on the light (output)

What is HIGH LOW It is a digital device Digital – 0 (LOW) 1

What is HIGH LOW It is a digital device Digital – 0 (LOW) 1 (HIGH) 0 – 0 v 1 – 5 v or 3. 3 v So a “ 1” can turn-ON some devices such as LED (Light emitting diode)

Defining the I/O pins Each I/O pin operates on a range of 0 to

Defining the I/O pins Each I/O pin operates on a range of 0 to +5 VDC (Volt direct current) A range of 0 to +2 V is said to be off or LOW while anything over about +3 V is said to be on or HIGH. Each pin can either source, or provide a positive biased current, or sink, to provide a negative biased current, up to 40 milliamps each.

Program syntax of Arduino sketch Arduino program is called a sketch and is saved

Program syntax of Arduino sketch Arduino program is called a sketch and is saved in a folder called a sketchbook. Arduino C follows a set of rules that govern syntax and structure and tell the interface board what to do.

Arduino software C is a top-down structural programming language, meaning that lines of code

Arduino software C is a top-down structural programming language, meaning that lines of code are executed in order from the top of the program until it reaches the end at the bottom

Basic Arduino C void setup(){ pin. Mode(13, OUTPUT); } // comment void loop(){ digital.

Basic Arduino C void setup(){ pin. Mode(13, OUTPUT); } // comment void loop(){ digital. Write (13, HIGH); delay(1000); digital. Write(13, LOW); delay(1000); } Basic syntax of Arduino C is similar to C/C++ Except that there is no main() Comment is based on // for single line or /* */ for multiple lines Use a ; after each statement NOTE: the language is Case Sensitive

Basic functions As a requirement of the Arduino library, every sketch must at the

Basic functions As a requirement of the Arduino library, every sketch must at the very least contain both a setup() and loop() function in order to operate, even if they are empty. So the most simple and valid Arduino program would be: void setup(){} void loop(){}

The setup function The setup function is used to initialize the system or setup

The setup function The setup function is used to initialize the system or setup various aspects of the Arduino board void setup () void is the return type of the setup function The setup() function is called only once at the beginning of the sketch Typical operations included in setup() ◦ Define the I/O pins – input or output ◦ Define the communication speed – baud rate ◦ Running certain tasks that are only needed one time

The loop function Loop function – will run continuously as a loop It begins

The loop function Loop function – will run continuously as a loop It begins after the last statement in the setup function finished executing void loop(){ ◦ ◦ digital. Write (13, HIGH); delay(1000); digital. Write(13, LOW); delay(1000); } In this loop function, the LED connected to pin 13 will turn ON then OFF continuously

Programming the Node. MCU To control the Node. MCU processor to perform your tasks,

Programming the Node. MCU To control the Node. MCU processor to perform your tasks, you need to write the program, which is a form of C++ programming language. Through the IDE, you can check for errors of the program, if no error is found then you can download the program to the processor. Once the program is downloaded then the program will be executed.

Blink program – output void setup() { pin. Mode(LED_BUILTIN, OUTPUT); } // Initialize the

Blink program – output void setup() { pin. Mode(LED_BUILTIN, OUTPUT); } // Initialize the LED_BUILTIN pin as an output // the loop function runs over and over again forever void loop() { digital. Write(RED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is acive low on the ESP-01) delay(1000); // Wait for a second digital. Write(RED, HIGH); // Turn the LED off by making the voltage HIGH delay(2000); // Wait for two seconds (to demonstrate the active low LED) }

Sending and reading digital signal If the pin is OUTPUT then to send a

Sending and reading digital signal If the pin is OUTPUT then to send a digital signal ◦ digital. Write (pin, state) ◦ Pin – pin to use ◦ State – either HIGH or LOW If the pin is INPUT then to read a digital signal ◦ X = digital. Read(pin) ◦ Pin – pin to read ◦ The digital. Read() will return the value read from the pin so you can use a variable to store the value

Upload the software You develop your software in the PC But you need to

Upload the software You develop your software in the PC But you need to download the program to the Arduino board You program must be compiled (verify) and then download (Upload) Or you can combine the compile and download in one single operation

Basic rules about I/O Define the pin as input or output using Pin. Mode

Basic rules about I/O Define the pin as input or output using Pin. Mode ◦ Eg pin. Mode(2, OUTPUT); Send ‘ 1’ or ‘ 0’ to a pin ◦ digital. Write (2, HIGH) ; ◦ digital. Write (2, LOW) ;

Pin definition in Node. MCU static static static const const const uint 8_t uint

Pin definition in Node. MCU static static static const const const uint 8_t uint 8_t uint 8_t D 0 D 1 D 2 D 3 D 4 D 5 D 6 D 7 D 8 D 9 D 10 = 16; = 5; = 4; = 0; = 2; = 14; = 12; = 13; = 15; = 3; = 1;

Exercise 1 Output a signal to control the RGB LED Connect the circuit Now

Exercise 1 Output a signal to control the RGB LED Connect the circuit Now when the output is “LOW” the LED will turn ON

What is Serial. print? Node. MCU is connected to the notebook computer via a

What is Serial. print? Node. MCU is connected to the notebook computer via a serial communication channel (COM X) ◦ This is called a com port Using this channel, Node. MCU can send message to the notebook To show the message, use the serial monitor of the Arduino IDE To send message, need to set the communication rate Serial. begin(9600) Serial. print is a function for sending messages Serial. println is similar to Serial. print but with a line -feed

Analogue output - PWM The PWM can be used to approximate an analogue signal

Analogue output - PWM The PWM can be used to approximate an analogue signal PWM – Pulse Width Modulation PWM is a pulse train – the output is ON and OFF PWM is related to the duty cycle (ON-time / period ) 50% duty cycle – ON-time = OFF-time

PWM – analogue

PWM – analogue

Analogue output To produce the PWM waveform ◦ analog. Write(pin, x) ◦ Pin –

Analogue output To produce the PWM waveform ◦ analog. Write(pin, x) ◦ Pin – pin to use ◦ X is a value between 0 to 1023: 0 (0% duty cycle) 1023 (100% duty cycle) ◦ Note X is not the duty cycle ◦ Not all pin can output the PWM ◦ Only Pins 3, 5, 6, 9, 10 and 11 can produce the PWM

PWM for Motor shield For A+ AD 1 – for PWM D 2 –

PWM for Motor shield For A+ AD 1 – for PWM D 2 – for direction (0, 1 – clockwise and anticlockwise) For B+ BD 3 – for PWM D 4 – for direction

Using a loop to change speed for (int x = 0; x<1000; x+=100){ analog.

Using a loop to change speed for (int x = 0; x<1000; x+=100){ analog. Write(MPWM, x); Serial. print(“Speed: ”); Serial. println(x); delay(5000); // 5 seconds }

Exercise 2 Control the speed of the motor Setup the circuit Connect a 5

Exercise 2 Control the speed of the motor Setup the circuit Connect a 5 v input to the motor shield Vm and GND Run the program Modify the program to change the rotation direction of the motor Change the color of LED by PWM

Using the Wi. Fi of the device Try the Wi. Fi. Scan example and

Using the Wi. Fi of the device Try the Wi. Fi. Scan example and test if the Wi. Fi is functional Just open the example, build and download the file Open the Serial Monitor to observe the result

What is needed ESP 8266 Wi. Fi. h Wi. Fi. Client. h ESP 8266

What is needed ESP 8266 Wi. Fi. h Wi. Fi. Client. h ESP 8266 Web. Server. h These are called header files and they provide functions needed by the system

Sample program Please refer to the sample program

Sample program Please refer to the sample program

Build a Webpage is based on HTML The server send a HTML code to

Build a Webpage is based on HTML The server send a HTML code to the client’s web browser then a webpage will be shown It is by ◦ server. send(200, “text/html”, website) Where the website is string of HTML codes

Test HTML Go to the following website https: //www. onlinehtmleditor. net/ Try typing the

Test HTML Go to the following website https: //www. onlinehtmleditor. net/ Try typing the HTML codes and observe the result ◦ <h 1>ESP 8266 Web Server</h 1> ◦ <p><button>HOME</button></p> ◦ <p><h 2>Condition read from flamesensor</h 2></p>

How to identify a button is pressed? Need to add reference to html codes

How to identify a button is pressed? Need to add reference to html codes to distinguish different buttons are being pressed The html codes for adding a reference <a href=“Red"><button>RED</button></a>

How to use the reference In the setup() add codes to perform operation according

How to use the reference In the setup() add codes to perform operation according to the reference value server. on("/RED", HTTP_GET, handle. RED); When the user refers to the page /RED then execute the handle. RED() function

Exercise Modify the program so that you can control all RGB components and the

Exercise Modify the program so that you can control all RGB components and the speed of the motor as well as turning it OFF