ทฤษฏี เนื้อหาที่เกี่ยวข้อง
1. ESP8266 on NodeMCU
Deaware เอา NodeMCU devkit มาให้ลองเล่น บอร์ดตัวนี้เป็นบอร์ดที่รวมเอา ESP8266
(ESP-12) + USB to Serial + NodeMCU firmware เข้าไว้ด้วยกัน ทำให้การใช้งานง่ายมากขึ้น ไม่ต้องมีอุปกรณ์ต่อพ่วงเยอะ
แถมมี GPIO เพิ่มเป็น 10 พอร์ท เพียงพอต่อการ
ผู้พัฒนา Board ก็มี NodeMCU Firmware ให้คุณสามารถเขียน Lua ใส่ลงไปได้
ทำให้การใช้งานง่ายมากขึ้น
ตัว devkit มี port
GPIO มาให้อยู่ 10 port เป็นแบบ Digital
และที่สำคัญมี Wifi Serial มาให้ด้วย
เพราะฉนั้นขอให้มี Wifi Router ต่ออินเตอร์เน็ตได้
คุณก็สามารถสร้างโครงงาน Internet of Thing ได้ง่ายๆ
จบในบอร์ดเดียว
ปัจจุบันสามารถเขียน Software ด้วย Arduino
IDE ยิ่งทำให้ผู้พัฒนาทุกคนเริ่มจะสนใจเป็นอีกหลายเท่าตัว โดย I/O
ของบอร์ด มีดังนี้
·
GPIO
13 ขา
·
ADC
(10 bit) 1 ขา
·
WiFi
a/b/g
· การสื่อสารแบบ Serial (Tx,Rx)
NodeMCU V0.9
ESP-12
Module
|
NodeMCU V1.0
ESP-12E
Module
|
NodeMCU V3.0
ESP-12E
Module
|
Arduino
IDE = Node0.9
|
Arduino
IDE = Node1.0
|
Arduino
IDE = Node1.0
|
·
USB-SERIAL
CH340
·
ใช้
Serial LED ที่ GPIO1
ได้ แต่ต้องไม่ใช้พร้อม Serial Communication
·
มี
LED Buid in ที่ GPIO16“BUILTIN_LED”
|
·
Silicon
Labs CP210x USB to
·
ใช้
Serial LED ที่ GPIO2
ได้ แต่ต้องไม่ใช้พร้อม Serial Communication
·
มี
LED Buid in ที่ GPIO16“BUILTIN_LED”
|
·
USB-SERIAL
CH340
·
ใช้
Serial LED ที่ GPIO2
ได้ แต่ต้องไม่ใช้พร้อม Serial Communication
|
Online
Support
·
NodeMCU
GitHub: https:// github.com/nodemcu
·
Firmware
Build and Example: http://
nodemcu-build.com/
2. MAX7219 Display
7 Segment 8-Digit with MAX7219 Module เป็นโมดูลแสดงผลตัวเลขขนาด
8 หลัก (8 Digits) โดยมี IC เบอร์ MAX7219 เป็นตัวควบคุมการทำงาน
เหมาะสำหรับงานที่มีการแสดงผลด้วยตัวเลข
ใช้งานง่ายสามารถควบคุมแต่ละหลักได้อิสระต่อกัน
The MAX7219 and MAX7221 Led
drivers
These
two integrated circuits from Maxim are for driving either 64 individual Led's,
or up to 8 digits of 7-segment displays. The drivers implement a SPI compatible
slave interface that can be controlled from the Arduino using only 3 of the
digital output pins. An extensive datasheet for the IC's is available from the
Maxim homepage. Since both chips are very similar, I will use the term MAX72XX
for both the MAX7221 and the MAX7219.
LedControl Library
LedControl is a Arduino library for the MAX7221 and
MAX7219. These two chips provide an easy way to control
either an array of 64 LEDs or up to 8 digits
of 7-segment displays. The fourth argument to LedControl(dataPin,clockPin,csPin,numDevices)
is the number of cascaded MAX72XX devices you're using with this LedControl.
The library can address up to 8 devices from a single LedControl-variable.
Wiring the Arduino
The
Gnd-Pins of the MAX72XX have to be connected to one of the Gnd-Pins on the
Arduino board. The positive power-supply pins (+5V/Vcc) can be connected to +5V-Pins
on Arduino-board.The three signal lines (DIn,CLK,Load(/CS)) have to be
connected to three digital outputs on the Arduino board. It depends on the
software which Arduino pins have to be used.
If you
read the datasheet for the MAX72XX you know that the drivers can be cascaded by
simply connecting the signal DOut from one chip to DIn on the next chip. The
signals Clk and Load(/CS) have to be connected in parallel to each MAX72XX.
There is no strict limit as to how many drivers can be cascaded that way. But
the SPI-interface is not capable of any error checking on the transmitted data,
so you are already limited with the length of the cables that run from one
MAX72XX to the next one. If your cables get longer than 10cm between each
MAX72XX you might already run into trouble.
3. HDC1080 High Accuracy Humidity-Temperature Sensor Module
Description
The
HDC1080 is a digital humidity sensor with integrated temperature sensor that
provides excellent measurement accuracy at very low power. The HDC1080 operates
over a wide supply range, and is a low cost, low power alternative to competitive
solution in a wide range of common applications. The humidity and temperature
sensors are factory calibrated.
Features
·
Relative
Humidity Accuracy ±2% (typical)
·
Temperature
Accuracy ±0.2°C (typical)
·
Excellent
Stability at High Humidity
·
14
Bit Measurement Resolution
·
100
nA Sleep Mode Current
·
Average
Supply Current:
o
710
nA @ 1sps, 11 bit RH Measurement
o
1.3
µA @ 1sps, 11 bit RH and Temperature Measurement
·
Supply
Voltage 2.7 V to 5.5 V
·
Small
3 mm × 3 mm Device Footprint
·
I2C
Interface
4. Lua Scrip
ภาษา Lua
(pronounced 'LOO-ah' หรือออกเสียงว่า 'ลัว'
หรือ 'ลูอ่า' ในภาษาโปรตุเกส)
อาจไม่เป็นที่รู้จักสำหรับโปรแกรมเมอร์ทั่วไปในวงกว้าง
แต่ก็เป็นภาษาคอมพิวเตอร์ที่น่าสนใจ และสามารถนำมาใช้งานได้
ลองมาทำความรู้จักกับภาษานี้ โดยเขียนสรุปเป็นข้อๆ ดังนี้
|
·
Lua
เป็นภาษาประเภท Scripting / Interpreted Language หรือภาษาสคริปต์ (อยู่ในประเภทเดียวกับภาษาอย่างเช่น Python)
·
มีต้นกำเนิดมาจากทีมพัฒนาในสถาบันการศึกษาแห่งหนึ่งในประเทศบราซิล
(Pontifical
Catholic University of Rio de Janeiro, Brazil) ในราวปีค.ศ. 1993
·
Lua
script จะถูกแปลงให้เป็น Bytecode ตามรูปแบบคำสั่งของคอมพิวเตอร์เสมือน
(Virtual Machine) สำหรับภาษานี้ (เช่นเดียวกับกรณีของ Python
และ Java ที่มีการทำงานในลักษณะนี้)
·
เป็นภาษาที่ชนิดของข้อมูลสำหรับตัวแปรปรับเปลี่ยนได้ (Dynamically-typed language)
·
มีชนิดข้อมูลพื้นฐานคือ boolean (true,false), numbers (integer &
double-precision floating point) และ string
·
มีโครงสร้างข้อมูลอย่างเช่น อาร์เรย์(array) ตาราง(table)
ซึ่งเป็น associative array และ เซต(set)
·
มีการจัดการหน่วยความจำ เช่น
การจองและคืนหน่วยความจำในขณะทำงาน (automatic memory management & garbage collection)
·
ตัวแปลคำสั่งของภาษา Lua (Lua Interpreter) มีขนาดเล็ก(small
& light-weight) และเขียนด้วยภาษา C
·
เป็นซอฟต์แวร์ประเภท Opensource (MIT license สำหรับเวอร์ชัน 5.x)
ดังนั้นจึงใช้งานได้ฟรี
·
ใช้ได้หลายแพลตฟอร์ม(multi-platform programming language) อย่างเช่น
Windows, Linux, Mac OS X
·
มีการใช้งานกับบอร์ดไมโครโทรลเลอร์ได้ อย่างเช่น ESP8266/NodeMCU, WiFiMCU/EMW3165 เป็นต้น
·
สามารถเรียกใช้จากภาษาอื่นได้ (embeddable) มีการนำไปใช้ในการพัฒนาเกมส์
หรือการพัฒนา Mobile Apps อย่างเช่น Corona SDK
·
อุปกรณ์เครือข่าย อย่างเช่น Wireless Router ที่ใช้ระบบปฏิบัติการ
OpenWrt ซึ่งเป็น Linux และมีการใช้โปรแกรม
Luci ซึ่งพัฒนาด้วยภาษา Lua สำหรับการจัดการผ่านหน้าเว็บเพื่อตั้งค่าต่างๆ
ของระบบ (Web configuration interface)
การทดลอง
1. NodeMCU
1.1
Add
NodeMCU Board to Arduino IDE 1.8.x (ตาม http://www.homeofmaker.com/?p=1023 )
1.2
สำคัญคือใส่ URL >> ลงใน Addition Board
Manager URLs: ดังนี้ http://arduino.esp8266.com/stable/package_esp8266com_index.json
1.3
ทดสอบการทำงาน ด้วยโปรแกรม File à Example à Basic à Blink
1.4
ทดสอบการทำงาน ให้ NodeMCU เป็น Access Point
1.4.1 Open File → Example → ESP8266Wifi → WifiAccessPoint
1.4.2 แก้ไขค่าชื่อ AP และ Password
§
const char *ssid = "testWichai";
§
const char *password = "12345678";
1.4.3 Upload โปรแกรมไปยัง
WeMos ดูผลการทำงานที่ Serial Monitor(Baud = 115200)
1.4.4 ใช้มือถือ หรือ Notebook Connect ไปยัง
AP ของตัวเอง
1.4.5 ใช้ Chrome เข้าไปยัง IP
ของ Server
1.5
ทดสอบการทำงาน ให้ NodeMCU เป็น Web Server
1.5.1 Read this: http://arduino-er.blogspot.com/2016/05/nodemcuesp8266-act-as-ap-access-point_3.html
1.5.2 ทดสอบโปรแกรม TestESP8622 – Web Control
1.5.3 ใช้ Chrome เข้าไปยัง IP
ของ Server
1.6
ทดสอบการทำงาน ให้ NodeMCU เป็น Web Client
1.6.1 เปิดแชร์มือถือตัวเองเป็น
AP
1.6.2 Open File → Example → ESP8266Wifi → NTPClient
1.6.3 แก้ไขค่าชื่อ AP และ Password
§
char ssid[] = "*************"; // your network SSID (name)
§
char pass[] = "********"; // your
network password
1.6.4 Upload โปรแกรมไปยัง
NodeMCU ดูผลการทำงานที่ Serial Monitor (Baud = 115200)
2. NodeMCU + MAX7219 Display
2.1
Add
Lib : https://github.com/squix78/MAX7219LedMatrix
2.2
Wiring
MAX7219_7segment {Vcc, GND, DIN, CS, CLK} à {3.3V, GND, D7, D8, D5}
2.3
Open
File → Example → MAX7219LedMatrix-master → AnimateText
2.4
Change
{ NUMBER_OF_DEVICES 1, CS_PIN D8 } and Click Upload
2.5
Using
Test Code for TestMax7219_1
2.6
Using
Test Code for TestMax7219_2
3. NodeMCU + HDC1080 Sensor
3.1
Add Lib : https://github.com/closedcube/ClosedCube_HDC1080_Arduino
3.2
ทดสอบโปรแกรม File à Example à ClosedCube HDC1080 à hdc1080demo
คำถามท้ายการทดลอง
1. ต้องการแสดงข้อความ
|
ตัวอย่างCode ในการรัน
#define NUMBER_OF_DEVICES 1
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
void setup() {
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(4); // range is 0-15
ledMatrix.clear();
ledMatrix.commit(); // commit send buffer to the displays
}
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(4); // range is 0-15
ledMatrix.clear();
ledMatrix.commit(); // commit send buffer to the displays
}
void loop() {
for (int i = 9999; i >= 0; i -= 7)
{ // ledMatrix.clear();
// ledMatrix.commit(); // commit send buffer to the displays
// delay(1000);
// Send2MAX7129(8, 1, 0);
// Send2MAX7129(7, 1, 0);
// Send2MAX7129(6, 2, 0);
// Send2MAX7129(5, 2, 0);
int xxx = i;
Send2MAX7129(4, xxx / 1000, 0); xxx = xxx % 1000;
Send2MAX7129(3, xxx / 100, 0); xxx = xxx % 100;
Send2MAX7129(2, xxx / 10, 0); xxx = xxx % 10;
Send2MAX7129(1, xxx / 1, 0);
delay(10);
}
}
{ // ledMatrix.clear();
// ledMatrix.commit(); // commit send buffer to the displays
// delay(1000);
// Send2MAX7129(8, 1, 0);
// Send2MAX7129(7, 1, 0);
// Send2MAX7129(6, 2, 0);
// Send2MAX7129(5, 2, 0);
int xxx = i;
Send2MAX7129(4, xxx / 1000, 0); xxx = xxx % 1000;
Send2MAX7129(3, xxx / 100, 0); xxx = xxx % 100;
Send2MAX7129(2, xxx / 10, 0); xxx = xxx % 10;
Send2MAX7129(1, xxx / 1, 0);
delay(10);
}
}
//===================================================
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] =
{ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111
};
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] =
{ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111
};
Value = charTable[Value];
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
//===================================================
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
2. ใช้ Arduino IDE ในการพัฒนาโปรแกรมให้อ่านอุณหภูมิจากเซ็นเซอร์แล้วแสดงผลที่
MAX7219_7Segment Display โดยมีรูปแบบดังรูป
วิํธีทำ
>>code ที่ใช้ในการรันข้อ2<<
}
}
// TestMax7219_2
//
#include <SPI.h>
#include "LedMatrix.h"
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
//
#include <SPI.h>
#include "LedMatrix.h"
#include <Wire.h>
#include "ClosedCube_HDC1080.h"
ClosedCube_HDC1080 hdc1080;
#define NUMBER_OF_DEVICES 1
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
#define CS_PIN D8
LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
void setup() {
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(8); // range is 0-15
ledMatrix.clear();
ledMatrix.commit(); // commit send buffer to the displays
Serial.begin(9600);
Serial.println("ClosedCube HDC1080 Arduino Test");
Serial.begin(115200); // For debugging output
ledMatrix.init();
ledMatrix.setIntensity(8); // range is 0-15
ledMatrix.clear();
ledMatrix.commit(); // commit send buffer to the displays
Serial.begin(9600);
Serial.println("ClosedCube HDC1080 Arduino Test");
// Heater off, 14 bit Temperature and Humidity Measurement Resolution
hdc1080.begin(0x40);
hdc1080.begin(0x40);
Serial.print("Manufacturer ID=0x");
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
Serial.print("Device ID=0x");
Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device
}
void loop() {
{
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
delay(300);
Serial.print("T=");
Serial.print(hdc1080.readTemperature());
Serial.print("C, RH=");
Serial.print(hdc1080.readHumidity());
Serial.println("%");
delay(300);
int xx = (int)(hdc1080.readTemperature() * 10);
Send2MAX7129(8,xx/100, 0); xx = xx %100;
Send2MAX7129(7,xx/10, 1); xx = xx %10;
Send2MAX7129(6,xx, 0);
Send2MAX7129(5,16, 0);
Send2MAX7129(8,xx/100, 0); xx = xx %100;
Send2MAX7129(7,xx/10, 1); xx = xx %10;
Send2MAX7129(6,xx, 0);
Send2MAX7129(5,16, 0);
int yy = (int)(hdc1080.readTemperature() * 10);
Send2MAX7129(4,yy/100, 0); yy = yy % 100;
Send2MAX7129(3,yy/10, 1); yy = yy %10;
Send2MAX7129(2,yy, 0);
Send2MAX7129(1,17, 0);
delay(300);
Send2MAX7129(4,yy/100, 0); yy = yy % 100;
Send2MAX7129(3,yy/10, 1); yy = yy %10;
Send2MAX7129(2,yy, 0);
Send2MAX7129(1,17, 0);
delay(300);
}
}
//===================================================
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] =
{ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111,
B00001101, B00010111
//===================================================
void Send2MAX7129(byte SegPosition, byte Value, bool dotDigit)
{ const static byte charTable [] =
{ B01111110, B00110000, B01101101, B01111001,
B00110011, B01011011, B01011111, B01110000,
B01111111, B01111011, B01110111, B00011111,
B00001101, B00111101, B01001111, B01000111,
B00001101, B00010111
}; //
Value = charTable[Value];
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
if (dotDigit == 1) Value |= 0x80;
digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (Value);
digitalWrite (CS_PIN, HIGH);
Serial.print(Value);
}
//===================================================
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น