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 most likely no translated for the post!





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

Two ESP8266 P2P with WS2812b  

  RSS

robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 18, 2019 8:46 AM  

Hello, everyone,

I have two ESP8266's and I would like it if you press a button on one ESP and turn on the LEDs on the other ESP.
The other way around.
I already have two working sketches that work with one LED each.
With the sketches it is however so that the LED goes with push of a button on and with repeated pressure off. I don't want that.
I would like a LED Stripe to run 3x and go out and for that I have my own sketch.
Could somebody help me to convert the sketch or to integrate it correctly into the existing sketch.

Thanks
Robert

This is the Transmitter ESP:

#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
  }
}

This is the Receiver ESP:

#include <SPI.h>
#include <ESP8266WiFi.h> // The Basic Function Of The ESP NOD MCU
// WIFI Module Config
char ssid[] = "MyWifi"; // SSID of your home WiFi
char pass[] = "1234567"; // password of your home WiFi
WiFiServer server(80);
IPAddress ip(192, 168, 3, 250); // IP address of the server
IPAddress gateway(192, 168, 3, 1); // gateway of your network
IPAddress subnet(255, 255, 255, 0); // subnet mask of your network
// Defining I/O Pins
#define LED1 D0 // LED Receiver One
#define SWITCH D1 // Button
//==========================================================================
void setup() {
  Serial.begin(115200); // only for debug
  WiFi.config(ip, gateway, subnet); // forces to use the fix IP
  WiFi.begin(ssid, pass); // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  server.begin(); // starts the server
  Serial.println("Connected to wifi");
  Serial.print("IP: "); Serial.println(WiFi.localIP());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
// =========================================================================
  pinMode(LED1, OUTPUT);
  pinMode(SWITCH, INPUT_PULLUP);
  digitalWrite(LED1, LOW);
}
void loop() {
  WiFiClient client = server.available();
  if (!client) {
return;
  }
// ========================================================================= 
  String request = client.readStringUntil('\r');
  if (request == "I am Transmitter") {
    digitalWrite(LED1, !digitalRead(LED1));
    Serial.print("Data Received: "); Serial.println(request);
    delay(200);
  }
  int reading = digitalRead(SWITCH);
  if (reading == LOW) {
    client.print("I am Receiver\r");
    delay(200);
  }
  client.println("Receiver\r"); // sends the answer to the client
  delay(100);
}

And this is the Sketch i want implement instead of the LED1 ( Hans will be familiar with this code ;-) )

#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 36
#define blinkcount 3
#define DATA_PIN 8
int button = 4; //D2 (gpio4)
int buttonState=0;
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() { 
      // Uncomment/edit one of the following lines for your leds arrangement.
       FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
       pinMode(button, INPUT);
       LEDS.setBrightness(60);
}
void loop() {
  buttonState=digitalRead(button); // put your main code here, to run repeatedly:
  if (buttonState == HIGH) {
    for(int blink=0; blink<blinkcount; blink++) {
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      // Cleanup swipe after swipe
      //setAll(0,0,0);
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(0,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
    }
    setAll(0,0,0);
  }
  else {
    setAll(0,0,0);
  }
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    leds.setRGB(red, green, blue); 
  }
  FastLED.show();
}

ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1309
October 20, 2019 2:23 AM  

It's a little much for me to try - especially since I still have "play with ESP8266" on my "To Do" list.

Some basics I can think of though;

From the the 3rd sketch, I'd make a separate function of the void loop code and create a global variable and bring it to the receiver code.

So

void loop() {
  ...
}

To something like (the global variable defined somewhere in the top of your code).

...
bool DoBlink = false;
...
void checkReceivedValue() {
  // remove: buttonState=digitalRead(button); // put your main code here, to run repeatedly:
  if (DoBlink == TRUE) { DoBlink = false; // reset the value
    for(int blink=0; blink<blinkcount; blink++) {
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      //setAll(0,0,0);
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(0,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
    }
    setAll(0,0,0);
  }
  else {
    setAll(0,0,0);
  }
}

Next step would be in the receiver code: 

- if a message was received, set "DoBlink = TRUE".

- In the void loop(): add a call to the new function checkReceivedValue(), so in each loop we check network messages and handle the LEDs if DoBlink==TRUE.


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 20, 2019 3:10 AM  

Thanks Hans,

i will try it tonight :-)


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 20, 2019 7:53 AM  

Whew! I tried, but my knowledge is too little for that.

Don't know where to start.But no matter, is not soo important.

Thought it would be easier ;-)

ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1309
October 21, 2019 3:55 AM  

