当前位置:   article > 正文

ESP8266+MQTT+SQL Server+C# 搭建简易的物联网平台_简单本地物联网平台

简单本地物联网平台

1. 概要

本文主要利用ESP8266模块EMQX平台(开源版)SQL Server数据库以及基于C#的简易前后端构建一个基础的物联网平台。文章从ESP8266硬件说明Arduino IDE硬件环境配置硬件代码编写基础SQL Server数据库安装SSMS工具安装,以及C#前后端编写等方面尽可能详细讲解了整个搭建过程,同时提供了完整的代码和工程,方便大家学习和实践,同时也是自身一个从零开始学习物联网技术的过程。希望各位大家能对我分享的过程多提意见和建议,我也会积极向大家学习。

该平台能够实现单个节点的数据采集和传输,并将通过MQTT服务器将数据存储到SQL Server数据库中,用户可以通过C#编写的界面进行数据的可视化展示和分析。整体思路的框图如下所示。

2. 整体流程

2.1 ESP8266开发

ESP8266具有高度集成、低功耗、低成本、广泛的应用领域等特点,是一款非常优秀的物联网芯片。我用的ESP8266就某宝买的。比较便宜,适合用于练习项目。

 这个ESP8266是基于Arduino的,所以需要安装配置好Arduino IDE平台,并且配置对应的支持库才能进行硬件程序编写,具体过程可以参考我之前发Arduino IDE 2.1.1 ESP8266开发板安装方案。

Arduino IDE 2.1.1 ESP8266开发板安装_Secede.的博客-CSDN博客

 Arduino在有对应支持库的情况下,编程是比较简单的。

  1. void setup() {
  2.   // 这写初始化的配置:
  3. }
  4. void loop() {
  5.   // 这写循环体:
  6. }

