Upload
others
View
9
Download
0
Embed Size (px)
Citation preview
โครงการจดอบรมพฒนาความรทกษะดาน Internet of Things
ไดรบการสนบสนนจาก งบประมาณแผนดน ป 2560ตามแผนบรณาการการพฒนาคนตลอดชวงวย
1
NodeMCU V2
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• ม 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
• ทาการ 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%)
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 InputAnalog Input : อานคาจากตวตานทานปรบคาไดAnalog Pin หรอ ADC Pin ใน สามารถรบ Input ไดเพยงแค 0‐1v เทานน
NodeMCU จะม Pin ทสามารถอานคา Analog ไดเพยง 1 pin คอ A0
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()DescriptionInitializes the WiFi library's network settings and provides the current status.
SyntaxWiFi.begin();WiFi.begin(ssid);WiFi.begin(ssid, pass);
ReturnsWL_CONNECTED when connected to a networkWL_IDLE_STATUS when not connected to a network, but powered on
Connect NodeMCU to Wireless Network
WiFi.status()DescriptionReturn the connection status.
SyntaxWiFi.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 expiresWL_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()DescriptionGets the WiFi shield's IP address
SyntaxWiFi.localIP();
Returnsthe IP address
WiFi.disconnect()DescriptionDisconnects the WiFi from the current network.
SyntaxWiFi.disconnect();
Returnsnothing
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 networkSerial.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() {
}
ตวอยางโปรแกรม
IoT with NodeMCU
• Publish#include <ESP8266WiFi.h>#include <PubSubClient.h>
const char* ssid = “xxxxxx";const char* password = “yyyyyy";const char* mqtt_server = "neutron.it.kmitl.ac.th";
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 outputSerial.begin(115200);setup_wifi();client.setServer(mqtt_server, 1883);
}
IoT with NodeMCU
• Publishvoid setup_wifi() {
delay(10);// We start by connecting to a WiFi networkSerial.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 reconnectedwhile (!client.connected()) {Serial.print("Attempting MQTT connection..."); // Attempt to connectchar clientID[15];String("iot-" + String(random(1000000))).toCharArray(clientID, 15);
//Random Client IDif (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 retryingdelay(1000);
}}
}
IoT with NodeMCU
• Publishvoid 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 = "neutron.it.kmitl.ac.th";
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 networkSerial.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 connectchar 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 retryingdelay(1000);
}}
}void loop() {
if (!client.connected()) {reconnect();
}client.loop();
}
แบบฝกหด
Web Browser
Public Broker
Local Broker
Arduino
Pub/sub
Pub/sub
Bridge
Internet Local
WebSocket
NodeMCU
Pub/sub
แบบฝกหด
‐ ทาการ Bridge ระหวาง Local Broker กบ Public Broker
‐ Arduino ทาการ Publish คาแรงดนทอานได จากการปรบตวตานทานปรบคาได ไปยง Local Broker
‐ NodeMCU ทาการ Subscribe คาทไดรบมา และทาการ หร หลอดไฟ LED ตามคาทไดรบ โดย Subscribe จาก Public Broker
‐ Web Browser แสดงผลกราฟคาแรงดนทรบมาจาก Public Broker ผาน Websocket
ขอขอบคณทกๆ ทาน มากๆ ครบหากมขอสงสยประการใด ตดตอสอบถามไดท