我是靠谱客的博主 朴素大船,最近开发中收集的这篇文章主要介绍【C#】用Visual Studio连接ONENET物联网平台做一个简易版微信,兼带连接esp8266通讯控继电器功能前言onenet端的配置Visual Studio的编程文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

简易版微信

  • 前言
  • onenet端的配置
  • Visual Studio的编程
    • 主界面
    • 代码
  • 文件

前言

这是一个期末设计。。。。时间有点赶,基本上是一天晚上做完,所以可能还有很多地方不完善,基础功能是可以互相通信聊天(微信的功能),以及可以和esp8266进行物联网通讯,控制esp8266状态。
效果图:
在这里插入图片描述

参考资料有:
onenet开发文档
C# 基于OneNet 的物联网数据通信
老师的公众号:图形编程与电子设计

onenet端的配置

关于onenet的详细配置和使用方法,可以看我之前的文章:物联网开发之esp8266连接onenet控制板载小灯(一)

esp8266端的配置参考:物联网开发之esp8266连接onenet控制板载小灯

正常连接调试后的效果是这样子的:
在这里插入图片描述

Visual Studio的编程

主界面

在这里插入图片描述
在这里插入图片描述

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.IO.Ports;

using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Collections.Specialized;

namespace QQ
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入
        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }
        string[] Redata = new string[1024];
        
        private void pictureBox1_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string url = "http://api.heclouds.com/devices/870266125/datapoints?";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "7KX1Igdrz55c2tz30HyzAKuOwBo=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{"datastreams": [{"id": "shuju","datapoints": [{"value":""+ textBox2.Text+""}]}]}";

            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                label1.Text = reader.ReadToEnd();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void shuju_chuli()
        {
            textBox1.Text = Redata[19];
            
        }



            private void button1_Click(object sender, EventArgs e)
        {
            string url = "http://api.heclouds.com/devices/870266125/datastreams/shuju";//设备地址
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            SetHeaderValue(request.Headers, "api-key", "7KX1Igdrz55c2tz30HyzAKuOwBo=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();      //设置一个变量来保存数据流读取到的
            myStreamReader.Close();
            myResponseStream.Close();
            label6.Text = retString;
            Redata = retString.Split('"');
            shuju_chuli();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string url = "http://api.heclouds.com/devices/870266125/datapoints?";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "7KX1Igdrz55c2tz30HyzAKuOwBo=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{"datastreams": [{"id": "LED","datapoints": [{"value":" + "1" + "}]}]}";   //在esp8266里面设置接收到1就打开

            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                label6.Text = reader.ReadToEnd();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string url = "http://api.heclouds.com/devices/870266125/datapoints?";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "7KX1Igdrz55c2tz30HyzAKuOwBo=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{"datastreams": [{"id": "LED","datapoints": [{"value":" + "0" + "}]}]}";    //在esp8266里面设置接收到0就关闭

            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                label6.Text = reader.ReadToEnd();
            }
        }
    }
}

文件

工程文件下载地址

最后

以上就是朴素大船为你收集整理的【C#】用Visual Studio连接ONENET物联网平台做一个简易版微信,兼带连接esp8266通讯控继电器功能前言onenet端的配置Visual Studio的编程文件的全部内容,希望文章能够帮你解决【C#】用Visual Studio连接ONENET物联网平台做一个简易版微信,兼带连接esp8266通讯控继电器功能前言onenet端的配置Visual Studio的编程文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部