Haha, well, we all had to start somewhere.

Do it in small steps.

Let's start with the 3rd sketch, and move the loop() function to a separate function. This would look something like this. Look at the part of the code that I made bold.

Now the entire code of the loop has become its own function.

#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 36
#define blinkcount 3
#define DATA_PIN 8
int button = 4; //D2 (gpio4)
int buttonState=0;
// Define the array of leds
CRGB leds[NUM_LEDS];
void setup() { 
      // Uncomment/edit one of the following lines for your leds arrangement.
       FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
       pinMode(button, INPUT);
       LEDS.setBrightness(60);
}
void loop() {
  doLEDEffect();
}
void doLEDEffect() {

  buttonState=digitalRead(button); // put your main code here, to run repeatedly:
  if (buttonState == HIGH) {
    for(int blink=0; blink<blinkcount; blink++) {
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      // Cleanup swipe after swipe
      //setAll(0,0,0);
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(0,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
    }
    setAll(0,0,0);
  }
  else {
    setAll(0,0,0);
  }
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    leds.setRGB(red, green, blue); 
  }
  FastLED.show();
}


ReplyQuote



Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1309
October 21, 2019 4:02 AM  

The next step would be merging sketch 2 and 3 ... I don't have the ability to test the code, so there may or may not be an error in it:

//==========================================================================
#include <SPI.h>
#include <ESP8266WiFi.h> // The Basic Function Of The ESP NOD MCU
// WIFI Module Config
char ssid[] = "MyWifi"; // SSID of your home WiFi
char pass[] = "1234567"; // password of your home WiFi
WiFiServer server(80);
IPAddress ip(192, 168, 3, 250); // IP address of the server
IPAddress gateway(192, 168, 3, 1); // gateway of your network
IPAddress subnet(255, 255, 255, 0); // subnet mask of your network
// Defining I/O Pins
#define LED1 D0 // LED Receiver One
#define SWITCH D1 // Button
//==========================================================================
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 36
#define blinkcount 3
#define DATA_PIN 8
// Define the array of leds
CRGB leds[NUM_LEDS];

//==========================================================================
void setup() {
  Serial.begin(115200); // only for debug
  WiFi.config(ip, gateway, subnet); // forces to use the fix IP
  WiFi.begin(ssid, pass); // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  server.begin(); // starts the server
  Serial.println("Connected to wifi");
  Serial.print("IP: "); Serial.println(WiFi.localIP());
  Serial.print("SSID: "); Serial.println(WiFi.SSID());
// For the LEDs   pinMode(LED1, OUTPUT);
  pinMode(SWITCH, INPUT_PULLUP);
  digitalWrite(LED1, LOW);
  
  // edit one of the following lines for your leds arrangement if needed.
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  pinMode(button, INPUT);
  LEDS.setBrightness(60);

}
void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  // =========================================================================  
  String request = client.readStringUntil('\r');
  if (request == "I am Transmitter") {
    digitalWrite(LED1, !digitalRead(LED1));
    Serial.print("Data Received: "); Serial.println(request);
    
    // received something! Call the LED function
    doLEDEffect();

    
    delay(200);
  }
  int reading = digitalRead(SWITCH);
  if (reading == LOW) {
    client.print("I am Receiver\r");
    delay(200);
  }
  client.println("Receiver\r"); // sends the answer to the client
  delay(100);
}
// ======= LED ========================================================
void doLEDEffect() {
  buttonState=digitalRead(button); // put your main code here, to run repeatedly:
  if (buttonState == HIGH) {
    for(int blink=0; blink<blinkcount; blink++) {
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      // Cleanup swipe after swipe
      //setAll(0,0,0);
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(0,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
    }
    setAll(0,0,0);
  }
  else {
    setAll(0,0,0);
  }
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    leds.setRGB(red, green, blue); 
  }
  FastLED.show();
}

Since I'm not entirely familiar with the ESP library, I'll admit that I'm not sure which is which when it comes to receiving messages in the receiver.
So I'm assume the "if (request == "I am Transmitter") {" part is where the receiver responds to a message from the transmitter.

The code (for now) of sketch doesn't need to be modified for testing purposes.

Give it a try 😊 


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 21, 2019 6:34 AM  

Thank you Hans,
That looks more logical to me.
I will study and try it tonight. :-)
Thank you for your tireless help


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 21, 2019 9:55 AM  

Hans Hans Hans,

I don't know what to say  Thank you Thank you Thank you

It works

What was missing was this: int buttonState=0;
What I have changed:
buttonState=digitalRead(SWITCH); instead of (button)
and
pinMode(SWITCH, INPUT_PULLUP); instead of pinMode(button, INPUT); 

