Quantcast
Channel: Tweaking4All.com » All Posts
Viewing all articles
Browse latest Browse all 142

Reply To: Two ESP8266 P2P with WS2812b

$
0
0

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[i].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[i].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[i].setRGB(red, green, blue); 
  }
  FastLED.show();
}

Viewing all articles
Browse latest Browse all 142

Trending Articles