【物聯網系統開發】使用TELNET通訊開發聊天室

【物聯網系統開發】使用TELNET通訊開發聊天室

物聯網開發中,TCP/IP通訊是網際網路的本質(曹永忠, 吳佳駿, 許智誠, & 蔡英德, 2017a, 2017b; 曹永忠, 許智誠, & 蔡英德, 2015a, 2015b, 2015c, 2015d),而且許多物聯網的裝置,都是透過Socket方式進行通訊方能正確運作,

文\曹永忠、吳佳駿、許智誠、蔡英德

筆者鑒於如此,本文要使用ARDUINO乙太網路擴充板,再具備TCP/IP之擷取通訊能力之後,我們將使用網路上最通用的Telnet通訊協定,來開發一個簡單的聊天室,讓讀者了解簡單的通訊原理(曹永忠, 吳佳駿, 許智誠, & 蔡英德, 2017c, 2017d; 曹永忠, 許智誠, & 蔡英德, 2015e, 2015g)。

本系列希望貢獻筆者一些經驗,讓ARDUINO創客神器(曹永忠, 許智誠, & 蔡英德, 2015f)可以運用到時間功能,進而發展出強大的物聯網應用。

本系列希望貢獻筆者一些經驗,讓非資訊、電機、電子等Makers可以學到在物聯網開發中,一些程式開發的技巧、原理、法則與穩固的技術,因本系列文章主要讀者為初學者,內容程度為基礎入門程度,深入之處不足,但請高手們給筆者賜教,也請讀者關注本系列。 

Telnet簡單聊天室

首先,組立W5100 以太網路模組是非常容易的一件事,如下圖所示,只要將W5100 以太網路模組堆疊到任何Arduino開發板之上就可以了。

圖 1 將Arduino開發板與W5100 以太網路模組堆疊組立

之後,在將組立好的W5100 以太網路模組,如下圖所示,只要將USB線差到Arduino開發板,再將RJ 45的網路線一端插到W5100 以太網路模組,另一端插到可以上網的集線器(Switch HUB)的任何一個區域網路接口(Lan Port)就可以了。

圖 2接上電源與網路線的W5100 以太網路模組堆疊卡

我們遵照前幾章所述,將Arduino 開發板的驅動程式安裝好之後,我們打開Arduino 開發板的開發工具:Sketch IDE整合開發軟體,攥寫一段程式,如下表所示之Telnet簡單聊天室測試程式,我們就可以讓W5100 以太網路模組堆疊卡變成一台簡易的Telnet簡單聊天室運作了。

表 1 Telnet簡單聊天室測試程式

Telnet Telnet簡單聊天室測試程式(ChatServer)

/*

 Chat  Server

 

 A simple server that distributes any incoming messages to all

 connected clients.  To use telnet to  your device's IP address and type.

 You can see the client's input in the serial monitor as well.

 Using an Arduino Wiznet Ethernet shield.

 

 Circuit:

 * Ethernet shield attached to pins 10, 11, 12, 13

 * Analog inputs attached to pins A0 through A5 (optional)

 

 created 18 Dec 2009

 by David A. Mellis

 modified 9 Apr 2012

 by Tom Igoe

 

 */

 

#include <SPI.h>

#include <Ethernet.h>

 

// Enter a MAC address for your controller below.

// Newer Ethernet shields have a MAC address printed on a sticker on the shield

byte mac[] = {

  0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF

};

IPAddress ip(192, 168, 30, 200);

IPAddress dnServer(168, 95, 1, 1);

// the router's gateway address:

IPAddress gateway(192, 168, 30, 254);

// the subnet:

IPAddress subnet(255, 255, 255, 0);

 

 

 

// telnet defaults to port 23

EthernetServer server(23);

boolean alreadyConnected = false; // whether or not the client was connected previously

 

void setup() {

  // initialize the ethernet device

  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients

  server.begin();

  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

 

 

  Serial.print("Chat server address:");

  Serial.println(Ethernet.localIP());

}

 

void loop() {

  // wait for a new client:

  EthernetClient client = server.available();

 

  // when the client sends the first byte, say hello:

  if (client) {

    if (!alreadyConnected) {

      // clead out the input buffer:

      client.flush();

      Serial.println("We have a new client");

      client.println("Hello, client!");

      alreadyConnected = true;

    }

 

    if (client.available() > 0) {

      // read the bytes incoming from the client:

      char thisChar = client.read();

      // echo the bytes back to the client:

      server.write(thisChar);

      // echo the bytes to the server as well:

      Serial.write(thisChar);

    }

  }

}

 

 

程式下載:https://github.com/brucetsao/arduino_RFProgramming


如下圖所示,讀者可以看到本次實驗- Telnet簡單聊天室,我們使用Putty通訊軟體,預備進行連線的畫面。

圖 3 Telnet簡單聊天室預備進行連線的畫面

如下圖所示,讀者可以看到本次實驗- Telnet簡單聊天室,我們使用Putty通訊軟體,進行Telnet簡單聊天室連線中的畫面。

圖 4 Telnet簡單聊天室連線中的畫面

如下圖所示,我們可以使用putty通訊軟體來測試聊天室的功能。

圖 5 Telnet簡單聊天室聊天進行的畫面

後續

本篇為『物聯網系統開發系列』系列之使用TELNET通訊開發聊天室,主要內容是要讓讀者使用創客神器Arduino開發板,透過乙太網路擴充板(Ethernet Shield)的網際網路連線之強大功能,透過讓Arduino開發板開發簡單的聊天室,讓讀者可以學習TCP/IP通訊的簡單用法,進而將這個基礎理念與技術,進階運用到雲端平台中互傳資訊的機制,進而在物聯網開發中,成為一個技術的核心能力,乃是筆者本篇內容想傳達的創作概念。