Now I will try it with the Transmitter


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 21, 2019 10:13 AM  

And the transmitter also work 


ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1309
October 22, 2019 2:40 AM  

Awesome! And you've done it by yourself! 


ReplyQuote



robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 24, 2019 1:06 PM  

Hello Hans,
I know you're busy, and I don't want to claim you.
But maybe you can help me out sometime.
I have now adapted the code to ESP32 and it does fine.
Now I would like the touch button instead of the button.
The ESP32 have that onboard, unfortunately I can't do it.
Greetings from Stuttgart

So this code works fine! How to implement this in my Code below?

#define BUTTON_1 T7 // Gpio 17 = Touch7
#define LED1 4
int touch_value = 100;
void setup(){
  Serial.begin(115200);
  Serial.println("Touch Test");
  pinMode(LED1, OUTPUT);
  digitalWrite (LED1, LOW);
}
void loop(){
  touch_value = touchRead(BUTTON_1);
  Serial.println(touch_value);
  if (touch_value < 60)
  {
    digitalWrite (LED1, HIGH);
  }
  else
  {
    digitalWrite (LED1, LOW);
  }
  delay(1000);
}

// Für ESP32 als Transmitter
#define FASTLED_INTERNAL
#include <SPI.h>
#include <WiFi.h> // The Basic Function Of The ESP NODEMCU
// Defining I/O Pins
#define LED1 4 // LED1
#define BUTTON_1 5 // Button 1
// WIFI Authentication Variables
char ssid[] = "MyWiFi"; // SSID of your home WiFi
char pass[] = "123456789"; // password of your home WiFi
// WIFI Module Mode & IP
IPAddress server(192,168,3,250); // the fix IP address of the server
WiFiClient client;
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 16
#define blinkcount 5
#define DATA_PIN 2 
int buttonState=0;
// Define the array of leds
CRGB leds[NUM_LEDS];
//====================================================================================
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);
  // edit one of the following lines for your leds arrangement if needed.
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  pinMode(BUTTON_1, INPUT_PULLUP);
  LEDS.setBrightness(60);
  
  }
//====================================================================================
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 response of receiver is equal to 'SWITCH'
    digitalWrite(LED1, !digitalRead(LED1)); // if it changes the status of the LED
    Serial.println("Data Received: " + answer);
    // received something! Call the LED function
    doLEDEffect();
    
    delay(200); // client will trigger the communication 200 milliseconds
  }
}
// ======= LED ========================================================
void doLEDEffect() {
buttonState=digitalRead(BUTTON_1); // put your main code here, to run repeatedly:
  if (buttonState == HIGH)
    {
    for(int blink=0; blink<blinkcount; blink++) {
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      // Cleanup swipe after swipe
      //setAll(0,0,0);
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(0,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
    }
    setAll(0,0,0);
  }
  else {
    setAll(0,0,0);
  }
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    leds.setRGB(red, green, blue); 
  }
  FastLED.show();
}

ReplyQuote
Hans
 Hans
(@hans)
Noble Member Admin
Joined: 7 years ago
Posts: 1309
October 24, 2019 10:44 PM  

Hi Robiv8 

It's OK to ask 

Well, I'd use the same approach we used before.
Determine in the sketch what the "working parts" are and transplant them to the other sketch.

So the key elements of the 1st sketch are:

