Transcript
Page 1: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

523372_Embedded Systems_4(3-3-9)

Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎและเนอหาทเกยวของ

1.NodeMCU

NodeMCU คออะไร ?

NodeMCU คอ แพลตฟอรมหนงทใชชวยในการสรางโปรเจค Internet of Things (IoT) ทประกอบไป

ดวย Development Kit (ตวบอรด) และ Firmware (Software บนบอรด) ทเปน open source สามารถเขยน

โปรแกรมดวยภาษา Lau ได ท าใหใชงานไดงายขน มาพรอมกบโมดล WiFi (ESP8266) ซงเปนหวใจส าคญในการ

ใชเชอมตอกบอนเตอรเนตนนเอง

ตวโมดลESP8266นนมอยดวยกนหลายรนมาก ตงแตเวอรชนแรกทเปน ESP-01 ไลไปเรอยๆจนปจจบนมถง ESP-

12 แลว และทฝงอยในNodeMCU version แรกนนกเปน ESP-12 แตใน version2 นนจะใชเปน ESP-12E แทน

ซงการใชงานโดยรวมกไมแตกตางกนมากนก NodeMCUนนมลกษณะคลายกบ Arduino ตรงทมพอรต Input

Output built in มาในตว สามารถเขยนโปรแกรมคอนโทรลอปกรณ I/O ไดโดยไมตองผาน อปกรณอนๆ

และเมอไมนานมานกมนกพฒนาทสามารถท าให Arduino IDE ใชงานรวมกบ Node MCU ได จงท าใหใชภาษา

C/C++ ในการเขยนโปรแกรมได ท าใหเราสามารถใชงานมนไดหลากหลายมากยงขน NodeMCUตวนสามารถท า

Page 2: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 2 of 21

อะไรไดหลายอยางมากโดยเฉพาะเรองทเกยวของกบ IoT ไมวาจะเปนการท า Web Server ขนาดเลก การควบคม

การเปดปดไฟผานWiFi และอนๆอกมากมาย

NodeMCU Specification : - ชดพฒนาน based on โมดลWiFi ทชอ ESP8266 - ม GPIO PWM, I2C, 1-Wire และ ADC รวมมาอยบนบอรดเดยว - ม USB-TTL มาในตว ไมตองซอแยกเหมอนกบการใช ESP8266 ปกต ท าใหใชงานไดสะดวกขน - มขา GPIO 10 ขา ทกๆขาสามารถเปน PWM, I2C และ 1-wire ได - ม PCB antenna ส าหรบรบสงสญญาณไรสาย - ใชคอนเนกเตอรแบบ micro-USB ส าหรบจายแรงดนไฟเลยงหรอเทากบ +5V และส าหรบดาวนโหลดเฟรมแวร NodeMCU Specification : - ชดพฒนาน based on โมดลWiFi ทชอ ESP8266 - ม GPIO PWM, I2C, 1-Wire และ ADC รวมมาอยบนบอรดเดยว - ม USB-TTL มาในตว ไมตองซอแยกเหมอนกบการใช ESP8266 ปกต ท าใหใชงานไดสะดวกขน - มขา GPIO 10 ขา ทกๆขาสามารถเปน PWM, I2C และ 1-wire ได - ม PCB antenna ส าหรบรบสงสญญาณไรสาย 2. MAX7219 Module ( 7 Segment 8-Digit )

7 Segment 8-Digit with MAX7219 Module เปนโมดลแสดงผลตวเลขขนาด 8 หลก (8 Digits) โดยม IC เบอร MAX7219 เปนตวควบคมการท างาน เหมาะส าหรบงานทมการแสดงผลดวยตวเลข ใชงานงายสามารถควบคมแตละหลกไดอสระตอกน

Page 3: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 3 of 21

Specification: Power: 5V Size: 82 * 15 * 18mm Color: Red LED

Information Support:

Example Code (http://www.thaieasyelec.com/images/doc/Example%20Code_7_Segment.zip) Library (http://www.thaieasyelec.com/images/doc/LedControl.zip) Datasheet MAX7219 (http://www.thaieasyelec.com/images/doc/Datasheet%20MAX7219-

MAX7221.pdf) Schematic

