ESP32

ESP32

上一个案例中,运算能力弱是急需改变的,有线网络连接也可以升级到无线网。
在调试环境不大改的情况下,选择了ESP32模块。
安装环境见:https://blog.csdn.net/qq_35174914/article/details/79328043
我购买的是零知ESP32,不建议购买,引脚不通用。既然买了就先用着。
同类的控制函数简介:https://blog.csdn.net/Naisu_kun/article/details/88567154#ADC_149

引脚定义

ESP32_第1张图片
ESP32_第2张图片

在函数上与UNO\MEGA 的差异

1.DAC
dacWrite(OUT,250);//OUT只能是26或26,数字可以0~255。DA公式2503.3/255。
2.ADC
int sensorValue = analogRead(A0);// AD公式A0
3.3/4095。
3.PWM
4.SPI
5.中断

建立AP控制LED

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED_BUILTIN 2   

const char *ssid = "yourAP";
const char *password = "yourPassword";

WiFiServer server(80);


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click here to turn ON the LED.
"
); client.print("Click here to turn OFF the LED.
"
); // The HTTP response ends with another blank line: client.println(); // break out of the while loop: break; } else { // if you got a newline, then clear currentLine: currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } // Check to see if the client request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")) { digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } }

SD卡操作

#include "SD_MMC.h"

// 接口连接如下:
// SD卡 - ESP32
// ------------
// DAT2 - IO12
// DAT3 - IO13
// CMD  - IO15
// CLK  - IO14
// DAT0 - IO2   1K上拉3v3
// DAT1 - IO4

void setup()
{
  Serial.begin(115200);
  Serial.println();

  //挂载文件系统
  if (!SD_MMC.begin())
  {
    Serial.println("存储卡挂载失败");
    return;
  }
  uint8_t cardType = SD_MMC.cardType();

  if (cardType == CARD_NONE)
  {
    Serial.println("未连接存储卡");
    return;
  }
  else if (cardType == CARD_MMC)
  {
    Serial.println("挂载了MMC卡");
  }
  else if (cardType == CARD_SD)
  {
    Serial.println("挂载了SDSC卡");
  }
  else if (cardType == CARD_SDHC)
  {
    Serial.println("挂载了SDHC卡");
  }
  else
  {
    Serial.println("挂载了未知存储卡");
  }

  //打开/建立 并写入数据
 File file = SD_MMC.open("/4test.txt", FILE_WRITE);
  if (file)
  {
    Serial.println("打开/建立 根目录下 test.txt 文件!");
  }

  char data[] = "hello world\r\n";
  file.write((uint8_t *)data, strlen(data));
  file.close();

  //重命名文件
  if (SD_MMC.rename("/3test.txt", "/4retest.txt"))
  {
    Serial.println("test.txt 重命名为 retest.txt !");
  }

  //读取文件数据
  file = SD_MMC.open("/4retest.txt", FILE_READ);
  if (file)
    {
    Serial.print("文件内容是:");
    while (file.available())
    {
      Serial.print((char)file.read());
    }
    file.close();
  }

  //打印存储卡信息
  Serial.printf("存储卡总大小是: %lluMB \n", SD_MMC.cardSize() / (1024 * 1024)); // "/ (1024 * 1024)"可以换成">> 20"
  Serial.printf("文件系统总大小是: %lluB \n", SD_MMC.totalBytes());
  Serial.printf("文件系统已用大小是: %lluB \n", SD_MMC.usedBytes());
}

void loop()
{
}

你可能感兴趣的:(ESP32,arduino)