Unit 2
Unit 2
An M2M area network comprises of machines( or M2M nodes) whiach have embedded
network modules for sensing, actuation and communicating various communiction
protocols can be used for M2M LAN such as ZigBee, Bluetooth, M-bus, Wireless M-Bus
etc., These protocols provide connectivity between M2M nodes within an M2M area
network.
The communication network provides connectivity to remote M2M area networks. The
communication network provides connectivity to remote M2M area network. The
communication networkcan use either wired or wireless network(IP based). While the
M2M are networks use either properietorary or non-IP baed communication protocols,
the communication network uses IP-based network. Since non-IP based protocols are
used within M2M area network, the M2M nodes within one network cannot
communicate with nodes in an externalnetwork.
To enable the communication between remote M2M are network, M2M gateways are
used.
Fig. Shows a block diagram of an M2M gateway. The communication between M2M nodes and
the M2M gateway is based on the communication protocols which are naive to the M2M are
network. M2M gateway performs protocol translations to enable Ip-connectivity for M2M are
networks. M2M gateway acts as a proxy performing translations from/to native protocols to/from
Internet Protocol(IP). With an M2M gateway, each mode in an M2M area network appears as a
virtualized node for external M2M area networks.
5) Applications
M2M data is collected in point solutions and can be accessed by on-premises
applications such as diagnosis applications, service management applications, and
on- premisis enterpriseapplications.
IoT data is collected in the cloud and can be accessed by cloud applications such
as analytics applications, enterprise applications, remote diagnosis and
management applications,etc.
Arduino is a software as well as hardware platform that helps in making electronic projects. It is an
open-source platform and has a variety of controllers and microprocessors. There are various types
of Arduino boards used for various purposes.
The Arduino is a single circuit board, which consists of different interfaces or parts. The board
consists of the set of digital and analog pins that are used to connect various devices and
components, which we want to use for the functioning of the electronic devices.
Most of the Arduino consists of 14 digital I/O pins.
The analog pins in Arduino are mostly useful for fine-grained control. The pins in the Arduino board
are arranged in a specific pattern. The other devices on the Arduino board are USB port, small
components (voltage regulator or oscillator), microcontroller, power connector, etc.
Features:
The features of Arduino are listed below:
o Arduino programming is a simplified version of C++, which makes the learning process easy.
o The Arduino IDE is used to control the functions of boards. It further sends the set of
specifications to the microcontroller.
o Arduino does not need an extra board or piece to load new code.
o Arduino can read analog and digital input signals.
o The hardware and software platform is easy to use and implement.
Arduino Boards:
There are variety of Arduino board used for different purposes. The board varies in I/O pins, size,
etc. The various components present on the Arduino boards are Microcontroller, Digital
Input/Output pins, USB Interface and Connector, Analog Pins, Reset Button, Power button, LED's,
Setup(): It contains an initial part of the code to be executed. The pin modes, libraries, variables,
etc., are initialized in the setup section. It is executed only once during the uploading of the
program and after reset or power up of the Arduino board.
The loop contains statements that are executed repeatedly. The section of code inside the curly
brackets is repeated depending on the value of variables.
Time in Arduino
The time in Arduino programming is measured in a millisecond.
Where, 1 sec = 1000 milliseconds.
We can adjust the timing according to the milliseconds. For example, for a 5-second delay, the time
displayed will be 5000 milliseconds.
Example 1:
Let's consider a simple LED blink example.
To do this experiment we need the following:
1. Arduino UNO board
2. Light Emitting Diode
3. Connecting Wires
4. Breadboard
PROCEDURE:
▪ Connect the circuit as shown in the circuit diagram.
▪ Place the LED on breadboard.
▪ Connect LED positive pin to D13 pin of Arduino UNO using Jumper wires.
▪ LED negative pin is connected to ground.
▪ Compile and Upload the following program code to Arduino UNO.
Circuit Diagram
pinMode ( )
The specific pin number is set as the INPUT or OUTPUT in the pinMode () function.
The Syntax is: pinMode (pin, mode)
Where,
pin: It is the pin number. We can select the pin number according to the requirements.
Mode: We can set the mode as INPUT or OUTPUT according to the corresponding pin number.
Let' understand the pinMode with an example.
Example: We want to set the 12 pin number as the output pin.
Code:
pinMode (12, OUTPUT);
delay ( )
The delay () function is a blocking function to pause a program from doing a task during the specified
duration in milliseconds.
For example, - delay (2000)
Where, 1 sec = 1000millisecond
Hence, it will provide a delay of 2 seconds.
Code:
digitalWrite (13, HIGH);
delay (2000);
digitalWrite (13, LOW);
delay (1000);
Example 2: To light the LED connected to pin number 13. We want to ON the LED for 4 seconds
and OFF the LED for 1.5 seconds.
Code:
void setup ()
{
pinMode ( 13, OUTPUT); // to set the OUTPUT mode of pin number 13.
}
void loop ()
{
digitalWrite (13, HIGH);
delay (4000); // 4 seconds = 4 x 1000 milliseconds
digitalWrite (13, LOW);
delay (1500); // 1.5 seconds = 1.5 x 1000 milliseconds
}
Serial.begin():
Serial Communication:
The serial communication is a simple scheme that uses the UART (Universal Asynchronous
Receiver/Transmitter) on the Microcontroller. It uses,
o 5V for logic 1 (high)
o 0V for logic 0 (low)
For a 3.3V board, it uses
o 3V for logic 1 (high)
o 0V for logic 0 (low)
Every message sent on the UART is in the form of 8 bits or 1 byte, where 1 byte = 8 bits.
The messages sent to the computer from Arduino are sent from PIN 1 of the Arduino board,
called Tx (Transmitter). The messages being sent to the Arduino from the computer are received
on PIN 0, called Rx (Receiver).
These two pins on the Arduino UNO board look like the below image:
The Serial.begin( ) is a part of the serial object in the Arduino. It tells the serial object to perform
initialization steps to send and receive data on the Rx and Tx (pins 1 and 0).
Serial.begin ( )
The serial.begin( ) sets the baud rate for serial data communication. The baud rate signifies the data
rate in bits per second.
The default baud rate in Arduino is 9600 bps (bits per second). We can specify other baud rates as
well, such as 4800, 14400, 38400, 28800, etc.
Arduino Serial.print ( )
The serial.print ( ) in Arduino prints the data to the serial port. The printed data is stored in the ASCII
(American Standard Code for Information Interchange) format, which is a human-readable text.
Each digit of a number is printed using the ASCII characters.
The printed data will be visible in the serial monitor, which is present on the right corner on the
toolbar.
The Serial.print( ) is declared in two formats, which are shown below:
o print( value )
o print( value, format)
Where,
serial: It signifies the serial port object.
print: The print ( ) returns the specified number of bytes written.
value: It signifies the value to print, which includes any data type value.
format: It consists of number base, such as OCT (Octal), BIN (Binary), HEX (Hexadecimal), etc. for the
integral data types. It also specifies the number of decimal places.
Serial.print( value )
The serial.print ( ) accepts the number using the ASCII character per digit and value upto two decimal
places for floating point numbers.
Example 1:
Serial.print(15.452732)
Output:
15.45
Serial.println ( )
The Serial.println ( ) means print line, which sends the string followed by the carriage return ('\r' or
ASCII 13) and newline ('\n' or ASCII 10) characters. It has a similar effect as pressing the Enter or
Return key on the keyboard when typing with the Text Editor.
The Serial.println( ) is also declared in two formats, which are shown below:
o println( value )
o println( value, format)
example:
void setup ( )
Serial.available( )
The Serial.available( ) function in Arduino gets the stored bytes from the serial port that are
available for reading. It is the data, which is already stored and arrived in the serial buffer. The serial
buffer in Arduino holds the 64 bytes.
Ex:
int arrivingdatabyte = 0; // initializing the incoming serial byte
void setup( )
{
Serial.begin(9600); // 9600 is the data rate in bps (bits per second).
}
void loop( ) // loop function that executes repeatedly
{
if(Serial.available( ) > 0) // It will only send data when the received data is greater than 0.
{
arrivingdatabyte = Serial.read( ); // It will read the incoming or arriving data byte
Serial.print("data byte received:");
Serial.println(arrivingdatabyte, DEC); // here, DEC means Decimal
}
}
Arduino Variables:
The variables are defined as the place to store the data and values. It consists of a name, value, and
type.
The variables can belong to any data type such as int, float, char, etc.
example:
int pin = 8;
Here, the int data type is used to create a variable named pin that stores the value 8. It also means
that value 8 is initialized to the variable pin.
Variables Scope
It means that in how many ways the variables can be declared.
The variables can be declared in two ways in Arduino, which are listed below:
o Local variables
Arduino constants:
The constants in Arduino are defined as the predefined expressions. It makes the code easy to read.
Logical level Constants
The logical level constants are true or false.
The value of true and false are defined as 1 and 0. Any non-zero integer is determined as true in
terms of Boolean language. The true and false constants are type in lowercase rather than uppercase
(such as HIGH, LOW, etc.).
Constant Keyword
The name const represents the constant keyword. The variable will remain the same as other
variables, but its value cannot be changed.
For example,
const int a =2;
//....
a = 7; // illegal - we cannot write to or modify a constant
Arduino Operators
The operators are used to solve logical and mathematical problems. For example, to calculate the
temperature given by the sensor based on some analog voltage.
The types of Operators classified in Arduino are:
1. Arithmetic Operators
2. Compound Operators
3. Boolean Operators
4. Comparison Operators
5. Bitwise Operators
Arithmetic Operators
There are six basic operators responsible for performing mathematical operations in Arduino, which
are listed below:
o Assignment Operator ( = )
The Assignment operator in Arduino is used to set the variable's value. It is quite different from the
equal symbol (=) normally used in mathematics.
o Addition ( + )
The addition operator is used for the addition of two numbers. For example, P + Q.
o Subtraction ( - )
Subtraction is used to subtract one value from the another. For example, P - Q.
o Multiplication ( * )
The multiplication is used to multiply two numbers. For example, P * Q.
o Division ( / )
The division is used to determine the result of one number divided with another. For example, P/Q.
Compound Operators
The compound operators perform two or more calculations at once.
The result of the right operand is assigned to the left operand, as already discussed above. The same
condition will apply to all the compound operators, which are listed below:
Let's consider a variable b.
o b++
Here, b = b + 1. It is called the increment operator.
o b+=
For example, b + = 4. It means, b = b+ 4.
o b--
Here, b = b - 1. It is called as the decrement operator.
o b-=
For example, b - = 3. It means, b = b - 3.
o b*=
For example, b * = 6. It means, b = b * 6.
o b/=
For example, b / = 5. It means, b = b / 5.
o b%=
For example, b % = 2. It means, b = b % 2.
Now, let's use the above operators with two variables, b and c.
o b + = c ( b = b + c)
o b - = c ( b = b - c)
Boolean Operators
The Boolean Operators are NOT ( ! ), Logical AND ( & & ), and Logical OR ( | | ).
Let's discuss the above operators in detail.
o Logical AND ( & & )
The result of the condition is true if both the operands in the condition are true.
Consider the below example:
if ( a = = b & & b = = c )
Above statement is true if both conditions are true. If any of the conditions is false, the statement
will be false.
o Logical OR ( | | )
The result of the condition is true, if either of the variables in the condition is true.
Consider the below example.
if ( a > 0 | | b > 0 )
The above statement is true, if either of the above condition ( a> 0 or b > 0 ) is true.
o NOT ( ! )
It is used to reverse the logical state of the operand.
For example, a ! = 2.
The NOT operator returns the value 1 or TRUE when the specified operand is FALSE. It also reverses
the value of the specified expression.
Comparison Operators
The comparison operators are used to compare the value of one variable with the other.
The comparison operators are listed below:
o less than ( < )
The less than operator checks that the value of the left operand is less than the right operand. The
statement is true if the condition is satisfied.
Consider the below code.
int b;
int c ;
void setup ( )
{
Serial.begin( 9600 );
}
void loop ( )
{
b = 3;
Bitwise Operators
The Bitwise operators operate at the binary level. These operators are quite easy to use.
There are various bitwise operators. Some of the popular operators are listed below:
o bitwise NOT ( ~ )
The bitwise NOT operator acts as a complement for reversing the bits.
Array Declaration
There are different methods to declare an array in Arduino, which are listed below:
o We can declare the array without specifying the size.
For example,
int myarray[ ] = { 1, 4, 6, 7 } ;
o We can declare the array without initializing its elements.
For example,
int myarray[ 5];
o We can declare the array by initializing the size and elements.
int myarray[ 8] = { 1, 4, 7, 9, 3, 2 , 4};
Arduino Delay
The delay( ) function pauses the program or task for a specified duration of time. The time is specified
inside the open and closed parentheses in milliseconds.
Where,
1 second = 1000 milliseconds
Example 1:
delay(500)
Here, the specified time is 0.5 seconds.
Arduino If statement
The if ( ) statement is the conditional statement, which is the basis for all types of programming
languages.
If the condition in the code is true, the corresponding task or function is performed accordingly. It
returns one value if the condition in a program is true. It further returns another value if the
condition is false.
It means that if ( ) statement checks for the condition and then executes a statement or a set of
statements.
Example:
if (condition)
{
// statements
}
else
{
//statements
}
Example 1:
int a = 5;
int b= 6;
void setup ( )
{
Serial.begin ( 9600 );
}
void loop ( )
{
if ( a > b )
{
Serial.println ( " a is greater " );
}
else
{
Serial.println ( " a is smaller " );
Example 1:
int a = 0;
do...while
The working of the do-while loop is similar to the while loop. The condition inside the do-while will
execute at least once. It is because the condition is tested at the end of the loop instead of the
beginning.
The syntax is:
do
{
// code or set of statements
} while (condition);
Flowchart
Arduino String
The string is an array of characters or a set of characters that consists of numbers,
spaces, and special characters from the ASCII table.
The string can be declared in the following ways:
o char StrA[10];
o char StringA[8] = {'w', 'e', 'l', 'c', 'o', 'm', 'e'};
We can also add an explicit null character
o char StringB[8] = {'w', 'e', 'l', 'c', 'o', 'm', 'e', '\0'};
We can also declare an array with extra space for a string constant StrA.
o char StrA[10]; = "Hello";
o char StrD[6]; = "Hello";
o char StrD[]; = "Welcome";
The data type is char. Each character is an element in string. For example, Arduino contains seven
characters. So, we need to declare an array atleast of size 8 (1 extra to store the null character at the
end of the string).
Consider the below example:
char myString[len] = "text";
The above declared string will be stored as:
Example:
const int length = 20;
char myString[length] = "Hello Arduino";
// we can also specify as char myString[20] = "Hello Arduino";
void setup()
{
Serial.begin(9600);
}
Core Concepts of IoT By Dr. V. Deepika - 29-
void loop()
{
for( int i=0; i<length; i = i+1)
{
if(myString[i]== '\0')
// it will stop printing when it sees a null character
{
break; //
}
Serial.print(myString[i]);
}
Serial.println();
delay(500);
}
The code will print Hello Arduino multiple times, as shown below:
Output: