Bonjour à tous,
Je suis en train de travailler sur un projet qui nécessite de passer d'une communication série à une communication Ethernet. L'objectif est de contrôler en temps réel des moteurs, des solénoïdes et des LEDs.
J'ai déjà développé un code fonctionnel pour contrôler des LEDs via des ESP8266.
Le voici:
/*
UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender
A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.
created 21 Aug 2010
by Michael Margolis
This code is in the public domain.
https://github.com/kitesurfer1404/WS2812FX
*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define LED_COUNT 93
#define NUMPIXELS 93
#define LED_PIN 3
#define PIN 3
// How many NeoPixels are attached to the Arduino?
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#include "SPI.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#ifndef STASSID
#define STASSID "F4IDM_2T2L"
#define STAPSK "xxxxxxxxxx"
#endif
#include <WS2812FX.h>
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int MODE, M_BR, Ba1, Pa2, Va2, Ra2, Ba2,Pa3, Va3, Ra3, Ba3, Pa4, Va4, Ra4, Ba4;
int ja1, ja2, jb1, jb2, jc1, jc2, jd1, jd2;
int val1a, val2a, val3a, val1b, val2b, val3b, val1c, val2c, val3c, val4a, val4b, val4c;
int Col1, Col2 ;
unsigned int localPort = 7778; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
WiFiUDP Udp;
// Set your Static IP Address Settings
IPAddress local_IP(192, 168, 1, 102 );
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); // this is optional
IPAddress secondaryDNS(8, 8, 4, 4); // this is optional
void setup() {
Serial.begin(115200);
// Print feedback if the settings are not configured
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
Serial.printf("UDP server on port %d\n", localPort);
Udp.begin(localPort);
ws2812fx.init();
// ws2812fx.setBrightness(2);
ws2812fx.start();
// segment 0 is the builtin comet effect
// ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE );
pixels.begin(); // This initializes the NeoPixel library.
// Update LED contents, to start they are all 'off'
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
//Serial.println("Contents:");
// Serial.println(packetBuffer);
String pacchetto = packetBuffer;
// Create a new String object to be split
String MODE_Int = splitString(pacchetto, ' ', 0);
String M_BR_Int = splitString(pacchetto, ' ', 1);
String a1 = splitString(pacchetto, ' ', 2);
String a2 = splitString(pacchetto, ' ', 3);
String a3 = splitString(pacchetto, ' ', 4);
String a4 = splitString(pacchetto, ' ', 5);
String a5 = splitString(pacchetto, ' ', 6);
String b1 = splitString(pacchetto, ' ', 7);
String b2 = splitString(pacchetto, ' ', 8);
String b3 = splitString(pacchetto, ' ', 9);
String b4 = splitString(pacchetto, ' ', 10);
String b5 = splitString(pacchetto, ' ', 11);
String c1 = splitString(pacchetto, ' ', 12);
String c2 = splitString(pacchetto, ' ', 13);
String c3 = splitString(pacchetto, ' ', 14);
String c4 = splitString(pacchetto, ' ', 15);
String c5 = splitString(pacchetto, ' ', 16);
String d1 = splitString(pacchetto, ' ', 17);
String d2 = splitString(pacchetto, ' ', 18);
String d3 = splitString(pacchetto, ' ', 19);
String d4 = splitString(pacchetto, ' ', 20);
String d5 = splitString(pacchetto, ' ', 21);
String Col_1 = splitString(pacchetto, ' ', 22);
String Col_2 = splitString(pacchetto, ' ', 23);
ja1 = a1.toInt();
ja2 = a2.toInt();
val1a = a3.toInt();
val1b = a4.toInt();
val1c = a5.toInt();
jb1 = b1.toInt();
jb2 = b2.toInt();
val2a = b3.toInt();
val2b = b4.toInt();
val2c = b5.toInt();
jc1 = c1.toInt();
jc2 = c2.toInt();
val3a = c3.toInt();
val3b = c4.toInt();
val3c = c5.toInt();
jd1 = d1.toInt();
jd2 = d2.toInt();
val4a = d3.toInt();
val4b= d4.toInt();
val4c = d5.toInt();
MODE = MODE_Int.toInt();
M_BR = M_BR_Int.toInt();
Col1 = Col_1.toInt();
Col2 = Col_2.toInt();
if ((MODE) == 1) {
{
byte A;
for (A = ja1 ; A < ja2; A++) {
pixels.setPixelColor (A, pixels.Color ( val1a, val1b, val1c));
}
}
{
byte B;
for (B = jb1 ; B < jb2; B++) {
pixels.setPixelColor (B, pixels.Color ( val2a, val2b, val2c));
}
}
{
byte C;
for (C = jc1 ; C < jc2; C++) {
pixels.setPixelColor (C, pixels.Color ( val3a, val3b, val3c));
}
}
{
byte D;
for (D = jd1 ; D < jd2; D++) {
pixels.setPixelColor (D, val4a, val4b, val4c);
}
}
pixels.show(); // Send the updated pixel colors to the hardware.
}
if ((MODE) == 2) {
if ((Col1) == 1) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, BLUE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2 ) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, GREEN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, RED, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, WHITE, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, YELLOW , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, CYAN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, MAGENTA, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, PURPLE, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, ORANGE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 3) {
if ((Col1) == 1) {
uint32_t colors[] = {BLUE, CYAN};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
uint32_t colors[] = {GREEN, RED};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
uint32_t colors[] = {RED, YELLOW};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
uint32_t colors[] = {MAGENTA, PURPLE};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
uint32_t colors[] = {ORANGE, RED};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
uint32_t colors[] = {WHITE, RED};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
uint32_t colors[] = {PINK, RED};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
uint32_t colors[] = {BLUE, RED};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_RUNNING_LIGHTS, colors , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, (LED_COUNT/2)-1, FX_MODE_RUNNING_LIGHTS, COLORS(RED, WHITE) , 20);
ws2812fx.setSegment(1, LED_COUNT/2, LED_COUNT-1, FX_MODE_RUNNING_LIGHTS, COLORS(GREEN, WHITE), 20);
}
if ((Col1) == 10) {
ws2812fx.setSegment(0, 0, (LED_COUNT/2)-1, FX_MODE_RUNNING_LIGHTS, COLORS(RED, GREEN) , 20);
ws2812fx.setSegment(1, LED_COUNT/2, LED_COUNT-1, FX_MODE_RUNNING_LIGHTS, COLORS(GREEN, WHITE), 20);
}
if ((Col1) == 11) {
ws2812fx.setSegment(0, 0, (LED_COUNT/2)-1, FX_MODE_RUNNING_LIGHTS, COLORS(RED, WHITE) , 20);
ws2812fx.setSegment(1, LED_COUNT/2, LED_COUNT-1, FX_MODE_RUNNING_LIGHTS, COLORS(GREEN, RED), 20);
}
if ((Col1) == 12) {
ws2812fx.setSegment(0, 0, (LED_COUNT/2)-1, FX_MODE_RUNNING_LIGHTS, COLORS(GREEN, RED) , 20);
ws2812fx.setSegment(1, LED_COUNT/2, LED_COUNT-1, FX_MODE_RUNNING_LIGHTS, COLORS(RED, WHITE), 20);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 4) {
if ((Col1) == 1) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, BLUE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, GREEN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, RED , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, CYAN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, ORANGE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, ULTRAWHITE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, PINK , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, PURPLE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.init();
uint32_t colors[] = {PINK, WHITE};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TWINKLE_FADE, MAGENTA , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 5) {
if ((Col1) == 1) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, BLUE, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, GREEN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, RED , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, CYAN , 20);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, ORANGE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, ULTRAWHITE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, PINK , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, PURPLE, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.init();
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_HYPER_SPARKLE, MAGENTA , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 6) {
if ((Col1) == 1) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, BLUE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, GREEN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, RED , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, CYAN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, ORANGE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, ULTRAWHITE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, PINK , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, PURPLE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_STROBE, MAGENTA , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 7) {
if ((Col1) == 1) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, BLUE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, GREEN , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, RED , 1);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, CYAN , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, ORANGE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, ULTRAWHITE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, PINK , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, PURPLE , 1);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_LARSON_SCANNER, MAGENTA , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 10) {
ws2812fx.setSegment(0, 0, (LED_COUNT/2) - 1, FX_MODE_LARSON_SCANNER, CYAN , 1 );
ws2812fx.setSegment(1, LED_COUNT/2, LED_COUNT-1, FX_MODE_LARSON_SCANNER, CYAN, 1, reverse);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 8) {
if ((Col1) == 1) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, BLUE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, GREEN , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, RED , 1);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, CYAN , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, ORANGE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, ULTRAWHITE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, PINK , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, PURPLE , 1);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIREWORKS, MAGENTA , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 9) {
if ((Col1) == 1) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, BLUE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, GREEN , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, RED , 1);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, CYAN , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, ORANGE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, ULTRAWHITE , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, PINK , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, PURPLE , 1);
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_FIRE_FLICKER_INTENSE, MAGENTA , 1 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
if ((MODE) == 10) {
if ((Col1) == 1) {
uint32_t colors[] = {RED, GREEN, WHITE};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TRICOLOR_CHASE, colors, 200 , true );
}
if ((Col1) == 2) {
uint32_t colors[] = {BLUE, RED, WHITE};
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_TRICOLOR_CHASE, colors, 200 , true );
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
}
delay (20);
}
String splitString(String str, char sep, int index)
{
int found = 0;
int strIdx[] = { 0, -1 };
int maxIdx = str.length() - 1;
for (int i = 0; i <= maxIdx && found <= index; i++)
{
if (str.charAt(i) == sep || i == maxIdx)
{
found++;
strIdx[0] = strIdx[1] + 1;
strIdx[1] = (i == maxIdx) ? i+1 : i;
}
}
return found > index ? str.substring(strIdx[0], strIdx[1]) : "";
}
uint16_t myCustomEffect(void) { // random chase
WS2812FX::Segment* seg = ws2812fx.getSegment(); // get the current segment
for(uint16_t i=seg->stop; i>seg->start; i--) {
ws2812fx.setPixelColor(i, ws2812fx.getPixelColor(i-1));
}
uint32_t color = ws2812fx.getPixelColor(seg->start + 1);
int r = random(6) != 0 ? (color >> 16 & 0xFF) : random(256);
int g = random(6) != 0 ? (color >> 8 & 0xFF) : random(256);
int b = random(6) != 0 ? (color & 0xFF) : random(256);
ws2812fx.setPixelColor(seg->start, r, g, b);
return seg->speed; // return the delay until the next animation step (in msec)
}
Cependant, lorsque j'essaie d'adapter ce code à un Arduino UNO avec un shield Ethernet pour gérer un volume de données plus important, je rencontre des problèmes de latence.
Plus précisément, j'utilise la fonction splitString
pour traiter les données reçues. Lorsque le nombre de données à analyser est faible (environ 5-6), je n'ai pas de problème. Mais dès que ce nombre augmente (par exemple, 24), la latence devient importante.
Ma question est la suivante : Est-ce que l'Arduino UNO est adapté à ce type de tâche, ou dois-je envisager une autre solution ? J'ai pensé à l'ESP32, mais je ne suis pas sûr si ajouter un module Ethernet supplémentaire comme le WT32-ETH01 serait judicieux.
J'ai des connaissances limitées en programmation, mais j'ai essayé de trouver des solutions par moi-même.
Merci d'avance pour votre aide et vos conseils !
Pourriez-vous me donner votre avis sur les points suivants :
- Choix du microcontrôleur: Arduino UNO, ESP32 avec module Ethernet, autre ?
- Optimisation du code: Quelles techniques puis-je utiliser pour réduire la latence (par exemple, optimiser la fonction
splitString
, utiliser des structures de données plus efficaces) ?
Pour info voici un exemple de code pour l'adaptation à l'Arduino UNO (désolé pour la mise en forme mais c'est pour des test):
/*
UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender
A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.
created 21 Aug 2010
by Michael Margolis
This code is in the public domain.
https://github.com/kitesurfer1404/WS2812FX
*/
#include "SPI.h" // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// 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(169,254,144,62);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define LED_COUNT 20
#define NUMPIXELS 20
#define LED_PIN 6
#define PIN 6
// How many NeoPixels are attached to the Arduino?
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#include <WS2812FX.h>
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int MODE, M_BR, Ba1, Pa2, Va2, Ra2, Ba2,Pa3, Va3, Ra3, Ba3, Pa4, Va4, Ra4, Ba4;
int ja1, ja2, jb1, jb2, jc1, jc2, jd1, jd2;
int val1a, val2a, val3a, val1b, val2b, val3b, val1c, val2c, val3c, val4a, val4b, val4c;
int Col1, Col2 ;
void setup() {
Serial.begin(115200);
// pinMode(A1, OUTPUT);
Ethernet.begin(mac, ip);
Udp.begin(localPort);
ws2812fx.init();
// ws2812fx.setBrightness(2);
ws2812fx.start();
// segment 0 is the builtin comet effect
// ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE );
pixels.begin(); // This initializes the NeoPixel library.
// Update LED contents, to start they are all 'off'
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
//Serial.println("Contents:");
// Serial.println(packetBuffer);
String pacchetto = packetBuffer;
// Create a new String object to be split
String MODE_Int = splitString(pacchetto, ' ', 0);
String M_BR_Int = splitString(pacchetto, ' ', 1);
String a1 = splitString(pacchetto, ' ', 12);
String a2 = splitString(pacchetto, ' ', 13);
String a3 = splitString(pacchetto, ' ', 4);
String a4 = splitString(pacchetto, ' ', 5);
String a5 = splitString(pacchetto, ' ', 6);
String b1 = splitString(pacchetto, ' ', 7);
String b2 = splitString(pacchetto, ' ', 8);
String b3 = splitString(pacchetto, ' ', 9);
String b4 = splitString(pacchetto, ' ', 10);
String b5 = splitString(pacchetto, ' ', 11);
String Col_1 = splitString(pacchetto, ' ', 2);
String Col_2 = splitString(pacchetto, ' ', 3);
MODE = MODE_Int.toInt();
M_BR = M_BR_Int.toInt();
Col1 = Col_1.toInt();
Col2 = Col_2.toInt();
if ((MODE) == 2) {
if ((Col1) == 1) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, BLUE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 2 ) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, GREEN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 3) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, RED, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 4) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, WHITE, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 5) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, YELLOW , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 6) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, CYAN , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 7) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, MAGENTA, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 8) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, PURPLE, 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
if ((Col1) == 9) {
ws2812fx.setSegment(0, 0, LED_COUNT - 1, FX_MODE_THEATER_CHASE, ORANGE , 20 );
ws2812fx.setSegment(1, LED_COUNT +1, LED_COUNT +2);
}
ws2812fx.setBrightness(M_BR);
ws2812fx.service();
}
}
delay (20);
}
String splitString(String str, char sep, int index)
{
int found = 0;
int strIdx[] = { 0, -1 };
int maxIdx = str.length() - 1;
for (int i = 0; i <= maxIdx && found <= index; i++)
{
if (str.charAt(i) == sep || i == maxIdx)
{
found++;
strIdx[0] = strIdx[1] + 1;
strIdx[1] = (i == maxIdx) ? i+1 : i;
}
}
return found > index ? str.substring(strIdx[0], strIdx[1]) : "";
}
uint16_t myCustomEffect(void) { // random chase
WS2812FX::Segment* seg = ws2812fx.getSegment(); // get the current segment
for(uint16_t i=seg->stop; i>seg->start; i--) {
ws2812fx.setPixelColor(i, ws2812fx.getPixelColor(i-1));
}
uint32_t color = ws2812fx.getPixelColor(seg->start + 1);
int r = random(6) != 0 ? (color >> 16 & 0xFF) : random(256);
int g = random(6) != 0 ? (color >> 8 & 0xFF) : random(256);
int b = random(6) != 0 ? (color & 0xFF) : random(256);
ws2812fx.setPixelColor(seg->start, r, g, b);
return seg->speed; // return the delay until the next animation step (in msec)
}
Merci beaucoup d'avance de votre aide.
vinchia