<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Yongyut Srisuban on Medium]]></title>
        <description><![CDATA[Stories by Yongyut Srisuban on Medium]]></description>
        <link>https://medium.com/@yongyutsrisuban?source=rss-b6a98fef471b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BTh-PWSCCCnGBo0E</url>
            <title>Stories by Yongyut Srisuban on Medium</title>
            <link>https://medium.com/@yongyutsrisuban?source=rss-b6a98fef471b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 23 May 2026 16:03:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yongyutsrisuban/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Mission 2 >> Notebook or PC Lab Moving 8 LED with Switch Control]]></title>
            <link>https://medium.com/@yongyutsrisuban/mission-2-notebook-or-pc-lab-moving-8-led-with-switch-control-c6349b7b86df?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6349b7b86df</guid>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 14:35:35 GMT</pubDate>
            <atom:updated>2019-06-27T15:28:35.631Z</atom:updated>
            <content:encoded><![CDATA[<h3>4. Mojo V3 — Display 7_Segment and UART Tx</h3><h3><strong>Week09 — Display 7_Segment and UART Tx</strong></h3><p><strong>Lab05 — MAX7219 Display</strong></p><h3>สำหรับศึกษาในหัวข้อต่างๆ</h3><p><a href="https://medium.com/@yongyutsrisuban/embedded-system-267ceee1665e">1. Install Raspberry Pi</a><br><a href="https://medium.com/@yongyutsrisuban/2-rpi-iots-166cb44a12bd">2. RPi — IoTs</a><br><a href="https://medium.com/@yongyutsrisuban/3-start-fpga-cc92f1807b7">3. Start FPGA</a><br><a href="https://medium.com/@yongyutsrisuban/mission-2-notebook-or-pc-lab-moving-8-led-with-switch-control-c6349b7b86df">4. Mojo V3 — Display 7_Segment and UART Tx</a><br><a href="https://medium.com/@yongyutsrisuban/1-install-raspberry-pi-684a25323b6e">5.FPGA — Timing Coding</a></p><p>Step 1: ทำการสร้างโปรเจค New Project &gt; Lab05</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*nYq-1KEiE2hBYXEs69QhYg.png" /></figure><p>Step 2: New Source &gt; VHDL Module &gt; ตั้งชื่อ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*gVauPzqcSW0dLoS0Miz2ew.png" /></figure><p>Step 3: พิมพ์คำสั่งดังนี้</p><pre><strong>-- ###########################################################</strong></pre><pre><strong>-- Driver for MAX7219 with 8 digit 7-segment display</strong></pre><pre><strong>-- http://stevenmerrifield.com/max7219/M7219.vhdl</strong></pre><pre><strong>-- sjm 15 May 2017</strong></pre><pre><strong>-- ###########################################################</strong></pre><pre><strong>library IEEE;</strong></pre><pre><strong>use IEEE.STD_LOGIC_1164.ALL;</strong></pre><pre><strong>entity drv_MAX7219 is</strong></pre><pre><strong>port (</strong></pre><pre><strong>clk : in std_logic;</strong></pre><pre><strong>parallel : in std_logic_vector(31 downto 0);</strong></pre><pre><strong>clk_out : out std_logic;</strong></pre><pre><strong>data_out : out std_logic;</strong></pre><pre><strong>load : out std_logic</strong></pre><pre><strong>);</strong></pre><pre><strong>end drv_MAX7219;</strong></pre><pre><strong>-- ###########################################################</strong></pre><pre><strong>architecture Behavioral of drv_MAX7219 is</strong></pre><pre><strong>attribute syn_encoding : string;</strong></pre><pre><strong>type state_machine is (init_1, init_2, init_3, init_4, read_data, dig_7, dig_6, dig_5,</strong></pre><pre><strong>dig_4, dig_3, dig_2, dig_1, dig_0);</strong></pre><pre><strong>attribute syn_encoding of state_machine : type is &quot;safe&quot;;</strong></pre><pre><strong>signal state : state_machine := init_1;</strong></pre><pre><strong>type driver_machine is (idle, start, clk_data, clk_high, clk_low, finished);</strong></pre><pre><strong>attribute syn_encoding of driver_machine : type is &quot;safe&quot;;</strong></pre><pre><strong>signal driver_state : driver_machine := idle;</strong></pre><pre><strong>signal command : std_logic_vector(15 downto 0) := x&quot;0000&quot;;</strong></pre><pre><strong>signal driver_start : std_logic := &#39;0&#39;;</strong></pre><pre><strong>-- -----------------------------------------------------------</strong></pre><pre><strong>function hex2seg(num : std_logic_vector(3 downto 0)) return std_logic_vector is</strong></pre><pre><strong>begin</strong></pre><pre><strong>case num is</strong></pre><pre><strong>when &quot;0000&quot; =&gt; return(&quot;01111110&quot;); -- 0</strong></pre><pre><strong>when &quot;0001&quot; =&gt; return(&quot;00110000&quot;); -- 1</strong></pre><pre><strong>when &quot;0010&quot; =&gt; return(&quot;01101101&quot;); -- 2</strong></pre><pre><strong>when &quot;0011&quot; =&gt; return(&quot;01111001&quot;); -- 3</strong></pre><pre><strong>when &quot;0100&quot; =&gt; return(&quot;00110011&quot;); -- 4</strong></pre><pre><strong>when &quot;0101&quot; =&gt; return(&quot;01011011&quot;); -- 5</strong></pre><pre><strong>when &quot;0110&quot; =&gt; return(&quot;01011111&quot;); -- 6</strong></pre><pre><strong>when &quot;0111&quot; =&gt; return(&quot;01110000&quot;); -- 7</strong></pre><pre><strong>when &quot;1000&quot; =&gt; return(&quot;01111111&quot;); -- 8</strong></pre><pre><strong>when &quot;1001&quot; =&gt; return(&quot;01111011&quot;); -- 9</strong></pre><pre><strong>when &quot;1010&quot; =&gt; return(&quot;01110111&quot;); -- A</strong></pre><pre><strong>when &quot;1011&quot; =&gt; return(&quot;00011111&quot;); -- b</strong></pre><pre><strong>when &quot;1100&quot; =&gt; return(&quot;00001101&quot;); -- c</strong></pre><pre><strong>when &quot;1101&quot; =&gt; return(&quot;00111101&quot;); -- d</strong></pre><pre><strong>when &quot;1110&quot; =&gt; return(&quot;01001111&quot;); --when &quot;1111&quot; =&gt; return(&quot;01000111&quot;); -- F</strong></pre><pre><strong>when others =&gt; return(&quot;00000000&quot;);</strong></pre><pre><strong>end case;</strong></pre><pre><strong>end hex2seg;</strong></pre><pre><strong>-- -----------------------------------------------------------</strong></pre><pre><strong>-- ###########################################################</strong></pre><pre><strong>begin -- of Behavioral</strong></pre><pre><strong>process</strong></pre><pre><strong>variable counter : integer := 0;</strong></pre><pre><strong>variable clk_counter : integer := 0;</strong></pre><pre><strong>variable latch_in : std_logic_vector(31 downto 0) := x&quot;00000000&quot;;</strong></pre><pre><strong>variable dig0_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig1_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig2_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig3_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig4_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig5_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig6_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>variable dig7_data : std_logic_vector(7 downto 0) := x&quot;00&quot;;</strong></pre><pre><strong>-- ===========================================================</strong></pre><pre><strong>begin -- of process</strong></pre><pre><strong>-- -----------------------------------------------------------</strong></pre><pre><strong>wait until rising_edge(clk);</strong></pre><pre><strong>-- -----------------------------------------------------------</strong></pre><pre><strong>case state is</strong></pre><pre><strong>when init_1 =&gt;</strong></pre><pre><strong>if (driver_state = idle) then</strong></pre><pre><strong>command &lt;= x&quot;0c01&quot;; -- shutdown / normal operation</strong></pre><pre><strong>driver_state &lt;= start;</strong></pre><pre><strong>state &lt;= init_2;</strong></pre><pre><strong>end if;</strong></pre><pre><strong>when dig_2 =&gt;</strong></pre><pre><strong>if (driver_state = idle) then</strong></pre><pre><strong>command &lt;= x&quot;03&quot; &amp; dig2_data;</strong></pre><pre><strong>driver_state &lt;= start;</strong></pre><pre><strong>state &lt;= dig_1;</strong></pre><pre><strong>end if;</strong></pre><pre><strong>when dig_1 =&gt;</strong></pre><pre><strong>if (driver_state = idle) then</strong></pre><pre><strong>command &lt;= x&quot;02&quot; &amp; dig1_data;</strong></pre><pre><strong>driver_state &lt;= start;</strong></pre><pre><strong>state &lt;= dig_0;</strong></pre><pre><strong>end if;</strong></pre><pre><strong>when dig_0 =&gt;</strong></pre><pre><strong>if (driver_state = idle) then</strong></pre><pre><strong>command &lt;= x&quot;01&quot; &amp; dig0_data;</strong></pre><pre><strong>driver_state &lt;= start;</strong></pre><pre><strong>state &lt;= read_data;</strong></pre><pre><strong>end if;</strong></pre><pre><strong>when others =&gt; null;</strong></pre><pre><strong>end case;</strong></pre><pre><strong>-- -----------------------------------------------------------</strong></pre><pre><strong>if (clk_counter &lt; 100) then</strong></pre><pre><strong>clk_counter := clk_counter + 1;</strong></pre><pre><strong>else</strong></pre><pre><strong>clk_counter := 0;</strong></pre><pre><strong>case driver_state is</strong></pre><pre><strong>E</strong></pre><pre><strong>when idle =&gt;</strong></pre><pre><strong>load &lt;= &#39;1&#39;;</strong></pre><pre><strong>clk_out &lt;= &#39;0&#39;;</strong></pre><pre><strong>when start =&gt;</strong></pre><pre><strong>load &lt;= &#39;0&#39;;</strong></pre><pre><strong>counter := 16;</strong></pre><pre><strong>driver_state &lt;= clk_data;</strong></pre><pre><strong>when clk_data =&gt;</strong></pre><pre><strong>counter := counter - 1;</strong></pre><pre><strong>data_out &lt;= command(counter);</strong></pre><pre><strong>driver_state &lt;= clk_high;</strong></pre><pre><strong>when clk_high =&gt;</strong></pre><pre><strong>clk_out &lt;= &#39;1&#39;;</strong></pre><pre><strong>driver_state &lt;= clk_low;</strong></pre><pre><strong>when clk_low =&gt;</strong></pre><pre><strong>clk_out &lt;= &#39;0&#39;;</strong></pre><pre><strong>if (counter = 0) then</strong></pre><pre><strong>load &lt;= &#39;1&#39;;</strong></pre><pre><strong>driver_state &lt;= finished;</strong></pre><pre><strong>else</strong></pre><pre><strong>driver_state &lt;= clk_data;</strong></pre><pre><strong>end if;</strong></pre><pre><strong>when finished =&gt;</strong></pre><pre><strong>driver_state &lt;= idle;</strong></pre><pre><strong>when others =&gt; null;</strong></pre><pre><strong>end case;</strong></pre><pre><strong>end if; -- clk_counter</strong></pre><pre><strong>-- -----------------------------------------------------------</strong></pre><pre><strong>end process;</strong></pre><pre><strong>-- ===========================================================</strong></pre><pre><strong>end Behavioral;</strong></pre><pre><strong>-- #######################################################</strong></pre><p>Step 4: ทำการ Create Schematic Symbol</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*3Mt5DCJ7sZiE-QYjBwQb2g.png" /></figure><p>Step 5: New Source &gt; Verilog Module &gt; ตั้งชื่อ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*oKc63i8FN6TrkiY3k8aeyg.png" /></figure><p>Step 6: พิมพ์คำสั่งดังนี้</p><pre><strong>`timescale 1ns / 1ps</strong></pre><pre><strong>module Verilog(input Clk_50MHz, rstCount, output [31:0] oData);</strong></pre><pre><strong>reg [31:0] cCounter;</strong></pre><pre><strong>reg [31:0] roData;</strong></pre><pre><strong>always@(posedge Clk_50MHz or negedge rstCount) begin</strong></pre><pre><strong>if(rstCount==0)</strong></pre><pre><strong>roData &lt;= 0;</strong></pre><pre><strong>else begin</strong></pre><pre><strong>cCounter &lt;= cCounter + 1&#39;b1;</strong></pre><pre><strong>if ( cCounter == 1_250_000) begin</strong></pre><pre><strong>cCounter &lt;= 0;</strong></pre><pre><strong>roData &lt;= roData + 1&#39;b1;</strong></pre><pre><strong>roData &lt;= 32&#39;b10110101100100011001010101100111;</strong></pre><pre><strong>end</strong></pre><pre><strong>end</strong></pre><pre><strong>end</strong></pre><pre><strong>assign oData = roData;</strong></pre><pre><strong>endmodule</strong></pre><p>Step 7: ทำการ Create Schematic Symbol</p><p>Step 8: New Source &gt; Schematic &gt; ตั้งชื่อ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*5nOmI1_G_KnoQSCRKK0Prw.png" /></figure><p>Step 9: นำ<strong> </strong>Schematic ที่เราสร้างมาต่อวงดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*6Vgztqp84TsT2WcBbIS5IQ.png" /></figure><p>Step 10: New Source &gt; Implementation &gt; ตั้งชื่อ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*PAXkLdTN7HYsyTzDNxjTvw.png" /></figure><p>Step 11: ทำการกำหนด pin ด้วยคำสั่ง</p><pre><strong>NET &quot;Clk_50MHz&quot; LOC = P56 | IOSTANDARD = LVTTL ;</strong></pre><pre><strong>NET &quot;Onboard&quot; LOC = P38    | IOSTANDARD = LVTTL ;</strong></pre><pre><strong>NET &quot;DIN&quot; LOC = P51 ;</strong></pre><pre><strong>NET &quot;CLK&quot; LOC = P41 ;</strong></pre><pre><strong>NET &quot;CS&quot; LOC = P35 ;</strong></pre><p>Step 12: ทำการ Generate Program</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/292/1*Y5WKRdJA21dknbA23NWfZQ.png" /></figure><p>Step 13: ทำการอัพโปรแกรมลงบอร์ด Mojo</p><p>Step 14: ทำการต่อวงจรดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*fdz1Fnw2Dsz5DESr7N4Pcw.png" /></figure><p>ผลการทดลอง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*7--iYBDKocmWb9F6sFiRgA.png" /></figure><p><strong>Lab06 — Serial Communication — Tx</strong></p><p>Step 1: ทำการสร้างโปรเจค New Project &gt; Lab06</p><p>Step 2: New Source &gt; Verilog Module &gt; ตั้งชื่อ</p><p>Step 3: พิมพ์คำสั่งดังนี้</p><pre><strong>//############################################################</strong></pre><pre><strong>module uart_send # (           parameter BAUD_RATE = 115200,</strong></pre><pre><strong>parameter CLOCK_SPEED_MHZ = 50)</strong></pre><pre><strong>(          input [7:0] data_byte,</strong></pre><pre><strong>input start_send,</strong></pre><pre><strong>input clk,</strong></pre><pre><strong>output tx,</strong></pre><pre><strong>output ready);</strong></pre><pre><strong>parameter integer CYCLES_WAIT = CLOCK_SPEED_MHZ * 1e6 / BAUD_RATE;</strong></pre><pre><strong>parameter IDLE = 0;</strong></pre><pre><strong>parameter START_BIT = 1;</strong></pre><pre><strong>parameter END_BIT = 2;</strong></pre><pre><strong>parameter DATA_BIT = 3;</strong></pre><pre><strong>reg [2:0] state = IDLE;</strong></pre><pre><strong>reg [15:0] cycle_count = 0;</strong></pre><pre><strong>reg [3:0] bit_index = 0;</strong></pre><pre><strong>reg [7:0] data;</strong></pre><pre><strong>assign tx = state == IDLE ? 1 :</strong></pre><pre><strong>state == START_BIT ? 0 :</strong></pre><pre><strong>state == END_BIT ? 1 :</strong></pre><pre><strong>data[bit_index];</strong></pre><pre><strong>assign ready = state == IDLE;</strong></pre><pre><strong>always @(posedge clk) begin</strong></pre><pre><strong>if(state != IDLE)</strong></pre><pre><strong>data &lt;= data_byte;</strong></pre><pre><strong>if(cycle_count == CYCLES_WAIT) cycle_count &lt;= 0;</strong></pre><pre><strong>else cycle_count &lt;= cycle_count + 1;</strong></pre><pre><strong>if(state == IDLE &amp;&amp; start_send) begin</strong></pre><pre><strong>state &lt;= START_BIT;</strong></pre><pre><strong>cycle_count &lt;= 0;</strong></pre><pre><strong>end else if(state == START_BIT &amp;&amp; cycle_count == CYCLES_WAIT) begin</strong></pre><pre><strong>state &lt;= DATA_BIT;</strong></pre><pre><strong>bit_index &lt;= 0;</strong></pre><pre><strong>end else if(state == DATA_BIT &amp;&amp; cycle_count == CYCLES_WAIT) begin</strong></pre><pre><strong>if(bit_index == 7) state &lt;= END_BIT;</strong></pre><pre><strong>else bit_index &lt;= bit_index + 1;</strong></pre><pre><strong>end else if(state == END_BIT &amp;&amp; cycle_count == CYCLES_WAIT) begin</strong></pre><pre><strong>state &lt;= IDLE;</strong></pre><pre><strong>end</strong></pre><pre><strong>end</strong></pre><pre><strong>endmodule</strong></pre><p>Step 4: ทำการ Create Schematic Symbol</p><p>Step 5: New Source &gt; Verilog Module &gt; ตั้งชื่อ</p><p>Step 6: พิมพ์คำสั่งดังนี้</p><pre><strong>//######################################################</strong></pre><pre><strong>module uart_test  #(parameter DelayStep=10000000</strong></pre><pre><strong>( input clk, n_rst, output start, output [7:0] data );</strong></pre><pre><strong>parameter ASCII_Start = &quot;A&quot;;</strong></pre><pre><strong>parameter ASCII_Stop  = &quot;Z&quot;;</strong></pre><pre><strong>reg [31:0]</strong></pre><pre><strong>count = 0;</strong></pre><pre><strong>reg [7:0]  rData = ASCII_Start;</strong></pre><pre><strong>reg [7:0]  cData = ASCII_Start;</strong></pre><pre><strong>reg</strong></pre><pre><strong>rStart = 0;</strong></pre><pre><strong>always @(posedge clk or negedge n_rst)</strong></pre><pre><strong>if (n_rst==0)</strong></pre><pre><strong>begin</strong></pre><pre><strong>cData &lt;= ASCII_Start;</strong></pre><pre><strong>rData &lt;= ASCII_Start;</strong></pre><pre><strong>end</strong></pre><pre><strong>else</strong></pre><pre><strong>begin</strong></pre><pre><strong>if (count == DelayStep)</strong></pre><pre><strong>begin</strong></pre><pre><strong>count &lt;= 0;</strong></pre><pre><strong>cData &lt;= cData + 1&#39;b1;</strong></pre><pre><strong>if(cData &lt;= ASCII_Stop)</strong></pre><pre><strong>rData &lt;= cData;</strong></pre><pre><strong>else if(cData == (ASCII_Stop+1))</strong></pre><pre><strong>rData &lt;= 8&#39;b00001101;</strong></pre><pre><strong>else if(cData == (ASCII_Stop+2))  rData &lt;= 8&#39;b00001010;</strong></pre><pre><strong>else cData &lt;= ASCII_Start;</strong></pre><pre><strong>end</strong></pre><pre><strong>else</strong></pre><pre><strong>begin</strong></pre><pre><strong>count &lt;= count + 1&#39;b1;</strong></pre><pre><strong>if (count == 100)   rStart &lt;= 1;  else   rStart &lt;= 0;</strong></pre><pre><strong>end</strong></pre><pre><strong>end</strong></pre><pre><strong>assign data = rData;</strong></pre><pre><strong>assign start = rStart;</strong></pre><pre><strong>endmodule</strong></pre><p>Step 7: ทำการ Create Schematic Symbol</p><p>Step 8: New Source &gt; Schematic &gt; ตั้งชื่อ</p><p>Step 9: นำ<strong> </strong>Schematic ที่เราสร้างมาต่อวงดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*81pjqmSZbc-z4zxQt_utpw.png" /></figure><p>Step 10: New Source &gt; Implementation &gt; ตั้งชื่อ</p><p>Step 11: ทำการกำหนด pin ด้วยคำสั่ง</p><pre><strong>#Created by Constraints Editor (xc6slx9-tqg144-3) - 2012/11/05</strong></pre><pre><strong>NET &quot;clk&quot; TNM_NET = clk;</strong></pre><pre><strong>TIMESPEC TS_clk = PERIOD &quot;clk&quot; 50 MHz HIGH 50%;</strong></pre><pre><strong>NET &quot;clk&quot;        LOC = P56 | IOSTANDARD = LVTTL;</strong></pre><pre><strong>NET &quot;rst_n&quot;    LOC = P38 | IOSTANDARD = LVTTL;</strong></pre><pre><strong>NET &quot;tx&quot;         LOC = P51 | IOSTANDARD = LVTTL;</strong></pre><p>Step 12: ทำการ Generate Program</p><p>Step 13: ทำการอัพโปรแกรมลงบอร์ด Mojo</p><p>Step 14: ทำการต่อวงจรดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*pUb5G_ULr32ufY-FG_q63g.png" /></figure><p><strong>Lab01 — Ripple Adder 4 bit + 4 Bit = 1 + 4Bit</strong></p><p>Step 1: ทำการสร้างโปรเจค New Project &gt; Lab01</p><p>Step 2: New Source &gt; Schematic &gt; ตั้งชื่อ</p><p>Step 3: ต่อวงจร Schematic ดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*10qRYjkw5TAzeb1b2aU4Uw.png" /></figure><p>Step 4: ไปที่ Design &gt; Design Utilities &gt; Create Schematic Symbol</p><p>Step 5: New Source &gt; Verilog Module &gt; ตั้งชื่อ decode_7seg</p><p>Step 6: ทำการใส่ source code นี้ลงไปไปที่ decode_7seg</p><pre><strong>`timescale 1ns / 1ps<br>module Decode_7Seg( D, C, B, A, ledt, leda, ledb, ledc, ledd, lede, ledf, ledg);<br>input D, C, B, A;<br>output ledt, leda, ledb, ledc, ledd, lede, ledf, ledg;<br>reg [7:0] seg_data;<br>reg [3:0] DataIn;<br>always @* begin<br>DataIn = { D, C, B, A };<br>end<br>always @(DataIn)<br>case (DataIn)<br>4&#39;b0000: seg_data = 8&#39;b01111110;<br>4&#39;b0001: seg_data = 8&#39;b00110000;<br>4&#39;b0010: seg_data = 8&#39;b01101101;<br>4&#39;b0011: seg_data = 8&#39;b01111001;<br>4&#39;b0100: seg_data = 8&#39;b00110011;<br>4&#39;b0101: seg_data = 8&#39;b01011011;<br>4&#39;b0110: seg_data = 8&#39;b01011111;<br>4&#39;b0111: seg_data = 8&#39;b01110000;<br>4&#39;b1000: seg_data = 8&#39;b01111111;<br>4&#39;b1001: seg_data = 8&#39;b01111011;<br>4&#39;b1010: seg_data = 8&#39;b01110111;<br>4&#39;b1011: seg_data = 8&#39;b00011111;<br>4&#39;b1100: seg_data = 8&#39;b01001110;<br>4&#39;b1101: seg_data = 8&#39;b00111101;<br>4&#39;b1110: seg_data = 8&#39;b01001111;<br>4&#39;b1111: seg_data = 8&#39;b01000111;<br>endcase<br>assign ledt = seg_data[7]; // if Active Low(Using ~)<br>assign leda = seg_data[6];<br>assign ledb = seg_data[5];<br>assign ledc = seg_data[4];<br>assign ledd = seg_data[3];<br>assign lede = seg_data[2];<br>assign ledf = seg_data[1];<br>assign ledg = seg_data[0];<br>endmodule</strong></pre><p>Step 7: ไปที่ Design &gt; Design Utilities &gt; Create Schematic Symbol</p><p>Step 8: New Source &gt; Schematic &gt; ตั้งชื่อ</p><p>Step 9: ไปที่ symbols &gt; filter &gt; เลือก fulladder ที่สร้างไว้ ในที่นี้ชื่อ ffff และ decode_7seg</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*fnPC9pxKcSqUULNWgyd_2Q.png" /></figure><p>Step 10: ต่อวงจรดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*jiy6OCQJer73P0loWwDDCw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*K9LbSGmZJJ8R_zp_OKZudw.png" /></figure><p>Step 11: New Source &gt; Implementation Constraints File &gt; ตั้งชื่อ pin</p><p>Step 12: ใส่ Source Code ดังนี้</p><pre><strong>NET &quot;A0&quot; LOC = P2;<br>NET &quot;A1&quot; LOC = P6;<br>NET &quot;A2&quot; LOC = P8;<br>NET &quot;A3&quot; LOC = P10;<br>NET &quot;B0&quot; LOC = P12;<br>NET &quot;B1&quot; LOC = P15;<br>NET &quot;B2&quot; LOC = P17;<br>NET &quot;B3&quot; LOC = P22;<br>NET &quot;Cin&quot; LOC = P41;<br>NET &quot;S4&quot; LOC = P51;<br>NET &quot;ledt&quot; LOC = P1;<br>NET &quot;leda&quot; LOC = P5;<br>NET &quot;ledb&quot; LOC = P7;<br>NET &quot;ledc&quot; LOC = P9;<br>NET &quot;ledd&quot; LOC = P11;<br>NET &quot;lede&quot; LOC = P14;<br>NET &quot;ledf&quot; LOC = P16;<br>NET &quot;ledg&quot; LOC = P21;</strong></pre><p>Step 13: คลิกขวา &gt; Implement Top Module</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/114/1*REgB4siJI5B3eeczwgIYxg.png" /></figure><p>Step 14: คลิกที่ Generate Programming File &gt; ติ้กถูกที่ Create Binary Conf.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*iTUuuO2Rr51q6AlHRRaDZg.png" /></figure><p>Step 15: Click &gt; Rerun All</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*azWzFdBy0BCEI6tcDXTzTQ.png" /></figure><p>Step 16: เปิดโปรแกรม Mojo Loader และอับโหลดไฟล์</p><p><strong>การต่อวงจร</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*2LY1fFv1xm2RAA-j3PxEAw.png" /></figure><p><strong>ผลการทดลอง</strong></p><p>เมื่อ 1111 + 1111 = E + Carry out (ไฟ LED สีแดง)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*NV37JmDT6ywS52wa3onfwQ.png" /></figure><p><strong>Lab02 — Carry Save Adder 4 number @3 bit</strong></p><p>Step 1: ทำการสร้างโปรเจค New Project &gt; Lab02</p><p>Step 2: New Source &gt; Schematic &gt; ตั้งชื่อ</p><p>Step 3: ต่อวงจร Schematic ดังภาพ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*10qRYjkw5TAzeb1b2aU4Uw.png" /></figure><p>Step 4: ไปที่ Design &gt; Design Utilities &gt; Create Schematic Symbol</p><p>Step 5: New Source &gt; Verilog Module &gt; ตั้งชื่อ</p><p>Step 6: ทำการใส่ source code นี้ลงไป</p><pre>`timescale 1ns / 1ps<br>module Decode_7Seg( D, C, B, A, ledt, leda, ledb, ledc, ledd, lede, ledf, ledg);<br>input D, C, B, A;<br>output ledt, leda, ledb, ledc, ledd, lede, ledf, ledg;<br>reg [7:0] seg_data;<br>reg [3:0] DataIn;<br>always @* begin<br>DataIn = { D, C, B, A };<br>end<br>always @(DataIn)<br>case (DataIn)<br>4&#39;b0000: seg_data = 8&#39;b01111110;<br>4&#39;b0001: seg_data = 8&#39;b00110000;<br>4&#39;b0010: seg_data = 8&#39;b01101101;<br>4&#39;b0011: seg_data = 8&#39;b01111001;<br>4&#39;b0100: seg_data = 8&#39;b00110011;<br>4&#39;b0101: seg_data = 8&#39;b01011011;<br>4&#39;b0110: seg_data = 8&#39;b01011111;<br>4&#39;b0111: seg_data = 8&#39;b01110000;<br>4&#39;b1000: seg_data = 8&#39;b01111111;<br>4&#39;b1001: seg_data = 8&#39;b01111011;<br>4&#39;b1010: seg_data = 8&#39;b01110111;<br>4&#39;b1011: seg_data = 8&#39;b00011111;<br>4&#39;b1100: seg_data = 8&#39;b01001110;<br>4&#39;b1101: seg_data = 8&#39;b00111101;<br>4&#39;b1110: seg_data = 8&#39;b01001111;<br>4&#39;b1111: seg_data = 8&#39;b01000111;<br>endcase<br>assign ledt = seg_data[7]; // if Active Low(Using ~)<br>assign leda = seg_data[6];<br>assign ledb = seg_data[5];<br>assign ledc = seg_data[4];<br>assign ledd = seg_data[3];<br>assign lede = seg_data[2];<br>assign ledf = seg_data[1];<br>assign ledg = seg_data[0];<br>endmodule</pre><p>Step 7: ไปที่ Design &gt; Design Utilities &gt; Create Schematic Symbol</p><p>Step 8: New Source &gt; Schematic &gt; ตั้งชื่อ lab2</p><p>Step 9: ไปที่ symbols &gt; filter &gt; เลือก fulladder ที่สร้างไว้ ในที่นี้ชื่อ ffff และ decode_7seg</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*k8vx3LE-J6QKPsv1E0tdQg.png" /></figure><p>Step 10: ต่อวงจรดังรูป</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*bYsYy3Y_KyrhCB2abjuSuw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*ayzcYRCHV9fEwPqY-TAoLA.png" /></figure><p>Step 11: New Source &gt; Implementation Constraints File &gt; ตั้งชื่อ pin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*rba8n4CfwXiUqzfblwtR7Q.png" /></figure><p>Step 12: ใส่ Source Code ดังนี้</p><pre>NET &quot;A0&quot; LOC = P2;<br>NET &quot;A1&quot; LOC = P6;<br>NET &quot;A2&quot; LOC = P8;</pre><pre>NET &quot;B0&quot; LOC = P10;<br>NET &quot;B1&quot; LOC = P12;<br>NET &quot;B2&quot; LOC = P15;<br> <br>NET &quot;C0&quot; LOC = P17;<br>NET &quot;C1&quot; LOC = P22;<br>NET &quot;C2&quot; LOC = P24;</pre><pre>NET &quot;D0&quot; LOC = P27;<br>NET &quot;D1&quot; LOC = P30;<br>NET &quot;D2&quot; LOC = P33;</pre><pre>NET &quot;S4&quot; LOC = P51;</pre><pre>NET &quot;ledt&quot; LOC = P1;<br>NET &quot;leda&quot; LOC = P5;<br>NET &quot;ledb&quot; LOC = P7;<br>NET &quot;ledc&quot; LOC = P9;<br>NET &quot;ledd&quot; LOC = P11;<br>NET &quot;lede&quot; LOC = P14;<br>NET &quot;ledf&quot; LOC = P16;</pre><p>Step 13: คลิกขวา &gt; Implement Top Module</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/114/1*REgB4siJI5B3eeczwgIYxg.png" /></figure><p>Step 14: คลิกที่ Generate Programming File &gt; ติ้กถูกที่ Create Binary Conf.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*iTUuuO2Rr51q6AlHRRaDZg.png" /></figure><p>Step 15: Click &gt; Rerun All</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*azWzFdBy0BCEI6tcDXTzTQ.png" /></figure><p>Step 16: เปิดโปรแกรม Mojo Loader และอับโหลดไฟล์</p><p><strong>การต่อวงจร</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*kx6J4zI9rUloYe7YbcFyAw.png" /></figure><p><strong>ผลการทดลอง</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*NV37JmDT6ywS52wa3onfwQ.png" /></figure><p>เมื่อ A = 6 : 110 , B = 1 : 001 , C = 3 : 011 , D = 7 : 111<br> S = A+B+C+D = 17 หรือ 10001</p><p>จะได้ 1 และ Led สีแดง ซึ่งหมายถึงมี carry out เป็น 1</p><p><strong>Lab03 — Single Digit 7-Segment</strong></p><p>Step 1: ทำการสร้างโปรเจค New Project &gt; Lab06</p><p>Step 2: New Source &gt; Verilog Module &gt; ตั้งชื่อ</p><p>Step 3: พิมพ์คำสั่งดังนี้</p><pre>`timescale 1ns / 1ps<br>module MainTest7Seg( Clk_50MHz, Reset_Onboard, Seg_A, Seg_B,<br>Seg_C, Seg_D, Seg_E, Seg_F, Seg_G, Seg_T, COMM_3);<br>input  Reset_Onboard, Clk_50MHz; <br>output COMM_3, Seg_A, Seg_B, Seg_C, Seg_D, Seg_E, Seg_F, <br>Seg_G, Seg_T;<br>reg [7:0]  seg_data; <br>reg [3:0]  Counter;  <br>reg [27:0]  Dly_Counter; <br> always@(posedge Clk_50MHz or negedge Reset_Onboard)<br> begin  <br>if(Reset_Onboard == 0) <br>Counter &lt;= 0; <br>else begin <br>Dly_Counter &lt;= Dly_Counter + 1&#39;b1; <br>if(Dly_Counter == 25_000_000) begin <br>Dly_Counter &lt;= 0; <br>Counter &lt;= Counter + 1&#39;b1; <br>   end <br>  end <br>end <br>always @(Counter) <br>case (Counter) <br>   4&#39;b0000: seg_data = 8&#39;b01111110; <br>   4&#39;b0001: seg_data = 8&#39;b00110000; <br>   4&#39;b0010: seg_data = 8&#39;b01101101; <br>   4&#39;b0011: seg_data = 8&#39;b01111001; <br>   4&#39;b0100: seg_data = 8&#39;b00110011; <br>   4&#39;b0101: seg_data = 8&#39;b01011011; <br>   4&#39;b0110: seg_data = 8&#39;b01011111; <br>   4&#39;b0111: seg_data = 8&#39;b01110000; <br>   4&#39;b1000: seg_data = 8&#39;b01111111; <br>   4&#39;b1001: seg_data = 8&#39;b01111011; <br>   4&#39;b1010: seg_data = 8&#39;b01110111; <br>   4&#39;b1011: seg_data = 8&#39;b00011111; <br>   4&#39;b1100: seg_data = 8&#39;b01001110; <br>   4&#39;b1101: seg_data = 8&#39;b00111101; <br>   4&#39;b1110: seg_data = 8&#39;b01001111; <br>   4&#39;b1111: seg_data = 8&#39;b01000111; <br>  endcase <br>assign Seg_T = ~seg_data[7]; // if Active Low(Using ~)  <br>assign Seg_A = ~seg_data[6];  <br>assign Seg_B = ~seg_data[5]; <br>assign Seg_C = ~seg_data[4]; <br>assign Seg_D = ~seg_data[3]; <br>assign Seg_E = ~seg_data[2]; <br>assign Seg_F = ~seg_data[1]; <br>assign Seg_G = ~seg_data[0]; <br>assign COMM_3 = 1&#39;b1; <br>endmodule</pre><p>Step 4: New Source &gt; Implementation &gt; ตั้งชื่อ</p><p>Step 5: ทำการกำหนด pin ด้วยคำสั่ง</p><pre>NET &quot;Clk_50MHz&quot; <br>LOC = P56 | IOSTANDARD = LVTTL; <br>NET &quot;Reset_Onboard&quot; LOC = P38 | IOSTANDARD = LVTTL ;</pre><pre>NET &quot;Seg_A&quot;  LOC = P41 ;<br>NET &quot;Seg_B&quot;  LOC = P27 ;<br>NET &quot;Seg_C&quot;  LOC = P32 ;<br>NET &quot;Seg_D&quot;  LOC = P40 ;<br>NET &quot;Seg_E&quot;  LOC = P50 ;<br>NET &quot;Seg_F&quot;  LOC = P35 ;<br>NET &quot;Seg_G&quot;  LOC = P29 ;<br>NET &quot;Seg_T&quot;  LOC = P34 ;<br>NET &quot;COMM_3&quot; LOC = P51 ;</pre><p>Step 6: ทำการ Generate Program</p><p>Step 7: ทำการอัพโปรแกรมลงบอร์ด Mojo</p><p><strong>ผลการทดลอง</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*38RbeJk7Jxfb94F_UzpyXQ.png" /></figure><p>5.FPGA — Timing Coding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6349b7b86df" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[3. Start FPGA]]></title>
            <link>https://medium.com/@yongyutsrisuban/3-start-fpga-cc92f1807b7?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc92f1807b7</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 14:33:34 GMT</pubDate>
            <atom:updated>2019-06-27T15:28:24.276Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Week05 — Start FPGA</strong></p><h3>สำหรับศึกษาในหัวข้อต่างๆ</h3><p><a href="https://medium.com/@yongyutsrisuban/embedded-system-267ceee1665e">1. Install Raspberry Pi</a><br><a href="https://medium.com/@yongyutsrisuban/2-rpi-iots-166cb44a12bd">2. RPi — IoTs</a><br><a href="https://medium.com/@yongyutsrisuban/3-start-fpga-cc92f1807b7">3. Start FPGA</a><br><a href="https://medium.com/@yongyutsrisuban/mission-2-notebook-or-pc-lab-moving-8-led-with-switch-control-c6349b7b86df">4. Mojo V3 — Display 7_Segment and UART Tx</a><br><a href="https://medium.com/@yongyutsrisuban/1-install-raspberry-pi-684a25323b6e">5.FPGA — Timing Coding</a></p><p>Step 1: เปิดโปรแกรม ISE Wabpack 14.7</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*fliSgU49vtegdDAO9WHAFQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*AeOvLE69GIY18vKNXpLodw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*FBT0v1L2mkrXNr6qkqzABw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/635/1*d_Ig0QeeZ6MmzN0C5066ww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*hSh-KJdmVkPzKHku_wvvjA.png" /></figure><p>Step 2: เขียนโค้ดดังนี้</p><pre>library IEEE;</pre><pre>use IEEE.STD_LOGIC_1164.ALL;</pre><pre>entity moving_led_1 is</pre><pre>port (reset, clk: in   std_logic;</pre><pre>yout : out  std_logic_vector(7 downto 0));</pre><pre>end moving_led_1;</pre><pre>architecture beh of moving_led_1 is</pre><pre>signal q : std_logic_vector(7 downto 0);</pre><pre>signal sq : std_logic;</pre><pre>component DIVIDER is</pre><pre>port (CLK : in std_logic;</pre><pre>Q : out std_logic);</pre><pre>end component;</pre><pre>begin</pre><pre>PROCESS (clk,reset)</pre><pre>BEGIN</pre><pre>IF(reset = &#39;0&#39;) THEN</pre><pre>q &lt;= &quot;00000001&quot;;</pre><pre>ELSE</pre><pre>IF(sq&#39;EVENT and sq = &#39;1&#39;) THEN</pre><pre>q(0) &lt;= q(7);</pre><pre>q(7 downto 1)&lt;=q(6 downto 0);</pre><pre>ELSE</pre><pre>q &lt;= q;</pre><pre>END IF;</pre><pre>END IF;</pre><pre>END PROCESS;</pre><pre>yout &lt;= q;</pre><pre>c1: DIVIDER port map(CLK, sq);</pre><pre>end beh;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*PNgC7GutZdwVedAhmXsHKg.png" /></figure><p><strong>เพิ่ม โปรแกรม Divider</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*LpBMjHomaBa31eTapTNx_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*_6nY4m6blx1MxJM0QcY4Zw.png" /></figure><p>Step 3: เขียนโค้ดโปรแกรม Divider</p><pre>library IEEE;</pre><pre>use IEEE.std_logic_1164.all;</pre><pre>entity DIVIDER is</pre><pre>generic (fin: integer := 50000000;</pre><pre>fout: integer := 4);</pre><pre>port (CLK: in std_logic;</pre><pre>Q : out std_logic );</pre><pre>end DIVIDER;</pre><pre>architecture RTL of DIVIDER is</pre><pre>signal COUNT : integer range 0 to (fin/(2*fout)) ;</pre><pre>signal qs : std_logic := &#39;0&#39;;</pre><pre>begin</pre><pre>process (CLK)</pre><pre>begin</pre><pre>if CLK&#39;event and CLK = &#39;1&#39; then</pre><pre>if (COUNT &gt;= (fin/(2*fout)-1)) then</pre><pre>COUNT &lt;= 0;</pre><pre>qs &lt;= not(qs);</pre><pre>else</pre><pre>COUNT &lt;= COUNT +1;</pre><pre>end if;</pre><pre>end if;</pre><pre>end process;</pre><pre>Q &lt;= qs ;</pre><pre>end RTL;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*Ch5-pNFA-FJG2dumjac1ng.png" /></figure><p>สร้างไฟล์เพื่อกำหนดขาอุปกรณ์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*MM9ufgyYZDFC4lwdS0TmTg.png" /></figure><p>Step 4: เขียนโค้ดกำหนดขา อุปกรณ์</p><pre># “moving_led_1.ucf” File</pre><pre>NET &quot;clk&quot; TNM_NET = clk;</pre><pre>TIMESPEC TS_clk = PERIOD &quot;clk&quot; 50 MHz HIGH 50%;</pre><pre>NET &quot;CLK&quot;  LOC = P56 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;0&gt;&quot; LOC = P134 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;1&gt;&quot; LOC = P133 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;2&gt;&quot; LOC = P132 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;3&gt;&quot; LOC = P131 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;4&gt;&quot; LOC = P127 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;5&gt;&quot; LOC = P126 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;6&gt;&quot; LOC = P124 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;7&gt;&quot; LOC = P123 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;reset&quot; LOC = P38 | IOSTANDARD = LVTTL;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*GsfSlzUTiyV60qm6CHt8xQ.png" /></figure><p><strong>Step 5: build ไฟล์</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*MM-WO9m84mrV13vKE_zPZg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*g7nkGeo_S4abDDBiZ4tBug.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*QTVyHypBWSBP9fvX1IW_Ng.png" /></figure><p>Step 6: อัพโปรแกรมลงบอร์ด mojo</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*mQBhxiMZ0UuZ4twS91mRpQ.png" /></figure><h3>Mission 2 &gt;&gt; Notebook or PC Lab Moving 8 LED with Switch Control</h3><figure><img alt="Go to the profile of Yongyut Srisuban" src="https://cdn-images-1.medium.com/proxy/0*BTh-PWSCCCnGBo0E" /></figure><p><a href="https://medium.com/@yongyutsrisuban">Yongyut Srisuban</a></p><p>Jun 27</p><p><strong>ระบบ</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*d3TZaaepOidAKkg3eGsIhg.png" /></figure><p><strong>การทํางาน</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*NHUb-qm1lRieCynOAO7cZw.png" /><figcaption><strong>หมายเหตุ x = don’t care</strong></figcaption></figure><p>Step 1: เขียนโปรแกรม</p><pre>library IEEE;</pre><pre>use IEEE.STD_LOGIC_1164.ALL;</pre><pre>entity no6 is</pre><pre>port (sh, lr, reset, clk: in   std_logic;</pre><pre>yout : out  std_logic_vector(7 downto 0));</pre><pre>end no6;</pre><pre>architecture beh of no6 is</pre><pre>signal q : std_logic_vector(7 downto 0);</pre><pre>begin</pre><pre>PROCESS (clk,reset)</pre><pre>BEGIN</pre><pre>IF(reset = &#39;0&#39;) THEN</pre><pre>q &lt;= &quot;00000001&quot;;</pre><pre>ELSE</pre><pre>IF(CLK&#39;EVENT and CLK = &#39;1&#39;) THEN</pre><pre>IF sh = &#39;1&#39; THEN</pre><pre>IF lr = &#39;0&#39; THEN</pre><pre>q(7) &lt;= q(0);</pre><pre>q(6 downto 0)&lt;=q(7 downto 1);</pre><pre>ELSE</pre><pre>q(0) &lt;= q(7);</pre><pre>q(7 downto 1)&lt;=q(6 downto 0);</pre><pre>END IF;</pre><pre>ELSE</pre><pre>q &lt;= q;</pre><pre>END IF;</pre><pre>ELSE</pre><pre>q &lt;= q;</pre><pre>END IF;</pre><pre>END IF;</pre><pre>END PROCESS;</pre><pre>yout &lt;= q;</pre><pre>end beh;</pre><p><strong>Step 2: เขียนโปรแกรมกหนดขาอุปกรณ์</strong></p><pre>NET &quot;yout&lt;0&gt;&quot; LOC = P134 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;1&gt;&quot; LOC = P133 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;2&gt;&quot; LOC = P132 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;3&gt;&quot; LOC = P131 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;4&gt;&quot; LOC = P127 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;5&gt;&quot; LOC = P126 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;6&gt;&quot; LOC = P124 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;yout&lt;7&gt;&quot; LOC = P123 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;reset&quot; LOC = P38 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;clk&quot; LOC = P50 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;sh&quot; LOC = P40 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;lr&quot; LOC = P34 | IOSTANDARD = LVTTL;</pre><p>การต่อวงจร</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*tV7xOluJEFAy2ZcXr0EKrw.png" /></figure><h3>Mission 3 →&gt; Notebook Only or PC Lab Question 3.2 วิ่งเร็วขึ้น / ช้าลง</h3><p>Step 1: สร้างไฟล์ Verilog</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/1*56XyrPnNinqrKYNk89fZsg.png" /></figure><p>Step 2: <strong>เขียนโปรแกรม</strong></p><pre>`timescale 1ns / 1ps</pre><pre>module MainTest7Seg(Clk_50MHz, Reset_Onboard, LED_Output);</pre><pre>input Reset_Onboard, Clk_50MHz;</pre><pre>output [7:0] LED_Output;</pre><pre>reg [3:0]               Counter;</pre><pre>reg [7:0]               rLED_Output;</pre><pre>reg [27:0]  Dly_Counter;</pre><pre>always@(posedge Clk_50MHz or negedge Reset_Onboard )</pre><pre>begin</pre><pre>if(Reset_Onboard == 0)</pre><pre>Counter &lt;= 0;</pre><pre>else</pre><pre>begin</pre><pre>Dly_Counter &lt;= Dly_Counter + 1&#39;b1;</pre><pre>if(Dly_Counter &gt;= 6_250_000)</pre><pre>begin</pre><pre>Dly_Counter &lt;= 0;</pre><pre>if(Counter &gt;= 13) Counter &lt;= 0;</pre><pre>else        Counter &lt;= Counter + 1&#39;b1;</pre><pre>end</pre><pre>end</pre><pre>end</pre><pre>always @(Counter)</pre><pre>case (Counter)</pre><pre>4&#39;b0000: rLED_Output = 8&#39;b10000000;</pre><pre>4&#39;b0001: rLED_Output = 8&#39;b01000000;</pre><pre>4&#39;b0010: rLED_Output = 8&#39;b00100000;</pre><pre>4&#39;b0011: rLED_Output = 8&#39;b00010000;</pre><pre>4&#39;b0100: rLED_Output = 8&#39;b00001000;</pre><pre>4&#39;b0101: rLED_Output = 8&#39;b00000100;</pre><pre>4&#39;b0110: rLED_Output = 8&#39;b00000010;</pre><pre>4&#39;b0111: rLED_Output = 8&#39;b00000001;</pre><pre>4&#39;b1000: rLED_Output = 8&#39;b00000001;</pre><pre>4&#39;b1001: rLED_Output = 8&#39;b00000010;</pre><pre>4&#39;b1010: rLED_Output = 8&#39;b00000100;</pre><pre>4&#39;b1011: rLED_Output = 8&#39;b00001000;</pre><pre>4&#39;b1100: rLED_Output = 8&#39;b00010000;</pre><pre>4&#39;b1101: rLED_Output = 8&#39;b00100000;</pre><pre>4&#39;b1110: rLED_Output = 8&#39;b01000000;</pre><pre>4&#39;b1111: rLED_Output = 8&#39;b10000000;</pre><pre>endcase</pre><pre>assign LED_Output = rLED_Output;</pre><pre>endmodule</pre><p><strong>Step 3: เขียนโปรแกรมกหนดขาอุปกรณ์</strong></p><pre>NET &quot;Clk_50MHz&quot;</pre><pre>LOC = P56 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;Reset_Onboard&quot;</pre><pre>LOC = P38 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;0&gt;&quot; LOC = P134 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;1&gt;&quot; LOC = P133 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;2&gt;&quot; LOC = P132 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;3&gt;&quot; LOC = P131 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;4&gt;&quot; LOC = P127 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;5&gt;&quot; LOC = P126 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;6&gt;&quot; LOC = P124 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;7&gt;&quot; LOC = P123 | IOSTANDARD = LVTTL;</pre><p><strong>Mission 4 Notebook Only Question</strong></p><p>-ต้องการให้โปรแกรมทำงาน 3 โหมด คือ (0)All On, (1)All Off, (2)Blink</p><p>- การเขียนโปรแกรม</p><p>- ใช้Verilog Code</p><p>Step 1:เขียนโปรแกรม</p><pre>`timescale 1ns / 1ps</pre><pre>module MainTest7Seg(Rst_OnBoard, LED_Output,Clk_50MHz);</pre><pre>input Rst_OnBoard,Clk_50MHz;</pre><pre>output [7:0] LED_Output;</pre><pre>reg [7:0] rLED_Output;</pre><pre>reg [27:0] Dly_Counter;</pre><pre>reg [1:0]rMode;</pre><pre>reg state = 0;</pre><pre>always@(negedge Rst_OnBoard) begin</pre><pre>if(rMode == 0)</pre><pre>rMode &lt;= rMode+1;</pre><pre>else if(rMode == 1)</pre><pre>rMode &lt;= rMode+1;</pre><pre>else</pre><pre>rMode &lt;= 0;</pre><pre>end</pre><pre>always@(posedge Clk_50MHz) begin</pre><pre>Dly_Counter &lt;= Dly_Counter + 1&#39;b1;</pre><pre>if(Dly_Counter &gt;= 6_250_000) begin</pre><pre>Dly_Counter &lt;= 0;</pre><pre>if(state)</pre><pre>state &lt;= 0;</pre><pre>else</pre><pre>state &lt;= 1;</pre><pre>end</pre><pre>end</pre><pre>always@(rMode or state) begin</pre><pre>case(rMode)</pre><pre>2&#39;b00: rLED_Output = 8&#39;b00000000;</pre><pre>2&#39;b01: rLED_Output = 8&#39;b11111111;</pre><pre>2&#39;b10: begin</pre><pre>if(state) rLED_Output = 8&#39;b00000000;</pre><pre>else rLED_Output = 8&#39;b11111111;</pre><pre>end</pre><pre>endcase</pre><pre>end</pre><pre>assign LED_Output = rLED_Output;</pre><pre>endmodule</pre><p><strong>Step 2: เขียนโ</strong></p><p>ปรแกรมกหนดขาอุปกรณ์</p><pre>NET &quot;Clk_50MHz&quot; LOC = P56 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;Rst_OnBoard&quot; LOC = P38 | IOSTANDARD = LVTTL | CLOCK_DEDICATED_ROUTE=FALSE;</pre><pre>NET &quot;LED_Output&lt;0&gt;&quot; LOC = P134 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;1&gt;&quot; LOC = P133 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;2&gt;&quot; LOC = P132 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;3&gt;&quot; LOC = P131 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;4&gt;&quot; LOC = P127 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;5&gt;&quot; LOC = P126 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;6&gt;&quot; LOC = P124 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;7&gt;&quot; LOC = P123 | IOSTANDARD = LVTTL;</pre><p>Step 3: ทำการ build โปรแกรม และ อัพลงบอร์ด เหมือน VHDL</p><p><strong>Mission 5 →&gt; Notebook Only Question 5.2</strong></p><ul><li>ต้องการให้โปรแกรมท างานเป็นไฟวิ่ง 3 รูปแบบตามแต่จิตนาการ</li></ul><p>Step 1:เขียนโปรแกรม</p><pre>`timescale 1ns / 1ps</pre><pre>module MainTest7Seg(Rst_OnBoard, LED_Output,Clk_50MHz);</pre><pre>input Rst_OnBoard,Clk_50MHz;</pre><pre>output [7:0] LED_Output;</pre><pre>reg [7:0] rLED_Output;</pre><pre>reg [27:0] Dly_Counter;</pre><pre>reg [1:0]rMode;</pre><pre>reg [3:0] Counter;</pre><pre>reg state = 0;</pre><pre>always@(negedge Rst_OnBoard) begin</pre><pre>if(rMode == 0)</pre><pre>rMode &lt;= rMode+1;</pre><pre>else if(rMode == 1)</pre><pre>rMode &lt;= rMode+1;</pre><pre>else</pre><pre>rMode &lt;= 0;</pre><pre>end</pre><pre>always@(posedge Clk_50MHz) begin</pre><pre>Dly_Counter &lt;= Dly_Counter + 1&#39;b1;</pre><pre>if(Dly_Counter &gt;= 6_250_000) begin</pre><pre>Dly_Counter &lt;= 0;</pre><pre>if(state)</pre><pre>state &lt;= 0;</pre><pre>else</pre><pre>state &lt;= 1;</pre><pre>end</pre><pre>end</pre><pre>always@(posedge Clk_50MHz) begin</pre><pre>Dly_Counter &lt;= Dly_Counter + 1&#39;b1;</pre><pre>if(Dly_Counter &gt;= 6_250_000) begin</pre><pre>Dly_Counter &lt;= 0;</pre><pre>if(Counter &gt;= 13)</pre><pre>Counter &lt;= 0;</pre><pre>else</pre><pre>Counter &lt;= Counter + 1&#39;b1;</pre><pre>end</pre><pre>end<br>                always@(rMode or Counter) begin</pre><pre>case(rMode)</pre><pre>2&#39;b00: case(Counter)</pre><pre>4&#39;b0000: rLED_Output = 8&#39;b10000000;</pre><pre>4&#39;b0001: rLED_Output = 8&#39;b01000000;</pre><pre>4&#39;b0010: rLED_Output = 8&#39;b00100000;</pre><pre>4&#39;b0011: rLED_Output = 8&#39;b00010000;</pre><pre>4&#39;b0100: rLED_Output = 8&#39;b00001000;</pre><pre>4&#39;b0101: rLED_Output = 8&#39;b00000100;</pre><pre>4&#39;b0110: rLED_Output = 8&#39;b00000010;</pre><pre>4&#39;b0111: rLED_Output = 8&#39;b00000001;</pre><pre>4&#39;b1000: rLED_Output = 8&#39;b11000000;</pre><pre>4&#39;b1001: rLED_Output = 8&#39;b00000111;</pre><pre>4&#39;b1010: rLED_Output = 8&#39;b11100000;</pre><pre>4&#39;b1011: rLED_Output = 8&#39;b00011111;</pre><pre>4&#39;b1100: rLED_Output = 8&#39;b11111111;</pre><pre>endcase</pre><pre>2&#39;b01: case(Counter)</pre><pre>4&#39;b0000: rLED_Output = 8&#39;b10000001;</pre><pre>4&#39;b0001: rLED_Output = 8&#39;b01000010;</pre><pre>4&#39;b0010: rLED_Output = 8&#39;b00100100;</pre><pre>4&#39;b0011: rLED_Output = 8&#39;b00011000;</pre><pre>4&#39;b0100: rLED_Output = 8&#39;b00011000;</pre><pre>4&#39;b0101: rLED_Output = 8&#39;b00100100;</pre><pre>4&#39;b0110: rLED_Output = 8&#39;b01000010;</pre><pre>4&#39;b0111: rLED_Output = 8&#39;b10000001;</pre><pre>4&#39;b1000: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b1001: rLED_Output = 8&#39;b11111111;</pre><pre>4&#39;b1010: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b1011: rLED_Output = 8&#39;b11111111;</pre><pre>4&#39;b1100: rLED_Output = 8&#39;b00000000;</pre><pre>endcase</pre><pre>2&#39;b10: case(Counter)</pre><pre>4&#39;b0000: rLED_Output = 8&#39;b10000000;</pre><pre>4&#39;b0001: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b0010: rLED_Output = 8&#39;b11000000;</pre><pre>4&#39;b0011: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b0100: rLED_Output = 8&#39;b11100000;</pre><pre>4&#39;b0101: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b0110: rLED_Output = 8&#39;b11110000;</pre><pre>4&#39;b0111: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b1000: rLED_Output = 8&#39;b11111000;</pre><pre>4&#39;b1001: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b1010: rLED_Output = 8&#39;b11111100;</pre><pre>4&#39;b1011: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b1100: rLED_Output = 8&#39;b11111110;</pre><pre>4&#39;b1101: rLED_Output = 8&#39;b00000000;</pre><pre>4&#39;b1110: rLED_Output = 8&#39;b11111111;<br>                                                                endcase</pre><pre>endcase</pre><pre>end</pre><pre>assign LED_Output = rLED_Output;</pre><pre>endmodule</pre><p>Step 2: เขียนโปรแกรมกหนดขาอุปกรณ์</p><pre>NET &quot;Rst_OnBoard&quot; LOC = P38 | IOSTANDARD = LVTTL | CLOCK_DEDICATED_ROUTE = FALSE;</pre><pre>NET &quot;Clk_50MHz&quot; LOC = P56 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;0&gt;&quot; LOC = P134 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;1&gt;&quot; LOC = P133 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;2&gt;&quot; LOC = P132 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;3&gt;&quot; LOC = P131 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;4&gt;&quot; LOC = P127 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;5&gt;&quot; LOC = P126 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;6&gt;&quot; LOC = P124 | IOSTANDARD = LVTTL;</pre><pre>NET &quot;LED_Output&lt;7&gt;&quot; LOC = P123 | IOSTANDARD = LVTTL;</pre><p>Step 3: ทำการ build โปรแกรม และ อัพลงบอร์ด เหมือน VHDL</p><p>4. Mojo V3 — Display 7_Segment and UART Tx</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc92f1807b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q06: Send Data to Firebase]]></title>
            <link>https://medium.com/@yongyutsrisuban/q06-send-data-to-firebase-4add0fc666c2?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4add0fc666c2</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 14:25:29 GMT</pubDate>
            <atom:updated>2019-06-27T14:25:29.436Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Registration</strong></p><p>Step1: ไปที่ <a href="https://console.firebase.google.com/">https://console.firebase.google.com/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*UUuorJgS5VBj9l-HXa53IA.png" /></figure><p>Step2: เพิ่มโครงการ (Add projrect)</p><p>ตั้งชื่อ เลือกประเทศ ตำแหน่งที่ตั้ง Server ยอมรับข้อตกลง คลิกสร้างโครงการ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*hBwGZ_HfgVOgb9mqNPVcsw.png" /></figure><p>Step3: เพิ่มโครงการ (Add projrect)</p><p>เลือก พัฒนา &gt;&gt; Database</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*IZedqnpOZHHui9aqh0drbw.png" /></figure><p>Step4: คลิก สร้างฐานข้อมูล</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*smQw1F7YFUxWF2diAFBjIA.png" /></figure><p>Step5: เลือก เริ่มต้นโหมดทดสอบ แล้วเปิดใช้งาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*z65XcohVzluzV_BBI6DH-A.png" /></figure><p>Step6: คัดลอก Url สำหรับเชื่อมต่อฐานข้อมูล</p><p>Step7:ต่อวงจรดังนี้</p><p>- ต่อไฟเลี้ยง 3.3–5 V ให้ขาแรกของเซนเซอร์</p><p>- ต่อขา Output (ขาที่2) เข้ากับ pin 7</p><p>- ต่อ Gnd ให้ขาสุดท้ายของเซนเซอร์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*qNNX6n4SNZ24wCOGkWJJog.png" /></figure><p>Step8: ติดตั้ง Adafruit_Python_DHT</p><pre>sudo apt-get update</pre><pre>sudo apt-get install build-essential python-dev</pre><pre>git clone https://github.com/adafruit/Adafruit_Python_DHT.git</pre><pre>cd Adafruit_Python_DHT</pre><pre>sudo python setup.py install</pre><p>Step9: ติดตั้ง Firebase Python Library</p><pre>sudo pip install requests==1.1.0</pre><pre>sudo pip install python-firebase</pre><p>Step10: clone Raspberrypi_Firebase</p><pre>git clone https://github.com/amphancm/Raspberrypi_Firebase</pre><p>Step11: แก้ไข ไฟล์ dht-firebase.py</p><pre>cd Raspberrypi_Firebase/</pre><p>แก้ไขเซนเซอร์เป็น DHT.22 <br> แก้ไข firebase url ที่คัดลอกไว้ <br> แล้วบันทึก ctlr+x &gt;&gt; y &gt;&gt; Enter</p><p>Step12: Launce Project</p><pre>sudo python dht-firebase.py</pre><p>ไปที่ firebase url</p><h3>3. Start FPGA</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4add0fc666c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q05: SQLite Database on a Raspberry Pi]]></title>
            <link>https://medium.com/@yongyutsrisuban/q05-sqlite-database-on-a-raspberry-pi-76a78ee04e5d?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/76a78ee04e5d</guid>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 14:12:14 GMT</pubDate>
            <atom:updated>2019-06-27T14:12:14.606Z</atom:updated>
            <content:encoded><![CDATA[<p>Step1: ติดตั้ง SQLite</p><pre>sudo apt-get install sqlite3</pre><p>Step2: Run Mosquitto broker</p><pre>mosquitto –d</pre><p>Step3: Install Flask</p><pre>sudo apt-get update</pre><pre>sudo apt-get upgrade</pre><pre>sudo apt-get install python-pip python-flask git-core</pre><pre>sudo pip install flask</pre><p>Step4: สร้าง Python web server script</p><pre>mkdir web-server</pre><pre>cd web-server</pre><pre>nano app.py</pre><p>Source Code</p><pre># Created by Rui Santos</pre><pre># Complete project details: https://randomnerdtutorials.com</pre><pre>import paho.mqtt.client as mqtt</pre><pre>from flask import Flask, render_template, request</pre><pre>import json</pre><pre>import sqlite3</pre><pre>app = Flask(__name__)</pre><pre>def dict_factory(cursor, row):</pre><pre>d = {}</pre><pre>for idx, col in enumerate(cursor.description):</pre><pre>d[col[0]] = row[idx]</pre><pre>return d</pre><pre># The callback for when the client receives a CONNACK response from the server.</pre><pre>def on_connect(client, userdata, flags, rc):</pre><pre>print(&quot;Connected with result code &quot; + str(rc))</pre><pre># Subscribing in on_connect() means that if we lose the connection and</pre><pre># reconnect then subscriptions will be renewed.</pre><pre>client.subscribe(&quot;/esp8266/dhtreadings&quot;)</pre><pre># The callback for when a PUBLISH message is received from the ESP8266.</pre><pre>def on_message(client, userdata, message):</pre><pre>if message.topic == &quot;/esp8266/dhtreadings&quot;:</pre><pre>print(&quot;DHT readings update&quot;)</pre><pre>#print(message.payload.json())</pre><pre>#print(dhtreadings_json[&#39;temperature&#39;])</pre><pre>#print(dhtreadings_json[&#39;humidity&#39;])</pre><pre>dhtreadings_json = json.loads(message.payload)<br>             # connects to SQLite database. File is named &quot;sensordata.db&quot; without the quotes</pre><pre># WARNING: your database file should be in the same directory of the app.py file or have the correct path</pre><pre>conn=sqlite3.connect(&#39;sensordata.db&#39;)</pre><pre>c=conn.cursor()</pre><pre>c.execute(&quot;&quot;&quot;INSERT INTO dhtreadings (temperature,</pre><pre>humidity, currentdate, currentime, device) VALUES((?), (?), date(&#39;now&#39;),</pre><pre>time(&#39;now&#39;), (?))&quot;&quot;&quot;, (dhtreadings_json[&#39;temperature&#39;],</pre><pre>dhtreadings_json[&#39;humidity&#39;], &#39;esp8266&#39;) )</pre><pre>conn.commit()</pre><pre>conn.close()</pre><pre>mqttc=mqtt.Client()</pre><pre>mqttc.on_connect = on_connect</pre><pre>mqttc.on_message = on_message</pre><pre>mqttc.connect(&quot;localhost&quot;,1883,60)</pre><pre>mqttc.loop_start()</pre><pre>@app.route(&quot;/&quot;)</pre><pre>def main():</pre><pre># connects to SQLite database. File is named &quot;sensordata.db&quot; without the quotes</pre><pre># WARNING: your database file should be in the same directory of the app.py file or have the</pre><pre>correct path</pre><pre>conn=sqlite3.connect(&#39;sensordata.db&#39;)</pre><pre>conn.row_factory = dict_factory</pre><pre>c=conn.cursor()</pre><pre>c.execute(&quot;SELECT * FROM dhtreadings ORDER BY id DESC LIMIT 20&quot;)</pre><pre>readings = c.fetchall()</pre><pre>#print(readings)</pre><pre>return render_template(&#39;main.html&#39;, readings=readings)</pre><pre>if __name__ == &quot;__main__&quot;:</pre><pre>app.run(host=&#39;0.0.0.0&#39;, port=8181, debug=True)</pre><p>Step5: สร้างดาต้าเบส ชื่อ sensordate.db</p><pre>sqlite3 sensordata.db</pre><p>Step6: สร้างตาราง ชื่อ dhtreading</p><pre>.fullschema</pre><pre>CREATE TABLE dhtreadings(id INTEGER PRIMARY KEY AUTOINCREMENT,</pre><pre>temperature NUMERIC, humidity NUMERIC, currentdate DATE, currentime TIME,</pre><pre>device TEXT);</pre><p>Step7: สร้างหน้าเว็บ Html</p><pre>mkdir templates</pre><pre>cd templates</pre><pre>nano main.html</pre><p>Source Code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*UBaElFawamBU12fNdx7bwQ.png" /></figure><p>Step8: ติดตั้ง DHT และ Adafruit_Sensor</p><p>ไปที่ <a href="https://github.com/adafruit/DHT-sensor-library/archive/master.zip">https://github.com/adafruit/DHT-sensor-library/archive/master.zip</a></p><p>และ <a href="https://github.com/adafruit/Adafruit_Sensor/archive/master.zip">https://github.com/adafruit/Adafruit_Sensor/archive/master.zip</a></p><p>เพื่อโหลด Library หลังจากนั้นแตกไฟล์ทั้งสอง แล้วย้ายไปที่ C:\Users\Administrator\Documents\Arduino\libraries</p><p>ให้เปลี่ยน ชื่อ DHT-sensor-library-master เป็น DTH</p><p>Step9: coding ESP-8266</p><p><strong>Source code</strong></p><pre>/*****</pre><pre>All the resources for this project:</pre><pre>https://rntlab.com/</pre><pre>*****/</pre><pre>// Loading the ESP8266WiFi library and the PubSubClient library</pre><pre>#include &lt;ESP8266WiFi.h&gt;</pre><pre>#include &lt;PubSubClient.h&gt;</pre><pre>#include &quot;DHT.h&quot;</pre><pre>// Uncomment one of the lines bellow for whatever DHT sensor type you&#39;re using!</pre><pre>//#define DHTTYPE DHT11   // DHT 11</pre><pre>//#define DHTTYPE DHT21   // DHT 21</pre><pre>(AM2301) #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321</pre><pre>// Change the credentials below, so your ESP8266 connects to your router</pre><pre>const char* ssid = &quot;YOUR_SSID&quot;;</pre><pre>const char* password = &quot;YOUR_PASSWORD&quot;;</pre><pre>// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker</pre><pre>const char* mqtt_server = &quot;YOUR_RPi_IP_Address&quot;;</pre><pre>// Initializes the espClient</pre><pre>WiFiClient espClient;</pre><pre>PubSubClient client(espClient);<br>// DHT Sensor</pre><pre>const int DHTPin = 14;</pre><pre>// Initialize DHT sensor.</pre><pre>DHT dht(DHTPin, DHTTYPE);</pre><pre>// Timers auxiliar variables</pre><pre>long now = millis();</pre><pre>long lastMeasure = 0;</pre><pre>char data[80];</pre><pre>// Don&#39;t change the function below. This functions connects your ESP8266 to your router</pre><pre>void setup_wifi() {</pre><pre>delay(10);</pre><pre>// We start by connecting to a WiFi network</pre><pre>Serial.println();</pre><pre>Serial.print(&quot;Connecting to &quot;);</pre><pre>Serial.println(ssid);</pre><pre>WiFi.begin(ssid, password);</pre><pre>while (WiFi.status() != WL_CONNECTED) {</pre><pre>delay(500);</pre><pre>Serial.print(&quot;.&quot;);</pre><pre>}</pre><pre>Serial.println(&quot;&quot;);</pre><pre>Serial.print(&quot;WiFi connected - ESP IP address: &quot;);</pre><pre>Serial.println(WiFi.localIP());</pre><pre>}</pre><pre>// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to</pre><pre>// Change the function below to add logic to your program, so when a device publishes a message to a topic that</pre><pre>// your ESP8266 is subscribed you can actually do something<br>void callback(String topic, byte* message, unsigned int length) {</pre><pre>Serial.print(&quot;Message arrived on topic: &quot;);</pre><pre>Serial.print(topic);</pre><pre>Serial.print(&quot;. Message: &quot;);</pre><pre>String messageTemp;</pre><pre>for (int i = 0; i &lt; length; i++) {</pre><pre>Serial.print((char)message[i]);</pre><pre>messageTemp += (char)message[i];</pre><pre>}</pre><pre>Serial.println();</pre><pre>}</pre><pre>// This functions reconnects your ESP8266 to your MQTT broker</pre><pre>// Change the function below if you want to subscribe to more topics with your ESP8266</pre><pre>void reconnect() {</pre><pre>// Loop until we&#39;re reconnected</pre><pre>while (!client.connected()) {</pre><pre>Serial.print(&quot;Attempting MQTT connection...&quot;);</pre><pre>// Attempt to connect</pre><pre>/*</pre><pre>YOU  NEED TO CHANGE THIS NEXT LINE, IF YOU&#39;RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS</pre><pre>To change the ESP device ID, you will have to give a unique name to the ESP8266.</pre><pre>Here&#39;s how it looks like now:</pre><pre>if (client.connect(&quot;ESP8266Client&quot;)) {</pre><pre>If you want more devices connected to the MQTT broker, you can do it like this:</pre><pre>if (client.connect(&quot;ESPOffice&quot;)) {</pre><pre>Then, for the other ESP:</pre><pre>if (client.connect(&quot;ESPGarage&quot;)) {</pre><pre>That should solve your MQTT multiple connections problem</pre><pre>THE SECTION IN loop() function should match your device name</pre><pre>*/<br>if (client.connect(&quot;ESP8266Client&quot;)) {</pre><pre>Serial.println(&quot;connected&quot;);</pre><pre>// Subscribe or resubscribe to a topic</pre><pre>// You can subscribe to more topics (to control more LEDs in this example)</pre><pre>} else {</pre><pre>Serial.print(&quot;failed, rc=&quot;);</pre><pre>Serial.print(client.state());</pre><pre>Serial.println(&quot; try again in 5 seconds&quot;);</pre><pre>// Wait 5 seconds before retrying</pre><pre>delay(5000);</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>// The setup function sets your DHT sensor, starts the serial communication at a baud rate of 115200</pre><pre>// Sets your mqtt broker and sets the callback function</pre><pre>// The callback function is what receives messages and actually controls the LEDs</pre><pre>void setup() {</pre><pre>dht.begin();</pre><pre>Serial.begin(115200);</pre><pre>setup_wifi();</pre><pre>client.setServer(mqtt_server, 1883);</pre><pre>client.setCallback(callback);</pre><pre>}</pre><pre>// For this project, you don&#39;t need to change anything in the loop function.</pre><pre>// Basically it ensures that you ESP is connected to your broker</pre><pre>void loop() {</pre><pre>if (!client.connected()) {</pre><pre>reconnect();</pre><pre>}</pre><pre>if(!client.loop())<br>/*</pre><pre>YOU  NEED TO CHANGE THIS NEXT LINE, IF YOU&#39;RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS</pre><pre>To change the ESP device ID, you will have to give a unique name to the ESP8266.</pre><pre>Here&#39;s how it looks like now:</pre><pre>client.connect(&quot;ESP8266Client&quot;);</pre><pre>If you want more devices connected to the MQTT broker, you can do it like this:</pre><pre>client.connect(&quot;ESPOffice&quot;);</pre><pre>Then, for the other ESP:</pre><pre>client.connect(&quot;ESPGarage&quot;);</pre><pre>That should solve your MQTT multiple connections problem</pre><pre>THE SECTION IN recionnect() function should match your device name</pre><pre>*/</pre><pre>client.connect(&quot;ESP8266Client&quot;);</pre><pre>now = millis();</pre><pre>// Publishes new temperature and humidity every 30 seconds</pre><pre>if (now - lastMeasure &gt; 10000) {</pre><pre>lastMeasure = now;</pre><pre>// Sensor readings may also be up to 2 seconds &#39;old&#39; (its a very slow sensor)</pre><pre>float h = dht.readHumidity();</pre><pre>// Read temperature as Celsius (the default)</pre><pre>float t = dht.readTemperature();</pre><pre>// Read temperature as Fahrenheit (isFahrenheit = true)</pre><pre>float f = dht.readTemperature(true);</pre><pre>// Check if any reads failed and exit early (to try again).</pre><pre>if (isnan(h) || isnan(t) || isnan(f)) {</pre><pre>Serial.println(&quot;Failed to read from DHT sensor!&quot;);</pre><pre>return;</pre><pre>}</pre><pre>// Computes temperature values in Celsius</pre><pre>float hic = dht.computeHeatIndex(t, h, false);</pre><pre>static char temperatureTemp[7];</pre><p>Step10: ต่อวงจรดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*VMO6Cp08agAnMfjR0R5Czg.png" /></figure><p>Step11: Launching the Web Server</p><pre>cd web-server &amp;&amp; sudo python app.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8lCYi-nO5rj8Zw44gZ1W4w.png" /></figure><p>แล้วไปที่ Rpi IP Address:8181 เพื่อมอนิเตอร์ค่าที่ส่งมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*xP9RThXL75vGZ9UZQ4Awgw.png" /></figure><p><strong>Q06: Send Data to Firebase</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76a78ee04e5d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q04-Blynk IoT Application]]></title>
            <link>https://medium.com/@yongyutsrisuban/q04-blynk-iot-application-bb2f8a749fe6?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb2f8a749fe6</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 14:07:54 GMT</pubDate>
            <atom:updated>2019-06-27T14:07:54.531Z</atom:updated>
            <content:encoded><![CDATA[<p>ทดสอบ Blynk จาก “Blynk Server” ด้วย ESP-32</p><p>Step1: ดาวน์โหลดแอป Blynk ค้นหาที่ Play store สำหรับAndroid และ Play Store</p><p>สำหรับ android</p><p>Step2: ทำการLogin ถ้ายังไม่มีรหัสสามารถสร้าง Account ขึ้นมาใหม่ได้หรือซิงค์ กับ facebook</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*h_IJpHaMDuRTOn9y5kTp6g.png" /></figure><p>Step3: New Project แล้วเลือกอุปกรณ์ที่ต้องการเชื่อมต่อ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/1*vA_Ckp9U0jtgM-RIHtKAkg.png" /></figure><p>Step4: ติดตั้ง Library Blynk ที่ Arduino IDE คลิกที่ <br> Sketch &gt;&gt; Include Library &gt;&gt; Manage Library ค้นหา Blynk</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*iUEQEzsJOtoDNJRApZifYA.png" /></figure><p>Step5: เปิดไฟล์ blynk example ตามอุปกรณ์ที่ใช้ <br> คลิก File &gt;&gt; Examples &gt;&gt; Blynk &gt;&gt; Board_WiFi &gt;&gt; ESP32_wifi</p><p>Step6: ทำการแก้ไป ssid ,pass ของ wifi ที่ใช้เชื่อมต่อ และ authToken</p><p>หลังจากสร้าง Project authToken จะถูกส่งไปยัง email ที่ใช้ในการlogin</p><p>Step7: เลือกฟังก์ชันการทำงานในแอป Blynk แล้วเชื่อม <br> เลือก Button &gt;&gt; เลือก pin = Gp2 &gt;&gt; Switch &gt;&gt; connect</p><p><strong>ทดสอบ Blynk จาก “Blynk Server” ด้วย RPi (RPi in Client Mode)</strong></p><p>Step1: ติดตั้ง Blynk Library <br> Add repositories:</p><pre>curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash –</pre><p>Install Node.js:</p><pre>sudo apt-get update &amp;&amp; sudo apt-get upgrade</pre><pre>sudo apt-get install build-essential nodejs –y</pre><p>Install Blynk globally:</p><pre>sudo npm install -g npm  sudo npm</pre><pre>install -g onoff</pre><pre>sudo npm install -g blynk-library</pre><p>Step2: New Project แล้วเลือกอุปกรณ์ที่ต้องการเชื่อมต่อ() แล้วไปรับ AuthToken</p><p>Step3: Run Blynk blynk-client “your auth token”</p><p><strong>ติดตั้งและทดสอบ Blynk Broker (RPi in Server Mode)</strong></p><p>Step1: ตรวจสอบ java</p><pre>java –version</pre><p>Step2: ติดตั้ง java jdk</p><pre>sudo apt-get install oracle-java8-jdk</pre><p>step3: Download blynk server ไปที่ <a href="https://github.com/blynkkk/blynk-server">https://github.com/blynkkk/blynk-server</a> <br> เพื่อรับเวอรชั่นล่าสุด</p><pre>wget &quot;https://github.com/blynkkk/blynk-server/releases/download/v0.41.5/server0.41.5-java8.jar&quot;</pre><p>step4: Start Blynk Server</p><pre>java -jar server-0.34.0-java8.jar  -dataFolder /home/pi/Blynk</pre><p>Step5: login app Blynk Custom Mode</p><p>username : admin@blynk.cc <br> password : admin <br> ip : net วงเดียวกัน <br> port : ตามไฟล์config เสร็จแล้วเราจะมีEnergy มากกว่า Blynk mode</p><p>Step6: ไปที่ <a href="https://RapiiIP:9443/admin">https://RapiiIP:9443/admin</a> เพื่อรับAuthToken</p><p>Step7: ทำการแก้ไป ssid ,pass ของwifi ที่ใช้เชื่อมต่อ, authToken , Rpi Ip Address ,port</p><p><strong>Q05: SQLite Database on a Raspberry Pi</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb2f8a749fe6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q03 — MQTT Protocol and IoTs Concept]]></title>
            <link>https://medium.com/@yongyutsrisuban/q03-mqtt-protocol-and-iots-concept-d5a8bbf5e85b?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d5a8bbf5e85b</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 14:04:01 GMT</pubDate>
            <atom:updated>2019-06-27T14:04:01.323Z</atom:updated>
            <content:encoded><![CDATA[<h3>Q03 — MQTT Protocol and IoTs Concept</h3><p>ทดสอบ iot eclips broker ด้วย MQTT Lens</p><p>Step1: ไปที่เพื่อติดตั้ง</p><pre><a href="https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?hl=th">https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?hl=th</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*EmbFmwoSf9RmXpa8760mUQ.png" /></figure><p>Step2: ไปที่ chrome://apps/ หรือค้นหาใน start windows คลิก MQTTLens</p><p>Step3: เพิ่ม Connections โดยตั้งค่าดังนี้ <br> Connection name &gt;&gt; ตั้งชื่อ<br> Hostname &gt;&gt; tcp:// iot.eclipse.org <br> Post &gt;&gt; 1883 <br> แล้วคลิก Create Connection จะได้หน้านี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*oYyVCyVIluweBjRqgvM7rA.png" /></figure><p>Step4: กำหนด Subscribe เพื่อสร้าง Channel ในการสื่อสาร</p><p>Step5: ส่ง Massage ไปยัง Subscribe ที่ต้องการ โดยระบุ ชื่อ และข้อความที่จะส่งแล้ว คลิก puglish เพื่อส่งข้อความ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*T4sSVh75GtUB-LBa9ugrvA.png" /></figure><p><strong>ทดสอบ “iot eclips broker” ด้วย ESP8266</strong></p><p>Step1: ติดตั้ง Library PubSubClient ใน Arduiono IDE โดย</p><p>คลิกที่ Sketch &gt;&gt; Include Library &gt;&gt; Manage Library</p><p>Step2: ค้น PubSubClient เลือกเวอร์ชัน (ใหม่สุด) แล้วทำการติดตั้ง</p><p>Step3: เขียนCode สำหรับเชื่อมต่อดังนี้ แล้วทำการอัพโหลดลง Wemos</p><pre>// Test Code</pre><pre>//#include &lt;WiFi.h&gt; // ESP32 Board</pre><pre>#include &lt;ESP8266WiFi.h&gt; // ESP8266, WeMos</pre><pre>#include &lt;PubSubClient.h&gt;</pre><pre>const char* ssid = &quot;SisterSister&quot;;</pre><pre>const char* password = &quot;123456789&quot;;</pre><pre>// const char* mqtt_server = &quot;iot.eclipse.org&quot;;</pre><pre>IPAddress My_mqtt_server(192,168,43,240);</pre><pre>const char* UName = &quot;mymqtt&quot;;</pre><pre>const char* PassW = &quot;myraspi&quot;;</pre><pre>const char* topic1 = &quot;mynew/test&quot;;</pre><pre>int TestLED = D4;</pre><pre>WiFiClient espClient;</pre><pre>PubSubClient client(espClient);</pre><pre>long lastMsg = 0;</pre><pre>char msg[50];</pre><pre>int value = 0;</pre><pre>void setup_wifi() {</pre><pre>delay(10);</pre><pre>Serial.println();</pre><pre>Serial.print(&quot;Connecting to &quot;);</pre><pre>Serial.println(ssid);</pre><pre>WiFi.begin(ssid, password);</pre><pre>while (WiFi.status() != WL_CONNECTED) {</pre><pre>delay(500);</pre><pre>Serial.print(&quot;.&quot;);</pre><pre>}</pre><pre>randomSeed(micros());</pre><pre>Serial.println(&quot;&quot;);</pre><pre>Serial.println(&quot;WiFi connected&quot;);</pre><pre>Serial.println(&quot;IP address: &quot;);</pre><pre>Serial.println(WiFi.localIP());</pre><pre>pinMode(TestLED, OUTPUT);</pre><pre>}<br>void callback(char* topic, byte* payload, unsigned int length)</pre><pre>{ Serial.print(&quot;Message arrived [&quot;);</pre><pre>Serial.print(topic1);</pre><pre>Serial.print(&quot;] &quot;);</pre><pre>for (int i = 0; i &lt; length; i++)</pre><pre>{ Serial.print((char)payload[i]);</pre><pre>}</pre><pre>Serial.println();</pre><pre>Serial.print(&quot; ---&gt; &quot;);</pre><pre>Serial.println((char)payload[0]);</pre><pre>if ((char)payload[0] == &#39;o&#39;) digitalWrite(TestLED, HIGH);</pre><pre>if ((char)payload[0] == &#39;f&#39;) digitalWrite(TestLED, LOW);</pre><pre>}</pre><pre>void reconnect()</pre><pre>{ while (!client.connected()) // Loop until we&#39;re reconnected</pre><pre>{ Serial.print(&quot;Attempting MQTT connection...&quot;);</pre><pre>String clientId = &quot;ESP8266Client-&quot;;</pre><pre>clientId += String(random(0xffff), HEX); // Create a random client ID</pre><pre>if (client.connect(clientId.c_str(), UName, PassW)) // Attempt to connect</pre><pre>{ Serial.println(&quot;connected&quot;); // Once connected, publish an announcement...</pre><pre>client.publish(topic1, &quot;Hello World Pk007&quot;); // ... and resubscribe</pre><pre>client.subscribe(topic1);</pre><pre>} else</pre><pre>{ Serial.print(&quot;failed, rc=&quot;);</pre><pre>Serial.print(client.state());</pre><pre>Serial.println(&quot; try again in 5 seconds&quot;);</pre><pre>delay(5000);</pre><pre>}</pre><pre>}</pre><pre>}<br>void setup()</pre><pre>{ Serial.begin(115200);</pre><pre>setup_wifi();</pre><pre>client.setServer(My_mqtt_server, 1883);</pre><pre>client.setCallback(callback);</pre><pre>pinMode(TestLED, OUTPUT);</pre><pre>}</pre><pre>void loop()</pre><pre>{ if (!client.connected()) reconnect();</pre><pre>client.loop();</pre><pre>long now = millis();</pre><pre>if (now - lastMsg &gt; 2000)</pre><pre>{ lastMsg = now;</pre><pre>++value;</pre><pre>snprintf (msg, 75, &quot;hello world #%ld&quot;, value);</pre><pre>Serial.print(&quot;Publish message: &quot;);</pre><pre>Serial.println(msg);</pre><pre>client.publish(topic1, msg);</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*cC2C4giV6467Wdc_IjzPyQ.png" /></figure><p>โปรแกรมส่งข้อมความไปยัง MQTT</p><p>Step4: ต่อวงจรดังนี้ <br> D4 &gt;&gt; LED</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/1*RuMzOJuPwbqmgDnzDIhgvg.png" /></figure><p>Step5: ทดลองส่ง Message แล้วเปิด Serial Monitor <br> ส่ง o เพื่อเปิดไฟ</p><p>ส่ง f เพื่อปิดไฟ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*c3QRNtDGgF8I8CVGp9Csxw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*2233LpXgJ0axHMHrvo2zyQ.png" /></figure><p><strong>ติดตั้งและทดสอบ Mosquito MQTT Broker</strong></p><p>Step1: ป้อนคำสั่งเพื่อ Update/ Upgrade Raspbian และ เริ่มบูทเครื่องใหม่</p><pre>sudo apt-get update</pre><pre>sudo apt-get upgrade</pre><pre>sudo reboot</pre><p>Step2: สร้างไดเรกเทอรี่ สำหรับ mosquitto</p><pre>mkdir mosquitto</pre><pre>cd mosquitto</pre><p>Step3: ดาวน์โหลด Key mosquitto</p><pre>wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key</pre><pre>sudo apt-key add mosquitto-repo.gpg.key</pre><p>Step4: enable Key mosquitto</p><pre>cd /etc/apt/sources.list.d/</pre><pre>sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list</pre><pre>sudo apt-get update</pre><pre>sudo apt-cache search mosquitto</pre><p>Step5: install mosquitto</p><pre>sudo apt-get install mosquitto</pre><pre>sudo apt-get install mosquito-clients</pre><p>Step6: ตรวจสอบสถานะของ mosquitto</p><pre>service mosquitto status</pre><ul><li>ตรงนี้จะเข้าvim วิธีออกจากvim กด~ที่คีย์บอร์ดหลายๆที ตามพิมพ์ :q แล้ว Enter</li></ul><pre>ps -ef | grep mosq</pre><pre>netstat -tln | grep 1883</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*hmiCtIyQQaLIdUIzqu2_nA.png" /></figure><p>Step7: แก้ไขไฟล์configuretionโดยเพิ่มบรรทัดใหม่ 3 บรรทัดที่ด้านล่างสุดของไฟล์ดังนี้</p><pre>sudo nano /etc/mosquitto/mosquitto.conf</pre><p>เพิ่ม code ดังต่อไปนี้ในบรรทัดล่างสุด</p><pre>allow_ananymous false</pre><pre>password_file /etc/mosquito/pwfile</pre><pre>listener 1883</pre><ul><li>บางกรณีต้อง ลบหรือคอมเม้น บรรทัด include_dir /etc/mosqitto/conf.d</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*YwTwcBr97ZWjSb2tK6E-MQ.png" /></figure><p>Step 8: สร้างยูสเซอร์ชื่อ mymqtt และกำหนดพาสเวิร์ดเป็น myraspi หรือพาสเวิร์ดอื่นๆ เก็บไว้ใน pwfile โดย ป้อนคำสั่ง</p><pre>sudo mosquitto_passwd -c /etc/mosquitto/pwfile mymqtt</pre><p>Step9: หลังจากนั้นทำการ Reboot ด้วยคำสั่ง</p><pre>sudo reboot</pre><p>Step10: ทดสอบการทำงานโดยเปิดTerminal 2 หน้าต่าง ใช้คำ สั่งดังนี้</p><pre>Ternimal 1(sub) &gt;&gt; mosquitto_sub -d -u mymqtt -P myraspi -t mynew/test</pre><pre>Ternimal 2(pub) &gt;&gt;mosquitto_pub -d -u mymqtt -P myraspi -t mynew/test -m “Test1234”</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*EC_nzfULqru_0PiSYiA0iQ.png" /></figure><p><strong>ทดสอบ “mosquito broker” ด้วย MQTT Lens</strong></p><p>Step 1: ทดสอบการทำงานโดยเปิดTerminal ใช้คำ สั่งดังนี้</p><pre>mosquitto_sub -d -u mymqtt -P myraspi -t mynew/test</pre><p>Step 2 : เพิ่ม Connections โดยตั้งค่าดังนี้</p><p>Connection name &gt;&gt; ตั้งชื่อ<br> Hostname &gt;&gt; (Rpi IP Address) <br> Post &gt;&gt; 1883 <br> Username &gt;&gt; mymqtt <br> Password &gt;&gt; myraspi</p><p>Step 3: กำหนด Subscribe เพื่อสร้าง Channel ในการสื่อสารส่ง Massage ไปยัง Subscribe ที่ต้องการ โดยระบุ ชื่อ และข้อความที่จะส่งแล้ว คลิก publish</p><p>Subscribe &gt;&gt; mynew/test <br> Publish &gt;&gt; mynew/test <br> Message &gt;&gt; Hello</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*IJbYb7TgX2MIDOsARyhLDw.png" /></figure><p>ข้อความจะปรากฎใน Terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*3x7PZSbNZQeFDa4RmWmeww.png" /></figure><p><strong>Q04-Blynk IoT Application</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d5a8bbf5e85b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q02 : All Over IP Age — NodeRED]]></title>
            <link>https://medium.com/@yongyutsrisuban/q02-all-over-ip-age-nodered-89b72f31cd31?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/89b72f31cd31</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 13:57:29 GMT</pubDate>
            <atom:updated>2019-06-27T13:57:29.132Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Q02 : All Over IP Age — NodeRED</strong></h3><p>· ทดสอบ Control Over IP &gt;&gt; with NODE RED</p><p><strong>การติดตั้ง Node Red</strong></p><p>Step 1: ติดตั้ง node red ด้วยคำสั่ง</p><pre>bash &lt;(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-ackage/master/resources/update-nodejs-and-nodered)</pre><p>Step 2: ติดตั้ง node red ui ด้วยคำสั่ง</p><pre>cd $HOME/.node-red</pre><pre>npm install node-red-contrib-ui</pre><p>Step3: Run Node-Red ด้วยคำสั่ง</p><pre>node-red-start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*W01-zpe1YfOA8kuuKc4IJg.png" /></figure><p>Step4: ไปที่ Rpi IP Address:1880</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*FVM_o4O3sbPP48cCoFcPmA.png" /></figure><p>Step5: ลากFlow ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*KfP-BcEdIj8vtXLeBa430g.png" /></figure><p>Step6: ตั้งค่า button Setting ดังนี้</p><p>Tab &gt;&gt; Home <br> Name &gt;&gt; Toggle Power <br> Payload &gt;&gt; 1 <br> Group &gt;&gt; Control LED <br> แล้วกด Done</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*afLR81P0i3ynvCLwi03eeQ.png" /></figure><p>Step7: ที่ function ตัวซ้ายเขียนฟังก์ชันสำหรับรับค่าจาก ui แล้วส่งค่าไปยัง LED <br> ตั้งชื่อว่า Routing แล้วใส่ครับสั่ง Code ดังนี้</p><pre>var i=context.get(&#39;i&#39;) || 0;</pre><pre>var payload=msg.payload;</pre><pre>context.set(&quot;i&quot;,Math.abs(i-payload));</pre><pre>var payload=msg.payload;</pre><pre>msg.payload=i*20;</pre><pre>return msg;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*WxAEtEOWx4P_qYvnvjxjRA.png" /></figure><p>Step8: Slider Setting ดังนี้</p><p>Tab &gt;&gt; Home (ถ้าไม่มีกดตรงรูปดนิสอ) <br> Min &gt;&gt; 0 <br> Max &gt;&gt; 20 <br> Group &gt;&gt; Control LED <br> แล้วกด Done</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*R8NgmYlEZWxB3sTNNumo6g.png" /></figure><p>Step9: ตั้งค่า Chart ดังนี้</p><p>Tab &gt;&gt; Home <br> Group &gt;&gt; Control LED <br> Old after &gt;&gt; 15 , unit &gt;&gt; sec(s) <br> แล้วกด Done</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*lsWpUPigIIdFniw4-xWmKA.png" /></figure><p>Step10: ที่ function ตัวขวาทำการเขียนฟังก์ชันสำหรับรับค่าจาก function Routing แล้วส่งค่าไปยัง Ui Text ตั้งชื่อว่า Read Status<br> Code :</p><pre>var status = &quot;on&quot;;</pre><pre>if(msg.payload === 1 || msg.payload &gt;0 || msg.payload === true ){</pre><pre>status = &quot;on&quot;;</pre><pre>msg.payload = status;</pre><pre>}</pre><pre>else{</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*znApl-nB_z1rC1nesn_Plg.png" /></figure><p>Step11: ไปที่ Rpi IP address :1880/ui แล้วทดลองควบคุมการทำงาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*2K_2ZuYLeSUd8pmHa_mepQ.png" /></figure><p><strong>Q03 — MQTT Protocol and IoTs Concept</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89b72f31cd31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[2. RPi — IoTs]]></title>
            <link>https://medium.com/@yongyutsrisuban/2-rpi-iots-166cb44a12bd?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/166cb44a12bd</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 13:53:43 GMT</pubDate>
            <atom:updated>2019-06-27T15:28:12.782Z</atom:updated>
            <content:encoded><![CDATA[<h3>2. RPi — IoTs</h3><h3>สำหรับศึกษาในหัวข้อต่างๆ</h3><p><a href="https://medium.com/@yongyutsrisuban/embedded-system-267ceee1665e">1. Install Raspberry Pi</a><br><a href="https://medium.com/@yongyutsrisuban/2-rpi-iots-166cb44a12bd">2. RPi — IoTs</a><br><a href="https://medium.com/@yongyutsrisuban/3-start-fpga-cc92f1807b7">3. Start FPGA</a><br><a href="https://medium.com/@yongyutsrisuban/mission-2-notebook-or-pc-lab-moving-8-led-with-switch-control-c6349b7b86df">4. Mojo V3 — Display 7_Segment and UART Tx</a><br><a href="https://medium.com/@yongyutsrisuban/1-install-raspberry-pi-684a25323b6e">5.FPGA — Timing Coding</a></p><h3><strong>Q01 — All Over IP Age — WebIO Pi</strong></h3><p><strong>การติดตั้ง WebIOpi</strong></p><p>Step 1: ดาวน์โหลดไฟล์ WebIOPi-0.7.1.tar.gz โดยใช้คำสั่ง</p><pre>wget http://sourceforge.net/projects/webiopi/files/WebIOPi-0.7.1.tar.gz</pre><p>Step 2: แตกไฟล์ WebIOPi-0.7.1.tar.gz โดยใช้คำสั่ง</p><pre>tar xvzf WebIOPi-0.7.1.tar.gz</pre><p>Step 3: เข้าไปในโฟลเดอร์ WebIOPi-0.7.1 โดยใชคำสั่ง</p><pre>cd WebIOPi-0.7.1</pre><p>Step 4: ดาวน์โหลด Patch เสริม webiopi-pi2bplus.patch โดยใช้คำสั่ง</p><pre>wget https://raw.githubusercontent.com/doublebind/raspi/master/webiopi-pi2bplus.patch</pre><p>Step 5: ติดตั้ง Patch webiopi-pi2bplus.patch โดยใช้คำสั่ง</p><pre>cd WebIOPi-0.7.1</pre><p>Step 6: ติดตั้ง WebIOPi-0.7.1 โดยใชคำสั่ง</p><pre>sudo ./setup.sh</pre><p>Step 7: Reboot ระบบโดยใชคำสั่ง</p><pre>sudo reboot</pre><p><strong>การเริ่มใช้งาน WebIOpi</strong></p><p>Step 1: เริ่มต้นการทำงาน WebIOpi โดยใชคำสั่ง</p><pre>sudo /etc/init.d/webiopi start</pre><p>ถ้าหยุดการทำงานโดยการใชคำสั่ง</p><pre>sudo /etc/init.d/webiopi stop</pre><p>Step 2: ไปที่ Rpi IP address:8000 เพื่อทำการ Login โดยใช้ Username และ Password เริ่มต้น</p><p>Username: webiopi</p><p>Password: raspberry</p><p>Step 3: เมื่อ login สำเร็จจะปรากฏหน้าเว็บดังนี้ <br> &gt;&gt; เลือก GPIO Header</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*IvnuGpeu2MyFPXkjZR4G-w.png" /></figure><p>Step 4: ต่อวงจร</p><p>GPIO18 = OUTPUT &gt;&gt; LED</p><p>GPIO93 = GROUND</p><p>Step 5: คลิกที่ช่อง GPIO เพื่อเปลี่ยนสถานะ</p><p>· off</p><p>· on</p><p><strong>ผลการทดลอง</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*KHYOlGqJKiJSdCdEOm0cEw.png" /></figure><p><strong>การทำ Start/Stop WebIOPi</strong></p><p>คําสั่งการทํางาน WebIOPi</p><pre>sudo /etc/init.d/webiopi start</pre><p>คําสั่งหยุดการทํางาน WebIOPi</p><pre>sudo /etc/init.d/webiopi stop</pre><p><strong>การทำ Auto start WebIopi at boot</strong></p><p>คำสั่งเปิดใช้ Auto Start WebIOPi</p><pre>sudo update-rc.d webiopi defaults</pre><p>คำสั่งยกเลิก Auto Start WebIOPi</p><pre>sudo update-rc.d webiopi remove</pre><p>การแก้ไข configure ของ webiopi</p><p>เข้าไปแก้ config ของ WebIO Pi ด้วยคำสั่ง</p><pre>sudo nano /etc/webiopi/config</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*0trXAJ65sozblYbfquRGLw.png" /></figure><p>แก้ไข Password ของ Web io</p><p>&gt;&gt; เปลี่ยน Password WebIOPI</p><pre>sudo webiopi-passwd</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*XCS_XK98IMlSfZ7lK8Da9w.png" /></figure><p>ยกเลิก Password ของ Web io</p><p>Step1: คำสั่งยกเลิก Password WebIOPI</p><pre>sudo nano /etc/webiopi/config</pre><p>Step2: แก้ไขบรรทัด passwd-file = /etc/webiopi/passwd ใส่เครื่องหมาย # ข้างหน้า</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*_ZbND7QG5MSRVRtD-3Z8CQ.png" /></figure><h3>Q02 : All Over IP Age — NodeRED</h3><p>· ทดสอบ Control Over IP &gt;&gt; with NODE RED</p><p><strong>การติดตั้ง Node Red</strong></p><p>Step 1: ติดตั้ง node red ด้วยคำสั่ง</p><pre>bash &lt;(curl -sL <a href="https://raw.githubusercontent.com/node-red/raspbian-deb-ackage/master/resources/update-nodejs-and-nodered)">https://raw.githubusercontent.com/node-red/raspbian-deb-ackage/master/resources/update-nodejs-and-nodered)</a></pre><p>Step 2: ติดตั้ง node red ui ด้วยคำสั่ง</p><pre>cd $HOME/.node-red</pre><pre>npm install node-red-contrib-ui</pre><p>Step3: Run Node-Red ด้วยคำสั่ง</p><pre>node-red-start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*W01-zpe1YfOA8kuuKc4IJg.png" /></figure><p>Step4: ไปที่ Rpi IP Address:1880</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*FVM_o4O3sbPP48cCoFcPmA.png" /></figure><p>Step5: ลากFlow ดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*KfP-BcEdIj8vtXLeBa430g.png" /></figure><p>Step6: ตั้งค่า button Setting ดังนี้</p><p>Tab &gt;&gt; Home <br> Name &gt;&gt; Toggle Power <br> Payload &gt;&gt; 1 <br> Group &gt;&gt; Control LED <br> แล้วกด Done</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*afLR81P0i3ynvCLwi03eeQ.png" /></figure><p>Step7: ที่ function ตัวซ้ายเขียนฟังก์ชันสำหรับรับค่าจาก ui แล้วส่งค่าไปยัง LED <br> ตั้งชื่อว่า Routing แล้วใส่ครับสั่ง Code ดังนี้</p><pre>var i=context.get(&#39;i&#39;) || 0;</pre><pre>var payload=msg.payload;</pre><pre>context.set(&quot;i&quot;,Math.abs(i-payload));</pre><pre>var payload=msg.payload;</pre><pre>msg.payload=i*20;</pre><pre>return msg;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*WxAEtEOWx4P_qYvnvjxjRA.png" /></figure><p>Step8: Slider Setting ดังนี้</p><p>Tab &gt;&gt; Home (ถ้าไม่มีกดตรงรูปดนิสอ) <br> Min &gt;&gt; 0 <br> Max &gt;&gt; 20 <br> Group &gt;&gt; Control LED <br> แล้วกด Done</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*R8NgmYlEZWxB3sTNNumo6g.png" /></figure><p>Step9: ตั้งค่า Chart ดังนี้</p><p>Tab &gt;&gt; Home <br> Group &gt;&gt; Control LED <br> Old after &gt;&gt; 15 , unit &gt;&gt; sec(s) <br> แล้วกด Done</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*lsWpUPigIIdFniw4-xWmKA.png" /></figure><p>Step10: ที่ function ตัวขวาทำการเขียนฟังก์ชันสำหรับรับค่าจาก function Routing แล้วส่งค่าไปยัง Ui Text ตั้งชื่อว่า Read Status<br> Code :</p><pre>var status = &quot;on&quot;;</pre><pre>if(msg.payload === 1 || msg.payload &gt;0 || msg.payload === true ){</pre><pre>status = &quot;on&quot;;</pre><pre>msg.payload = status;</pre><pre>}</pre><pre>else{</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*znApl-nB_z1rC1nesn_Plg.png" /></figure><p>Step11: ไปที่ Rpi IP address :1880/ui แล้วทดลองควบคุมการทำงาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*2K_2ZuYLeSUd8pmHa_mepQ.png" /></figure><h3>Q03 — MQTT Protocol and IoTs Concept</h3><p>ทดสอบ iot eclips broker ด้วย MQTT Lens</p><p>Step1: ไปที่เพื่อติดตั้ง</p><pre><a href="https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?hl=th">https://chrome.google.com/webstore/detail/mqttlens/hemojaaeigabkbcookmlgmdigohjobjm?hl=th</a></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*EmbFmwoSf9RmXpa8760mUQ.png" /></figure><p>Step2: ไปที่ chrome://apps/ หรือค้นหาใน start windows คลิก MQTTLens</p><p>Step3: เพิ่ม Connections โดยตั้งค่าดังนี้ <br> Connection name &gt;&gt; ตั้งชื่อ<br> Hostname &gt;&gt; tcp:// iot.eclipse.org <br> Post &gt;&gt; 1883 <br> แล้วคลิก Create Connection จะได้หน้านี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*oYyVCyVIluweBjRqgvM7rA.png" /></figure><p>Step4: กำหนด Subscribe เพื่อสร้าง Channel ในการสื่อสาร</p><p>Step5: ส่ง Massage ไปยัง Subscribe ที่ต้องการ โดยระบุ ชื่อ และข้อความที่จะส่งแล้ว คลิก puglish เพื่อส่งข้อความ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*T4sSVh75GtUB-LBa9ugrvA.png" /></figure><p><strong>ทดสอบ “iot eclips broker” ด้วย ESP8266</strong></p><p>Step1: ติดตั้ง Library PubSubClient ใน Arduiono IDE โดย</p><p>คลิกที่ Sketch &gt;&gt; Include Library &gt;&gt; Manage Library</p><p>Step2: ค้น PubSubClient เลือกเวอร์ชัน (ใหม่สุด) แล้วทำการติดตั้ง</p><p>Step3: เขียนCode สำหรับเชื่อมต่อดังนี้ แล้วทำการอัพโหลดลง Wemos</p><pre>// Test Code</pre><pre>//#include &lt;WiFi.h&gt; // ESP32 Board</pre><pre>#include &lt;ESP8266WiFi.h&gt; // ESP8266, WeMos</pre><pre>#include &lt;PubSubClient.h&gt;</pre><pre>const char* ssid = &quot;SisterSister&quot;;</pre><pre>const char* password = &quot;123456789&quot;;</pre><pre>// const char* mqtt_server = &quot;iot.eclipse.org&quot;;</pre><pre>IPAddress My_mqtt_server(192,168,43,240);</pre><pre>const char* UName = &quot;mymqtt&quot;;</pre><pre>const char* PassW = &quot;myraspi&quot;;</pre><pre>const char* topic1 = &quot;mynew/test&quot;;</pre><pre>int TestLED = D4;</pre><pre>WiFiClient espClient;</pre><pre>PubSubClient client(espClient);</pre><pre>long lastMsg = 0;</pre><pre>char msg[50];</pre><pre>int value = 0;</pre><pre>void setup_wifi() {</pre><pre>delay(10);</pre><pre>Serial.println();</pre><pre>Serial.print(&quot;Connecting to &quot;);</pre><pre>Serial.println(ssid);</pre><pre>WiFi.begin(ssid, password);</pre><pre>while (WiFi.status() != WL_CONNECTED) {</pre><pre>delay(500);</pre><pre>Serial.print(&quot;.&quot;);</pre><pre>}</pre><pre>randomSeed(micros());</pre><pre>Serial.println(&quot;&quot;);</pre><pre>Serial.println(&quot;WiFi connected&quot;);</pre><pre>Serial.println(&quot;IP address: &quot;);</pre><pre>Serial.println(WiFi.localIP());</pre><pre>pinMode(TestLED, OUTPUT);</pre><pre>}<br>void callback(char* topic, byte* payload, unsigned int length)</pre><pre>{ Serial.print(&quot;Message arrived [&quot;);</pre><pre>Serial.print(topic1);</pre><pre>Serial.print(&quot;] &quot;);</pre><pre>for (int i = 0; i &lt; length; i++)</pre><pre>{ Serial.print((char)payload[i]);</pre><pre>}</pre><pre>Serial.println();</pre><pre>Serial.print(&quot; ---&gt; &quot;);</pre><pre>Serial.println((char)payload[0]);</pre><pre>if ((char)payload[0] == &#39;o&#39;) digitalWrite(TestLED, HIGH);</pre><pre>if ((char)payload[0] == &#39;f&#39;) digitalWrite(TestLED, LOW);</pre><pre>}</pre><pre>void reconnect()</pre><pre>{ while (!client.connected()) // Loop until we&#39;re reconnected</pre><pre>{ Serial.print(&quot;Attempting MQTT connection...&quot;);</pre><pre>String clientId = &quot;ESP8266Client-&quot;;</pre><pre>clientId += String(random(0xffff), HEX); // Create a random client ID</pre><pre>if (client.connect(clientId.c_str(), UName, PassW)) // Attempt to connect</pre><pre>{ Serial.println(&quot;connected&quot;); // Once connected, publish an announcement...</pre><pre>client.publish(topic1, &quot;Hello World Pk007&quot;); // ... and resubscribe</pre><pre>client.subscribe(topic1);</pre><pre>} else</pre><pre>{ Serial.print(&quot;failed, rc=&quot;);</pre><pre>Serial.print(client.state());</pre><pre>Serial.println(&quot; try again in 5 seconds&quot;);</pre><pre>delay(5000);</pre><pre>}</pre><pre>}</pre><pre>}<br>void setup()</pre><pre>{ Serial.begin(115200);</pre><pre>setup_wifi();</pre><pre>client.setServer(My_mqtt_server, 1883);</pre><pre>client.setCallback(callback);</pre><pre>pinMode(TestLED, OUTPUT);</pre><pre>}</pre><pre>void loop()</pre><pre>{ if (!client.connected()) reconnect();</pre><pre>client.loop();</pre><pre>long now = millis();</pre><pre>if (now - lastMsg &gt; 2000)</pre><pre>{ lastMsg = now;</pre><pre>++value;</pre><pre>snprintf (msg, 75, &quot;hello world #%ld&quot;, value);</pre><pre>Serial.print(&quot;Publish message: &quot;);</pre><pre>Serial.println(msg);</pre><pre>client.publish(topic1, msg);</pre><pre>}</pre><pre>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*cC2C4giV6467Wdc_IjzPyQ.png" /></figure><p>โปรแกรมส่งข้อมความไปยัง MQTT</p><p>Step4: ต่อวงจรดังนี้ <br> D4 &gt;&gt; LED</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/294/1*RuMzOJuPwbqmgDnzDIhgvg.png" /></figure><p>Step5: ทดลองส่ง Message แล้วเปิด Serial Monitor <br> ส่ง o เพื่อเปิดไฟ</p><p>ส่ง f เพื่อปิดไฟ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*c3QRNtDGgF8I8CVGp9Csxw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*2233LpXgJ0axHMHrvo2zyQ.png" /></figure><p><strong>ติดตั้งและทดสอบ Mosquito MQTT Broker</strong></p><p>Step1: ป้อนคำสั่งเพื่อ Update/ Upgrade Raspbian และ เริ่มบูทเครื่องใหม่</p><pre>sudo apt-get update</pre><pre>sudo apt-get upgrade</pre><pre>sudo reboot</pre><p>Step2: สร้างไดเรกเทอรี่ สำหรับ mosquitto</p><pre>mkdir mosquitto</pre><pre>cd mosquitto</pre><p>Step3: ดาวน์โหลด Key mosquitto</p><pre>wget <a href="http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key">http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key</a></pre><pre>sudo apt-key add mosquitto-repo.gpg.key</pre><p>Step4: enable Key mosquitto</p><pre>cd /etc/apt/sources.list.d/</pre><pre>sudo wget <a href="http://repo.mosquitto.org/debian/mosquitto-stretch.list">http://repo.mosquitto.org/debian/mosquitto-stretch.list</a></pre><pre>sudo apt-get update</pre><pre>sudo apt-cache search mosquitto</pre><p>Step5: install mosquitto</p><pre>sudo apt-get install mosquitto</pre><pre>sudo apt-get install mosquito-clients</pre><p>Step6: ตรวจสอบสถานะของ mosquitto</p><pre>service mosquitto status</pre><ul><li>ตรงนี้จะเข้าvim วิธีออกจากvim กด~ที่คีย์บอร์ดหลายๆที ตามพิมพ์ :q แล้ว Enter</li></ul><pre>ps -ef | grep mosq</pre><pre>netstat -tln | grep 1883</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*hmiCtIyQQaLIdUIzqu2_nA.png" /></figure><p>Step7: แก้ไขไฟล์configuretionโดยเพิ่มบรรทัดใหม่ 3 บรรทัดที่ด้านล่างสุดของไฟล์ดังนี้</p><pre>sudo nano /etc/mosquitto/mosquitto.conf</pre><p>เพิ่ม code ดังต่อไปนี้ในบรรทัดล่างสุด</p><pre>allow_ananymous false</pre><pre>password_file /etc/mosquito/pwfile</pre><pre>listener 1883</pre><ul><li>บางกรณีต้อง ลบหรือคอมเม้น บรรทัด include_dir /etc/mosqitto/conf.d</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*YwTwcBr97ZWjSb2tK6E-MQ.png" /></figure><p>Step 8: สร้างยูสเซอร์ชื่อ mymqtt และกำหนดพาสเวิร์ดเป็น myraspi หรือพาสเวิร์ดอื่นๆ เก็บไว้ใน pwfile โดย ป้อนคำสั่ง</p><pre>sudo mosquitto_passwd -c /etc/mosquitto/pwfile mymqtt</pre><p>Step9: หลังจากนั้นทำการ Reboot ด้วยคำสั่ง</p><pre>sudo reboot</pre><p>Step10: ทดสอบการทำงานโดยเปิดTerminal 2 หน้าต่าง ใช้คำ สั่งดังนี้</p><pre>Ternimal 1(sub) &gt;&gt; mosquitto_sub -d -u mymqtt -P myraspi -t mynew/test</pre><pre>Ternimal 2(pub) &gt;&gt;mosquitto_pub -d -u mymqtt -P myraspi -t mynew/test -m “Test1234”</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*EC_nzfULqru_0PiSYiA0iQ.png" /></figure><p><strong>ทดสอบ “mosquito broker” ด้วย MQTT Lens</strong></p><p>Step 1: ทดสอบการทำงานโดยเปิดTerminal ใช้คำ สั่งดังนี้</p><pre>mosquitto_sub -d -u mymqtt -P myraspi -t mynew/test</pre><p>Step 2 : เพิ่ม Connections โดยตั้งค่าดังนี้</p><p>Connection name &gt;&gt; ตั้งชื่อ<br> Hostname &gt;&gt; (Rpi IP Address) <br> Post &gt;&gt; 1883 <br> Username &gt;&gt; mymqtt <br> Password &gt;&gt; myraspi</p><p>Step 3: กำหนด Subscribe เพื่อสร้าง Channel ในการสื่อสารส่ง Massage ไปยัง Subscribe ที่ต้องการ โดยระบุ ชื่อ และข้อความที่จะส่งแล้ว คลิก publish</p><p>Subscribe &gt;&gt; mynew/test <br> Publish &gt;&gt; mynew/test <br> Message &gt;&gt; Hello</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*IJbYb7TgX2MIDOsARyhLDw.png" /></figure><p>ข้อความจะปรากฎใน Terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*3x7PZSbNZQeFDa4RmWmeww.png" /></figure><h3>Q04-Blynk IoT Application</h3><p>ทดสอบ Blynk จาก “Blynk Server” ด้วย ESP-32</p><p>Step1: ดาวน์โหลดแอป Blynk ค้นหาที่ Play store สำหรับAndroid และ Play Store</p><p>สำหรับ android</p><p>Step2: ทำการLogin ถ้ายังไม่มีรหัสสามารถสร้าง Account ขึ้นมาใหม่ได้หรือซิงค์ กับ facebook</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*h_IJpHaMDuRTOn9y5kTp6g.png" /></figure><p>Step3: New Project แล้วเลือกอุปกรณ์ที่ต้องการเชื่อมต่อ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/166/1*vA_Ckp9U0jtgM-RIHtKAkg.png" /></figure><p>Step4: ติดตั้ง Library Blynk ที่ Arduino IDE คลิกที่ <br> Sketch &gt;&gt; Include Library &gt;&gt; Manage Library ค้นหา Blynk</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*iUEQEzsJOtoDNJRApZifYA.png" /></figure><p>Step5: เปิดไฟล์ blynk example ตามอุปกรณ์ที่ใช้ <br> คลิก File &gt;&gt; Examples &gt;&gt; Blynk &gt;&gt; Board_WiFi &gt;&gt; ESP32_wifi</p><p>Step6: ทำการแก้ไป ssid ,pass ของ wifi ที่ใช้เชื่อมต่อ และ authToken</p><p>หลังจากสร้าง Project authToken จะถูกส่งไปยัง email ที่ใช้ในการlogin</p><p>Step7: เลือกฟังก์ชันการทำงานในแอป Blynk แล้วเชื่อม <br> เลือก Button &gt;&gt; เลือก pin = Gp2 &gt;&gt; Switch &gt;&gt; connect</p><p><strong>ทดสอบ Blynk จาก “Blynk Server” ด้วย RPi (RPi in Client Mode)</strong></p><p>Step1: ติดตั้ง Blynk Library <br> Add repositories:</p><pre>curl -sL <a href="https://deb.nodesource.com/setup_6.x">https://deb.nodesource.com/setup_6.x</a> | sudo -E bash –</pre><p>Install Node.js:</p><pre>sudo apt-get update &amp;&amp; sudo apt-get upgrade</pre><pre>sudo apt-get install build-essential nodejs –y</pre><p>Install Blynk globally:</p><pre>sudo npm install -g npm  sudo npm</pre><pre>install -g onoff</pre><pre>sudo npm install -g blynk-library</pre><p>Step2: New Project แล้วเลือกอุปกรณ์ที่ต้องการเชื่อมต่อ() แล้วไปรับ AuthToken</p><p>Step3: Run Blynk blynk-client “your auth token”</p><p><strong>ติดตั้งและทดสอบ Blynk Broker (RPi in Server Mode)</strong></p><p>Step1: ตรวจสอบ java</p><pre>java –version</pre><p>Step2: ติดตั้ง java jdk</p><pre>sudo apt-get install oracle-java8-jdk</pre><p>step3: Download blynk server ไปที่ <a href="https://github.com/blynkkk/blynk-server">https://github.com/blynkkk/blynk-server</a> <br> เพื่อรับเวอรชั่นล่าสุด</p><pre>wget &quot;https://github.com/blynkkk/blynk-server/releases/download/v0.41.5/server0.41.5-java8.jar&quot;</pre><p>step4: Start Blynk Server</p><pre>java -jar server-0.34.0-java8.jar  -dataFolder /home/pi/Blynk</pre><p>Step5: login app Blynk Custom Mode</p><p>username : admin@blynk.cc <br> password : admin <br> ip : net วงเดียวกัน <br> port : ตามไฟล์config เสร็จแล้วเราจะมีEnergy มากกว่า Blynk mode</p><p>Step6: ไปที่ <a href="https://RapiiIP:9443/admin">https://RapiiIP:9443/admin</a> เพื่อรับAuthToken</p><p>Step7: ทำการแก้ไป ssid ,pass ของwifi ที่ใช้เชื่อมต่อ, authToken , Rpi Ip Address ,port</p><h3>Q05: SQLite Database on a Raspberry Pi</h3><p>Step1: ติดตั้ง SQLite</p><pre>sudo apt-get install sqlite3</pre><p>Step2: Run Mosquitto broker</p><pre>mosquitto –d</pre><p>Step3: Install Flask</p><pre>sudo apt-get update</pre><pre>sudo apt-get upgrade</pre><pre>sudo apt-get install python-pip python-flask git-core</pre><pre>sudo pip install flask</pre><p>Step4: สร้าง Python web server script</p><pre>mkdir web-server</pre><pre>cd web-server</pre><pre>nano app.py</pre><p>Source Code</p><pre># Created by Rui Santos</pre><pre># Complete project details: <a href="https://randomnerdtutorials.com">https://randomnerdtutorials.com</a></pre><pre>import paho.mqtt.client as mqtt</pre><pre>from flask import Flask, render_template, request</pre><pre>import json</pre><pre>import sqlite3</pre><pre>app = Flask(__name__)</pre><pre>def dict_factory(cursor, row):</pre><pre>d = {}</pre><pre>for idx, col in enumerate(cursor.description):</pre><pre>d[col[0]] = row[idx]</pre><pre>return d</pre><pre># The callback for when the client receives a CONNACK response from the server.</pre><pre>def on_connect(client, userdata, flags, rc):</pre><pre>print(&quot;Connected with result code &quot; + str(rc))</pre><pre># Subscribing in on_connect() means that if we lose the connection and</pre><pre># reconnect then subscriptions will be renewed.</pre><pre>client.subscribe(&quot;/esp8266/dhtreadings&quot;)</pre><pre># The callback for when a PUBLISH message is received from the ESP8266.</pre><pre>def on_message(client, userdata, message):</pre><pre>if message.topic == &quot;/esp8266/dhtreadings&quot;:</pre><pre>print(&quot;DHT readings update&quot;)</pre><pre>#print(message.payload.json())</pre><pre>#print(dhtreadings_json[&#39;temperature&#39;])</pre><pre>#print(dhtreadings_json[&#39;humidity&#39;])</pre><pre>dhtreadings_json = json.loads(message.payload)<br>             # connects to SQLite database. File is named &quot;sensordata.db&quot; without the quotes</pre><pre># WARNING: your database file should be in the same directory of the app.py file or have the correct path</pre><pre>conn=sqlite3.connect(&#39;sensordata.db&#39;)</pre><pre>c=conn.cursor()</pre><pre>c.execute(&quot;&quot;&quot;INSERT INTO dhtreadings (temperature,</pre><pre>humidity, currentdate, currentime, device) VALUES((?), (?), date(&#39;now&#39;),</pre><pre>time(&#39;now&#39;), (?))&quot;&quot;&quot;, (dhtreadings_json[&#39;temperature&#39;],</pre><pre>dhtreadings_json[&#39;humidity&#39;], &#39;esp8266&#39;) )</pre><pre>conn.commit()</pre><pre>conn.close()</pre><pre>mqttc=mqtt.Client()</pre><pre>mqttc.on_connect = on_connect</pre><pre>mqttc.on_message = on_message</pre><pre>mqttc.connect(&quot;localhost&quot;,1883,60)</pre><pre>mqttc.loop_start()</pre><pre>@app.route(&quot;/&quot;)</pre><pre>def main():</pre><pre># connects to SQLite database. File is named &quot;sensordata.db&quot; without the quotes</pre><pre># WARNING: your database file should be in the same directory of the app.py file or have the</pre><pre>correct path</pre><pre>conn=sqlite3.connect(&#39;sensordata.db&#39;)</pre><pre>conn.row_factory = dict_factory</pre><pre>c=conn.cursor()</pre><pre>c.execute(&quot;SELECT * FROM dhtreadings ORDER BY id DESC LIMIT 20&quot;)</pre><pre>readings = c.fetchall()</pre><pre>#print(readings)</pre><pre>return render_template(&#39;main.html&#39;, readings=readings)</pre><pre>if __name__ == &quot;__main__&quot;:</pre><pre>app.run(host=&#39;0.0.0.0&#39;, port=8181, debug=True)</pre><p>Step5: สร้างดาต้าเบส ชื่อ sensordate.db</p><pre>sqlite3 sensordata.db</pre><p>Step6: สร้างตาราง ชื่อ dhtreading</p><pre>.fullschema</pre><pre>CREATE TABLE dhtreadings(id INTEGER PRIMARY KEY AUTOINCREMENT,</pre><pre>temperature NUMERIC, humidity NUMERIC, currentdate DATE, currentime TIME,</pre><pre>device TEXT);</pre><p>Step7: สร้างหน้าเว็บ Html</p><pre>mkdir templates</pre><pre>cd templates</pre><pre>nano main.html</pre><p>Source Code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*UBaElFawamBU12fNdx7bwQ.png" /></figure><p>Step8: ติดตั้ง DHT และ Adafruit_Sensor</p><p>ไปที่ <a href="https://github.com/adafruit/DHT-sensor-library/archive/master.zip">https://github.com/adafruit/DHT-sensor-library/archive/master.zip</a></p><p>และ <a href="https://github.com/adafruit/Adafruit_Sensor/archive/master.zip">https://github.com/adafruit/Adafruit_Sensor/archive/master.zip</a></p><p>เพื่อโหลด Library หลังจากนั้นแตกไฟล์ทั้งสอง แล้วย้ายไปที่ C:\Users\Administrator\Documents\Arduino\libraries</p><p>ให้เปลี่ยน ชื่อ DHT-sensor-library-master เป็น DTH</p><p>Step9: coding ESP-8266</p><p><strong>Source code</strong></p><pre>/*****</pre><pre>All the resources for this project:</pre><pre><a href="https://rntlab.com/">https://rntlab.com/</a></pre><pre>*****/</pre><pre>// Loading the ESP8266WiFi library and the PubSubClient library</pre><pre>#include &lt;ESP8266WiFi.h&gt;</pre><pre>#include &lt;PubSubClient.h&gt;</pre><pre>#include &quot;DHT.h&quot;</pre><pre>// Uncomment one of the lines bellow for whatever DHT sensor type you&#39;re using!</pre><pre>//#define DHTTYPE DHT11   // DHT 11</pre><pre>//#define DHTTYPE DHT21   // DHT 21</pre><pre>(AM2301) #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321</pre><pre>// Change the credentials below, so your ESP8266 connects to your router</pre><pre>const char* ssid = &quot;YOUR_SSID&quot;;</pre><pre>const char* password = &quot;YOUR_PASSWORD&quot;;</pre><pre>// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker</pre><pre>const char* mqtt_server = &quot;YOUR_RPi_IP_Address&quot;;</pre><pre>// Initializes the espClient</pre><pre>WiFiClient espClient;</pre><pre>PubSubClient client(espClient);<br>// DHT Sensor</pre><pre>const int DHTPin = 14;</pre><pre>// Initialize DHT sensor.</pre><pre>DHT dht(DHTPin, DHTTYPE);</pre><pre>// Timers auxiliar variables</pre><pre>long now = millis();</pre><pre>long lastMeasure = 0;</pre><pre>char data[80];</pre><pre>// Don&#39;t change the function below. This functions connects your ESP8266 to your router</pre><pre>void setup_wifi() {</pre><pre>delay(10);</pre><pre>// We start by connecting to a WiFi network</pre><pre>Serial.println();</pre><pre>Serial.print(&quot;Connecting to &quot;);</pre><pre>Serial.println(ssid);</pre><pre>WiFi.begin(ssid, password);</pre><pre>while (WiFi.status() != WL_CONNECTED) {</pre><pre>delay(500);</pre><pre>Serial.print(&quot;.&quot;);</pre><pre>}</pre><pre>Serial.println(&quot;&quot;);</pre><pre>Serial.print(&quot;WiFi connected - ESP IP address: &quot;);</pre><pre>Serial.println(WiFi.localIP());</pre><pre>}</pre><pre>// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to</pre><pre>// Change the function below to add logic to your program, so when a device publishes a message to a topic that</pre><pre>// your ESP8266 is subscribed you can actually do something<br>void callback(String topic, byte* message, unsigned int length) {</pre><pre>Serial.print(&quot;Message arrived on topic: &quot;);</pre><pre>Serial.print(topic);</pre><pre>Serial.print(&quot;. Message: &quot;);</pre><pre>String messageTemp;</pre><pre>for (int i = 0; i &lt; length; i++) {</pre><pre>Serial.print((char)message[i]);</pre><pre>messageTemp += (char)message[i];</pre><pre>}</pre><pre>Serial.println();</pre><pre>}</pre><pre>// This functions reconnects your ESP8266 to your MQTT broker</pre><pre>// Change the function below if you want to subscribe to more topics with your ESP8266</pre><pre>void reconnect() {</pre><pre>// Loop until we&#39;re reconnected</pre><pre>while (!client.connected()) {</pre><pre>Serial.print(&quot;Attempting MQTT connection...&quot;);</pre><pre>// Attempt to connect</pre><pre>/*</pre><pre>YOU  NEED TO CHANGE THIS NEXT LINE, IF YOU&#39;RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS</pre><pre>To change the ESP device ID, you will have to give a unique name to the ESP8266.</pre><pre>Here&#39;s how it looks like now:</pre><pre>if (client.connect(&quot;ESP8266Client&quot;)) {</pre><pre>If you want more devices connected to the MQTT broker, you can do it like this:</pre><pre>if (client.connect(&quot;ESPOffice&quot;)) {</pre><pre>Then, for the other ESP:</pre><pre>if (client.connect(&quot;ESPGarage&quot;)) {</pre><pre>That should solve your MQTT multiple connections problem</pre><pre>THE SECTION IN loop() function should match your device name</pre><pre>*/<br>if (client.connect(&quot;ESP8266Client&quot;)) {</pre><pre>Serial.println(&quot;connected&quot;);</pre><pre>// Subscribe or resubscribe to a topic</pre><pre>// You can subscribe to more topics (to control more LEDs in this example)</pre><pre>} else {</pre><pre>Serial.print(&quot;failed, rc=&quot;);</pre><pre>Serial.print(client.state());</pre><pre>Serial.println(&quot; try again in 5 seconds&quot;);</pre><pre>// Wait 5 seconds before retrying</pre><pre>delay(5000);</pre><pre>}</pre><pre>}</pre><pre>}</pre><pre>// The setup function sets your DHT sensor, starts the serial communication at a baud rate of 115200</pre><pre>// Sets your mqtt broker and sets the callback function</pre><pre>// The callback function is what receives messages and actually controls the LEDs</pre><pre>void setup() {</pre><pre>dht.begin();</pre><pre>Serial.begin(115200);</pre><pre>setup_wifi();</pre><pre>client.setServer(mqtt_server, 1883);</pre><pre>client.setCallback(callback);</pre><pre>}</pre><pre>// For this project, you don&#39;t need to change anything in the loop function.</pre><pre>// Basically it ensures that you ESP is connected to your broker</pre><pre>void loop() {</pre><pre>if (!client.connected()) {</pre><pre>reconnect();</pre><pre>}</pre><pre>if(!client.loop())<br>/*</pre><pre>YOU  NEED TO CHANGE THIS NEXT LINE, IF YOU&#39;RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS</pre><pre>To change the ESP device ID, you will have to give a unique name to the ESP8266.</pre><pre>Here&#39;s how it looks like now:</pre><pre>client.connect(&quot;ESP8266Client&quot;);</pre><pre>If you want more devices connected to the MQTT broker, you can do it like this:</pre><pre>client.connect(&quot;ESPOffice&quot;);</pre><pre>Then, for the other ESP:</pre><pre>client.connect(&quot;ESPGarage&quot;);</pre><pre>That should solve your MQTT multiple connections problem</pre><pre>THE SECTION IN recionnect() function should match your device name</pre><pre>*/</pre><pre>client.connect(&quot;ESP8266Client&quot;);</pre><pre>now = millis();</pre><pre>// Publishes new temperature and humidity every 30 seconds</pre><pre>if (now - lastMeasure &gt; 10000) {</pre><pre>lastMeasure = now;</pre><pre>// Sensor readings may also be up to 2 seconds &#39;old&#39; (its a very slow sensor)</pre><pre>float h = dht.readHumidity();</pre><pre>// Read temperature as Celsius (the default)</pre><pre>float t = dht.readTemperature();</pre><pre>// Read temperature as Fahrenheit (isFahrenheit = true)</pre><pre>float f = dht.readTemperature(true);</pre><pre>// Check if any reads failed and exit early (to try again).</pre><pre>if (isnan(h) || isnan(t) || isnan(f)) {</pre><pre>Serial.println(&quot;Failed to read from DHT sensor!&quot;);</pre><pre>return;</pre><pre>}</pre><pre>// Computes temperature values in Celsius</pre><pre>float hic = dht.computeHeatIndex(t, h, false);</pre><pre>static char temperatureTemp[7];</pre><p>Step10: ต่อวงจรดังนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/188/1*VMO6Cp08agAnMfjR0R5Czg.png" /></figure><p>Step11: Launching the Web Server</p><pre>cd web-server &amp;&amp; sudo python app.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8lCYi-nO5rj8Zw44gZ1W4w.png" /></figure><p>แล้วไปที่ Rpi IP Address:8181 เพื่อมอนิเตอร์ค่าที่ส่งมา</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*xP9RThXL75vGZ9UZQ4Awgw.png" /></figure><h3>Q06: Send Data to Firebase</h3><p><strong>Registration</strong></p><p>Step1: ไปที่ <a href="https://console.firebase.google.com/">https://console.firebase.google.com/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*UUuorJgS5VBj9l-HXa53IA.png" /></figure><p>Step2: เพิ่มโครงการ (Add projrect)</p><p>ตั้งชื่อ เลือกประเทศ ตำแหน่งที่ตั้ง Server ยอมรับข้อตกลง คลิกสร้างโครงการ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*hBwGZ_HfgVOgb9mqNPVcsw.png" /></figure><p>Step3: เพิ่มโครงการ (Add projrect)</p><p>เลือก พัฒนา &gt;&gt; Database</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*IZedqnpOZHHui9aqh0drbw.png" /></figure><p>Step4: คลิก สร้างฐานข้อมูล</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*smQw1F7YFUxWF2diAFBjIA.png" /></figure><p>Step5: เลือก เริ่มต้นโหมดทดสอบ แล้วเปิดใช้งาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*z65XcohVzluzV_BBI6DH-A.png" /></figure><p>Step6: คัดลอก Url สำหรับเชื่อมต่อฐานข้อมูล</p><p>Step7:ต่อวงจรดังนี้</p><p>- ต่อไฟเลี้ยง 3.3–5 V ให้ขาแรกของเซนเซอร์</p><p>- ต่อขา Output (ขาที่2) เข้ากับ pin 7</p><p>- ต่อ Gnd ให้ขาสุดท้ายของเซนเซอร์</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*qNNX6n4SNZ24wCOGkWJJog.png" /></figure><p>Step8: ติดตั้ง Adafruit_Python_DHT</p><pre>sudo apt-get update</pre><pre>sudo apt-get install build-essential python-dev</pre><pre>git clone <a href="https://github.com/adafruit/Adafruit_Python_DHT.git">https://github.com/adafruit/Adafruit_Python_DHT.git</a></pre><pre>cd Adafruit_Python_DHT</pre><pre>sudo python setup.py install</pre><p>Step9: ติดตั้ง Firebase Python Library</p><pre>sudo pip install requests==1.1.0</pre><pre>sudo pip install python-firebase</pre><p>Step10: clone Raspberrypi_Firebase</p><pre>git clone <a href="https://github.com/amphancm/Raspberrypi_Firebase">https://github.com/amphancm/Raspberrypi_Firebase</a></pre><p>Step11: แก้ไข ไฟล์ dht-firebase.py</p><pre>cd Raspberrypi_Firebase/</pre><p>แก้ไขเซนเซอร์เป็น DHT.22 <br> แก้ไข firebase url ที่คัดลอกไว้ <br> แล้วบันทึก ctlr+x &gt;&gt; y &gt;&gt; Enter</p><p>Step12: Launce Project</p><pre>sudo python dht-firebase.py</pre><p>ไปที่ firebase url</p><h4><a href="https://medium.com/@yongyutsrisuban/3-start-fpga-cc92f1807b7">3. Start FPGA</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=166cb44a12bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q08 — NodeRED for Q6 DHT22+MAX719]]></title>
            <link>https://medium.com/@yongyutsrisuban/q08-nodered-for-q6-dht22-max719-3ceaf1b072cc?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ceaf1b072cc</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 13:46:30 GMT</pubDate>
            <atom:updated>2019-06-27T13:47:27.949Z</atom:updated>
            <content:encoded><![CDATA[<h3>Q08 — NodeRED for Q6 DHT22+MAX719</h3><p>อุปกรณ์ที่ใช้</p><ul><li>บอร์ด Raspberry Pi 3 Model B</li><li>สาย LAN</li><li>Micro USB Adapter สำหรับจ่ายไฟให้ Raspberry Pi 3</li><li>Micro SD card ขนาด 16 GB ขึ้นไป ที่ลง OS Raspbian แล้ว</li><li>เครื่องคอมพิวเตอร์ / Laptop</li><li>สาย Jumper</li><li>DHT22</li><li>MAX7219 Display</li></ul><p>ขั้นตอนการทดลอง</p><ul><li>เริ่มใช้งาน Node-Red</li></ul><p>Step 1: ทำการรันเซิร์ฟเวอร์โดยการเข้าที่ Terminal พิมพ์</p><pre>node-red start</pre><p><a href="https://www.realvnc.com/en/connect/download/viewer/windows/">Step 2: เข้าเว็บ Brower ไปที่ URL </a><a href="http://127.0.0.1:1880/">http://127.0.0.1:1880</a></p><p>Step 3: ทำการเลือก Input &gt; Inject</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/0*_lcN5YyZAlq5kNTu" /></figure><p>Step 4: เลือกที่ Advance &gt; exec</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/0*IpcVCaU0756ya0nF" /></figure><p>Step 5: ดับเบิ้ลคลิ๊กที่ exec และใส่ command เรียกใช้ .py script จาก Q6</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/0*SHnArYJ-dElKZ3kc" /></figure><p>Step 6: ลากเส้นเชื่อมและกด Deploy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/0*CYeS5qqBQ1wELak4" /></figure><p>Step 7: คลิ๊กที่ปุ่มตรง timestamp</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/0*4mCFYP9r240Idl3T" /></figure><ul><li>ต่อวงจรดังรูป</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/0*PuTt4--YqmB3Ov7u" /></figure><p>ผลการทำงาน</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/0*-aINKFr0QaGH-j4j" /></figure><h3>2. RPi — IoTs</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ceaf1b072cc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Q07 — NodeRED for Q4 กดติด กดดับ]]></title>
            <link>https://medium.com/@yongyutsrisuban/q07-nodered-for-q4-%E0%B8%81%E0%B8%94%E0%B8%95%E0%B8%B4%E0%B8%94-%E0%B8%81%E0%B8%94%E0%B8%94%E0%B8%B1%E0%B8%9A-b877c923a0c4?source=rss-b6a98fef471b------2</link>
            <guid isPermaLink="false">https://medium.com/p/b877c923a0c4</guid>
            <dc:creator><![CDATA[Yongyut Srisuban]]></dc:creator>
            <pubDate>Thu, 27 Jun 2019 13:45:07 GMT</pubDate>
            <atom:updated>2019-06-27T13:45:07.746Z</atom:updated>
            <content:encoded><![CDATA[<h3>Q07 — NodeRED for Q4 กดติด กดดับ</h3><p><strong>อุปกรณ์ที่ใช้</strong></p><ul><li>บอร์ด Raspberry Pi 3 Model B</li><li>สาย LAN</li><li>Micro USB Adapter สำหรับจ่ายไฟให้ Raspberry Pi 3</li><li>Micro SD card ขนาด 16 GB ขึ้นไป ที่ลง OS Raspbian แล้ว</li><li>เครื่องคอมพิวเตอร์ / Laptop</li><li>LED</li><li>สาย Jumper</li><li>Switch Button</li></ul><p><strong>ขั้นตอนการทดลอง</strong></p><ul><li><strong><em>ติดตั้ง Node-Red</em></strong></li></ul><p>Step 1: ทำการติดตั้ง Node-Red โดยการเข้า Terminal พิมพ์คำสั่ง</p><pre>bash &lt;(curl -sL <a href="https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered">https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered</a>)</pre><p>Step 2: ทำการติดตั้ง WiringPi เพื่อควบคุม GPIO โดยการเข้า Terminal พิมพ์คำสั่ง</p><pre>cd ~<br>git clone git://git.drogon.net/wiringPi<br>cd wiringPi<br>./build</pre><p>Step 3: ทำการติดตั้ง module wiring-pi สำหรับ Node.js โดยการเข้า Terminal พิมพ์</p><pre>cd .node-red<br>npm install wiringpi-nod</pre><p>Step 4: ตรวจสอบการติดตั้ง wiring-pi โดยการพิมพ์</p><pre>gpio -v</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*xILr05hlIU8mWcB_PfgLEA.png" /></figure><ul><li>เริ่มใช้งาน Node-Red</li></ul><p>Step 1: ทำการรันเซิร์ฟเวอร์โดยการเข้าที่ Terminal พิมพ์</p><pre>node-red start</pre><p>Step 2: เข้าเว็บ Brower ไปที่ URL <a href="http://127.0.0.1:1880/">http://127.0.0.1:1880</a></p><p>Step 3: เลือกที่ Input rpi gpio และตั้งค่า</p><ul><li>เลือก Pin 11</li><li>เลือก Resistor เป็น Pull up</li><li>ตั้งชื่อเป็น SwitchButton</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*sGqkUTJQN0C0wxBKAQFgrw.png" /></figure><p>Step 4: เลือก function และทำการใส่ Code เพื่อควบคุม State</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*HdE_5Vwn2T6IgjEbI6O4hw.png" /></figure><pre>context.state = context.state | false;</pre><pre>context.state = !context.state</pre><pre>var myContext = context.state;</pre><pre>// initialise the counter to 0 if it doesn’t exist already</pre><pre>var count = context.get(‘count’)||0;</pre><pre>count += 1;</pre><pre>// store the value back</pre><pre>context.set(‘count’,count);</pre><pre>// make it part of the outgoing msg object</pre><pre>msg.count = count;</pre><pre>//define function isOdd 1 === isOdd</pre><pre>function isOdd(num) { return num % 2;}</pre><pre>/*if myContext is true and count is odd send 1,</pre><pre>else send 0 */</pre><pre>if(myContext === true &amp;&amp; isOdd((count+1)/2) ===1){</pre><pre>msg.payload = 1;</pre><pre>return msg;</pre><pre>} else if (myContext === true &amp;&amp; isOdd((count+1)/2) ===0){</pre><pre>msg.payload = 0;</pre><pre>return msg;</pre><pre>}</pre><p>Step 5: เลือกที่ Output rpi gpio และตั้งค่า</p><ul><li>เลือก Pin 11</li><li>ตั้งชื่อเป็น LED</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*hA3L6bJKYkq3587FWD6CLw.png" /></figure><p>Step 6: ทำการต่อเส้นและกด Deploy</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*wfmoJIAMHHq7kG3nkKyXVw.png" /></figure><p><strong>การต่อวงจร</strong></p><ul><li>ใช้ขา 3 (GND) ต่อเข้ากับ Board</li><li>ใช้ขา 12 (GPIO17) ต่อเข้ากับขาบวกของ LED</li><li>ต่อ GND เข้ากับขาลบของ LED</li><li>ใช้ขา 11 (GPIO18) ต่อเข้ากับขาข้างหนึ่งของ Switch Button</li><li>ต่อ GND เข้ากับขาอีกข้างหนึ่งของ Switch Button</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*D5RVnz7BFB5ZuXZDnTkpRg.png" /></figure><p><strong>Q08 — NodeRED for Q6 DHT22+MAX719</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b877c923a0c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>