i want to give you @Hans mY first code with udp communication ..it not work , it is because i want to fallow the code on this suject. The first esp just work fine but dont send to the next esp to light the same color, in reality, nothing work in the next esp8266(unit with just led and no button),it just light color REDÂ
______________code boutton send (esp8266 with button and 4 ws2812b led)___________
Â
#include <FastLED.h>
#include <JC_Button.h> // Button library. Includes press, long press, double press detection.
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// Set WiFi credentials
#define WIFI_SSID "TheOtherESP"
#define WIFI_PASS "flashmeifyoucan"
// UDP
WiFiUDP UDP;
IPAddress remote_IP(192,168,4,1);
#define UDP_PORT 4210
// Pushbutton pin definition
const int buttonPin = 2; // Digital pin used for debounced pushbutton
// int buttonState = 0;
// int lastButtonState = 0;
Button myBtn(buttonPin, true, true, 50); // Declare the button
#define DATA_PIN 3
//#define CLK_PIN 11
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 4
CRGB leds[NUM_LEDS];
#define FRAMES_PER_SECOND 120
uint8_t max_bright = 64;
void setup() {
// Setup IO
pinMode(2, INPUT);
Serial.begin(115200);
Serial.println();
// Begin WiFi
WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.mode(WIFI_STA);
// Connecting to WiFi...
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
// Loop continuously while WiFi is not connected
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
// Connected to WiFi
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Begin UDP port
UDP.begin(UDP_PORT);
Serial.print("Opening UDP port ");
Serial.println(UDP_PORT);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(max_bright);
}
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = {boutton0, boutton1, boutton2, boutton3, boutton4, bouttonAll };
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
void loop()
{
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
// EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
readbutton(); // Button press increases the ledMode up to last contiguous mode and then starts over at 0.
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void boutton0()
{
leds[0] = CRGB::Black;
leds[1] = CRGB::Black;
leds[2] = CRGB::Black;
leds[3] = CRGB::Black;
FastLED.show();
}
void boutton1()
{
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
FastLED.show();
}
void boutton2()
{
leds[0] = CRGB::Green;
leds[1] = CRGB::Green;
leds[2] = CRGB::Green;
leds[3] = CRGB::Green;
FastLED.show();
}
void boutton3()
{
leds[0] = CRGB::Blue;
leds[1] = CRGB::Blue;
leds[2] = CRGB::Blue;
leds[3] = CRGB::Blue;
FastLED.show();
}
void boutton4()
{
leds[0] = CRGB::Yellow;
leds[1] = CRGB::Yellow;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Yellow;
FastLED.show();
}
Â
void bouttonAll()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
void readbutton(){ // Read the button and increase the mode
// Read button
char buttonState = myBtn.read();
// Send Packet
UDP.beginPacket(remote_IP, UDP_PORT);
UDP.write(buttonState);
UDP.endPacket();
delay(100);
UDP.flush();
myBtn.read();
if(myBtn.wasReleased()) {
nextPattern();
}
} // readbutton()
Â
Â
_________________THE CODE BUTTON RECEIVE(SECOND ESP8266 WITH 4 WS2812B LED) _____________
Â
#include <FastLED.h>
#include <JC_Button.h> // Button library. Includes press, long press, double press detection.
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// Set AP credentials
#define AP_SSID "TheOtherESP"
#define AP_PASS "flashmeifyoucan"
// UDP
WiFiUDP UDP;
IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
#define UDP_PORT 4210
// UDP Buffer
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
// Pushbutton pin definition
const int buttonPin = 2; // Digital pin used for debounced pushbutton
// int buttonState = 0;
// int lastButtonState = 0;
Button myBtn(buttonPin, true, true, 50); // Declare the button
#define DATA_PIN 3
//#define CLK_PIN 11
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 4
CRGB leds[NUM_LEDS];
#define FRAMES_PER_SECOND 120
uint8_t max_bright = 64;
void setup() {
// Setup LED pin
pinMode(2, OUTPUT);
// Setup serial port
Serial.begin(115200);
Serial.println();
// Begin Access Point
Serial.println("Starting access point...");
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(AP_SSID, AP_PASS);
Serial.println(WiFi.localIP());
// Begin listening to UDP port
UDP.begin(UDP_PORT);
Serial.print("Listening on UDP port ");
Serial.println(UDP_PORT);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(max_bright);
}
// List of patterns to cycle through. Each is defined as a separate function below.
// Receive packet
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = {boutton0, boutton1, boutton2, boutton3, boutton4, bouttonAll };
uint8_t gCurrentPatternNumber = 0 ; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
void loop()
{
// Receive packet
gPatterns[gCurrentPatternNumber]();
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);
// do some periodic updates
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
// EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
readbutton();
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
void boutton0()
{
leds[0] = CRGB::Black;
leds[1] = CRGB::Black;
leds[2] = CRGB::Black;
leds[3] = CRGB::Black;
FastLED.show();
}
void boutton1()
{
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
FastLED.show();
}
void boutton2()
{
leds[0] = CRGB::Green;
leds[1] = CRGB::Green;
leds[2] = CRGB::Green;
leds[3] = CRGB::Green;
FastLED.show();
}
void boutton3()
{
leds[0] = CRGB::Blue;
leds[1] = CRGB::Blue;
leds[2] = CRGB::Blue;
leds[3] = CRGB::Blue;
FastLED.show();
}
void boutton4()
{
leds[0] = CRGB::Yellow;
leds[1] = CRGB::Yellow;
leds[2] = CRGB::Yellow;
leds[3] = CRGB::Yellow;
FastLED.show();
}
Â
void bouttonAll()
{
// FastLED's built-in rainbow generator
fill_rainbow( leds, NUM_LEDS, gHue, 7);
}
Â
void readbutton() { // Read the button and increase the mode
UDP.parsePacket();
UDP.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
if (packetBuffer[1]){
digitalWrite(2, HIGH);
} else {
nextPattern();
}
} // readbutton()