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

Reply To: Two ESP8266 P2P with WS2812b

$
0
0

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[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();
}

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 😊 


Viewing all articles
Browse latest Browse all 142

Trending Articles