nodemcuv2panwit/iot-61-002.pdf• an open-source firmware and development kit that helps you to...
TRANSCRIPT
โครงการจดอบรมพฒนาความรทกษะดานInternet of Things
ไดรบการสนบสนนจากงบประมาณแผนดน ป 2561ตามแผนบรณาการการพฒนาคนตลอดชวงวย
NodeMCU V2
ความเปนมาของ ESP8266
ความเปนมาของ ESP8266
ความเปนมาของ ESP8266
....
Development Board
Development Board
Development Board
What is NodeMCU
• An open-source firmware and development kit that helps you to prototype your IOT product within a few Lua script lines.
Features
• Open-source, Interactive, Programmable, Low cost, Simple, Smart, WI-FI enabled
NodeMCU Specification
• based on โมดลWiFi ทชอ ESP8266 เบอร ESP-12E
• ม GPIO, PWM, I2C, 1-Wire และ ADC รวมอยบนบอรดเดยว• ม USB-TTL มาในตว ใช IC CP2102 ของบรษท Slabs
• ม PCB antenna สาหรบรบสงสญญาณไรสาย• ใช micro-USB สาหรบจายไฟเล )ยงและสาหรบดาวนโหลดเฟรมแวร• มขา A0 รบอนพตแรงดนแบบ analog ( ADC ขนาด 10 บต) ผานวงจรแบงแรงดนดวยตวตานทาน
100k / 220k (ลดแรงดนอนพตจาก 0V – 3.3V ลงมา ใหอยในชวง 0V - 1V)
• มวงจรควบคมแรงดน 3.3V บนบอรด
NodeMCU V2 pinout
การตดต �ง Board NodeMCU ใหกบ Arduino IDE
• เปดโปรแกรม Arduino IDE
• ไปท เมน File เลอก Preference จะแสดงหนาตาดงรป
การตดต �ง Board NodeMCU ใหกบ Arduino IDE
• พมพ http://arduino.esp8266.com/stable/package_esp8266com_index.json
ลงในชอง Additional Boards Manager URLs แลวกด OK
• จากน )นไปท เมน Tools เลอก Board แลวเลอก Boards Manager
การตดต �ง Board NodeMCU ใหกบ Arduino IDE
• จะปรากฏหนาตางดงรป
การตดต �ง Board NodeMCU ใหกบ Arduino IDE
• ใหพมพคาวา esp8266 ในชอง filter your search
• จากน )นกด ป ม Install
การตดต �ง Board NodeMCU ใหกบ Arduino IDE
• เมอตดต )งเสรจแลว จะปรากฏบอรด nodeMCU ในรายการ Boards
Generic ESP8266 Module
บอรด ESP8266 ทใชงานท )วไป หรอ บอรดทสรางข )นเอง
ESPresso Lite 1.0 และ 2.0
บอรด ESP8266 ของ Espert และ Chiangmai
Maker Club
Olimex MOD-WIFI-ESP8266(-DEV)
บอรด ESP8366 ทบรษท Olimex เปนผสราง
NodeMCU (ESP8266 ESP-12 Module)
บอรด ESP8266 ทเปนบอรด NodeMCU
WeMos D1
บอรด ESP8266 ทเปนบอรด wemos
การตดต �ง Driver ของ USB-TTL
• Download Driver ไดท
http://www.it.kmitl.ac.th/~panwit/CP210x_Windows_Drivers.zip
หรอhttps://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
• ทาการ Unzip และตดต )ง driver
Basic Output
void setup() {
pinMode(D1,OUTPUT);
}
void loop()
{
digitalWrite(D1,LOW);
delay(500);
digitalWrite(D1,HIGH);
delay(500);
}
int val;
void setup()
{
pinMode(D1,OUTPUT);
}
void loop()
{
for(val=0;val<1000;val+=50)
{
analogWrite(D1,val);
delay(100);
}
}
Note!! คา Duty Cycle
ไดในชวง 0..1023 (0% ถง 100%)หรอ คา val สามารถมคาไดต )งแต 0-1023
Digital Output : Blink Analog Output : fading
Basic Input
Digital Input : อานคาจาก Switch
void setup() {
pinMode(D0,OUTPUT);
pinMode(D1,INPUT);
}
void loop()
{
if(!digitalRead(D1))
digitalWrite(D0,HIGH);
else
digitalWrite(D0,LOW);
}
2204.7k
Switch
3.3V
LEDD1 D0
NodeMCU
Basic Input
Pull Up or Pull Down
Input Pull Up
pinMode(button,INPUT_PULLUP)pinMode(button,INPUT)
Basic Input
Analog Input : อานคาจากตวตานทานปรบคาได
Analog Pin หรอ ADC Pin ใน สามารถรบ Input
ไดเพ ยงแค 0- 3.3v เทาน �น
NodeMCU จะม Pin ทสามารถอานคา Analog ไดเพยง 1 pin คอ A0 อานคาไดออกมา 0-1023 (10 bit )
const int AnalogIn = A0;
int val = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(AnalogIn);
Serial.println(val);
}
ตวอยางโปรแกรม
Connect NodeMCU to Wireless Network
WiFi.begin()
Description
Initializes the WiFi library's network settings and provides the
current status.
Syntax
WiFi.begin();
WiFi.begin(ssid);
WiFi.begin(ssid, pass);
Returns
WL_CONNECTED when connected to a network
WL_IDLE_STATUS when not connected to a network, but
powered on
Connect NodeMCU to Wireless Network
WiFi.status()
Description
Return the connection status.
Syntax
WiFi.status();
ReturnsWL_CONNECTED: assigned when connected to a WiFi network;
WL_IDLE_STATUS: it is a temporary status assigned when WiFi.begin() is
called and remains active until the number of attempts expires
WL_NO_SSID_AVAIL: assigned when no SSID are available;
WL_CONNECT_FAILED: assigned when the connection fails for all the attempts;
WL_CONNECTION_LOST: assigned when the connection is lost;
WL_DISCONNECTED: assigned when disconnected from a network;
Connect NodeMCU to Wireless Network
WiFi.localIP()
Description
Gets the WiFi shield's IP address
Syntax
WiFi.localIP();
Returns
the IP address
WiFi.disconnect()
Description
Disconnects the WiFi from the
current network.
Syntax
WiFi.disconnect();
Returns
nothing
Connect NodeMCU to Wireless Network
#include <ESP8266WiFi.h>
const char* ssid = “SSID_name";
const char* password = “PASSWORD";
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
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");
// Print the IP address
Serial.print("IP=");
Serial.println(WiFi.localIP());
}
void loop() {
}
ตวอยางโปรแกรม
IFTTT (IF THIS THEN THAT)
• IFTTT เปนเวบทจะทางานเมอมเงอนไขทกาหนดไว – ให copy รปลง dropbox เมอ มการ tag
– ใหปดเสยงโทรศพท เมอ มาถงททางาน
– เปด wifi โทรศพท เมอ ถงบาน
– แชรรปบน Facebook เมอ โพสต Instragram
25
ไปท Website https://ifttt.com/
การใชงาน IFTTT
ทาการสมคร กด Sign Up
การใชงาน IFTTT
ไปท My Applets
เลอก New Applet
การใชงาน IFTTT
การใชงาน IFTTT
กดป ม + ตรงหลงคาวา if
การใชงาน IFTTT
กดป ม + ตรงหลงคาวา if
การใชงาน IFTTT
Search Service ดวยคาวา Webhooks และกดเลอก
การใชงาน IFTTT
กดเลอก Receive a Web Request จะปรากฏจอใหต )งชอ Trigger
ใหทาการต )งชอ Trigger ในทน )ต )งคาวา Alert
การใชงาน IFTTT
จากน )นจะปรากฏหนาตาง ดงรป ใหกด + หลงคาวา then
การใชงาน IFTTT
ใหทาการเลอก Service ในทน )เลอกโปรแกรม Line
การใชงาน IFTTT
ระบบจะเชอมตอกบโปรแกรม Line ใหใส username password เพอเชอมตอ
การใชงาน IFTTT
จากน )นเลอก Action ในทน )คอการ Send Message
กดเลอก Send Message และไปกาหนดรปแบบขอความทจะสง
การใชงาน IFTTT
จะปรากฏหนาจอ Review and Finish และเมอกด Finish จะปรากฎวา Event Alert ทาการ On
การใชงาน IFTTT
ให Click ทสญลกษณของ Webhook แลวเลอก Document จะปรากฏหนาจอ
ซงคาทตองนาไปใชคอ Key กบชอ Service
ตวอยางโปรแกรมบน NodeMCU เพ %อใชงาน IFTTT
#include <ESP8266WiFi.h>
const char* ssid = "AndroidAP";
const char* password = "tyid3180";
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
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");
// Print the IP address
Serial.print("IP=");
Serial.println(WiFi.localIP());
}
void loop() {
Line_Notify(1234);
delay(10000);
}
ตวอยางโปรแกรมบน NodeMCU เพ %อใชงาน IFTTT
const char* host = "maker.ifttt.com";
const char *privateKey = "cltguEH14kgGRHGAtuu1RS"; // แกใหตรงกบท สมครไว
const char *event = "Alert1"; // แกใหตรงกบท สมครไว
void Line_Notify(int msg) {
WiFiClientSecure client;
const int httpPort = 443;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/";
url += event;
url += "/with/key/";
url += privateKey;
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + msg + "\r\n");
ตวอยางโปรแกรมบน NodeMCU เพ %อใชงาน IFTTT
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
} else {
delay(50);
};
}
}
Blynk
รายละเอ ยด http://www.blynk.cc
ข �นตอนการใชงาน Blynk
Download Library ไดจาก http://www.blynk.cc/getting-started/
ข �นตอนการใชงาน Blynk
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>#include <BlynkSimpleEsp8266.h>
char auth[] = "YourAuthToken";char ssid[] = "YourNetworkName";char pass[] = "YourPassword";
void setup()
{Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{Blynk.run();
}
เขยน Code และ Upload และทดลอง RUN
ข �นตอนการใชงาน Blynk
ข �นตอนการใชงาน Blynk
1. ตอหลอด LED ทขา D0 หรอขา GPIO16
2. เปด Application
แบบฝกหด
ทดลองสราง Application
- ควบคม ปดเปด LED ไดหลายๆ ดวง- ตอตวตานทานปรบคาได ทบอรด NodeMCU เขาทขา A0 และสราง
Application ทแสดงกราฟแรงดนทเปลยนแปลงได
IoT with NodeMCU
• Publish#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = “xxxxxx";
const char* password = “yyyyyy";
const char* mqtt_server = " broker.mqttdashboard.com ";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char outTopic[] = "iot/outTopic";
char buffer_str[40];
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
IoT with NodeMCU
• Publishvoid setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
IoT with NodeMCU
• Publishvoid reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection..."); // Attempt to connect
char clientID[15];
String("iot-" + String(random(1000000))).toCharArray(clientID, 15);
//Random Client ID
if (client.connect(clientID)) {
Serial.println("Successfully connected with MQTT");
Serial.print("Client: ");
Serial.println(clientID);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying
delay(1000);
}
}
}
IoT with NodeMCU
• Publish
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
String outPayload = "Hello, I'm Arduino.";
outPayload.toCharArray(buffer_str, 40);
client.publish(outTopic, buffer_str);
delay(10000);
}
Download Code ไดท %
http://www.it.kmitl.ac.th/~panwit/nodeMCU_code.zip
IoT with NodeMCU
• Subscribe#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = “xxxxxx";
const char* password = “yyyyyy";
const char* mqtt_server = " broker.mqttdashboard.com";
WiFiClient espClient;
PubSubClient client(espClient);
char inTopic[] = "iot/inTopic"; //*****
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback); //*****
}
IoT with NodeMCU
• Subscribevoid setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
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");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
IoT with NodeMCU
• Subscribe
void callback(char* topic, byte* payload, unsigned int length) {
String payload_str = "";
String topic_str = String(topic);
for (int i = 0; i < length; i++) {
payload_str += (char)payload[i];
}
Serial.print("Message arrived [");
Serial.print(topic_str);
Serial.print("] ");
Serial.println(payload_str);
}
IoT with NodeMCU
• Subscribevoid reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection..."); // Attempt to connect
char clientID[15];
String("iot-" + String(random(1000000))).toCharArray(clientID, 15);
if (client.connect(clientID)) {
Serial.println("Successfully connected with MQTT");
Serial.print("Client: ");
Serial.println(clientID);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying
delay(1000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
Internet
Public Broker
แบบฝกหด
ปรบตวตานทานปรบคาได ท NodeMCU
ไปหรไฟ ท Arduino