ESP8266硬件编写代码如下所示:

  1. #include <AHT10.h>
  2. #include <Wire.h>
  3. #include <ESP8266WiFi.h>
  4. #include <PubSubClient.h>
  5. #include <ArduinoJson.h>
  6. #ifndef STASSID
  7. #define STASSID "abbc"
  8. #define STAPSK "123456"
  9. #endif
  10. const char* ssid = STASSID;
  11. const char* password = STAPSK;
  12. const char* mqtt_server = "127.0.0.1"; //PC
  13. long lastReconnectAttempt = 0;
  14. DynamicJsonDocument msg4device(100);
  15. char msg[100] = {0};
  16. int stats = 0;
  17. String output;
  18. WiFiClient espClient;
  19. PubSubClient client(espClient);
  20. boolean reconnect() {
  21. if (client.connect("arduinoClient")) {
  22. // Once connected, publish an announcement...
  23. client.publish("announcement","ready");
  24. // ... and resubscribe
  25. client.subscribe("test3");
  26. client.subscribe("refresh");
  27. }
  28. return client.connected();
  29. }
  30. void callback(char* topic, byte* payload, unsigned int length) {
  31. byte* n_payload = (byte*)malloc(length);
  32. memcpy(n_payload,payload,length);
  33. if(!strcmp(topic,"test3"))
  34. {
  35. // Switch on the LED if an 1 was received as first character
  36. if ((char)n_payload[0] == '1') {
  37. digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
  38. stats = 1;
  39. // but actually the LED is on; this is because
  40. // it is acive low on the ESP-01)
  41. } else {
  42. digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
  43. stats = 0;
  44. }
  45. }
  46. if(!strcmp(topic,"refresh"))
  47. {
  48. reconnect();
  49. }
  50. }
  51. void setup() {
  52. pinMode(LED_BUILTIN, OUTPUT);
  53. digitalWrite(LED_BUILTIN, HIGH);
  54. Serial.begin(115200);
  55. // We start by connecting to a WiFi network
  56. Wire.begin(12,14);
  57. if (!AHT10::begin()) {
  58. Serial.println(F("AHT10 not detected!"));
  59. Serial.flush();
  60. }
  61. Serial.print("Connecting to ");
  62. Serial.println(ssid);
  63. WiFi.mode(WIFI_STA);
  64. WiFi.begin(ssid, password);
  65. while (WiFi.status() != WL_CONNECTED) {
  66. delay(500);
  67. Serial.print(".");
  68. }
  69. Serial.println("");
  70. Serial.println("WiFi connected");
  71. Serial.println("IP address: ");
  72. Serial.println(WiFi.localIP());
  73. Serial.print("Setting MQTT Server");
  74. client.setServer(mqtt_server, 1883);
  75. client.setCallback(callback);
  76. }
  77. void loop()
  78. {
  79. if (!client.connected())
  80. {
  81. long now = millis();
  82. if (now - lastReconnectAttempt > 2000)
  83. {
  84. lastReconnectAttempt = now;
  85. // Attempt to reconnect
  86. if (reconnect())
  87. {
  88. lastReconnectAttempt = 0;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. // Client connected
  95. client.loop();
  96. float temp, hum;
  97. if (AHT10::measure(&temp, &hum))
  98. {
  99. msg4device["humidity"] = hum;
  100. msg4device["temperature"] = temp;
  101. msg4device["state"] = stats;
  102. }
  103. else
  104. {
  105. Serial.println(F("AHT10 read error!"));
  106. }
  107. output.clear();
  108. serializeJson(msg4device,output);
  109. Serial.print("Publish message: ");
  110. Serial.println(output);
  111. client.publish("test", output.c_str());
  112. delay(1500);
  113. }
  114. }

上面的代码有关AHT10的部分是AHT10温湿度传感器的相关驱动程序,如果需要下载可以直接在Github上搜索,然后放在首选项所设定的文件夹下的libraries目录即可。如果在Github上下载不方便的话,可以提供对应的下载连接如下:

https://download.csdn.net/download/qq_41858327/88238320

硬件代码中具体函数和变量对应的意义入下表所示:

STASSID WiFi 名字
STAPSK WFi 密码
mqtt_server MQTT服务器地址
msg4device传感器数据存入Json
reconnect()ESP8266与MQTT服务器断连重连函数
callback()ESP8266接收MQTT服务器发送信息回调函数
订阅主题 testESP8266 发送 物联网控制平台 温湿度数据包
订阅主题 test3物联网控制平台 发送 ESP8266 数据包

2.2 MQTT服务器与SQL Server数据库

MQTT服务器选择EMQX开源版 。     

官网连接:下载 EMQX,下载对应zip文件解压即可。

 bin文件夹下有emqx.cmd,运行emqx.cmd,输入start,MQTT服务器就运行起来了。

此外,也可以在命令行输入E:\MQTT\emqx-windows-4.3.16\emqx\bin\emqx.cmd start(你的MQTT文件夹路径)。

进入EMQX的Dashboard :http://localhost:18083/       默认账户admin 默认密码public

然后是配置SQL Server, SQL Server数据库的基础安装配置现有介绍已经非常全面了,大家自行查找即可。

安装好SQL Server数据库与SSMS工具之后,我们要创建对应的数据库与数据表(无所谓系统或者数据库类型,能做到后续程序连接到对应的数据库即可),用于存储我们从ESP8266传输的温湿度数据。如下:

这里用的是本地的SQL Server数据库,如果您使用的是云服务器,那就对您的数据库进行数据库和表进行配置即可。

2.3 C#的订阅发布功能实现

要想在EMQX开源版本中实现对于硬件发送数据的持久化存储,常见的方法有两种:①通过在后台创建超级用户,订阅所有主题,对对应主题进行二次的响应;②利用EMQX自带的WebHook + Web API 实现自动将硬件数据存储到数据库中。方案1主要存在的问题在于响应速度慢,较大数据量的情况不适用。方法2稍微复杂一点,需要对EMQX中的WebHook功能进行配置,再就是需要有web端后台的设计。本此实验主要是对第一种方案进行探索和研究,会在后期对第二种方案进行研究。

此外,针对不同的编程语言和应用场景,EMQX官方都有对应的入门文档可以参考,推荐大家配合官方文档进行学习。

回到本次项目,本次项目主要利用C#和对应的支持库MQTTnet对后台订阅发布功能进行实现。实现的主体由3部分组成:①MQTT服务器的配置连接;②MQTT订阅与回调函数配置;③MQTT回调函数编写

首先在Visual Studio中添加MQTTnet支持库[项目-> 管理NuGet程序包]

 安装MQTTnet和MQTTnet.Extensions.ManagedClient两个包。

首先应该建立MQTT Client,并设置对应的订阅主题

  1. private IMqttClient mqttClient;
  2. public const string topic1 = "test";
  3. public const string topic2 = "announcement";
  4. public const string mantopic2 = "test2";
  5. public const string pubtopic3 = "test3";
  6. public const string refreshtopic = "refresh";
  7. private SQLexe sql = new SQLexe();
  8. Task task,task1;
  9. public void MqttConnectAsync()
  10. {
  11. try
  12. {
  13. var mqttFactory = new MqttFactory();
  14. var mqttClientOptions = new MqttClientOptionsBuilder()
  15. .WithTcpServer("127.0.0.1", 1883)
  16. .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
  17. .WithClientId("TransferService")
  18. .WithCleanSession(false)
  19. .WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
  20. .WithCredentials(null)
  21. .Build();
  22. mqttClient = mqttFactory.CreateMqttClient();
  23. var mqttSubscribeOptions = mqttFactory.CreateSubscribeOptionsBuilder()
  24. .WithTopicFilter(
  25. f =>
  26. {
  27. f.WithTopic(topic1);
  28. }
  29. )
  30. .WithTopicFilter(
  31. f =>
  32. {
  33. f.WithTopic(topic2);
  34. }
  35. )
  36. .WithTopicFilter(
  37. f =>
  38. {
  39. f.WithTopic(mantopic2);
  40. }
  41. )
  42. .Build();
  43. mqttClient.DisconnectedAsync += MqttClient_DisconnectedAsync;
  44. mqttClient.ConnectedAsync += MqttClient_ConnectedAsync;
  45. mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync;
  46. task = mqttClient.ConnectAsync(mqttClientOptions, CancellationToken.None);
  47. task.Wait();
  48. task1 = mqttClient.SubscribeAsync(mqttSubscribeOptions, CancellationToken.None);
  49. task1.Wait();
  50. Console.WriteLine("MQTT client subscribed to ({0} , {1} , {2}) topics.", topic1, topic2, pubtopic3);
  51. }
  52. catch (Exception ex)
  53. {
  54. task.Dispose();
  55. task1.Dispose();
  56. mqttClient.Dispose();
  57. Console.WriteLine(ex.ToString());
  58. }
  59. }

根据设置定义回调函数

MQTT服务连接成功回调函数

  1. private Task MqttClient_ConnectedAsync(MqttClientConnectedEventArgs args)
  2. {
  3. Console.WriteLine($"Mqtt Client Connected.");
  4. return Task.CompletedTask;
  5. }

MQTT服务断开连接回调函数

  1. private Task MqttClient_DisconnectedAsync(MqttClientDisconnectedEventArgs arg)
  2. {
  3. Console.WriteLine($"Mqtt Client DisConnected.");
  4. return Task.CompletedTask;
  5. }

MQTT消息接收处理回调函数

  1. private async Task MqttClient_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg)
  2. {
  3. if (arg.ReasonCode == MqttApplicationMessageReceivedReasonCode.Success)
  4. {
  5. string topic_n = arg.ApplicationMessage.Topic;
  6. string mess = Encoding.UTF8.GetString(arg.ApplicationMessage.PayloadSegment);
  7. if (!string.IsNullOrEmpty(mess))
  8. {
  9. switch (topic_n)
  10. {
  11. case topic1:
  12. Dictionary<string, string> msg = JsonToDictionary(mess);
  13. float humidity = float.Parse(msg.ElementAt(0).Value);
  14. float temperature = float.Parse(msg.ElementAt(1).Value);
  15. state = int.Parse(msg.ElementAt(2).Value);
  16. sql.insertTest(humidity, temperature, state);
  17. Console.WriteLine("humidity is {0}%\ntemperature is {1}℃\nStats is {2}", humidity, temperature, state);
  18. // Max humidity should not be bigger than 92%
  19. if (humidity > 92)
  20. {
  21. state = 0;
  22. var publishMessage = new MqttApplicationMessageBuilder()
  23. .WithTopic(pubtopic3)
  24. .WithPayload("0") // shutdown device
  25. .Build();
  26. await mqttClient.PublishAsync(publishMessage, CancellationToken.None);
  27. }
  28. break;
  29. case topic2:
  30. if (mess.Equals("ready"))
  31. {
  32. Console.WriteLine("MQTT Server Reconnect Ready");
  33. }
  34. break;
  35. case mantopic2:
  36. if (mess.Equals("open"))
  37. {
  38. var publishMessage = new MqttApplicationMessageBuilder()
  39. .WithTopic(pubtopic3)
  40. .WithPayload("1") // shutdown device
  41. .Build();
  42. await mqttClient.PublishAsync(publishMessage, CancellationToken.None);
  43. state = 1;
  44. }
  45. else
  46. {
  47. var publishMessage = new MqttApplicationMessageBuilder()
  48. .WithTopic(pubtopic3)
  49. .WithPayload("0") // shutdown device
  50. .Build();
  51. await mqttClient.PublishAsync(publishMessage, CancellationToken.None);
  52. state = 0;
  53. }
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. }
  60. }

消息接收处理回调函数中主要是对不同订阅主题的数据进行简单的操作和处理,其中就包括对发送Json数据的解包和数据库存储,异常数据的快速响应处理(上述代码中就针对湿度超过92%的情况进行了报警,在湿度超过92%后,向ESP8266发送关闭指令,模拟实际场景下某种指标超过预警阈值对设备进行急停操作)。

  1. Dictionary<string, string> msg = JsonToDictionary(mess);
  2. float humidity = float.Parse(msg.ElementAt(0).Value);
  3. float temperature = float.Parse(msg.ElementAt(1).Value);
  4. state = int.Parse(msg.ElementAt(2).Value);
  5. sql.insertTest(humidity, temperature, state);

上述代码中这5行就是主要对硬件数据进行Json解包,然后将数据存入数据库中的关键操作。

数据库操作这块,应该是要做个接口去做数据存储,我这边就是弄得简单点,直接直连了(FreeSQL),代码如下:

  1. using ABV物联网监控平台.Entity;
  2. using dotNettest.Entity;
  3. namespace ABV物联网监控平台
  4. {
  5. internal class SQLexe
  6. {
  7. public static IFreeSql freeSql { get; } = new FreeSql.FreeSqlBuilder()
  8. .UseConnectionString(FreeSql.DataType.SqlServer, @"server=127.0.0.1;database=TutorialDB;User Id=sa;Password=123456;TrustServerCertificate=true;Pooling=true;Min Pool Size=1;Packet Size=512")
  9. .UseAutoSyncStructure(true)
  10. .Build();
  11. private bool errorCode = true;
  12. public bool Check(string usrname, string password)
  13. {
  14. List<Info> result = freeSql.Select<Info>()
  15. .Where(a => a.usrname.Equals(usrname) && a.password.Equals(password))
  16. .ToList();
  17. if (result.Count != 0 )
  18. {
  19. return true;
  20. }
  21. return false;
  22. }
  23. public void insertTest(float humidity, float temperature, int state)
  24. {
  25. freeSql.Insert(new 温湿度监测设备
  26. {
  27. humidity = humidity,
  28. temperature = temperature,
  29. state = state
  30. }).IgnoreColumns(x => x.id).ExecuteAffrows();
  31. }
  32. public List<温湿度监测设备> GetInfo()
  33. {
  34. try
  35. {
  36. List<温湿度监测设备> result = freeSql.Select<温湿度监测设备>()
  37. .WithSql("select top 100 * from 温湿度监测设备 order by id desc")
  38. .ToList();
  39. return result;
  40. }
  41. catch (Exception ex)
  42. {
  43. if (errorCode)
  44. {
  45. errorCode = false;
  46. MessageBox.Show("SQL Server服务器连接超时");
  47. }
  48. }
  49. return null;
  50. }
  51. }
  52. }

顺便贴个String转Dictionary的常用代码吧(Newtonsoft.Json)

  1. public static Dictionary<string, string> JsonToDictionary(string jsonStr)
  2. {
  3. try
  4. {
  5. Dictionary<string, string> dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
  6. return dic;
  7. }
  8. catch (Exception ex)
  9. {
  10. Console.WriteLine(ex.ToString());
  11. }
  12. return null;
  13. }

2.4 C#界面设计

界面的话,就弄个自己喜欢的样子就行,我这就做的winForm的,搞得比较简单。数据直接定时从数据库进行拉取,然后刷新图表就可以了,然后根据硬件上传数据中的硬件开关状态更新界面上显示的提示文字,再就没啥了。

 帆软报表界面

 c#前端代码

  1. using MQTTClient;
  2. using Newtonsoft.Json;
  3. using ScottPlot;
  4. using System.Text;
  5. using System.Timers;
  6. namespace ABV物联网监控平台
  7. {
  8. public partial class Form2 : Form
  9. {
  10. MQTTexe mQT = new MQTTexe();
  11. FORMexe fORMexe;
  12. TransferService transferservice = new TransferService();
  13. private System.Timers.Timer timer, timer1 = null;
  14. ScottPlot.Plot plt1, plt2 = null;
  15. public Form2()
  16. {
  17. this.MaximizeBox = false;
  18. InitializeComponent();
  19. notifyIcon1.Icon = ABV物联网监控平台.Properties.Resources.WST_notify_LOGO;
  20. notifyIcon1.BalloonTipClosed += NotifyIcon1_BalloonTipClosed;
  21. }
  22. private void NotifyIcon1_BalloonTipClosed(object? sender, EventArgs e)
  23. {
  24. notifyIcon1.Visible = false;
  25. }
  26. private void Form2_Load(object sender, EventArgs e)
  27. {
  28. notifyIcon1.BalloonTipTitle = "登陆成功";
  29. notifyIcon1.Visible = true;
  30. notifyIcon1.BalloonTipText = "个人主页已被允许操作";
  31. notifyIcon1.ShowBalloonTip(0);
  32. TreeNode fcun1 = treeView1.Nodes.Add("功能1");
  33. fcun1.Nodes.Add("detail1");
  34. fcun1.Nodes.Add("detail2");
  35. fcun1.Nodes.Add("detail3");
  36. fcun1.ExpandAll();
  37. TreeNode fcun2 = treeView1.Nodes.Add("功能2");
  38. fcun2.Nodes.Add("detail1");
  39. fcun2.Nodes.Add("detail2");
  40. fcun2.Nodes.Add("detail3");
  41. fcun2.Nodes.Add("detail4");
  42. fcun2.ExpandAll();
  43. TreeNode fcun3 = treeView1.Nodes.Add("功能3");
  44. fcun3.Nodes.Add("detail1");
  45. fcun3.Nodes.Add("detail2");
  46. fcun3.Nodes.Add("detail3");
  47. fORMexe = new FORMexe(formsPlot1, formsPlot2);
  48. }
  49. private void MQTTtrans_Click(object sender, EventArgs e)
  50. {
  51. transferservice.MqttConnectAsync();
  52. if (transferservice.GetMQTTStats())
  53. {
  54. this.label1.Text = "已启动";
  55. this.label1.ForeColor = Color.Green;
  56. notifyIcon1.BalloonTipTitle = "提示";
  57. notifyIcon1.Visible = true;
  58. notifyIcon1.BalloonTipText = "MQTT业务服务已启动成功";
  59. notifyIcon1.ShowBalloonTip(0);
  60. timer = new System.Timers.Timer();
  61. timer.Interval = 1000;
  62. timer.AutoReset = true;
  63. timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  64. timer.Start();
  65. }
  66. else
  67. {
  68. this.label1.Text = "未启动";
  69. this.label1.ForeColor = Color.Red;
  70. notifyIcon1.BalloonTipTitle = "提示";
  71. notifyIcon1.Visible = true;
  72. notifyIcon1.BalloonTipText = "MQTT业务服务启动失败";
  73. notifyIcon1.ShowBalloonTip(0);
  74. }
  75. }
  76. private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  77. {
  78. }
  79. private void dataGrap_CheckedChanged(object sender, EventArgs e)
  80. {
  81. timer1 = new System.Timers.Timer();
  82. timer1.Interval = 1000;
  83. timer1.AutoReset = true;
  84. timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
  85. timer1.Start();
  86. }
  87. private void startMQTTservice_Click(object sender, EventArgs e)
  88. {
  89. CMDexe cM = new CMDexe();
  90. cM.StartService();
  91. if (mQT.GetMQTTStats("startCheck"))
  92. {
  93. this.MQTTstats.Text = "已启动";
  94. this.MQTTstats.ForeColor = Color.Green;
  95. notifyIcon1.BalloonTipTitle = "提示";
  96. notifyIcon1.Visible = true;
  97. notifyIcon1.BalloonTipText = "MQTT服务器已启动成功";
  98. notifyIcon1.ShowBalloonTip(0);
  99. }
  100. else
  101. {
  102. this.MQTTstats.Text = "未启动";
  103. this.MQTTstats.ForeColor = Color.Red;
  104. notifyIcon1.BalloonTipTitle = "提示";
  105. notifyIcon1.Visible = true;
  106. notifyIcon1.BalloonTipText = "MQTT服务器启动失败";
  107. notifyIcon1.ShowBalloonTip(0);
  108. }
  109. }
  110. private void timer1_Elapsed(object? sender, ElapsedEventArgs e)
  111. {
  112. UpdateChart();
  113. }
  114. private void UpdateChart()
  115. {
  116. //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
  117. Action invokeAction = new Action(UpdateChart);
  118. //判断操作控件的线程是否创建控件的线程
  119. //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
  120. if (this.InvokeRequired)
  121. {
  122. //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
  123. this.Invoke(invokeAction);
  124. }
  125. else
  126. {
  127. fORMexe.UpdateChart(formsPlot1, formsPlot2);
  128. }
  129. }
  130. private void timer_Elapsed(object? sender, ElapsedEventArgs e)
  131. {
  132. UpdateUIText();
  133. }
  134. private void UpdateUIText()
  135. {
  136. //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
  137. Action invokeAction = new Action(UpdateUIText);
  138. //判断操作控件的线程是否创建控件的线程
  139. //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
  140. if (this.InvokeRequired)
  141. {
  142. //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
  143. this.Invoke(invokeAction);
  144. }
  145. else
  146. {
  147. if (transferservice.GetMQTTStats())
  148. {
  149. this.MQTTstats.Text = "已启动";
  150. this.MQTTstats.ForeColor = Color.Green;
  151. }
  152. else
  153. {
  154. this.MQTTstats.Text = "未启动";
  155. this.MQTTstats.ForeColor = Color.Red;
  156. }
  157. if (transferservice.GetDeviceStats())
  158. {
  159. this.devicestats.Text = "打开";
  160. this.devicestats.ForeColor = Color.Green;
  161. }
  162. else
  163. {
  164. this.devicestats.Text = "关闭";
  165. this.devicestats.ForeColor = Color.Red;
  166. }
  167. }
  168. }
  169. private void stopMQTTservice_Click(object sender, EventArgs e)
  170. {
  171. CMDexe cM = new CMDexe();
  172. cM.StoptService();
  173. if (!mQT.GetMQTTStats("stopCheck"))
  174. {
  175. this.MQTTstats.Text = "未启动";
  176. this.MQTTstats.ForeColor = Color.Red;
  177. notifyIcon1.BalloonTipTitle = "提示";
  178. notifyIcon1.Visible = true;
  179. notifyIcon1.BalloonTipText = "MQTT服务器已关闭成功";
  180. notifyIcon1.ShowBalloonTip(0);
  181. }
  182. else
  183. {
  184. this.MQTTstats.Text = "已启动";
  185. this.MQTTstats.ForeColor = Color.Green;
  186. notifyIcon1.BalloonTipTitle = "提示";
  187. notifyIcon1.Visible = true;
  188. notifyIcon1.BalloonTipText = "MQTT服务器关闭失败";
  189. notifyIcon1.ShowBalloonTip(0);
  190. }
  191. }
  192. private void MQTTstats_Click(object sender, EventArgs e)
  193. {
  194. }
  195. private void activateMoniter_Click(object sender, EventArgs e)
  196. {
  197. try
  198. {
  199. if (transferservice.GetMQTTStats())
  200. {
  201. mQT.ActivateDevice();
  202. }
  203. else
  204. {
  205. notifyIcon1.BalloonTipTitle = "提示";
  206. notifyIcon1.Visible = true;
  207. notifyIcon1.BalloonTipText = "M QTT服务未激活,请先激活MQTT服务";
  208. notifyIcon1.ShowBalloonTip(0);
  209. }
  210. }
  211. catch (Exception ex)
  212. {
  213. notifyIcon1.BalloonTipTitle = "提示";
  214. notifyIcon1.Visible = true;
  215. notifyIcon1.BalloonTipText = "设备联通失败,请检查MQTT服务是否正常";
  216. notifyIcon1.ShowBalloonTip(0);
  217. Console.WriteLine(ex.ToString());
  218. }
  219. }
  220. private void disposeMoniter_Click(object sender, EventArgs e)
  221. {
  222. try
  223. {
  224. if (transferservice.GetMQTTStats())
  225. {
  226. mQT.DeactivateDevice();
  227. }
  228. else
  229. {
  230. notifyIcon1.BalloonTipTitle = "提示";
  231. notifyIcon1.Visible = true;
  232. notifyIcon1.BalloonTipText = "MQTT服务未激活,请先激活MQTT服务";
  233. notifyIcon1.ShowBalloonTip(0);
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. notifyIcon1.BalloonTipTitle = "提示";
  239. notifyIcon1.Visible = true;
  240. notifyIcon1.BalloonTipText = "设备联通失败,请检查MQTT服务是否正常";
  241. notifyIcon1.ShowBalloonTip(0);
  242. Console.WriteLine(ex.ToString());
  243. }
  244. }
  245. private void refresh_device_stats_Click(object sender, EventArgs e)
  246. {
  247. transferservice.RefreshDeviceStats();
  248. }
  249. private async void GPT_Click(object sender, EventArgs e)
  250. {
  251. var values = new Dictionary<string, string>
  252. {
  253. { "Gretting", "Nice to talk to you" },
  254. };
  255. var jsonData = JsonConvert.SerializeObject(values);
  256. var contentData = new StringContent(jsonData, Encoding.UTF8, "application/json");
  257. var client = new HttpClient();
  258. var content = new FormUrlEncodedContent(values);
  259. var response = await client.PostAsync("http://127.0.0.1:5000", content);
  260. var responseString = await response.Content.ReadAsStringAsync();
  261. MessageBox.Show(responseString);
  262. }
  263. }
  264. }

3.小结

主要介绍了一种基于ESP8266、EMQX开源版本MQTT服务器与C#的物联网平台实验,主要在不借助云服务平台的情况下,第一次打通了从硬件到服务端的双向数据交互过程,实现了硬件数据的定时上传以及服务器端对硬件的及时控制,我认为此次实验所达到的物联网服务距离真正的工业场景可能还有比较大的差距,但是针对智能家居领域应该还是可以应用的,本地化不需要借助各类的云服务。

现在的开发过程还是过于野路子,后续需要对整个项目进行规范化设计,再多深入思考思考,多看看更底层的原理。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号