(http://www.thaieasyelec.com/images/pic_product_info/Schematic_7Segment.jpg)

3. HDC1080 High Accuracy Humidity-Temperature Sensor Module

HDC1080 เปนเซนเซอรวดความชนและเซนเซอรวดอณหภม 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:

Page 4: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 4 of 21

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

ภาษา Lua คออะไร Lua อานวา 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 เปนตน

Page 5: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 5 of 21

สามารถเรยกใชจากภาษาอนได (embeddable) มการน าไปใชในการพฒนาเกมส หรอการพฒนา Mobile Apps อยางเชน Corona SDK

อปกรณเครอขาย อยางเชน Wireless Router ทใชระบบปฏบตการ OpenWrt ซงเปน Linux และมการใชโปรแกรม Luci ซงพฒนาดวยภาษา Lua ส าหรบการจดการผานหนาเวบเพอตงคาตางๆ ของระบบ (Web configuration interface)

การทดลอง ชวงท 1 NodeMCU

1. การ connec NodeMCU ESP8266

แบบ micro-USB ส าหรบจายแรงดนไฟเลยงหรอเทากบ +5V และส าหรบดาวนโหลดเฟรมแวร

- ใสURL >> ลงใน Addition Board Manager URLs:

ดงน http://arduino.esp8266.com/stable/package_esp8266com_index.json

(สามารถดขอมลเพมเตมหากมการเปลยนแปลงได จากhttps://github.com/esp8266/Arduino)

Page 6: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 6 of 21

-แลวกด OK

3.จากนนไปท Menu Tools >> Boar:”xxxxxx” >> Board Manager…

Page 7: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 7 of 21

4.เลอก Type เปน Contributed ไปท ESP8266 และกด Install

5.รอจนตดตงเสรจ

6.เมอตดตง ESP8266 เสรจเรยบรอยแลว ใหปดโปรแกรม Arduino IDE กอน แลวจงเปดขนมาใหม

7.เมอเปดโปรแกรม Arduino IDE ขนมาใหม ใหลองไปท Menu Tools >> Board:”xxxxxx” จะพบวาม

Menu ส าหรบเลอกใชงาน ESP8266 กบ Arduino IDE ขนมาใหเลอกใชงานแลวคะ

Page 8: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 8 of 21

Reference

http://www.thaieasyelec.com

http://www.ayarafun.com

http://www.homeofmaker.com

http://www.sat2you.com

2. หลงจาก connec ไดแลว ทดสอบการท างาน ดวยโปรแกรม File Example Basic

Blink 3. ทดสอบการท างาน ให NodeMCU เปน Access Point

Open File → Example → ESP8266Wifi → WifiAccessPoint แกไขคาชอ AP และ Password

Page 9: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 9 of 21

#include <ESP8266WiFi.h>

#include <WiFiClient.h> #include <ESP8266WebServer.h>

const char *ssid = "ESPap"; // const char *ssid = "testWichai"; const char *password = "thereisnospoon"; // const char *password = "12345678"; ESP8266WebServer server(80); void handleRoot() { server.send(200, "text/html", "<h1>You are connected</h1>"); }

void setup() { delay(1000); Serial.begin(115200); Serial.println(); Serial.print("Configuring access point..."); WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP();

Serial.print("AP IP address: "); Serial.println(myIP); server.on("/", handleRoot); server.begin(); Serial.println("HTTP server started"); } void loop() {

server.handleClient(); }

Upload โปรแกรมไปยง WeMos ดผลการท างานท Serial Monitor(Baud = 115200)

ใชมอถอ หรอ Notebook Connect ไปยง AP ของตวเอง

ใช Chrome เขาไปยง IP ของ Server

4. ทดสอบการท างาน ให NodeMCU เปน Web Server Read this: http://arduino-er.blogspot.com/2016/05/nodemcuesp8266-act-

as-ap-access-point_3.html

Page 10: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 10 of 21

ทดสอบโปรแกรม TestESP8622 – Web Control ใช Chrome เขาไปยง IP ของ Server

5. ทดสอบการท างาน ให NodeMCU เปน Web Client

เปดแชรมอถอตวเองเปน AP Open File → Example → ESP8266Wifi → NTPClient แกไขคาชอ AP และ Password

#include <ESP8266WiFi.h> #include <WiFiUdp.h> char ssid[] = "*************";

char pass[] = "********"; unsigned int localPort = 2390; IPAddress timeServerIP; const char* ntpServerName = "time.nist.gov"; const int NTP_PACKET_SIZE = 48; byte packetBuffer[ NTP_PACKET_SIZE];

WiFiUDP udp; void setup() { Serial.begin(115200); Serial.println(); Serial.println(); Serial.print("Connecting to ");

Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("");

Serial.println("WiFi connected");

Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); udp.begin(localPort); Serial.print("Local port: ");

Serial.println(udp.localPort()); } void loop() { WiFi.hostByName(ntpServerName, timeServerIP); sendNTPpacket(timeServerIP); delay(1000);

Page 11: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 11 of 21

int cb = udp.parsePacket();

if (!cb) { Serial.println("no packet yet");

} else { Serial.print("packet received, length="); Serial.println(cb); udp.read(packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);

unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long secsSince1900 = highWord << 16 | lowWord; Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(secsSince1900); Serial.print("Unix time = "); const unsigned long seventyYears = 2208988800UL; unsigned long epoch = secsSince1900 - seventyYears;

Serial.println(epoch); Serial.print("The UTC time is "); Serial.print((epoch % 86400L) / 3600); Serial.print(':'); if ( ((epoch % 3600) / 60) < 10 ) { Serial.print('0');

} Serial.print((epoch % 3600) / 60); Serial.print(':'); if ( (epoch % 60) < 10 ) { Serial.print('0'); }

Serial.println(epoch % 60); }

delay(10000); } unsigned long sendNTPpacket(IPAddress& address) { Serial.println("sending NTP packet...");

memset(packetBuffer, 0, NTP_PACKET_SIZE); packetBuffer[0] = 0b11100011; packetBuffer[1] = 0; packetBuffer[2] = 6; packetBuffer[3] = 0xEC;

packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52;

udp.beginPacket(address, 123); udp.write(packetBuffer, NTP_PACKET_SIZE);

udp.endPacket(); }

Upload โปรแกรมไปยง NodeMCU ดผลการท างานท Serial Monitor (Baud =

115200)

Page 12: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 12 of 21

ชวงท 2 NodeMCU + MAX7219 Display

1. Add Lib : https://github.com/squix78/MAX7219LedMatrix 2. Wiring MAX7219_7segment {Vcc, GND, DIN, CS, CLK} {3.3V, GND, D7, D8, D5} 3. Open File → Example → MAX7219LedMatrix-master → AnimateText 4. Change { NUMBER_OF_DEVICES 1, CS_PIN D8 } and Click Upload 5. Using Test Code for TestMax7219_1

// TestMax7219_1

// #include <SPI.h>

#include "LedMatrix.h" #define NUMBER_OF_DEVICES 1 #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 }

void loop() { SendData2MAX7129(1, 0xff); delay(500); SendData2MAX7129(1, 0); delay(500); }

//=================================================== // ##SegPosition >> 87654321 ##SegData >> tabcdefg //===================================================

Page 13: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 13 of 21

void SendData2MAX7129(byte SegPosition, byte SegData)

{ digitalWrite(CS_PIN, LOW); SPI.transfer (SegPosition);

SPI.transfer (SegData); digitalWrite (CS_PIN, HIGH); }

6. Using Test Code for TestMax7219_2

// TestMax7219_2 // #include <SPI.h> #include "LedMatrix.h"

#define NUMBER_OF_DEVICES 1

#define CS_PIN D8 //น า D ออก 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

} void loop() { ledMatrix.clear(); ledMatrix.commit(); // commit send buffer to the displays delay(1000);

Send2MAX7129(5, 1, 0);

Send2MAX7129(4, 2, 0); Send2MAX7129(3, 3, 1); Send2MAX7129(2, 4, 0); Send2MAX7129(1, 5, 0); delay(1000); }

