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

Two ESP8266 P2P with WS2812b

$
0
0

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[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