Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Two ESP8266 P2P wit...
 
Share:
Notifications
Clear all

[Solved] Two ESP8266 P2P with WS2812b

27 Posts
3 Users
0 Likes
4,746 Views
(@j5am2020)
Active Member
Joined: 3 years ago
Posts: 6
 

@hans hello , i just test your code and dont work for me :( 

i want to use two esp8266 , the first esp8266 with one button and 4 ws2912b led and the next, one esp8266 with 4 ws2812b led. When i push button on the first esp, juste show different light but i want to resend the signal to the esp #2 like a streaming light ! 

i hAVE Check over all the internet ..many hours (sorry for my english, am canada french !)  

my fastled code is : 

#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

// 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() {
Serial.begin(57600);
delay(3000); // 3 second delay for recovery

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
myBtn.read();
if(myBtn.wasReleased()) {
nextPattern();
}
} // readbutton()

 

 

I want to use the same code of @robiv8 for the transmiter but i want to get my transmitter work like the second esp8268 ! 

#include <SPI.h>
#include <ESP8266WiFi.h> // The Basic Function Of The ESP NODEMCU
// Defining I/O Pins
#define LED1 D0 // LED1
#define BUTTON_1 D1 // Button 1
// WIFI Authentication Variables
char ssid[] = "MyWifi"; // SSID of your home WiFi
char pass[] = "1234567"; // password of your home WiFi
// WIFI Module Mode & IP
IPAddress server(192,168,3,250); // the fix IP address of the server
WiFiClient client;
//====================================================================================
void setup() {
  Serial.begin(115200); // only for debug
  WiFi.begin(ssid, pass); // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("Connected to wifi");
  Serial.print("IP: "); Serial.println(WiFi.localIP());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
 
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON_1, INPUT_PULLUP); // ESP Pin: INPUT_PULLUP 
  digitalWrite(LED1, LOW);
  }
//====================================================================================
void loop() {
  ContinuousConnection();
}
//====================================================================================
 void ContinuousConnection(){
  client.connect(server, 80); // Connection to the server
  ReadButton(); // Read Button from Transmitter
 }
//====================================================================================
void ReadButton() {
  int reading = digitalRead(BUTTON_1);
  if (reading == LOW) {
    client.print("I am Transmitter\r");
    delay(200);
 }else{
   ClientContinue(); 
  }
}
//====================================================================================
void ClientContinue(){
  client.println("Transmitter"); // sends the message to the server
  String answer = client.readStringUntil('\r'); // receives the answer from the sever
  if (answer == "I am Receiver") { // compares if the response of the receiver is equal to 'SWITCH'
    digitalWrite(LED1, !digitalRead(LED1)); // if it changes the status of the LED
    Serial.println("Data Received: " + answer);
    delay(200); // client will trigger the communication 200 milliseconds
  }

i hope you want to help me ...i dont view anything about this on all the web :|
thank a lot

   
ReplyQuote
(@j5am2020)
Active Member
Joined: 3 years ago
Posts: 6
 

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

This post was modified 3 years ago 2 times by j5am2020

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Hi j5am2020,

I honestly have never connected two ESP8266's (or any Arduino like device).

Not sure if you have done this, but I'd start with a super simple project first to make sure we get communication to work and understand, or get a feel for, how stable this works.

So setup one ESP8266 as a sender, and one as a receiver, and once a basic message works, define your own "send" and "receive" functions which you can later use in your project.

Once you've got that basic part working, integration in your LED project can start.

Seeing a big chunk of code makes it hard and time consuming to go through, and there can be many factors that can make things not work.


   
ReplyQuote
(@j5am2020)
Active Member
Joined: 3 years ago
Posts: 6
 

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

 
 
 
 
 
Posted by: @j5am2020

@hans hello , i just test your code and dont work for me :( 

i want to use two esp8266 , the first esp8266 with one button and 4 ws2912b led and the next, one esp8266 with 4 ws2812b led. When i push button on the first esp, juste show different light but i want to resend the signal to the esp #2 like a streaming light ! 

i hAVE Check over all the internet ..many hours (sorry for my english, am canada french !)  

my fastled code is : 

#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

// 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() {
Serial.begin(57600);
delay(3000); // 3 second delay for recovery

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
myBtn.read();
if(myBtn.wasReleased()) {
nextPattern();
}
} // readbutton()

 

 

I want to use the same code of @robiv8 for the transmiter but i want to get my transmitter work like the second esp8268 ! 

#include <SPI.h>
#include <ESP8266WiFi.h> // The Basic Function Of The ESP NODEMCU
// Defining I/O Pins
#define LED1 D0 // LED1
#define BUTTON_1 D1 // Button 1
// WIFI Authentication Variables
char ssid[] = "MyWifi"; // SSID of your home WiFi
char pass[] = "1234567"; // password of your home WiFi
// WIFI Module Mode & IP
IPAddress server(192,168,3,250); // the fix IP address of the server
WiFiClient client;
//====================================================================================
void setup() {
  Serial.begin(115200); // only for debug
  WiFi.begin(ssid, pass); // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("Connected to wifi");
  Serial.print("IP: "); Serial.println(WiFi.localIP());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
 
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON_1, INPUT_PULLUP); // ESP Pin: INPUT_PULLUP 
  digitalWrite(LED1, LOW);
  }
