Introduction to Node MCU

4,270 views 49 slides May 30, 2020
Slide 1
Slide 1 of 49
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49

About This Presentation

1. Explaining the importance of platform based development
2. Understanding The importance of NodeMCU and demonstrate its interfacing with various devices and sensors.


Slide Content

Presented By Amarjeetsingh Thakur NODEMCU 1

Introduction to IoT Introduction to ESP8266 What is N odeMCU What you can do with it ? NodeMCU development board Pin Description ESP8266 Block diagram Connecting different devices with ESP8266 Introduction to things board IoT platform 2

3

4

We will now have connectivity for anything. From any time, any place connectivity for anyone !!! 5

Various Names, One Concept For over a decade after the introduction of the term Internet-of-Things, different organizations and working groups have been providing various definitions. M2M (Machine to Machine) “Internet of Everything” (Cisco Systems) “World Size Web” (Bruce Schneier) “Skynet” (Terminator movie) Cloud of Things Web of Things 6

7

What is NodeMCU ? The NodeMCU (Node MicroController Unit) is an open source software and hardware development environment that is built around a very inexpensive System-on-a-Chip ( SoC ) called the ESP8266. An Arduino -like device Main component: ESP8266 With programmable pins And built-in wifi Power via USB Low cost 8

What you can do with it ? Program it via C or LUA Access it via wifi (ex. HTTP ) Connect pins to any device ( in or out) 9

Main Component 10

Pin Description 11

Figure : ESP8266EX Block Diagram 12

13

14

The Arduino software consists of a development environment (IDE) and the core libraries. The IDE is written in Java and based on the processing environment. The core libraries are written in C and C++ and compiled using avr -gcc compiler. 15

16

Program Structure Setup( ) { // A function that runs once at the start of a program and is used to set // pinMode or initialize serial communication } loop( ) { // This function includes the code to be executed continuously – reading inputs, //triggering outputs, etc. // This function is the core of all Arduino programs and does the bulk of the //work. } 17

BREAD BOARD 18

19

20

Activity 1.1 Type : Team of 2 Duration : 20 Minutes Single LED blink program using web . Anode of LED Cathode of LED 21

#include <ESP8266WiFi.h> #include < WiFiClient.h > const char* ssid = "XXXX"; //Mention SSID const char* password = "XXXX"; //Mention password int ledPin = 5; //pin D1 of nodemcu WiFiServer server(80); void setup() { Serial.begin (115200); delay(10); 22

pinMode ( ledPin , OUTPUT); digitalWrite ( ledPin , LOW); // Connect to WiFi network Serial.println (); Serial.println (); WiFi.mode (WIFI_AP); /* You can remove the password parameter if you want the AP to be open. */ Serial.print ("Connecting to "); Serial.println ( ssid ); 23

WiFi.begin ( ssid , password); while ( WiFi.status () != WL_CONNECTED) { delay(500); Serial.print ("."); } Serial.println (""); Serial.println (" WiFi connected"); // Start the server server.begin (); Serial.println ("Server started"); 24

// Print the IP address Serial.print ("Use this URL to connect: "); Serial.print ("http://"); Serial.print ( WiFi.localIP ()); Serial.println ("/"); } 25

void loop() { // Check if a client has connected WiFiClient client = server.available (); if (!client) { return; } // Wait until the client sends some data Serial.println ("new client"); while (! client.available ()) { delay(1); } 26

// Read the first line of the request String request = client.readStringUntil ('\r'); Serial.println (request); client.flush (); // Match the request int value = LOW; if ( request.indexOf ("/LED=ON") != -1) { digitalWrite ( ledPin , HIGH); value = HIGH; } if ( request.indexOf ("/LED=OFF") != -1) { digitalWrite ( ledPin , LOW); value = LOW; } 27

// Return the response client.println ("HTTP/1.1 200 OK"); client.println ("Content-Type: text/html"); client.println (""); // do not forget this one client.println ("<!DOCTYPE HTML>"); client.println ("<html>"); client.print ("Led pin is now: "); if (value == HIGH) { client.print ("On"); } else { client.print ("Off"); } client.println ("< br >< br >"); client.println ("<a href =\"/LED=ON\"\"><button>Turn On </button></a>"); client.println ("<a href =\"/LED=OFF\"\"><button>Turn Off </button></a>< br />"); client.println ("</html>"); 28

delay(1); Serial.println ("Client disonnected "); Serial.println (""); } 29

30

Activity 1.2 Type : Team of 2 Duration : 20 Minutes Two LEDs blink program using web 31

#include <ESP8266WiFi.h> #include < WiFiClient.h > const char* ssid = " xxxx "; //Mention SSID const char* password = " xxxx "; //Mention password int ledPin = 5; //pin D2 of nodemcu int ledPin1 = 4; //pin D1 of nodemcu WiFiServer server(80); void setup() { Serial.begin (115200); delay(10); pinMode (ledPin1, OUTPUT); pinMode ( ledPin , OUTPUT); digitalWrite ( ledPin , LOW); 32

// Connect to WiFi network Serial.println (); Serial.println (); WiFi.mode (WIFI_AP); /* You can remove the password parameter if you want the AP to be open. */ Serial.print ("Connecting to "); Serial.println ( ssid ); WiFi.begin ( ssid , password); while ( WiFi.status () != WL_CONNECTED) { delay(500); Serial.print ("."); } Serial.println (""); Serial.println (" WiFi connected"); // Start the server server.begin (); Serial.println ("Server started"); 33

// Print the IP address Serial.print ("Use this URL to connect: "); Serial.print ("http://"); Serial.print ( WiFi.localIP ()); Serial.println ("/"); } void loop() { // Check if a client has connected WiFiClient client = server.available (); 34

if (!client) { return; } // Wait until the client sends some data Serial.println ("new client"); while (! client.available ()) { delay(1); } // Read the first line of the request String request = client.readStringUntil ('\r'); Serial.println (request); client.flush (); 35

// Match the request int value = LOW; if ( request.indexOf ("/LED=ON") != -1) { digitalWrite ( ledPin , HIGH); value = HIGH; } if ( request.indexOf ("/LED=OFF") != -1) { digitalWrite ( ledPin , LOW); value = LOW; } if ( request.indexOf ("/LED1=ON") != -1) { digitalWrite (ledPin1, HIGH); value = HIGH; } if ( request.indexOf ("/LED1=OFF") != -1) { digitalWrite (ledPin1, LOW); value = LOW; } 36

// Return the response client.println ("HTTP/1.1 200 OK"); client.println ("Content-Type: text/html"); client.println (""); // do not forget this one client.println ("<!DOCTYPE HTML>"); client.println ("<html>"); client.print ("Led pin is now: "); if (value == HIGH) { client.print ("On"); } else { client.print ("Off"); } 37

client.println ("< br >< br >"); client.println ("<a href =\"/LED=ON\"\"><button>Turn On </button></a>"); client.println ("<a href =\"/LED=OFF\"\"><button>Turn Off </button></a>< br />"); client.println ("< br >< br >"); client.println ("<a href =\"/LED1=ON\"\"><button>Turn On1 </button></a>"); client.println ("<a href =\"/LED1=OFF\"\"><button>Turn Off1 </button></a>< br />"); client.println ("</html>"); delay(1); Serial.println ("Client disonnected "); Serial.println (""); } 38

