Beetle ESP32-C3 微信小程序控制8X8点阵

2023-05-22260
AI 快速预览详细 收起
本文介绍了如何使用Beetle ESP32-C3和微信小程序实现8X8点阵的远程控制。通过特定的UDP端口和验证机制确保数据包格式正确,提高系统的稳定性和可靠性。适用于各种应用场景,特别是STEM教育。

硬件介绍:

ESP32是一系列低成本、低功耗的单片机微控制器

集成了Wi-Fi和双模蓝牙

超高的射频性能、稳定性、通用性、可靠性

以及超低的功耗,满足不同的功耗需求

适用于各种应用场景

视频介绍:

代码:

 

代码
#include <WiFi.h>
#include <WiFiUdp.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>

#define ssid      "2202"
#define password  "66666666668888888888"
#define upd_port  1234

Max72xxPanel myMatrix = Max72xxPanel(7,1,1);
uint8_t  LEDArray[8];
//连接wifi的图案

byte w[8] = {0x3C, 0x42, 0x99, 0x24, 0x42, 0x18, 0x0, 0x18};

//实例化WiFiUDP对象
WiFiUDP Udp;

// 接收UDP数据包数组
byte packetBuffer[9];


void show(byte *data){
  memcpy_P(&LEDArray, data, 8);
  for(int index_i=0; index_i<8; index_i++)
  {
    for(int index_j=0*8; index_j<0*8+8; index_j++)
    {
      if((LEDArray[index_i]&0x01)>0)
        myMatrix.drawPixel(index_j, 7-index_i,1);
      else
        myMatrix.drawPixel(index_j, 7-index_i,0);
      LEDArray[index_i] = LEDArray[index_i]>>1;
    }
  }
  myMatrix.write();
}


void setup() {
  //设置亮度
  myMatrix.setIntensity(8); 
  // 打开串口
  Serial.begin(9600);
  // 连接到wifi
  WiFi.begin(ssid, password);
  // 等待连接
  while (WiFi.status() != WL_CONNECTED) {
    delay(2000);
  }
  Serial.println("连接成功");
  // 启动Udp监听服务
  if (Udp.begin(upd_port)) {
    Serial.println("监听成功");
    // 打印本地的ip地址,在UDP工具中会使用到
    //WiFi.localIP().toString().c_str()用于将获取的本地IP地址转化为字符串
    Serial.printf("现在收听IP:%s, UDP端口:%d\n", WiFi.localIP().toString().c_str(), upd_port);
  } else {
    Serial.println("监听失败");
  }
  show(w);
}

void loop() {
  //解析Udp数据包
  int packetSize = Udp.parsePacket();
  //解析包不为空
  if (packetSize) {
    //收到Udp数据包
    //Udp.remoteIP().toString().c_str()用于将获取的远端IP地址转化为字符串
    Serial.printf("收到来自远程IP:%s(远程端口:%d)的数据包字节数:%d\n", Udp.remoteIP().toString().c_str(), Udp.remotePort(), packetSize);
    Udp.read(packetBuffer, 9);
  // 这里我们将第9位数据固定为某个值,用于判断我们的数据包格式是否正常,(避免随便发个数据包都可以生效)
    if (packetBuffer[8] == 0xef) {
      //向udp工具发送消息
      show(packetBuffer);
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      uint8_t replyPacket=0x01;
      //把数据写入发送缓冲区
      Udp.write(replyPacket);
      //发送数据
      Udp.endPacket();
    }
  }
}

85cd942055444f16b9601dc64e4d84b4.png

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(0)
- 没有更多了 -