📜  示例项目 WITTY ESP8266 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:47.601000             🧑  作者: Mango

代码示例1
/*
    This sketch demonstrates how to scan for available WiFi networks.
    A button input is used to initiate the scan and the on-board LED
    is lit to indicate when a scan is in process
    On each loop, also check the analog input connected to the LDR and adjust
    the brightness of the RGB Green LED to match the measured brightness.
*/
#include "ESP8266WiFi.h"

const int BUTTON_PIN = 4;    // Define pin the button is connected to
const int ON_BOARD_LED = 2;  // Define pin the on-board LED is connected to
const int RGB_G_PIN = 12;    // RGB Green LED
const int LDR_PIN = A0;      // Define the analog pin the LDR is connected to
//===============================================================================
//  Initialization
//===============================================================================
void setup() {
  pinMode(ON_BOARD_LED, OUTPUT);       // Initialize the LED_BUILTIN pin as an output
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Initialize button pin with built-in pullup.
  digitalWrite(ON_BOARD_LED, HIGH);    // Ensure LED is off
  Serial.begin(115200);               // Set comm rate to 115200

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("Setup done");
}
//===============================================================================
//  Main
//===============================================================================
void loop() {
  int btn_Status = HIGH;
  int lightIntensity;

  lightIntensity = analogRead(LDR_PIN);  // Read the light intensity
  analogWrite( RGB_G_PIN, map(lightIntensity, 40, 1023, 0, 1023));
  btn_Status = digitalRead (BUTTON_PIN);  // Check status of button
  if (btn_Status == LOW) {                // Button pushed, so do something
    Serial.print("Light Intensity Reading: ");
    Serial.println(lightIntensity);
    Serial.println("scan start");
    digitalWrite(ON_BOARD_LED, LOW);       // Turn LED ON
    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    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));
        Serial.print(" (");
        Serial.print(WiFi.RSSI(i));
        Serial.print(")");
        Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " : Unsecure" : " : Encrypted");
        delay(10);
      }
    }
    Serial.println("");
    digitalWrite(ON_BOARD_LED, HIGH);    // Turn LED Off
  }
}