วันศุกร์ที่ 31 มีนาคม พ.ศ. 2560

ใบงานวันที่31 มีนาคม 2560

ใบงานวันที่31 มีนาคม 2560
รหัสนักศึกษา  B5711826 ชื่อ นางสาวไอลดา พลพฤกษ์

1. ตองการแสดงขอความ

โค้ดในArduinoที่ใช้ในการรัน
//
// TestMax7219_2
//
#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() {
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);
}
}
//===================================================
//===================================================
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);
}
//===================================================
// ##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 โดยมีรูปแบบดังรูป 



โค้ดในArduinoที่ใช้ในการรัน

//
#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);
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");
// Heater off, 14 bit Temperature and Humidity Measurement Resolution
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
}
void loop() {
{
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);
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);

}
}
//===================================================
//===================================================
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);
}
//===================================================
// ##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. จากการพัฒนาโปรแกรมโดยใชLua Scrip ใหใชNodeMCU อานคาจาก VR นําคาที่ไดมาแสดง เปน แรงดันที่อานเขามา ดวยทศนิยม 4 ตําแหนง  Analog Input = [0 – 3.3]  Digital Read = [0 - 1024]  Digital Disply = [0.0000 – 3.300] 

โค้ดที่ใช้รันสำหรับการรัน
--Test Max7219 7seg Lua --
-- MAX7219 registers
MAXREG_DECODEMODE = 0x09
MAXREG_INTENSITY = 0x0a
MAXREG_SCANLIMIT = 0x0b
MAXREG_SHUTDOWN = 0x0c
MAXREG_DISPTEST = 0x0f
DIN = 7 -- 13 - data in pin
CS = 6 -- 12 - load (CS) pin
CLK = 5 -- 14 - clock pin
gpio.mode(DIN,gpio.OUTPUT)
gpio.mode(CS,gpio.OUTPUT)
gpio.mode(CLK,gpio.OUTPUT)
function wrByte(data)
i=8
while (i>0)
do
mask = bit.lshift(0x01,i-1)
--print(mask)
gpio.write( CLK, 0) -- tick
dser = bit.band(data,mask)
if (dser > 0)
then gpio.write(DIN, 1) -- send 1
--print("1")
else gpio.write(DIN, 0) -- send 0
--print("0")
end --endif
--print(dser)
gpio.write( CLK, 1) -- tick
i=i-1
end --while
end
function setReg(reg, value)
gpio.write(CS, 0)
wrByte(reg) -- specify register
tmr.delay(10)
wrByte(value) -- send data
gpio.write(CS, 0)
--tmr.delay(10)
gpio.write(CS, 1)
end
function print_led_int(c)
th = string.format("%d",c / 1000)
h = string.format("%d",(c-th*1000) / 100)
t = string.format("%d", (c-th*1000-h*100) / 10)
u = string.format("%d", c-th*1000-h*100-t*10)
--print(string.format("%d %d %d %d", th,h,t,u))
setReg(4, th)
setReg(3, h)
setReg(2, t)
setReg(1, u)
end
function zero_all()
v=1
while (v<9) do
setReg(v,0)
v=v+1
end
end
setReg(MAXREG_SCANLIMIT, 0x07)
tmr.delay(100)
setReg(MAXREG_DECODEMODE, 0xFF) -- full decode mode BCD
tmr.delay(100)
setReg(MAXREG_SHUTDOWN, 0x01) -- not in shutdown mode
tmr.delay(100)
setReg(MAXREG_DISPTEST, 0x00) -- no display test
tmr.delay(100)
setReg(MAXREG_INTENSITY, 0x08) -- set Brightness
zero_all() -- set all to ZERO
count=0
tmr.alarm(0,1000,1,
function()
count=count+1;
--print(count);
print_led_int(count)
if (count>9999) then count=0;zero_all()
end
end)



4. จากการพัฒนาโปรแกรมโดยใชLua Scrip ใหใชNodeMCU ตอกับ DS1820 แลวอานคาและ แสดงอุณหภูมิ4 ตําแหนง พรอมแสดงออกที่ MAX7219 Display Board  http://www.instructables.com/id/MAX7219-8-Digit-LED-Display-Module-Driver-for-ESP8/  https://frightanic.com/iot/nodemcu-max7219-8x8-led-matrix-display

โค้ดที่ใช้รันสำหรับการรัน