//=================================================== //=================================================== 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);

}

Page 14: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 14 of 21

//===================================================

// ##SegPosition >> 87654321 ##SegData >> tabcdefg //=================================================== void SendData2MAX7129(byte SegPosition, byte SegData) { digitalWrite(CS_PIN, LOW); SPI.transfer (SegPosition);

SPI.transfer (SegData); digitalWrite (CS_PIN, HIGH); }

ชวงท 3 NodeMCU + HDC1080 Sensor

1. Add Lib : https://github.com/closedcube/ClosedCube_HDC1080_Arduino 2. ทดสอบโปรแกรม File Example ClosedCube HDC1080 hdc1080demo

ชวงท 4 NodeMCU with Lua Scrip 1. Burn boot loader

การ Build Firmware สามารถท าไดท web support ของ nodeMcu - http://nodemcu-build.com/

Page 15: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 15 of 21

File from NodeMCU Builder float: http://nodemcu-build.com/builds/nodemcu-master-20-modules-2016-04-23-09-16-

22-float.bin integer: http://nodemcu-build.com/builds/nodemcu-master-20-modules-2016-04-23-09-

16-22-integer.bin 3.1.1 Tools: ESP8266Flasher 3.1.2 File from NodeMCU Builder ส าหรบตงคาเปน Boot Loader