筆者本系列是針對非資訊、電機、電子等Makers攥寫的物聯網系統開發系列,這四、五年來在物聯網系統開發領域寫書、發表文章、辦展、授課,常遇到許多學子訓練不足,以交作業的心態來學習,並沒有把程式底子打好。

後續筆者還會繼續發表『物聯網系統開發系列』系列的文章,在未來我們可以創造出更優質,更具未來性的物聯網(Internet of Thing:IOT)產品開發相關技術。

敬請期待更多的文章。

曹永忠 (Yung-Chung Tsao) 目前為自由作家暨專業Maker,專研於軟體工程、軟體開發與設計、物件導向程式設計,商品攝影及人像攝影。長期投入創客運動、資訊系統設計與開發、企業應用系統開發、軟體工程、新產品開發管理、商品及人像攝影等領域,並持續發表作品及相關專業著作。

Email:prgbruce@gmail.com

Line ID:dr.brucetsao

【物聯網系統開發】使用TELNET通訊開發聊天室

作者網站:https://www.cs.pu.edu.tw/~yctsao/

臉書社群(Arduino.Taiwan):https://www.facebook.com/groups/Arduino.Taiwan/

Github網站:https://github.com/brucetsao/

吳佳駿 (Chia-Chun Wu),國立中興大學資訊科學與工程學系博士,現任教於國立金門大學工業工程與管理學系專任助理教授,目前兼任國立金門大學計算機與網路中心資訊網路組組長,主要研究為軟體工程與應用、行動裝置程式設計、物件導向程式設計、網路程式設計、動態網頁資料庫、資訊安全與管理。

Email: ccwu0918@nqu.edu.tw

許智誠 (Chih-Cheng Hsu),美國加州大學洛杉磯分校(UCLA) 資訊工程系博士,曾任職於美國IBM等軟體公司多年,現任教於中央大學資訊管理學系專任副教授,主要研究為軟體工程、設計流程與自動化、數位教學、雲端裝置、多層式網頁系統、系統整合。

Email: khsu@mgt.ncu.edu.tw

蔡英德 (Yin-Te Tsai),國立清華大學資訊科學系博士,目前是靜宜大學資訊傳播工程學系教授、靜宜大學計算機及通訊中心主任,主要研究為演算法設計與分析、生物資訊、軟體開發、視障輔具設計與開發。

Email:yttsai@pu.edu.tw

參考文獻:

曹永忠, 吳佳駿, 許智誠, & 蔡英德. (2017a). Ameba程式設計(物聯網基礎篇):An Introduction to Internet of Thing by Using Ameba RTL8195AM (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠, 吳佳駿, 許智誠, & 蔡英德. (2017b). Ameba程序设计(物联网基础篇):An Introduction to Internet of Thing by Using Ameba RTL8195AM (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠, 吳佳駿, 許智誠, & 蔡英德. (2017c). 【物聯網系統開發】運用 ARDUINO 乙太網路擴充板建立用戶端工具程式. 智慧家庭.  Retrieved from http://www.techbang.com/posts/52039-internet-system-development-established-client-tools-using-arduino-ethernet-add-on-board-procedures

曹永忠, 吳佳駿, 許智誠, & 蔡英德. (2017d). 【物聯網系統開發】運用 ARDUINO 乙太網路擴充板建立簡單網頁伺服器. 智慧家庭.  Retrieved from http://www.techbang.com/posts/51518-internet-system-using-arduino-ethernet-add-on-board-to-build-a-simple-web-server

曹永忠, 許智誠, & 蔡英德. (2015a). Arduino物联网系统开发(入门篇):Using Arduino Yun to Develop an Application for Internet of Things (Basic Introduction) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠, 許智誠, & 蔡英德. (2015b). Arduino程式教學(無線通訊篇):Arduino Programming (Wireless Communication) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠, 許智誠, & 蔡英德. (2015c). Arduino编程教学(无线通讯篇):Arduino Programming (Wireless Communication) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠, 許智誠, & 蔡英德. (2015d). Arduino物聯網系統開發(入門篇):Using Arduino Yun to Develop an Application for Internet of Things (Basic Introduction) (初版 ed.). 台湾、彰化: 渥瑪數位有限公司.

曹永忠, 許智誠, & 蔡英德. (2015e). 【Maker 實戰】實作 Arduino Ethernet Shield 應用,透過網路監控居家亮度.   Retrieved from http://www.techbang.com/posts/25109-future-of-humanity-through-network-monitoring-internet-home-brightness

曹永忠, 許智誠, & 蔡英德. (2015f). 創客神器ARDUINO到底是什麼呢?.   Retrieved from http://makerdiwo.com/archives/1893

曹永忠, 許智誠, & 蔡英德. (2015g). 邁入『物聯網』的第一步:如何使用無線傳輸:基本篇. 物聯網.  Retrieved from http://www.techbang.com/posts/25459-technology-in-the-future-internet-of-things-business-model-how-to-use-wireless-transmission-the-basic-text

曹永忠
作者

曹永忠,國立中央大學資訊管理學系博士,目前在暨南大學電機工程學系兼任助理教授與自由作家,專注於軟體工程、軟體開發與設計、物件導向程式設計......並持續發表作相關專業著作。

使用 Facebook 留言
發表回應
謹慎發言,尊重彼此。按此展開留言規則