-- MAX7219 registers
MAXREG_DECODEMODE = 0x09
MAXREG_INTENSITY = 0x0a
MAXREG_SCANLIMIT = 0x0b
MAXREG_SHUTDOWN = 0x0c
MAXREG_DISPTEST = 0x0f
DIN = 7 -- 13 - data in pin
CS = 6 -- 12 - load (CS) pin
CLK = 5 -- 14 - clock pin
gpio.mode(DIN,gpio.OUTPUT)
gpio.mode(CS,gpio.OUTPUT)
gpio.mode(CLK,gpio.OUTPUT)
function wrByte(data)
i=8
while (i>0)
do
mask = bit.lshift(0x01,i-1)
--print(mask)
gpio.write( CLK, 0) -- tick
dser = bit.band(data,mask)
if (dser > 0)
then gpio.write(DIN, 1) -- send 1
--print("1")
else gpio.write(DIN, 0) -- send 0
--print("0")
end --endif
--print(dser)
gpio.write( CLK, 1) -- tick
i=i-1
end --while
end
function fillBlank(data)
i=8
while (i>0)
do
gpio.write( CLK, 0) -- tick
gpio.write(DIN, 1) -- send 0
gpio.write( CLK, 1) -- tick
i=i-1
end --while
end
function setReg(reg, value)
gpio.write(CS, 0)
wrByte(reg) -- specify register
tmr.delay(10)
wrByte(value) -- send data
gpio.write(CS, 0)
tmr.delay(10)
gpio.write(CS, 1)
end
function setBlank(reg, value)
gpio.write(CS, 0)
wrByte(reg) -- specify register
tmr.delay(10)
fillBlank(value) -- send data
gpio.write(CS, 0)
tmr.delay(10)
gpio.write(CS, 1)
end
function print_led_int(c)
c = c*100
th = string.format("%d",c / 1000)
h = string.format("%d",(c-th*1000) / 100)
t = string.format("%d", (c-th*1000-h*100) / 10)
u = string.format("%d", c-th*1000-h*100-t*10)
--print(string.format("%d %d %d %d", th,h,t,u))
setBlank(4, 0)
setBlank(3, 0)
setBlank(2, 0)
setBlank(1, 0)
setReg(8, th+0x80)
setReg(7, h)
setReg(6, t)
setReg(5, u)
end
function zero_all()
v=1
while (v<9) do
setReg(v,0)
v=v+1
end
end
setReg(MAXREG_SCANLIMIT, 0x07)
tmr.delay(100)
setReg(MAXREG_DECODEMODE, 0xFF) -- full decode mode BCD
tmr.delay(100)
setReg(MAXREG_SHUTDOWN, 0x01) -- not in shutdown mode
tmr.delay(100)
setReg(MAXREG_DISPTEST, 0x00) -- no display test
tmr.delay(100)
setReg(MAXREG_INTENSITY, 0x08) -- set Brightness
zero_all() -- set all to ZERO
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)
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_led_int(t)
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)





ข้อ5 จากการทดลองที่ 2 Step 2/3 ใหทดสอบตามตัวอยาง แลวปรับแกใหมีLED ตัว ชื่อ  LED_1, LED_2, LED_3, LED_4





WEEK 02 Arduino IoTs – MQTT and Anto Interface


Arduino IoTs – MQTT and Anto Interface


1. IoT and MQTT 
Message Queuing Telemetry Transport (MQTT) เป็นProtocol ที่ออกแบบมาเพื่อการเชื่อมต่อแบบ M2M (machine-to-machine) คืออุปกรณ์กับอุปกรณ์ สนับสนุนเทคโนโลยีiot (Internet of Things) คือเทคโนโลยีที่อินเทอร์เน็ตเชื่อมต่อกับอุปกรณ์ต่าง ๆ เช่น โทรศัพท์มือถือ รถยนต์ โทรทัศน์ ตู้เย็น เข้ากับอินเทอร์เน็ตทำให้สามารถเชื่อมโยงสื่อสารกับอุปกรณ์ต่าง ๆ ได้ โดยผ่านเครือข่ายอินเทอร์เน็ต ซึ่งจะทำให้มนุษย์สามารถ ควบคุมอุปกรณ์ต่าง ๆ จากที่อื่นได้ เช่นการสั่งปิดเปิดไฟในบ้านจากที่อื่น ๆ
เนื่องจาก โปรโตคอลตัวนี้มีน้ำหนักเบา ออกแบบมาเพื่อ ใช้งานกับอุปกรณ์อิเล็กทรอนิกส์ขนาดเล็ก การรับส่งข้อมูลในเครื่อข่ายที่มีขนาดเล็ก แบนร์วิธต่ำ ใช้หลักการแบบ  publisher / subscriber คล้ายกับหลักการที่ใช้ใน  Web  Service ที่ต้องใช้ Web Server เป็นตัวกลางระหว่างคอมพิวเตอร์ของผู้ใช้ แต่ MQTT จะใช้ตัวกลางที่เรียกว่า Brokerเพื่อทำหน้าที่ จัดการคิว รับ - ส่ง ข้อมูลระหว่างอุปกรณ์ และทั้งในส่วนที่เป็น Publisher และSubscriber ปัจจุบันเทคโนโลยีที่กำลังมาแรงสำหรับนักพัฒนาด้าน Embedded
IoT มันคืออะไร พอค้นดูมีหลายลิงค์อธิบายไว้มากมาย เช่น Internet of Things เมื่อคอมพิวเตอร์เริ่มคุยกันเองได้ , โลกแห่ง IoT มาถึงแล้ว IoT เทคโนโลยีที่ธุรกิจต้องรู้ลองนึกภาพดูครับว่าถ้าหากอุปกรณ์สามารถสั่งงานไปมาหากันได้ผ่าน www ไม่ว่าจะเป็น PC, Smart Phone หรือแม้แต่อุปกรณ์ขนาดเล็กพวก Micro-Controller, PLC, HUB, Switchหรืออะไรก็แล้วแต่ที่มันสามารถต่อระบบ Network ไม่ว่ามันจะอยู่ที่บ้าน ที่โรงงาน ไร่ นา ฟาร์มโรงเรือน โรงงานอุตสาหกรรมหรือที่อื่นๆที่มีระบบเน็ตเวอร์ที่เข้าถึง www ได้เราจะสามารถควบคุมมันได้ทั้งหมดที่ไหนก็ได้ในโลกใบนี้ IoT ทำมีวิธีการทำงานอย่างไร องค์ประกอบหลักของ IoT จะมี ส่วนคือ Broker, Publisher และ Subscriber. ซึ่งการรับและส่งข้อมูลนั้นมันจะส่งข้อมูลไปมาหากันนั้นจะส่งผ่านตัวกลางนั้นก็คือ Broker Server โดยตัวส่งนี้จะเรียกว่า Publisher ส่งข้อมูลขึ้นไปยัง Broker พร้อมระบุหัวข้อ (Topic)ที่ต้องการส่งข้อออกไป จากนั้นตัวรับซึ่งเรียกว่า Subscriber ถ้าหากตัวรับต้องการรับข้อมูลจากตัวส่งจะต้องทำการ Subscribe หัวข้อ Topic ของ Publisher นั้นๆ ผ่าน Broker เช่นกัน


