【问题标题】:Connect NodeMCU server using C#使用 C# 连接 NodeMCU 服务器
【发布时间】:2016-11-05 10:45:06
【问题描述】:

我有一个带有 ESP8266E 芯片的 NodeMCU 0.9 板。我可以使用下面的简单代码打开和关闭 LED。此代码使用 Arduino IDE 编写。

            #include <ESP8266WiFi.h>

            const char* ssid = "iPhone";
            const char* password = "1234567890ABCD";

            int ledPin = D0; // GPIO16
            WiFiServer server(80);

            void setup() {
              Serial.begin(9600);
              delay(10);

              pinMode(ledPin, OUTPUT);
              digitalWrite(ledPin, LOW);

              // Connect to WiFi network
              Serial.println();
              Serial.println();
              Serial.print("Connecting to ");
              Serial.println(ssid);

              WiFi.begin(ssid, password);

              while (WiFi.status() != WL_CONNECTED) {
                delay(500);
                Serial.print(".");
              }
              Serial.println("");
              Serial.println("WiFi connected");

              // Start the server
              server.begin();
              Serial.println("Server started");

              // Print the IP address
              Serial.print("Use this URL to connect: ");
              Serial.print("http://");
              Serial.print(WiFi.localIP());
              Serial.println("/");

            }

            void loop() {
              // Check if a client has connected
              WiFiClient client = server.available();
              if (!client) {
                return;
              }

              // Wait until the client sends some data
              Serial.println("new client");
              while(!client.available()){
                delay(1);
              }

              // Read the first line of the request
              //String request = client.readStringUntil('\r');
              String request = client.readString();
              Serial.println(request);
              client.flush();

              // Match the request

              int value = LOW;
              if (request.indexOf("/LED=ON") != -1)  {
                digitalWrite(ledPin, LOW);
                value = LOW;
              }
              if (request.indexOf("/LED=OFF") != -1)  {
                digitalWrite(ledPin, HIGH);
                value = HIGH;
              }

            // Set ledPin according to the request
            //digitalWrite(ledPin, value);

              // Return the response
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println(""); //  do not forget this one
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");

              client.print("Led pin is now: ");

              if(value == LOW) {
                client.print("On");
              } else {
                client.print("Off");
              }
              client.println("<br><br>");
              client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
              client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  
              client.println("</html>");

              delay(1);
              Serial.println("Client disonnected");
              Serial.println("");

            }

然后我可以使用我的网络浏览器连接到 NodeMCU 并打开和关闭 LED。NodeMCU 还将 LED 状态数据发送到浏览器。但是,我想使用 C# 表单应用程序来做到这一点。 我尝试使用此代码。它在 Visual Studio 2013 中没有给出任何编译错误。但是,它无法连接到 NodeMCU 服务器。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;


    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {

            static TcpClient tcpClient = new TcpClient();
            public static readonly byte[] ip = new byte[] { 172, 20, 10, 2 };

            static IPAddress ipAddress = new IPAddress(ip);
            IPEndPoint hostEndPoint = new IPEndPoint(ipAddress, 80);
            static Socket soc = new Socket(AddressFamily.InterNetwork,  SocketType.Stream, ProtocolType.IP);
            ASCIIEncoding asen = new ASCIIEncoding();


            public Form1()
            {

                InitializeComponent();

            }


            private void button2_Click(object sender, EventArgs e)
            {
                soc.Connect(ipAddress, 80);
                NetworkStream stm = new NetworkStream(soc);
                tcpClient.Connect(ipAddress, 80);
                stm = tcpClient.GetStream();
                byte[] ba = asen.GetBytes("/LED=ON/r");
                stm.Write(ba,0,ba.Length);
            }

            private void button3_Click(object sender, EventArgs e)
            {
                soc.Connect(ipAddress, 80);
                NetworkStream stm = new NetworkStream(soc);
                tcpClient.Connect(ipAddress, 80);
                stm = tcpClient.GetStream();
                byte[] ba = asen.GetBytes("/LED=OFF/r");
                stm.Write(ba, 0, ba.Length);
            }
        }
    }

帮我用 C# 实现一个代码。

【问题讨论】:

  • 结果如何?它会抛出异常吗?您可以发布异常消息吗?
  • @Peter Visual Studio 没有给出任何异常。

标签: c# visual-studio arduino esp8266


【解决方案1】:

虽然您可以使用低级 API 发出 HTTP 请求,但最好使用 HttpClient 类:https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.110).aspx

这是您的 button2 事件处理程序的简单示例:

private async void button2_Click(object sender, EventArgs e)
{
    using (var client = new HttpClient())
    {
        HttpResponseMessage request = await client.GetAsync("http://172.16.20.10/LED=on");
    }
}

【讨论】:

  • 第一个错误是您缺少 HTTP 标头,可能还有其他问题。
  • 谢谢..我会尝试..但是,我的 C# 代码有什么问题?
猜你喜欢
  • 2016-04-06
  • 2011-12-13
  • 2017-09-07
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多