简体   繁体   中英

Project: NodeMCU esp8266-esp12e to connect to another esp8266-esp12e - social distancing

I need to ring the buzzer within the range of 1 meter only, my esp8266 is connected to a buzzer but the buzzer will ring on and off within the range of 1 meter using two esp8266, or sometimes it does not ring at all. The project is social distancing, what else should I do? Here is the code I am using:

#include <ESP8266WiFi.h>

const char* APssid = "Social Distancing"; //acess point of the device
const char* APpassword = "qwertyuiop";

const int RSSI_MAX = -37;//maximum strength of signal in dBm
const int RSSI_MIN =-100;//minimum strength of signal in dBm

void setup() 
{
  WiFi.mode(WIFI_OFF);
  WiFi.disconnect(); 
  delay(50); //this part turns off the wifi and resets it if it was already on

  Serial.begin(115200);
  pinMode(14,OUTPUT); 

  Serial.println();
  
  WiFi.mode(WIFI_AP_STA); //configuring the board in hybrid mode 
  Serial.print("Configuring access point...");
  WiFi.softAP(APssid, APpassword);

  Serial.println(WiFi.softAPIP());
}

void loop() 
{
  Serial.println("Wifi is scanning");
  int n = WiFi.scanNetworks();
  Serial.println("Wifi scan ended");
  if (n == 0) 
  {
    Serial.println("no networks found");
  } 
  else 
  {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) 
    {
      //Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(") ");
      Serial.print(WiFi.SSID(i));//SSID
                  
      Serial.print(WiFi.RSSI(i));//Signal strength in dBm  
      Serial.print("dBm (");
  
     if(WiFi.SSID(i) == "Social Distancing")
     {
      if(WiFi.RSSI(i) > -37)//THIS -37 (RSSI) is the threshold value, this value is set 
according to the distance of 1m
      {
      digitalWrite(14,HIGH);//(Generic esp8266 : (14,HIGH) , NodeMCU : (D5,HIGH) )
      Serial.println("Social Distancing");
      break;
      }
     }
      else
      {
       digitalWrite(14,LOW);
      }
     }
      delay(50);
    } 
  Serial.println("");

  delay(50);

  WiFi.scanDelete();  
}

Hi Lisa I have modified your code and I have added a 64x48 OLED display connected to the I2C connections, to visualize the results and it works beautifully. Moreover I have used the built in led (see: LED_BUILTIN) instead of the buzzer. You have had a great idea. Now it works great and you can interrogate any Access point.

Please find attached the code:

//OLED
#include <Arduino.h>
#include <U8g2lib.h>
#include <U8x8lib.h> //this one in use

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8X8_SSD1306_64X48_ER_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);   // EastRising 0.66" OLED breakout board, Uno: A4=SDA, A5=SCL, 5V powered

// End of constructor list

#define U8LOG_WIDTH 16
#define U8LOG_HEIGHT 8
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
U8X8LOG u8x8log;
//OLED end

#include <ESP8266WiFi.h>

const char* APssid = "My_SSID"; //acess point of the device
const char* APpassword = "My_Password";
//const char* myconstcharstarstring = "------------------------------";

const int RSSI_MAX = -37;//maximum strength of signal in dBm
const int RSSI_MIN =-100;//minimum strength of signal in dBm

void setup() 
{
//OLED
  u8x8.begin();
  u8x8.setFont(u8x8_font_chroma48medium8_r);

  u8x8log.begin(u8x8, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
  u8x8log.setRedrawMode(1);    // 0: Update screen with newline, 1: Update screen for every char  
//OLED end  
  WiFi.mode(WIFI_OFF);
  WiFi.disconnect(); 
  delay(50); //this part turns off the wifi and resets it if it was already on

  Serial.begin(115200);
  pinMode(LED_BUILTIN,OUTPUT); 

  Serial.println();

  WiFi.mode(WIFI_AP_STA); //configuring the board in hybrid mode 
  Serial.print("Configuring access point...");
  WiFi.softAP(APssid, APpassword);

  Serial.println(WiFi.softAPIP());
}

void loop() 
{
  digitalWrite(LED_BUILTIN,LOW); 
  Serial.println("Wifi is scanning");
  int n = WiFi.scanNetworks();
  Serial.println("Wifi scan ended");
  if (n == 0) 
  {
    Serial.println("no networks found");
  } 
  else 
  {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) 
    {
      //Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(") ");
      Serial.print(WiFi.SSID(i));//SSID
              
      Serial.print(WiFi.RSSI(i));//Signal strength in dBm  
      Serial.print("dBm (");

      if(WiFi.RSSI(i) > -37)//THIS -37 (RSSI) is the threshold value, this value is set according to the distance of 1m
      {
      digitalWrite(LED_BUILTIN,HIGH);//(Generic esp8266 : (14,HIGH) , NodeMCU : (D5,HIGH) )
      Serial.println("Social Distancing");
      delay(500);
     }
      //OLED
       u8x8log.print(WiFi.SSID(i));
       u8x8log.print("\n");
       u8x8log.print(WiFi.RSSI(i));
       u8x8log.print("\n");
       u8x8log.print("-------------");
       u8x8log.print("\n");       

      delay(2000);
    } 
  Serial.println("");

  delay(50);

  WiFi.scanDelete();  
  }
}

Hope this answers All the best

Hi Lisa please add digitalWrite(14,HIGH); at the beginning of the loop, it will work:

void loop() 
{
digitalWrite(14,HIGH);
/... rest of your code...

I don't know how you have wired your buzzer, try forcing your ouput pin HIGH or LOW depending on your circuit.

Hope this helps All the best

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM