Wifi Data Logging Using Arduino

ตัวอย่างการใช้งาน Arduino ส่ง Data ผ่าน TCP ไปแสดงผลยัง Server

by STK@TEE>

ตัวอย่างนี้เป็นการใช้งานบอร์ด Arduino กับ Wifi โมดูล M03 เพื่ออ่านค่าอุณหภูมิจาก Sensor LM35 ซึ่งให้ Output เป็นสัญญาณ Analog คือตัว Sensor จะให้ค่าแรงดัน Output 10mV/1C การอ่านค่าอุณหภูมิทำได้โดยต่อสัญญาณเอาท์พุทของ LM 35 เข้ากับช่องแปลงสัญญาณ Analog to Digital ของ Arduino แล้วแปลงค่าแรงดันเป็น อุณหภูมิเพื่อส่ง Data ผ่าน UART ไปยังโมดูล LVTTL UART to Wi-Fi แต่เนื่องจากตัวโมดูล Wi-fi ใช้ระดับสัญญาณเป็น LVTTL (0-3.3V) ซึ่งต่างจาก Arduino ที่ใช้ระดับสัญญาณ ในระดับ TTL (5V) จึงจำเป็นต้องมีการลดทอนแรงดัน ที่ Arduino จะจ่ายแรงดันไปยังโมดูล Wi-Fi คือขาที่ Tx ของ Arduino ส่งไปยังขา Rx ของโมดูล Wi-Fi โดยใช้วงจร R Divider ตัวต้านทาน 10K และ 20K

ในหัวข้อเรื่องการตั้งค่าการเชื่อมต่อโม ดูล LVTTL UART to Wi-Fi กับ Access Point ที่ได้กล่าวถึงไปในหัวข้อก่อนหน้านี้เราได้ทำการเซตให้โมดูลทำงานแบบ “Auto work Mode” โดยเมื่อโมดูลเริ่มทำงานจะ Connect เข้ากับ Access Point และถ้าหาก Connect สำเร็จ ตัวโมดูลจะส่งทุก Data ที่รับจากขา Rx ส่งไปยัง Server เรากำหนดเอาไว้ และจะส่ง Data ทั้งหมดที่ได้รับจากระบบ Network ออกมาทางขา Tx ของโมดูล

เมื่อเรานำค่าอุณหภูมิที่อ่านได้ส่งจาก ขา Tx ของ Arduino ไปเข้าขา Rx ของโมดูล Wi-Fi ข้อมูลนั้นจะถูกส่งไปทาง Wi-Fi ไปยัง Server โดยที่ Server ได้มีการเขียน Software เปิด Port รอรับ Data เอาไว้แล้วนำข้อมูลนั้นมาแสดงผลเป็นกราฟ

วิธีเชื่อมต่อ Arduino กับ WIFI To Serial และ Sensor LM35

Source Code Arduino

int sensorPin = 0;
void setup()
{
   Serial.begin(115200);
}
void loop()
{
   int reading = analogRead(sensorPin);
   float voltage = reading * 5.0;
   voltage /= 1024.0;
   float temperatureC = voltage*100 ;
   Serial.print(temperatureC);
   Serial.print(" C\r\n");
   delay(1000);
}

Source Code C# (Visual Studio 2010) 

** ผู้ใช้ Code ควรมีพื้นฐานการเขียนโปรแกรม ทาง ThaiEasyElec จะไม่อธิบายพื้นฐานใด ๆ สำหรับตัวอย่างโปรแกรมนี้ เป็นหน้าที่ของผู้สนใจที่จะต้องศึกษาต่อเพิ่มเติมด้วยตนเอง แต่ถ้าหาก Run โปรแกรมแล้วติด Error สามารถสอบถามได้ครับ

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//add
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace TCP_Server
{
   public partial class Form1 : Form
   {
      private TcpListener tcpListener;
      private Thread listenThread;
      TcpClient client;
      public delegate void InvokeDelegate();
      string encoder;
      double XX = 0,YY=0,xn=0;      public Form1()
      {
           InitializeComponent();      }
      private void button1_Click(object sender, EventArgs e)
      {
          if (int.Parse(textBox1.Text) > 1024)
          {
               tcpListener = new TcpListener(IPAddress.Any, int.Parse(textBox1.Text));
               listenThread = new Thread(new ThreadStart(ListenForClients));
               listenThread.Start();
          }
      }
      private void ListenForClients()
      {
            tcpListener.Start();            while (true)
            {
            client = this.tcpListener.AcceptTcpClient();
            Thread clientThread = new Thread(newThreadStart(HandleClientComm));
            clientThread.Start();
            }
      }
      private void HandleClientComm()
      {
            NetworkStream clientStream = client.GetStream();
            byte[] message = new byte[4096];
            int bytesRead;            while (true)
            {
                  bytesRead = 0;
                  try
                  {
                       bytesRead = clientStream.Read(message, 0, 4096);
                  }
                  catch
                  {
                       break;
                  }
                  if (bytesRead == 0)
                  {
                       break;                  }
                  encoder = System.Text.Encoding.ASCII.GetString(message, 0, bytesRead);
                  textBox2.BeginInvoke(new InvokeDelegate(updateTextbox));
             }
             client.Close();
       }
                   void updateTextbox()
       {
             string[] words = encoder.Split(' ');
             textBox2.Text = encoder;
             encoder = "";
             if (words[0] != "\r\n") ;
             {
                  try
                  {
                        YY = float.Parse(words[0]);
                        if (YY > 0)
                        {
                             chart1.Series["Temp"].Points.AddXY(XX, YY);
                             XX++;
                             xn++;
                        }
                  if (xn > 20)
                  {
                        chart1.Series["Temp"].Points.Clear();
                        xn = 0;
                  }
             }
             catch
             {
             }
       }
    }
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
       listenThread.Abort();
       client.Close();
       tcpListener.Stop();
    }
  }
}