Header:
(i'm a little confused about the first define though ... should this be "T7" or "17"?).
This part we will need to add to the top of the sketch in which we combine both sketches (taking the 2nd sketch as a base).

#define BUTTON_1 T7 // Gpio 17 = Touch7
#define LED1 4
int touch_value = 100;

Setup:

This only sets the LED, so I'm not sure if we still will need this:

  pinMode(LED1, OUTPUT);
  digitalWrite (LED1, LOW);

Loop:

We transplant that to your ReadButton function:

  touch_value = touchRead(BUTTON_1);
  Serial.println(touch_value);
  if (touch_value < 60)
  {
    digitalWrite (LED1, HIGH);
  }
  else
  {
    digitalWrite (LED1, LOW);
  }

Your ReadButton function could become something like this depending on how this should work; touching would trigger the LED effect.
But, and this is more complex, does the release of the touch button also stop the LED effect? 

void ReadButton() {
  touch_value = touchRead(BUTTON_1);
  Serial.println(touch_value);
  if (touch_value < 60)
  {
    client.print("I am Transmitter\r"); // assuming te touch_value<60 = touch button
    delay(200);
  }
 // else // dropped the release the button for now
// {
 //   digitalWrite (LED1, LOW);
 // }
}

If the effect has to stop when releasing the touch button, then you'll need to create 2 "command".
For example, send "Effect ON" when pressed, and "Effect OFF" when released.
In your received sketch you'll need to handle the received messages accordingly.
The issue you'll run into is this: When the LED effect is running, the 2nd ESP8266 is "busy" and will not (yet) read the "Effect OFF" command.

A way to try to catch this would be by creating a function that checks if a command came in.
Before going that direction I'd clean up the code of both transmitter and receiver sketch first.
reading the code, and seeing the text "I am Transmitter" or "I am Receiver" confuse me each time hahah.

Also not that this part of the code:

void loop() {
  ContinuousConnection();
}
//====================================================================================
 void ContinuousConnection(){
  client.connect(server, 80); // Connection to the server
  ReadButton(); // Read Button from Transmitter
 }

doesn't really contribute. Short, cleaner and better would be:

void loop() {
  client.connect(server, 80); // Connection to the server
  ReadButton(); // Read Button from Transmitter
 }

and drop the "ContinuousConnection" function - it is not being used anywhere else.


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 25, 2019 12:35 PM  

Just got home from the late shift.
I will look at it tomorrow in peace and implement it.
Thank you already times.
Robi


ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 29, 2019 12:20 PM  

What's the matter with that?

It doesn't quite match the touch code.

pinMode(BUTTON_1, INPUT_PULLUP); <--------
and this
void doLEDEffect() {buttonState=digitalRead(BUTTON_1); <-------  if (buttonState == HIGH)....

Thanks again,

Hier der geänderte Code nach deiner Anweisung :-)
(code by accident removed)

ReplyQuote
robiv8
(@robiv8)
Active Member
Joined: 2 years ago
Posts: 19
October 29, 2019 12:54 PM  

Sorry for so much text ;-)I managed it once.Now only the sensitivity is disturbing, it triggers from time to time by itself because it falls below 20.

Could one there something tinker that it only from approx. 200ms touch triggers.
A kind of debouncing time

So here the working code:

// Für ESP32 als Transmitter
#define FASTLED_INTERNAL
#include <SPI.h>
#include <WiFi.h> // The Basic Function Of The ESP NODEMCU
// Defining I/O Pins
#define LED1 4 // LED1
#define BUTTON_1 T7 // Button 1
int touch_value = 100;
// WIFI Authentication Variables
char ssid[] = "MyWifi"; // SSID of your home WiFi
char pass[] = "12345"; // password of your home WiFi
// WIFI Module Mode & IP
IPAddress server(192,168,3,250); // the fix IP address of the server
WiFiClient client;
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 16
#define blinkcount 5
#define DATA_PIN 2 
//int buttonState=0;
// Define the array of leds
CRGB leds[NUM_LEDS];
//====================================================================================
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(1000);
  }
  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);
  // edit one of the following lines for your leds arrangement if needed.
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  //pinMode(BUTTON_1, INPUT_PULLUP);
  LEDS.setBrightness(40);
  
  }
//====================================================================================
void loop() {
  client.connect(server, 80); // Connection to the server
  ReadButton(); // Read Button from Transmitter
 }
//====================================================================================
void ReadButton() {
  touch_value = touchRead(BUTTON_1);
  Serial.println(touch_value);
  delay(1000);
  if (touch_value < 50)
  {
    client.print("I am Transmitterr"); // assuming te touch_value<60 = touch button
    delay(200);
  }
 // else // dropped the release the button for now
 // {
 // digitalWrite (LED1, LOW);
 // }
}
//====================================================================================
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 response of receiver is equal to 'SWITCH'
    digitalWrite(LED1, !digitalRead(LED1)); // if it changes the status of the LED
    Serial.println("Data Received: " + answer);
    // received something! Call the LED function
    doLEDEffect();
    
    delay(200); // client will trigger the communication 200 milliseconds
  }
}
// ======= LED ========================================================
void doLEDEffect() {
touch_value = touchRead(BUTTON_1); // put your main code here, to run repeatedly:
  if (touch_value < 50)
    {
    for(int blink=0; blink<blinkcount; blink++) {
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      // Cleanup swipe after swipe
      //setAll(0,0,0);
      for(int i=0; i<NUM_LEDS; i++) {
        leds.setRGB(0,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
    }
    setAll(0,0,0);
  }
  else {
    setAll(0,0,0);
  }
}
// Set all LEDs to a given color and apply it (visible)
void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    leds.setRGB(red, green, blue); 
  }
  FastLED.show();
}

ReplyQuote



Share: