0

我只需要在 1 米范围内响铃,我的 esp8266 连接到蜂鸣器,但是使用两个 esp8266 蜂鸣器会在 1 米范围内响起和关闭,或者有时根本不响。该项目是社会疏远,我还应该怎么做?这是我正在使用的代码:

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

2 回答 2

0

嗨,丽莎,我已经修改了您的代码,并添加了一个连接到 I2C 连接的 64x48 OLED 显示器,以可视化结果并且它工作得很好。此外,我使用了内置 LED(参见:LED_BUILTIN)而不是蜂鸣器。你有一个好主意。现在它工作得很好,您可以询问任何接入点。

附上代码:

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

希望这个答案一切顺利

于 2021-09-24T13:30:12.640 回答
-1

嗨,丽莎,请添加 digitalWrite(14,HIGH); 在循环开始时,它将起作用:

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

我不知道你是如何连接你的蜂鸣器的,试着根据你的电路强制你的输出引脚高或低。

希望这有助于一切顺利

于 2021-09-22T13:08:04.160 回答