//====================================================================================
void loop() {
  ContinuousConnection();
}
//====================================================================================
 void ContinuousConnection(){
  client.connect(server, 80); // Connection to the server
  ReadButton(); // Read Button from Transmitter
 }
//====================================================================================
void ReadButton() {
  int reading = digitalRead(BUTTON_1);
  if (reading == LOW) {
    client.print("I am Transmitter\r");
    delay(200);
 }else{
   ClientContinue(); 
  }
}
//====================================================================================
void ClientContinue(){
  client.println("Transmitter"); // sends the message to the server
  String answer = client.readStringUntil('\r'); // receives the answer from the sever
  if (answer == "I am Receiver") { // compares if the response of the receiver is equal to 'SWITCH'
    digitalWrite(LED1, !digitalRead(LED1)); // if it changes the status of the LED
    Serial.println("Data Received: " + answer);
    delay(200); // client will trigger the communication 200 milliseconds
  }

i hope you want to help me ...i dont view anything about this on all the web :|
thank a lot

 


   
ReplyQuote
(@j5am2020)
Active Member
Joined: 3 years ago
Posts: 6
 

@hans i know what you said , oufff am so lost ...many many hours i have to make the project work just for the first esp ! i dont know how to begin. where to begin  (sorry my English is not verry well i normally speak french)

This post was modified 3 years ago by j5am2020

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Note: I did find this project which uses 2 ESP8266's as well to communicate over WiFi.
It seems to come with a good explanation (in case you have a need for that).


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Don't worry about your English - you're doing fine 😉 
(apologies that I do not speak French)

Did you look at the link I posted (this one)?


   
ReplyQuote
(@j5am2020)
Active Member
Joined: 3 years ago
Posts: 6
 

@hans (je rafraîchis mon message car je n'ai pas fait le bon lien) oui j'ai vu cela dans le passé..pour une raison quelconque  j'ai pris ceci: https: //siytek.com/communication -entre-deux-esp8266 / # Setup_LED .. J'ai utilisé ceci pour créer mon code pour la communication udp mais ne fonctionne toujours pas .. le premier esp fonctionne très bien et la deuxième lumière est juste ROUGE! 

 

 

This post was modified 3 years ago 5 times by j5am2020

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Downside of UDP is that there is no packet received confirmation (unlike TCP).
This could be one reason why it doesn't always work, but it may also depend on the code.

For example; when one of the ESP's is running an effect, it won't have time to listen to incoming commands through WiFi.

Developing for both ESP's at the same time comes with it own challenges of course. 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Maybe this is helpful to get started:

This post explains certain thing in detail, makes me even want to write an article myself 😊 


   
ReplyQuote
(@j5am2020)
Active Member
Joined: 3 years ago
Posts: 6
 

@hans okay thank for the explication about why TCP is better than UDP protocol ! Thank too for the link , i Will fallow this tutorial at the begin to the END for better learn. If you want to create a article about connection between two unit with TCP protocol and FASTLED, i want tell Tell me !  I have no view about this on the web ..after 2hour of keyword on Google , i have Found this forum and the forum is on page 6 on one of my Google Search .thank a lot for allô .. i Will studie and Return with a easy to learn code !


 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2678
 

Cool!

Well based on your question, I did start an article, which quickly span out of control - it will probably result in 2 or 3 articles.

Good to study those articles - and again; try to make a simple working example first so you get an idea and feel for how this works, b efore mixing it with LED effects. If you have questions; feel free to ask 😊 


   
ReplyQuote
Page 2 / 2
Share: