VB.net网络服务器

时间:2016-02-27 18:29:24

标签: vb.net arduino serial-port webserver

我有一个Arduino MEGA 2560,我正在建造一个智能家居。它没有WiFi或以太网端口所以我写了一个读取SerialPort的vb.net程序。但我不知道如何建立一个能够显示温度传感器价值的网站。

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情......

带有AJAX计时器的简单HTML页面....

<!DOCTYPE html>
<html>
<body>

<p>A script on this page starts this clock:</p>

<p id="demo"></p>

<p><b>Status:</b> <span id="A1"></span></p>
<p><b>Status text:</b> <span id="A2"></span></p>
<p><b>Response:</b> <span id="A3"></span></p>

<script>
var myVar = setInterval(myTimer, 1000);

function myTimer() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
  if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById('A1').innerHTML = xhttp.status;
      document.getElementById('A2').innerHTML = xhttp.statusText;
      document.getElementById('A3').innerHTML = xhttp.responseText;
  }
  };
  xhttp.open("GET", 'TempXML.xml', true);
  xhttp.send();
}
</script>

</body>
</html>

简单的XML .....

<TempXML>
<DateTime>28\02\2016 22:10:00 :: </DateTime>
<body>31 &#186;C :: 87 &#186;F</body>
</TempXML>

现在,将XML作为'TempXML.xml'保存在SAME文件夹中作为HTML页面,然后将HTML加载到浏览器中...... HTML页面中的计时器将每秒触发并读取XML,您需要做的就是将来自Arduino的数据写入\保存到XML文件中,HTML将使用新数据进行更新....

'' '' '' '' '' '' '' '' '' '' '' 编辑 '' '' '' '' '' '' '' '' '' '' ''

以下是一个写入XML文件的示例控制台应用程序

Imports System.Xml

Module Module1

Sub Main()
    Dim NewTemp = New TempData

    ' Set you DateTime and Temp here
    NewTemp.DateTime = DateTime.Now
    NewTemp.fahrenheit = 80 ' Data from your Arduino 
    NewTemp.celsius = 36    ' Data from your Arduino 

    ' Create XmlWriterSettings.
    Dim settings As XmlWriterSettings = New XmlWriterSettings()
    settings.Indent = True

    Using writer As Xml.XmlWriter = Xml.XmlWriter.Create("<Path to your XML Temp file>\TempXML.xml", settings)
        ' Begin writing.
        writer.WriteStartDocument()
        writer.WriteStartElement("TempXML") ' Root.

        writer.WriteElementString("DateTime", NewTemp.DateTime.ToString)
        writer.WriteElementString("body", String.Format(" :: {0} °C :: {1} °F", NewTemp.celsius.ToString, NewTemp.fahrenheit.ToString))

        ' End document.
        writer.WriteEndElement()
        writer.WriteEndDocument()
    End Using
End Sub

End Module


Public Class TempData

Private _DateTime As DateTime
Public Property DateTime() As DateTime
    Get
        Return _DateTime
    End Get
    Set(ByVal value As DateTime)
        _DateTime = value
    End Set
End Property

Private _fahrenheit As Int32
Public Property fahrenheit() As Int32
    Get
        Return _fahrenheit
    End Get
    Set(ByVal value As Int32)
        _fahrenheit = value
    End Set
End Property

Private _celsius As Int32
Public Property celsius() As Int32
    Get
        Return _celsius
    End Get
    Set(ByVal value As Int32)
        _celsius = value
    End Set
End Property

End Class

请记住在“Xml.XmlWriter.Create”行中更改XML文件的路径以适合您PC上的位置....

顺便提一下,要获得'°'度符号(在上面的代码中看到)...将光标定位在正确的位置,按住'Alt'键并输入0176然后让'Alt '去.....