Arduino ESP8266 通过WiFi、串口与DMX灯光通讯

24 篇文章 0 订阅
4 篇文章 0 订阅

之前有项目做串口自定数据转dmx协议控制灯光。
链接
奔驰灯光互动项目开发历险记,2019春节前10天 https://blog.csdn.net/qq_38288618/article/details/88559412

python 设备ArtNetToDMX512的协议测试 https://blog.csdn.net/qq_38288618/article/details/87564627 

 


前一阵呢有同仁看了找我交流,看能不能改wifi无线控制灯光。

正好有esp8266模块,wifi通讯是没问题的,觉得有戏,看有没有前辈老师们造好的轮子。 

列出Brief : 

1、smartconfig配网,8266连接AP
2、开UDP端口,接收解析数据
3、开串口,接收解析数据 
4、将这些数据转换成DMX协议,发送给设备
5、按键控制配网任务
6、led显示运行状态 
7、串口和udp数据自定协议 

网上搜索了不少相关资料:

https://www.instructables.com/ESP8266-Artnet-to-DMX/

https://github.com/mtongnz/ESP8266_ArtNetNode_v2

https://github.com/JonasArnold/EthernetDmxNode_esp8266

https://github.com/sparkfun/SparkFunDMX

https://github.com/forkineye/E131

https://github.com/forkineye/ESPAsyncE131

https://github.com/rstephan/ArtnetWifi

https://github.com/FastLED/FastLED

https://arduinojson.org/v6/api/jsondocument/

https://github.com/mtongnz/espDMX

使用 espDMX库做个实践活动:
引用原文部分说明

esp8266DMX v2
Initial version by Matthew Tong, June 2016. This library is derived from the HardwareSerial library.

This library has been updated to support new functionality but is compatible with the previous library.

This library will transmit up to 2 DMX universes from an ESP8266 module. It utilizes the hardware UARTs and is entirely interupt driven meaning the DMX output has very precise timing.

The DMX will refresh at a minimum rate of 44Hz. The library will detect how many channels have been set, outputting less than 512 if possible to increase the refresh rate. This increases the responsiveness of fixtures. It will still output a full 512 channels at least once per second.

View my Instructable for an ESP8266 Artnet to DMX device: http://www.instructables.com/id/ESP8266-Artnet-to-DMX/

If this library is helpful and you're feeling generous, why not shout me a beer: https://www.paypal.me/mtongnz

主要大意简译:
8266开了两路dmx;
刷新频率至少44Hz,
作者DIY的链接
觉得有意义,可以请作者喝两杯啤酒,还是折现吧https://www.paypal.me/mtongnz(应该搞个微信、支付宝收款码效果会更好) 

 

good,我觉得有意义。thanks,不过没办法请你喝两杯了,帮着宣传宣传吧,赠人玫瑰手留余香也好。 

 

使用前文简易的diy多任务框架,造车主文件代码 : 

前文任务框架在这里

