The document provides an overview of NodeMCU, an open-source development environment based on the ESP8266 microcontroller, enabling low-cost, Wi-Fi capable projects. It includes programming instructions using C or Lua, along with practical activities for interfacing with LEDs and serial communication. Additionally, it emphasizes the importance of platform-based development and outlines learning outcomes for participants.
We will nowhave connectivity for anything. From
any time, any place connectivity for anyone!!!
5
5.
Various Names, OneConcept
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
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
8.
What you cando with it?
Program it via C or LUA
Access it via wifi (ex. HTTP)
Connect pins to any device
(in or out)
9
Programming an Arduino
•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
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
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
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
24.
// Print theIP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
25
25.
void loop()
{
// Checkif 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
26.
// Read thefirst 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
27.
// Return theresponse
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
// Connect toWiFi 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
33.
// Print theIP 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
34.
if (!client)
{
return;
}
// Waituntil 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
35.
// Match therequest
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
36.
// Return theresponse
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
The word serialmeans "one after the other."
What is Baud rate ?
Number of symbols transferred per sec
40
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
41.
Eg. Print INDIAon 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
Activity 1.3
Type :Team of 2 Duration : 20 Minutes
Wi-Fi Network Scanning by NodeMCU
44
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
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
Learning Outcomes
At theend of the workshop the student should be able to
1. Explain the importance of platform based development
2. Understand The importance of NodeMCU and demonstrate
its interfacing with various devices and sensors.
48
48.
For more informationcontact:
amarjeetsinght@gmail.com
linkedin.com/in/amarjeetsingh-thakur-54915955
49