float: nodemcu-master-20-modules-2016-04-23-09-16-22-float.bin

Page 16: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 16 of 21

2. Test Code with Lua Tools: ESPlorer File: -,(save to “XXXX.lua” and SEND to ESP for test run) Test LED Blink

Page 17: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 17 of 21

-- Blink using timer alarm -- timerId = 0

dly = 500 ledPin = 1 – 0 LED On NodeMCUV2 Board gpio.mode(ledPin,gpio.OUTPUT) ledState = 0 tmr.alarm( timerId, dly, 1, function() ledState = 1 - ledState;

gpio.write(ledPin, ledState) end)

3. Test Analog Read

-- Blink using timer alarm -- timerId = 0 dly = 500

ledPin = 1 gpio.mode(ledPin,gpio.OUTPUT) ledState = 0

tmr.alarm( timerId, dly, 1, function() ledState = 1 - ledState; gpio.write(ledPin, ledState);

print(adc.read(0)) end)

Page 18: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 18 of 21

3.2 เรมตนกบ LUA Programming Tools: ESPlorer File: -,(save to “init.lua” and SAVE to ESP for power on run)

3.2.1 Test DS1820 with Lua Programming Read this http://nodemcu.readthedocs.org/en/dev/en/modules/ow/

pin = 4 function getTemp() -- 18b20 Example ow.setup(pin) count = 0

repeat count = count + 1 addr = ow.reset_search(pin) addr = ow.search(pin) tmr.wdclr() until (addr ~= nil) or (count > 100)

Page 19: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 19 of 21

if addr == nil then print("No more addresses.")

else --print(addr:byte(1,8)) crc = ow.crc8(string.sub(addr,1,7)) if crc == addr:byte(8) then if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then --print("Device is a DS18S20 family device.")

ow.reset(pin) ow.select(pin, addr) ow.write(pin, 0x44, 1) tmr.delay(10000) present = ow.reset(pin) ow.select(pin, addr) ow.write(pin,0xBE,1)

--print("P = "..present) data = nil data = string.char(ow.read(pin)) for i = 1, 8 do data = data .. string.char(ow.read(pin)) end --print(data:byte(1,9))

crc = ow.crc8(string.sub(data,1,8)) --print("CRC= "..crc) if crc == data:byte(9) then t = ((data:byte(1)+data:byte(2)*256)*625)/10000 print("Temperature = "..t.." Centigrade") end

tmr.wdclr() else

print("Device family is not recognized.") end else print("CRC is not valid!") end

end --print() end -- MAIN tmr.alarm(1, 1000, 1, function() getTemp() end)

Page 20: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 20 of 21

Test DTH22 with Lua Programming Read this http://nodemcu.readthedocs.org/en/dev/en/modules/dht/

pin = 2 function getTemp() -- 18b20 Example status, temp, humi, temp_dec, humi_dec = dht.read(pin) if status == dht.OK then

print("DHT Temperature:"..temp..";".."Humidity:"..humi) elseif status == dht.ERROR_CHECKSUM then print( "DHT Checksum error." ) elseif status == dht.ERROR_TIMEOUT then

print( "DHT timed out." ) end

end -- MAIN tmr.alarm(1, 1000, 1, function() getTemp() end)

Page 21: 523372 Embedded Systems 4(3-3-9) · 523372_Embedded Systems_4(3-3-9) Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors) ทฤษฎีและเนื้อหาที่เกี่ยวข้อง

Page 21 of 21

ค าถามทายการทดลอง 1. ตองการแสดงขอความ

2. ใช Arduino IDE ในการพฒนาโปรแกรมใหอานอณหภมจากเซนเซอรแลวแสดงผลท

MAX7219_7Segment Display โดยมรปแบบดงรป

3. จากการพฒนาโปรแกรมโดยใช Lua Scrip ใหใช NodeMCU อานคาจาก VR น าคาทไดมาแสดงเปน

แรงดนทอานเขามา ดวยทศนยม 6 ต าแหนง Digital Disply = [0.0000 – 3.30000]


Top Related