[作品提交] 【得捷电子Follow me第4期】基础任务二:主控板建立TCPIP或UDP服务器

zygalaxy   2024-2-24 21:30 楼主

基础任务二:主控板建立TCPIP或UDP服务器,局域网PC使用TCPIP或UDP客户端进行连接并发送数据,主控板接收到数据后,送液晶屏显示(没有则通过串口打印显示);通过抓包软件抓取交互报文,展示并分析。(TCP和UDP二选一,或者全都操作)

 

由于之前的任务,已经完成了基本的网络配置,本任务只需要在本地启动一个http服务即可。

 

根据示例中的代码修改即可成为一个完整的http服务器。通过网页控制灯的开关

以下是具体代码:

#include <SPI.h>
#include <Ethernet.h>

// Screen
#include "PDLS_EXT3_Basic_Global.h"

// SDK
// #include <Arduino.h>
#include "hV_HAL_Peripherals.h"

// Include application, user and local libraries
// #include <SPI.h>

// Configuration
#include "hV_Configuration.h"

Screen_EPD_EXT3 myScreen(eScreen_EPD_EXT3_370, boardRaspberryPiPico_RP2040);

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 122);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

String reqArgs;
// Utilities
///
/// [url=home.php?mod=space&uid=159083]@brief[/url] Wait with countdown
/// @param second duration, s
///
void wait(uint8_t second)
{
    for (uint8_t i = second; i > 0; i--)
    {
        Serial.print(formatString(" > %i  \r", i));
        delay(1000);
    }
    Serial.print("         \r");
}

void handlerHttp()

{
    myScreen.clear();
    if (reqArgs.indexOf("LED=on") > -1)
    {
        digitalWrite(25, HIGH);
        myScreen.gText(10, 10, "LED is on");
        myScreen.flush();
    }

    if (reqArgs.indexOf("LED=off") > -1)
    {
        digitalWrite(25, LOW);
        myScreen.gText(10, 10, "LED is off");
        myScreen.flush();
    }

    if (reqArgs.indexOf("LED=sw") > -1)
    {
        digitalWrite(25, !digitalRead(25));
        myScreen.gText(10, 10, formatString("LED is %s", digitalRead(25) ? "on" : "off"));
        myScreen.flush();
    }
}
void setup()

{
    // 配置LED
    pinMode(25, OUTPUT);
    digitalWrite(25, LOW);
    // Ethernet.init(17);
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for native USB port only
    }
    Serial.println("Ethernet WebServer Example");

    // start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);

    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware)
    {
        Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        while (true)
        {
            delay(1); // do nothing, no point running without Ethernet hardware
        }
    }
    if (Ethernet.linkStatus() == LinkOFF)
    {
        Serial.println("Ethernet cable is not connected.");
    }

    // start the server
    myScreen.begin();
    Serial.println(formatString("%s %ix%i", myScreen.WhoAmI().c_str(), myScreen.screenSizeX(), myScreen.screenSizeY()));
    myScreen.clear();
    myScreen.setOrientation(7);

    myScreen.selectFont(Font_Terminal12x16);
    myScreen.flush();
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
}

void loop()
{
    // listen for incoming clients
    EthernetClient client = server.available();
    if (client)
    {
        Serial.println("new client");
        // an HTTP request ends with a blank line
        bool currentLineIsBlank = true;
        while (client.connected())
        {
            if (client.available())
            {
                char c = client.read();
                Serial.write(c);
                reqArgs += c;
                // if you've gotten to the end of the line (received a newline
                // character) and the line is blank, the HTTP request has ended,
                // so you can send a reply
                if (c == '\n' && currentLineIsBlank)
                {
                    // send a standard HTTP response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close"); // the connection will be closed after completion of the response
                    client.println("Refresh: 5");        // refresh the page automatically every 5 sec
                    client.println();
                    client.println("<!DOCTYPE HTML>");
                    client.println("<html>");

                    client.println("</html>");
                    handlerHttp();
                    break;
                }
                if (c == '\n')
                {
                    // you're starting a new line
                    currentLineIsBlank = true;
                }
                else if (c != '\r')
                {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }
            }
        }
        // give the web browser time to receive the data
        delay(1);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
    }
}

上传代码到pico以后,观察串口等待服务器启动

 

然后在浏览器地址栏目输入http://192.168.0.122/?led=on 即可打开板载的灯

输入http://192.168.0.122/?led=off ,就会把打开的灯再次关闭,输入http://192.168.0.122/?led=sw 板载的led灯会切换亮灭

image.png  

 

可以观察到灯跟随请求亮灭并在显示屏上显示亮灭

image.png  

image.png  

并且成功使用wireshark抓取到请求的数据

image.png  

总结:

    通过w5500-pico自身创建tcp服务器可以无线控制灯等外设,控制灯的亮灭还可以使用发送一个网页给浏览器然后在用户界面使用button来控制亮灭的,不过本此任务就先完善到这里,以后有时间再修改加上去。

 

 

本帖最后由 zygalaxy 于 2024-2-23 17:41 编辑

回复评论 (1)

本地启动一个http服务,期待后续用button来控制亮灭,,

点赞  2024-2-25 08:14
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复