วันศุกร์ที่ 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