Serial communication What is serial communication ? 39

The word  serial  means "one after the other." What is Baud rate ? Number of symbols transferred per sec 40

Serial Display Functions Serial.begin ( baud_rate ) //baud rate(characters per sec) between computer and board is typically 9600 although you can work with other speeds by changing settings of COM Port Serial.print (value), //value could be any data and even string Serial.println (value) //print in new line 41

Eg. Print INDIA on serial monitor void setup( ) { Serial.begin (9600); // 9600 is default baud rate of serial com port of a computer } void loop( ) { Serial.println (“INDIA”); // Send the value “INDIA” } 42

Serial Monitor 43

Activity 1.3 Type : Team of 2 Duration : 20 Minutes Wi-Fi Network Scanning by NodeMCU 44

#include <ESP8266WiFi.h> void setup() { Serial.begin (115200); // Set WiFi to station mode WiFi.mode (WIFI_STA); WiFi.disconnect (); delay(100); Serial.println ("Setup done"); } void loop() { Serial.println ("scan start"); int n = WiFi.scanNetworks (); // WiFi.scanNetworks will return the number of networksfound Serial.println ("scan done"); 45

if (n == 0) Serial.println ("no networks found"); else { Serial.print (n); Serial.println (" networks found"); for ( int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found Serial.print (i + 1); Serial.print (": "); Serial.print ( WiFi.SSID (i)); Serial.print (" ("); Serial.print ( WiFi.RSSI (i)); Serial.print (")"); Serial.println (( WiFi.encryptionType (i) == ENC_TYPE_NONE) ? " " : "*"); delay(10); } 46

} Serial.println (""); delay(5000); } 47

Learning Outcomes At the end of the workshop the student should be able to Explain the importance of platform based development Understand The importance of NodeMCU and demonstrate its interfacing with various devices and sensors. 48

For more information contact: [email protected] linkedin.com/in/amarjeetsingh-thakur-54915955 49