C#用Visual Studio连接ONENET物联网平台做一个简易版微信,兼带连接esp8266通讯控继电器功能

Posted 创客协会的阿蛋°

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#用Visual Studio连接ONENET物联网平台做一个简易版微信,兼带连接esp8266通讯控继电器功能相关的知识,希望对你有一定的参考价值。

前言

这是一个期末设计。。。。时间有点赶,基本上是一天晚上做完,所以可能还有很多地方不完善,基础功能是可以互相通信聊天(微信的功能),以及可以和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通讯控继电器功能的主要内容,如果未能解决你的问题,请参考以下文章

ESP系列SDK开发连接 oneNET Studio 平台(包含token生成算法)

ESP系列SDK开发连接 oneNET Studio 平台(包含token生成算法)

中国移动物联网开放平台OneNET学习笔记——设备接入(MQTT协议)OneNET Studio篇

中国移动物联网开放平台OneNET学习笔记——设备接入(MQTT协议)OneNET Studio篇

中国移动物联网开放平台OneNET学习笔记——设备接入测试(MQTT协议)OneNET Studio篇

中国移动物联网开放平台OneNET学习笔记——设备接入测试(MQTT协议)OneNET Studio篇