Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

測試頁面.vb 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Imports uPLibrary.Networking.M2Mqtt
  2. Imports uPLibrary.Networking.M2Mqtt.Messages
  3. Imports System.Security.Cryptography.X509Certificates
  4. Imports System.IO
  5. Imports System.Text.RegularExpressions
  6. Imports Newtonsoft.Json
  7. Public Class 測試頁面
  8. ' MQTT 客戶端變數
  9. Dim client As MqttClient
  10. Dim endpoint As String = "a3kltpd88hr7qj-ats.iot.ap-southeast-2.amazonaws.com" ' AWS IoT 端點
  11. Dim port As Integer = 8883
  12. Dim topic As String = "gcmserver"
  13. Dim pfxCertPath As String
  14. Dim caCertPath As String
  15. Dim pfxPassword As String = "1234"
  16. Dim IDname As String = Guid.NewGuid().ToString()
  17. Dim dataStringArray As String()
  18. Public Class MyMessage
  19. Public Property message As String
  20. Public Property number As String
  21. Public Property id As String
  22. Public Property data As List(Of Integer)
  23. End Class
  24. Private Sub 測試頁面_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  25. ' 取得專案路徑
  26. Dim basePath As String = AppDomain.CurrentDomain.BaseDirectory
  27. ' 設定根證書 (CA證書) 和 pfx 檔案 (包含裝置證書和私鑰)
  28. caCertPath = Path.Combine(basePath, "gcm_CA.pem")
  29. pfxCertPath = Path.Combine(basePath, "gcm_unity_cert.pfx")
  30. ' 嘗試建立 MQTT 連接
  31. Try
  32. ' 載入 pfx 檔案並指定密碼
  33. Dim clientCert As New X509Certificate2(pfxCertPath, pfxPassword)
  34. ' 建立 MQTT 客戶端
  35. client = New MqttClient(endpoint, port, True, New X509Certificate(caCertPath), clientCert, MqttSslProtocols.TLSv1_2)
  36. ' 連接到 AWS IoT
  37. client.Connect("VBClient")
  38. If client.IsConnected Then
  39. Console.WriteLine("已連接到 AWS IoT")
  40. ' 訂閱主題
  41. client.Subscribe(New String() {topic}, New Byte() {MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE})
  42. Timer1.Interval = 3000 ' 3 秒
  43. Timer1.Start()
  44. ' 處理接收到的消息
  45. AddHandler client.MqttMsgPublishReceived, AddressOf OnMessageReceived
  46. Else
  47. Console.WriteLine("無法連接到 AWS IoT")
  48. End If
  49. Catch ex As Exception
  50. Console.WriteLine("連接失敗: " & ex.Message)
  51. End Try
  52. End Sub
  53. ' 當收到消息時觸發
  54. Private Sub OnMessageReceived(sender As Object, e As MqttMsgPublishEventArgs)
  55. Dim str As String = System.Text.Encoding.UTF8.GetString(e.Message)
  56. ' 步驟 1: 確認 str 不為空
  57. If Not String.IsNullOrEmpty(str) Then
  58. ' 步驟 2: 使用 Newtonsoft.Json 解析 JSON 字串
  59. Try
  60. ' 將 JSON 字串轉換為 MyMessage 物件
  61. Dim myMessage As MyMessage = JsonConvert.DeserializeObject(Of MyMessage)(str)
  62. ' 步驟 3: 確認 id 不為空且 message 為 "return",且 id 與 IDname 匹配
  63. If Not String.IsNullOrEmpty(myMessage.id) AndAlso myMessage.message = "return" AndAlso myMessage.id = IDname Then
  64. Console.WriteLine("已接收消息: " & str)
  65. ' 步驟 4: 直接從 myMessage.data 中獲取數據並進行處理
  66. If myMessage.data.Count >= 6 Then
  67. ' 獲取數值並進行計算
  68. Dim value3 As Decimal = myMessage.data(3) / 10
  69. Dim value4 As Decimal = myMessage.data(4) / 100
  70. Dim value5 As Decimal = myMessage.data(5) / 100
  71. ' 更新 TextBox 的內容
  72. If TextBox1.InvokeRequired Then
  73. ' 使用 Invoke 呼叫主執行緒更新 UI
  74. TextBox1.Invoke(Sub()
  75. TextBox1.Text = value3.ToString()
  76. End Sub)
  77. Else
  78. ' 如果已經在主執行緒,直接更新
  79. TextBox1.Text = value3.ToString()
  80. End If
  81. If TextBox2.InvokeRequired Then
  82. TextBox2.Invoke(Sub()
  83. TextBox2.Text = (value4 + value5).ToString()
  84. End Sub)
  85. Else
  86. TextBox2.Text = (value4 + value5).ToString()
  87. End If
  88. Else
  89. Console.WriteLine("data 中的元素不足")
  90. End If
  91. Else
  92. Console.WriteLine("條件不符合")
  93. End If
  94. Catch ex As Exception
  95. Console.WriteLine("JSON 解析失敗: " & ex.Message)
  96. End Try
  97. Else
  98. Console.WriteLine("收到的消息為空")
  99. End If
  100. End Sub
  101. ' 按鈕點擊事件,發送消息到 AWS IoT
  102. Private Sub timer_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  103. ' 檢查是否已經連接到 AWS IoT
  104. If client IsNot Nothing AndAlso client.IsConnected Then
  105. ' 發送消息到 AWS IoT
  106. Dim message As String = "{""message"": ""send"", ""number"": ""EL-00000001-GCM"", ""id"": """ & IDname & """}"
  107. client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(message), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, False)
  108. Console.WriteLine("已發送消息: " & message)
  109. End If
  110. End Sub
  111. End Class