IoT
จากรูปภาพด้านบนจะมีตัว Publisher ทำการ Public ข้อความ “Hello” ใน Topic Device1 เมื่อและถ้าหากมีคอมพิวเตอร์ หรืออุปกรณ์อื่นๆทำการ Subscribe หัวข้อ Topic Device1 เมื่อ Publisher ทำการส่งข้อมูลไปยัง Topic อุปกรณ์ Subscribe จะได้ข้อความ“Hello” เช่นเดียวกัน. ก็คล้ายๆกับที่ใช้งานไลน์ที่คุยกันเป็นกลุ่มนั้นเลยครับ. ซึ่งจะเห็นข้อความ“Hello” ในเวลาเดียวกันนั้นหมายความว่าอุปกรณ์ใดๆที่ทำการ Subscribe Topicเดียวกันก็จะได้ข้อความเดียวกันครับ
โปรโตคอลที่ใช้สำหรับรับและส่งข้อมูลนั้นคือ MQTT ปัจจุบันถึง Version 3. 1 ในที่นี้จะมาทำการทดลองส่งข้อมูลกันตัว Server จะมีอยู่ด้วยกันหลายค่ายครับสำหรับที่ลิสมาด้านล่างนี้

Open Source MQTT Broker Server
·        Mosquitto
·        RSMB
·        ActiveMQ
·        Apollo
·        Moquette
·        Mosca
·        RabbitMQ

MQTT Client
·        Paho
·        Xenqtt
·        mqtt.js
·        node_mqtt_client
·        Ascoltatori
·        Arduino MQTT Client

สำหรับ MQTT Broker Server ฟรีที่พอค้นได้ก็มีดังนี้
·        test.mosquitto.org
·        broker.mqttdashboard.com

วันพฤหัสบดีที่ 30 มีนาคม พ.ศ. 2560

NodeMCU with Lua Scrip

4. NodeMCU with Lua Scrip
3.1   Burn boot loader
3.1.1  การ ฺBuild Firmware สามารถทำได้ที่ web support ของ nodeMcu - http://nodemcu-build.com/



3.1.2  File from NodeMCU Builder
3.1.3  Tools: ESP8266Flasher
3.1.4  File from NodeMCU Builder สำหรับตั้งค่าเป็น Boot Loader
·        float: nodemcu-master-20-modules-2016-04-23-09-16-22-float.bin






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

    
-- 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)
end)












วันศุกร์ที่ 24 มีนาคม พ.ศ. 2560

Week01: Arduino - Basic I/O (Digital Input/Output, Shields, Sensors)

ทฤษฏี เนื้อหาที่เกี่ยวข้อง
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 UART Bridge
·         ใช้ 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.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.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.2   ทดสอบโปรแกรม File à Example à ClosedCube HDC1080 à hdc1080demo



คำถามท้ายการทดลอง

1.  ต้องการแสดงข้อความ

           ตัวอย่างCode ในการรัน
//
// TestMax7219_2
//
#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() {
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);
}
}
//===================================================
//===================================================
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);
}
//===================================================
// ##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"
ClosedCube_HDC1080 hdc1080;
#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(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);
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
}
void loop() {
{
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);
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);

}
}
//===================================================
//===================================================
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);
}
//===================================================
// ##SegPosition >> 87654321 ##SegData >> tabcdefg
//===================================================
void SendData2MAX7129(byte SegPosition, byte SegData)
{ digitalWrite(CS_PIN, LOW);
SPI.transfer (SegPosition);
SPI.transfer (SegData);
digitalWrite (CS_PIN, HIGH);
}