/*
  dmxChaser - a simple example for espDMX library
  Copyright (c) 2016, Matthew Tong
  https://github.com/mtongnz/espDMX

  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
  later version.

  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with this program.
  If not, see http://www.gnu.org/licenses/
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#include <WiFiUdp.h>
WiFiUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged\r\n";       // a string to send back

#include <espDMX.h>

unsigned char dd[512];
void dmxWrite(int id , unsigned char v) {
  dd[id] = v;

}
#include "sys_rw.h"
#include "rw_KEY.h"
#include "rw_ledblink.h"
#include "rw_smartconfig.h"
#include "FramePkg.h"
FramePkg pkg = FramePkg(520);
//-------------
//ff
//[1~2]frame length
//[3~4] startID
//[5~n] data
//crc
//fe
//-------------
//n=5~516
//frame length=7~519   pkg[1]+pkg[2]<<8
//arr={3-n};
//len=frame length-5;
static void on_pkg_data_ok(unsigned char arr[], int len) {
  if (len >= 2) {
    //on_FramePkg_ok(arr, len);
    uint16_t startID = arr[0] + (arr[1] << 8);
    for (uint16_t i = 2; i < len; i++) {
      dmxWrite(startID + i - 2, arr[i]);
      //Serial.print(arr[i], 16);
      //Serial.print(" ");
    }
    dmxB.setChans(dd);
  } else {
    Serial.print("pkg err");
  }
  //  Serial.println("ok");
}

// 10 steps with 10 channels in each
//byte dmxChase[][10] = { { 255, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
//  { 0, 255, 0, 0, 0, 0, 0, 0, 0, 0 },
//  { 0, 0, 255, 0, 0, 0, 0, 0, 0, 0 },
//  { 0, 0, 0, 255, 0, 0, 0, 0, 0, 0 },
//  { 0, 0, 0, 0, 255, 0, 0, 0, 0, 0 },
//  { 0, 0, 0, 0, 0, 255, 0, 0, 0, 0 },
//  { 0, 0, 0, 0, 0, 0, 255, 0, 0, 0 },
//  { 0, 0, 0, 0, 0, 0, 0, 255, 0, 0 },
//  { 0, 0, 0, 0, 0, 0, 0, 0, 255, 0 },
//  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 255},
//  { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 },
//  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
//};

//===================================================
typedef unsigned long clock_t;
clock_t nowt ;
clock_t oldt;
unsigned char waitWIFI=0;
void setup() {
  pkg.dispatch_event = &on_pkg_data_ok;
  Serial.begin(256000);
  // Start dmxA, status LED on pin 12 with full intensity
  //dmxA.begin(12);
  //dmxA.begin();
  // Start dmxB, status LED on pin 13 with 75/255 intensity
  //dmxB.begin(13, 75);
  dmxB.begin();
  //

  //
  nowt   =   millis();
  rw_KEY_setup(0);
  rw_ledblink_setup(2);

  if (start_on_key_down == 1) {

    rw_smartconfig_setup(1);

  } else {


    WiFi.mode(WIFI_STA);
    delay(1000);
    //WiFi.begin("robotnet", "robotnetpass");
    //WiFi.setAutoConnect(true);
    //WiFi.setAutoReconnect(true);
    while (WiFi.status() != WL_CONNECTED) {
      delay(1000);
      waitWIFI++;
      if(waitWIFI>10){
        Serial.printf("wifi wait time out.");
        break;
      }
      Serial.printf("wifi wait %d\n.",waitWIFI);
    }
    unsigned int localPort = 6454;      // local port to listen on
    Serial.printf("UDP server on port %d\n", localPort);
    Udp.begin(localPort);


  }
  oldt = nowt;
}

unsigned char sbuf[16];
void loop() {
  nowt   =   millis();
  clock_t dis ;
  if (nowt >= oldt) {
    dis = nowt - oldt;
  } else {
    dis = (~oldt - 1) + nowt;
  }
  rw_run((int)dis);


  int packetSize = Udp.parsePacket();
  //Serial.println("packetSize");
  //Serial.println(packetSize);


  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());

    int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    packetBuffer[n] = 0;
    //    Serial.print("lenth=");
    //    Serial.println(n);
    //    Serial.println("Contents:");
    //    for (int i = 0; i < n; i++) {
    //      Serial.write(packetBuffer[i]);
    //    }
    pkg.writeArrayToBuf((unsigned char *)packetBuffer, n);
  }


  while (Serial.available()) {
    int numdata = Serial.readBytes(sbuf, 1);
    //Serial.write( sbuf[0]);
    pkg.writeArrayToBuf(sbuf, numdata);
    //Serial.write( pkg.getDatLen() );
  }


  //  for (int i = 0; i < 10; i++) {
  //
  //    // Output channels 1 - 10 on dmxA
  //    dmxA.setChans(dmxChase[i], 10, 1);
  //
  //    // Map values 1-5 to dmx channels 101 - 105 on dmxB
  //    dmxB.setChans(dmxChase[i], 5, 101);
  //
  //    // 1 second between each step
  //    delay(1000);
  //  }

  //
  //
  //  for (int i = 0; i < 170; i++) {
  //
  //    int id = i * 3 ;
  //    dmxWrite(id, 0xff);
  //    dmxWrite(id + 1, 0x00);
  //    dmxWrite(id + 2, 0x00);
  //
  //  }
  //  dmxB.setChans(dd);
  //  delay(1000);
  //  for (int i = 0; i < 170; i++) {
  //
  //    int id = i * 3 ;
  //    dmxWrite(id, 0x00);
  //    dmxWrite(id + 1, 0x00);
  //    dmxWrite(id + 2, 0xff);
  //
  //  }
  //  dmxB.setChans(dd);
  //  delay(1000);
  //  for (int i = 0; i < 170; i++) {
  //
  //    int id = i * 3 ;
  //    dmxWrite(id, 0x00);
  //    dmxWrite(id + 1, 0xff);
  //    dmxWrite(id + 2, 0x00);
  //
  //  }
  //  dmxB.setChans(dd);
  //  delay(1000);
  //
  //  for (int i = 0; i < 170; i++) {
  //
  //    int id = i * 3 ;
  //    dmxWrite(id, 0xff);
  //    dmxWrite(id + 1, 0xff);
  //    dmxWrite(id + 2, 0xff);
  //
  //  }
  //  dmxB.setChans(dd);
  //  delay(1000);
  //  for (int i = 0; i < 170; i++) {
  //
  //    int id = i * 3 ;
  //    dmxWrite(id, 0x00);
  //    dmxWrite(id + 1, 0x00);
  //    dmxWrite(id + 2, 0x00);
  //
  //  }
  //  dmxB.setChans(dd);
  //  delay(1000);
  //
  //  for (int i = 0; i < 170; i++) {
  //
  //    int id = i * 3 ;
  //    dmxWrite(id, 0xff);
  //    dmxWrite(id + 1, 0xff);
  //    dmxWrite(id + 2, 0xff);
  //    dmxB.setChans(dd);
  //    Serial.println(i);
  //    if (i > 30 && i < 50)
  //      delay(500);
  //
  //
  //  }
  //  dmxB.setChans(dd);
  //  delay(1000);
  //
  //

  oldt = nowt;
}

If this is helpful and you're feeling generous, why not shout me a beer。呵呵,maotai也是极好的。

 

 

 

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Arduino ESP8266WiFi库是一种特定于ESP8266芯片的库文件,用于在Arduino开发环境中方便地连接和操作WiFi网络。ESP8266芯片是一种功能强大且资源丰富的WiFi模块,通过该库文件,我们可以轻松将ESP8266模块与Arduino进行通信,实现无线网络连接和数据传输。 该库文件提供了一系列易于使用的函数和方法,允许我们扫描和连接无线网络,以及通过TCP/IP协议进行数据传输。通过Arduino ESP8266WiFi库,我们可以轻松地设置和配置ESP8266模块的无线网络参数,包括SSID、密码和加密方式。 使用该库文件,我们可以利用ESP8266模块连接到现有的WiFi网络,从而实现与互联网的连接。这使得我们能够使用传感器、执行器等外部设备,通过WiFi网络与远程服务器进行通信。这为物联网应用提供了便利,例如远程监测、控制和数据传输等。 除了连接到现有WiFi网络,该库还允许我们创建一个独立的WiFi接入点。这使得我们可以将ESP8266模块配置为一个独立的WiFi网络,其他设备可以连接到该网络并与其通信。这种功能对于构建自己的WiFi网络,例如智能家居、自动化控制等项目非常有用。 总之,Arduino ESP8266WiFi库是一个强大而方便的工具,使得我们能够轻松地利用ESP8266模块实现WiFi网络连接和通信。无论是连接到现有的网络还是创建自己的网络,该库都为我们提供了简洁而有效的函数和方法。 ### 回答2: Arduino ESP8266WiFi库是一种用于连接和控制ESP8266 WiFi模块的库。ESP8266 WiFi模块是一种集成了WiFi功能的低成本、低功耗的芯片,可以通过无线网络连接到互联网。 使用ESP8266WiFi库,我们可以轻松地在Arduino板上开发WiFi应用。该库提供了一些简单的函数和方法,使我们能够连接到Wi-Fi网络、发送HTTP请求、创建TCP/UDP服务器和客户端等。 首先,我们可以使用begin()函数初始化ESP8266 WiFi模块,并设置连接的Wi-Fi网络的SSID和密码。然后,使用connect()函数连接到Wi-Fi网络。连接成功后,我们可以通过WiFi.status()函数获取连接状态。 接下来,我们可以使用WiFiClient类创建一个TCP或UDP客户端,以便与其他设备通信。通过WiFiClient对象,我们可以使用connect()函数连接到指定的主机和端口,然后使用print()或write()函数发送数据,通过read()或available()函数接收数据。我们还可以使用stop()函数关闭连接。 如果我们希望创建一个服务器,我们可以使用WiFiServer类。通过WiFiServer对象,我们可以使用begin()函数开始监听指定端口,并使用available()函数接收来自客户端的连接。一旦有连接请求,我们可以使用client()函数接受连接,并使用write()函数发送数据,通过read()函数接收数据。 其他常用的函数包括hostname()函数用于获取ESP8266 WiFi模块的主机名、localIP()函数用于获取模块的IP地址、macAddress()函数用于获取模块的MAC地址等。 综上所述,Arduino ESP8266WiFi库是一个强大且易于使用的库,可以帮助我们方便地实现与ESP8266 WiFi模块的通信和控制,从而实现各种基于无线网络的物联网应用。 ### 回答3: Arduino是一种开源的电子开发工具,而ESP8266是一种低成本的Wi-Fi模块。当它们结合使用时,我们可以利用ESP8266的Wi-Fi功能,通过Arduino来控制各种电子设备。 ESP8266可以通过连接到Wi-Fi网络,从而使我们的电子设备能够与其他设备或互联网进行通信。为了更方便地使用ESP8266的Wi-Fi功能,Arduino提供了一个ESP8266WiFi库。 ESP8266WiFi库可以用来连接到Wi-Fi网络、发送HTTP请求和接收HTTP响应等。通过该库,我们可以方便地将Arduino与Wi-Fi网络进行连接,并编写代码来完成各种任务,例如远程控制、数据采集和云端存储等。 使用ESP8266WiFi库的第一步是将该库添加到Arduino IDE中。然后,我们可以使用所提供的函数连接到Wi-Fi网络,并配置相应的参数,例如SSID和密码。一旦连接成功,我们可以使用库中的其他函数来发送HTTP请求或接收HTTP响应。 ESP8266WiFi库还提供了一些有用的功能,例如获取本地IP地址、设置静态IP地址、创建TCP或UDP服务器等。这些功能使得我们能够更灵活地配置和管理Wi-Fi连接。 总而言之,Arduino ESP8266WiFi库为开发者提供了方便易用的方式来利用ESP8266的Wi-Fi功能。通过该库,我们可以轻松地将Arduino与互联网连接起来,并进行各种控制和通信任务。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值