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 in AP M...
 
Share:
Notifications
Clear all

[Solved] Two ESP8266 in AP Mode instead of STA?

2 Posts
2 Users
0 Reactions
1,542 Views
(@robiv8)
Eminent Member
Joined: 6 years ago
Posts: 20
Topic starter  

Hallo at all,

I have two ESP8266,
each logs into a wifi and have LEDs connected.
both have 1 button to trigger a light signal on the other ESP.
This also works very well, when I press a button on ESP1, the LEDs light up on ESP2.
Now I want to use it in the office, but I can not log into the Wlan.
Would it be possible that the ESP builds its own Wlan where then the second ESP2 logs on and these then communicate.

Would be great if this would also be feasible with the WLED Project.
But this is only a dream :-)

Translated with www.DeepL.com/Translator (free version) Original in German.

Attached is the code of the two ESPs now

ESP1 Transmitter

//#define FASTLED_INTERNAL
#include <SPI.h>
#include <ESP8266WiFi.h>            // The Basic Function Of The ESP NODEMCU

// Defining I/O Pins
#define       LED1        16   // LED1
#define       BUTTON_1    5   // Button 1

// WIFI Authentication Variables

char ssid[] = "myNet";                // SSID of your home WiFi
char pass[] = "myPass";               // 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 36
#define blinkcount 3
#define DATA_PIN  14 //D5 FastLED 
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(40);
  
  }
//====================================================================================
void loop() {
  digitalWrite(LED1, HIGH);
  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[i].setRGB(255,0,0);
        FastLED.show();
        delay(10);
      }
      delay(100);
      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();
}

ESP Receiver

//#define FASTLED_INTERNAL
#include <SPI.h>
#include <ESP8266WiFi.h>    // The Basic Function Of The ESP NOD MCU

// WIFI Module Config
char ssid[] = "MyNet";                   // SSID of your home WiFi
char pass[] = "myPass";               // 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      16 //D0        // LED Receiver One
#define       SWITCH    5 //D1        // Button

//==========================================================================

#include <FastLED.h>
// How many leds in your strip?

#define NUM_LEDS 36
#define blinkcount 3
#define DATA_PIN 14 //D5
int buttonState=0;

// 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(SWITCH, INPUT_PULLUP);
  LEDS.setBrightness(60);
}
void loop() {
  digitalWrite(LED1, HIGH);
  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(SWITCH); // 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(7);
      }
      delay(100);
      for(int i=0; i<NUM_LEDS; i++) {
        leds[i].setRGB(0,0,0);
        FastLED.show();
        delay(7);
      }
      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();
}

 

Thanks

Robi


   
ReplyQuote
Topic Tags
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2741
 

Yes, as far as I know, you can setup an ESP as an access point.
It is a little hidden in the ESP manual, but here is some documentation with example code.

Hope this helps 😊 


   
ReplyQuote
Share: