Professional Documents
Culture Documents
Gvhd: Ths Võ Thiện Lĩnh - Bm Điện -Điện Tử Utc2
Gvhd: Ths Võ Thiện Lĩnh - Bm Điện -Điện Tử Utc2
Gvhd: Ths Võ Thiện Lĩnh - Bm Điện -Điện Tử Utc2
Cộng 12 0 0 0 24 60
I. CÁC TOÁN TỬ
STT TOÁN TỬ Ý NGHĨA
1 = Toán tử gán
2 + cộ ng
3 - trừ
4 * nhân
5 / chia
6 % lấy phần dư (trong phép chia)
7 & AND Logical AND
8 | OR Logical OR
! NOT
&& AND
|| OR
Ví dụ:
a. for
b. while
– Kit Arduino Wifi ESP8266 WeMos D1 R2 là phiên bản mới nhất từ WeMos, kit được
thiết kế với hình dáng tương tự Arduino Uno nhưng trung tâm lại là module wifi Soc
ESP8266 được build lại firmware để có thể chạy với chương trình Arduino.
– Kit Arduino Wifi ESP8266 WeMos D1 R2 thích hợp và dễ dàng thực hiện các ứng
dụng thu thập dữ liệu và điều khiển qua Wifi.
Có 2 loại board trên thị trường tương ứng với thứ tự chân theo bảng dưới:
RX RX<-D0 D0 3
TX TX->D1 D1 1
D0 D2 D2 16
SCL/D1 D15/SCL/D3 D3 5
SDA/D2 D14/SDA/D4 D4 4
D3 D8 D5 0=GPIO0
D4 TX1/D9 D6 2
D5 D13/SCK/D5 D7 14
D6 D12/MISO/D6 D8 12
D7 D11/MOSI/D7 D9 13
A0 A0 A0 A0
Chân của ESP8266 cũng là chân của phần mềm lập trình
Thông số kỹ thuật
• IC wifi ESP-8266EX
• Điện áp hoạt động: 3.3V
• Số chân I/O Digital: 11
• Số chân Analog: 1 chân (Max input: 3.2V)
• Xung clock: 80MHz/160MHz
• Bộ nhớ Flash: 4 MB
Warnings: All IO is work at 3.3V.
Các thiết bị dựa trên nền tảng Arduino được lập trình bằng ngôn riêng. Ngôn ngữ này
dựa trên ngôn ngữ Wiring được viết cho phần cứng nói chung. Và Wiring lại là một biến
thể của C/C++. Môi trường lập trình Arduino được gọi là Arduino IDE
(Intergrated Development Environment).
Programming:Arduino IDE
http://arduino.esp8266.com/stable/package_esp8266com_index.json
Giống tập lệnh của Arduino UNO.(Xem trang arduino.cc hoặc arduino.vn)
• Socket Server nằm ở tầng cao nhất gọi là tầng Server.(nội bộ có thể là
modem=192.168.1.1)
• ESP8266 và Trình duyệt Web nằm ở tầng thứ 2 gọi là tầng Client
• Arduino ở tầng thứ 3 gọi là Application.
• Mỗi tầng chỉ có thể liên lạc với tầng kế tiếp hoặc bên dưới nó.
Như vậy, chỉ cần xây dựng các phần mô hình giao tiếp ổn định giữa các tầng là chúng ta
đã có ngay mô hình IoT đơn giản.
Để giao tiếp giữa tầng 1 và tầng 2, chúng ta sử dụng kết nối Internet thông qua các gói
dữ liệu được đóng gói theo chuẩn JSON.
JSON là một kiểu dữ liệu đặc biệt, nó sẽ giúp ta đóng gói tập hợp các dữ liệu thành một
chuỗi đơn giản theo quy chuẩn nhất định. Chuỗi này sẽ dễ dàng được vận chuyển thông
qua môi trường Internet.
JSON là kiểu dữ liệu chuẩn, dễ encode và decode nhất, hầu hết các thiết bị và nền tảng
lập trình đều hỗ trợ kiểu dữ liệu này.
Để giao tiếp giữa tầng 2 và tầng 3, chúng ta sử dụng cổng Serial truyền thống mà các
bạn đã có thể làm quen thông qua việc làm xe điều khiển từ xa thông qua bluetooth.Nói
chung Serial cũng đã là một giao tiếp chuẩn và tất cả các mạch ESP8266 và Arduino đều
hỗ trợ ít nhất một cổng Serial trong hệ thống IO của mình.
#include <ESP8266WiFi.h>
const char* ssid = "Thien Linh";
const char* password = "987654321";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
//Bật baudrate ở mức 115200 để giao tiếp với máy tính qua Serial
Serial.begin(115200);
delay(10);
Serial.println();
- Nếu WeMos D1 được kết nối với server thì gửi từ WeMos D1 lên server một tin nhắn
:" Hello IoT Class"
Chúng ta sẽ bật/tắt một Led trên chân GPIO16thông qua một trình duyệt web.
1. Firstly initialize the digital port GPIO16 as an output port and keep the initial state of
the LED ON.
2. At the bottom of setup() function add the following lines of code:
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
At the bottom of loop() function add the following lines of code:
//Read what the browser has sent into a String class and print the request to the monitor
// 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);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();//xóa các giá trị củabiến clientclass
Copy this code and add it at the bottom of the Loop() function:
- Tạo đoạn code .htmltrên WeMos D1 và gửi đoạn code này đến Web server.
- Mục đích là tạo ra 2 nút Button trên web để điều khiển Led, 2 Button này có chức
năng kích hoạt (onclick) và gửi phản hồi về WeMos D1 dưới dạng chuỗi ký tự: “ON” ,
“OFF”.
- WeMos D1 nhận được chuỗi ký tự phản hồi sẽ thực hiện công việc tương ứng như
đoạn code đã nêu ở trên phần B là:
Ví dụ 1:
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/0 will set the GPIO2 low,
* http://server_ip/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
A simple web server that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser
Circuit:
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiServer server(80);
void setup() {
if (WiFi.status() == WL_NO_SHIELD) {
if (fv != "1.1.0") {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
delay(10000);
void loop() {
String currentLine = ""; // make a String to hold incoming data from the
client
// if the current line is blank, you got two newline characters in a row.
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("Content-type:text/html");
client.println();
client.println();
break;
currentLine = "";
} else if (c != '\r') { // if you got anything else but a carriage return character,
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
if (currentLine.endsWith("GET /L")) {
client.stop();
Serial.println("client disonnected");
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
Serial.println(ip);
Serial.print(rssi);
Serial.println(" dBm");
Serial.println(ip);
}
D. BLINK A LED WITH BLYNK APP (WEMOS D1)
https://www.blynk.cc/getting-started/
https://www.instructables.com/id/Blink-a-LED-With-Blynk-App-Wemos-D1-Mini-Pro/
• Blynk_Release_v0.5.3.zip
NOTE: Don’t share your Auth Token with anyone, unless you want someone to have
access to your hardware
BlynkTimer timer;
// V1 LED Widget is blinking
void blinkLedWidget()
{d8=digitalRead(0);// D8 = led đơn
if(d8==0) {
led1.off();
Serial.println("LED on V1: off");
} else {
led1.on();
Serial.println("LED on V1: on");
}
}
voidsetup()
{
// Debug console
}
Bước 5:Run The Project
Ví dụ 2:PWM với Led chân D8: sử dụng Widget = Slider tạo PWM điều khiển trực tiếp
trên D8, hoặc Gauge.
}
}
else
{
digitalWrite(LED, LOW);
}
GVHD: ThS Võ Thiện Lĩnh – BM ĐIỆN –ĐIỆN TỬ UTC2 Page 48
}
Code Button-Blynk:
/* Comment this out to disable prints and save space */
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SPI.h>
#define BLYNK_PRINT Serial
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " f8781ee65b46434b99e4f4bbe87a3d55";
char ssid[] = "Thien Linh";
char pass[] = "987654321";
int nutnhan;
BlynkTimer timer;
WidgetLED led1(V1);
void myTimerEvent()
{
nutnhan = digitalRead(13); // D7
if (nutnhan==0)
{//Blynk.virtualWrite(V1,0);
led1.on();
}
else
{//Blynk.virtualWrite(V1,255);
led1.off();
}
}
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, myTimerEvent);
}
void loop()
{
Blynk.run();
timer.run();
}
https://www.hackster.io/Matei_Tudose/how-to-read-temperature-and-humidity-on-
blynk-with-dht11-f5fbae
voidsetup()
{
Serial.begin(9600);// Debug console
Blynk.begin(auth,ssid,pass);
dht.setup(14, DHTesp::DHT11); // Connect DHT sensor=GPIO17
timer.setInterval(2000,sendUptime);
}
voidsendUptime()
{ float h = dht.getHumidity();//đọc giá trị nhiệt độ
float t = dht.getTemperature();//đọc giá trị độ ẩm
voidloop()
{
Blynk.run();
timer.run();
}
Phần cứng:
WeMos D1:
1 VCC +3.3V
GVHD: ThS Võ Thiện Lĩnh – BM ĐIỆN –ĐIỆN TỬ UTC2 Page 51
2 DATA D5
3 GND GND
-Servo SG90:
Phần cứng:
Kết nối dây màu đỏ với 5V, dây màu nâu với mass, dây màu cam với chân phát
xung D4của Wemos D1. Ở chân xung cấp một xung từ 1ms-2ms để điều khiển góc quay
theo ý muốn.
Phần mềm:
- Chọn thang đo (0:180) tương ứng với góc quay của Servo.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <Servo.h> // Thư viện điều khiển servo
#define BLYNK_PRINT Serial
char auth[] = " f8781ee65b46434b99e4f4bbe87a3d55";
void setup()
{
myservo1.attach(4);// ngo ra dieu khien servo 1
Serial.begin(9600);
GVHD: ThS Võ Thiện Lĩnh – BM ĐIỆN –ĐIỆN TỬ UTC2 Page 53
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
timer.run();
}
-Giao tiếp động cơ bước 28BYJ-48:
L298N:
void setup()
pinMode(ENA,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin, LOW);
//reverse(step,delay);
reverse(80,20);
//forward(step,delay);
forward(80,20);
void loop()
digitalWrite(ENA, HIGH);
digitalWrite(ENB, HIGH);
while (1) {
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(j);
i--;
if (i < 1) break;
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
i--;
if (i < 1) break;
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
delay(j);
i--;
if (i < 1) break;
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(j);
i--;
if (i < 1) break;
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
} // end reverse()
digitalWrite(ENA, HIGH);
while (1) {
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(j);
i--;
if (i < 1) break;
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 0);
digitalWrite(IN4, 1);
delay(j);
i--;
if (i < 1) break;
digitalWrite(IN1, 1);
digitalWrite(IN2, 0);
digitalWrite(IN3, 1);
digitalWrite(IN4, 0);
delay(j);
i--;
if (i < 1) break;
digitalWrite(IN1, 0);
digitalWrite(IN2, 1);
digitalWrite(IN4, 0);
delay(j);
i--;
if (i < 1) break;
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
} // end forward()
A. Phần cứng:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#define BLYNK_PRINT Serial
char auth[] = " f8781ee65b46434b99e4f4bbe87a3d55";
char ssid[] = "Thien Linh";
char pass[] = "987654321";
int nutnhan=1;
int val;
int sensorPin = A0;
BlynkTimer timer;
WidgetLED led1(V1);
WidgetLED led2(V2);
void myTimerEvent()
{ blinkLedWidget();
val = analogRead(sensorPin)*100/1024;// max=1024, chuyen sang thang do 100
Blynk.virtualWrite(V0,val);//bien ao V0 là giá trị chuyen doi từ cam bien
nutnhan = digitalRead(13);//D7=GPIO13
if (nutnhan==0)
{//Blynk.virtualWrite(V1,0);
GVHD: ThS Võ Thiện Lĩnh – BM ĐIỆN –ĐIỆN TỬ UTC2 Page 61
led2.on();
Blynk.notify("CAM BIEN 1");
}
else
{//Blynk.virtualWrite(V1,255);
led2.off();
}
}
void blinkLedWidget()
{
if (led1.getValue()) {
led1.off();
Serial.println("LED on V1: off");
} else {
led1.on();
Serial.println("LED on V1: on");
}
}
/*void emailOnButtonPress()
{
// *** WARNING: You are limited to send ONLY ONE E-MAIL PER 5 SECONDS!
***
// Let's send an e-mail when you press the button
// connected to digital pin 2 on your Arduino
int isButtonPressed = !digitalRead(2); // Invert state, since button is "Active LOW"
if (isButtonPressed) // You can write any condition to trigger e-mail sending
{
Blynk.email("vothien22@gmail.com", "Subject: Canh bao tu Blynk ", "hello");
// Or, if you want to use the email specified in the App (like for App Export):
//Blynk.email("Subject: Button Logger", "You just pushed the button...");
}
}*/
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, myTimerEvent);
//Blynk.email("vothien22@gmail.com", "Subject", "My Blynk project is online.");
pinMode(13,INPUT);//D7
// Attach pin 2 interrupt to our handler
void loop()
{
Blynk.run();
timer.run();
}
• http://www.wemos.cc/tutorial/getstartedin_arduino.html
• http://forum.wemos.cc/topic/102/wemos-d1-r2-not-see-usb-ports-on-mac/2
• http://www.instructables.com/id/Programming-the-WeMos-Using-Arduino-SoftwareIDE/?ALLSTEPS
• http://forum.wemos.cc/topic/102/wemos-d1-r2-not-see-usb-ports-on-mac/2
• https://www.instructables.com/id/Programming-a-HTTP-Server-on-ESP-8266-12E/
• https://www.hackster.io/blynk/products/blynk
Xử lý lỗi Uploading:
Initial Checks
In order to troubleshoot “espcomm_sync failed” error, please proceed step by step through
the checklist below. This list is organized starting with most common and simple to more
complex issues.
2. If a module is connected to the serial port but not responding as a valid ESP8266 device, the
message will read slightly different (see below). If you have other modules connected to your PC,
make sure that you are uploading code to ESP8266 and not to e.g. Arduino UNO.
3. To have your PC talking to ESP, select exact ESP type in upload menu. If selection is incorrect
then the upload may fail.
4. Upload may be also failing due to too high speed. If you have long or poor quality USB cable,
try reducing selection under Upload Speed.