• esp8266通过udp控制电脑滚轮操作


    8266端

    通过监听8888端口所收到的消息,记录所收到消息的ip和端口,并在编码器动作的时候给该端口发送编码器的值

    点击查看代码
    #include <Arduino.h>
    
    #include <ESP8266WiFi.h>
    #include <WiFiUdp.h>
    #include "AiEsp32RotaryEncoder.h"
    
    #define STASSID "HangLing"
    #define STAPSK "1a2b3c4d"
    //静态地址、网关、子网掩码
    IPAddress local_IP(192, 168, 31, 220);
    IPAddress gateway(192, 168, 31, 1);
    IPAddress subnet(255, 255, 255, 0);
    unsigned int localPort = 8888; // local port to listen on
    
    // 定义远程IP和端口
    IPAddress remote_IP(192, 168, 31, 171);
    unsigned int remotePort = 8888;
    // buffers for receiving and sending data
    char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; // buffer to hold incoming packet,
    
    WiFiUDP Udp;
    
    #define ROTARY_ENCODER_A_PIN D5
    #define ROTARY_ENCODER_B_PIN D6
    #define ROTARY_ENCODER_BUTTON_PIN D7
    
    #define ROTARY_ENCODER_STEPS 4
    AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);
    volatile long change_encode = 0;
    volatile int last_button = 0;
    
    void IRAM_ATTR readEncoderISR()
    {
      rotaryEncoder.readEncoder_ISR();
    }
    
    // 更新编码器数值
    void updateRotaryEncoder()
    {
    
      change_encode = rotaryEncoder.encoderChanged();
      last_button = rotaryEncoder.isEncoderButtonClicked();
    
      // 验证数据有效性
      if (change_encode != 0 || last_button != 0)
      {
        String send_data = "{\"encode\":" + String(change_encode) + ",\"button\":" + String(last_button) + "}";
        // Serial.println(send_data);
        // send a reply, to the IP address and port that sent us the packet we received
        Udp.beginPacket(remote_IP, remotePort);
        Udp.write(send_data.c_str());
        Udp.endPacket();
      }
    }
    
    void setup()
    {
      Serial.begin(9600);
    
      //设置
      WiFi.mode(WIFI_STA);
      WiFi.config(local_IP, gateway, subnet); //设置静态IP
      WiFi.begin(STASSID, STAPSK);
      while (WiFi.status() != WL_CONNECTED)
      {
        Serial.print('.');
        delay(200);
      }
    
      rotaryEncoder.begin();
      rotaryEncoder.setup(readEncoderISR);
      rotaryEncoder.setAcceleration(1);
    
      Serial.print("Connected! IP address: ");
      Serial.println(WiFi.localIP());
      Serial.printf("UDP server on port %d\n", localPort);
      Udp.begin(localPort);
    }
    
    void loop()
    {
      int packetSize = Udp.parsePacket();
      if (packetSize)
      {
        // Serial.printf("Received packet of size %d from %s:%d\n    (to %s:%d, free heap = %d B)\n",
        //               packetSize,
        //               Udp.remoteIP().toString().c_str(), Udp.remotePort(),
        //               Udp.destinationIP().toString().c_str(), Udp.localPort(),
        //               ESP.getFreeHeap());
    
        // read the packet into packetBufffer
        int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
        packetBuffer[n] = 0;
        // Serial.print(F("Contents:"));
        // Serial.println(packetBuffer);
        // 赋值远程IP和端口
        remote_IP = Udp.remoteIP();
        remotePort = Udp.remotePort();
      }
    
      updateRotaryEncoder();
    }
    
    

    电脑端

    运行Python,功能是监听ESP8266的固定ip和端口
    将收到的消息解析并通过pyautogui执行滚轮操作

    点击查看代码
    import socket
    import time
    import pyautogui as pg
    import json
    
    
    pg.FAILSAFE = False
    
    print('开始')
    # 创建 socket
    sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    addr = ('192.168.31.220', 8888)
    i = 1
    while True:
        msg = f"{i}"
        i += 1
        # 发送数据报
        # print(f'发送数据报:{msg}')
        sk.sendto(msg.encode('utf-8'), addr)
        # 接收数据报
        msg_recv, addr = sk.recvfrom(1024)
        # 打印
        print(msg_recv.decode('utf-8'))
        # {"encode":0,"button":0}
        json_data = json.loads(msg_recv.decode('utf-8'))
        encoder = json_data.get('encode')
        encoder = int(encoder)*100
        button = json_data.get('button')
        if not encoder == 0 :
            # print(encoder, button)
            pg.scroll(encoder)
    
    # 关闭 socket
    # sk.close()
    
    
  • 相关阅读:
    读书分析和读书步骤的方法
    如何在招聘中挖掘面试人员的能力
    接口测试自动化之Robot Framework
    电商后台系统产品逻辑全解析
    RobotFramework之环境安装、关键字、库详解(三)
    管理学相关系统知识
    软件测试岗位人员离职文档交接
    软件测试人员招聘计划
    测试总结过程中的质量分析报告
    测试人员应该怎么写测试总结?
  • 原文地址:https://www.cnblogs.com/dapenson/p/16279829.html
Copyright © 2020-2023  润新知