首页 > 其他分享 >esp32 arduino 支持eeprom保存配置参数

esp32 arduino 支持eeprom保存配置参数

时间:2023-06-13 17:22:06浏览次数:48  
标签:ROM arduino esp32 param str println Serial eeprom SIZE

完整代码如下

#include <WiFi.h>
#include <WiFiUdp.h>
#include "WString.h"
#include "EEPROM.h"

void sendSerial1(String str, bool addline = true);
typedef void(*funcSlice)();

#define ROM_STRING_SIZE   16
#define ROM_INT_SIZE      4

enum TaskType
{
  TaskTypeLed,
  TaskTypeParse,
  TaskTypeSerialRecv,
  TaskTypeUDPRecv,
  TaskTypeCount
};

enum Status
{
  LedStatusOff = HIGH,
  LedStatusOn = LOW 
};

enum AddrEEProm
{
  AddressSSID,
  AddressPassword = AddressSSID + ROM_STRING_SIZE,
  AddressPeerHost = AddressPassword + ROM_STRING_SIZE,
  AddressPeerPort = AddressPeerHost + ROM_STRING_SIZE,
  AddressCount = AddressPeerPort + ROM_INT_SIZE
};

struct GlobalParam
{
  uint32_t chipId;
  uint32_t uruntimemsec;
  uint32_t uledPin;
  uint32_t uledValue;
  uint16_t uUdpPort;
  bool bLedBlink;
  bool wifiConnected;

  String udpPeerHost;
  uint16_t udpPeerPort;

  String strCache;
  String strWifissid;
  String strWifiPassword;
};

struct ProgramBlock
{
  uint32_t runLast;
  uint32_t runSlice;
  funcSlice func;
};

int pinsda = GPIO_ID_PIN(0);
int pinscl = GPIO_ID_PIN(4);
ProgramBlock pb[TaskTypeCount] = {0};
GlobalParam param = {0};
//The udp library class
WiFiUDP udp;
EEPROMClass romflash("rom", AddressCount);

void showUsage()
{
  Serial.println("============== menu ==============");
  Serial.println("?: help menu");
  Serial.println("s: status info");
  sendSerial1("reboot: reboot device");
  sendSerial1("ssid=xxxx:save wifi ssid");
  sendSerial1("password=xxxx:save wifi password");  
  sendSerial1("remhost=xxxx:save udp peer host");  
  sendSerial1("remport=xxxx:save udp peer port");  
  Serial.println("==================================");
}

void showStatus()
{
    Serial.println("=============== status =============");
    Serial.printf("chip Id:%d\n", param.chipId);
    Serial.print("wifi connected:");
    Serial.println(param.wifiConnected);
    Serial.print("local ip:");
    Serial.println(WiFi.localIP()); 
    // Serial.printf("has run time(misecs):%d\n", param.uruntimemsec); 
    Serial.print("led blink:");
    Serial.println(param.bLedBlink);
    String str;
    str = "ssid:" + param.strWifissid;
    sendSerial1(str);

    str = "password:" + param.strWifiPassword;
    sendSerial1(str);

    str = "has run:" + timeFormat();
    sendSerial1(str);

    Serial.printf("udp listen port:%d\n", param.uUdpPort);
    Serial.print("udp peer host:");
    Serial.println(param.udpPeerHost);
    Serial.printf("udp peer port:%d\n", param.udpPeerPort);
    Serial.println("====================================");
}

String timeFormat()
{
  int secs = param.uruntimemsec / 1000;
  int nday = 24*3600;
  String str;
  int day = secs / (nday);
  if(day > 0)
  {
    str += day;
    str += "d ";
  }
  int hor = secs % nday;
  int h = hor/3600;
  if(h){
    str += h;
    str += "h ";
  }
  hor = hor % 3600;
  int m = hor /60;
  if(m)
  {
    str += m;
    str += "m ";
  }
  int s = hor % 60;
  if(s)
  {
    str += s;
    str += "s";
  }
  str += "(";
  str += secs;
  str += "secs)";
  return str;
}

void initUDP()
{
  param.uUdpPort = 10888;
  udp.begin(param.uUdpPort);
}

void initEEPRom()
{
    char buf[ROM_STRING_SIZE] = {0};
    if(!romflash.begin(romflash.length()))
    {
      sendSerial1("failed to init eeprom wifissid");
    }else {
      memset(buf, 0, ROM_STRING_SIZE);
      int ret = romflash.readString(AddressSSID, buf, ROM_STRING_SIZE);
      param.strWifissid = buf;

      memset(buf, 0, ROM_STRING_SIZE);
      ret = romflash.readString(AddressPassword, buf, ROM_STRING_SIZE);
      param.strWifiPassword = buf;

      memset(buf, 0, ROM_STRING_SIZE);
      ret = romflash.readString(AddressPeerHost, buf, ROM_STRING_SIZE);
      param.udpPeerHost = buf;

      param.udpPeerPort = romflash.readInt(AddressPeerPort);
    }
}

void sendUDP(String str)
{
  udp.beginPacket(param.udpPeerHost.c_str(), param.udpPeerPort);
    udp.println(str);
    udp.endPacket();
}

void sendSerial1(String str, bool addline)
{
  if(addline)
  {
      Serial.println(str);
  }else {
      Serial.print(str);
  }
    
}

void WiFiEvent(WiFiEvent_t event){
    switch(event) {
      case SYSTEM_EVENT_STA_GOT_IP:
          //When connected set 
          Serial.print("WiFi connected! IP address: ");
          Serial.println(WiFi.localIP());  
          //initializes the UDP state
          //This initializes the transfer buffer
          // udp.begin(WiFi.localIP(),udpPort);
          param.wifiConnected = true;
          pb[TaskTypeLed].runSlice = 1000;
          param.bLedBlink = true;
          initUDP();
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          Serial.println("WiFi lost connection");
          param.wifiConnected = false;
          pb[TaskTypeLed].runSlice = 3000;
          param.bLedBlink = false;
          param.uledValue = LedStatusOff;
          break;
      default: break;
    }
}

void connectToWIFI()
{
  WiFi.disconnect(true);
  WiFi.onEvent(WiFiEvent);
  WiFi.begin(param.strWifissid.c_str(), param.strWifiPassword.c_str());
}

void getChipInfo()
{
  param.chipId = 0;
  for(int i=0; i<17; i=i+8) {
	  param.chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
	}
  // Serial.printf("ESP32 Chip model = %s Rev %d\n", ESP.getChipModel(), ESP.getChipRevision());
	// Serial.printf("This chip has %d cores\n", ESP.getChipCores());
  // Serial.print("Chip ID: "); 
  // Serial.println(chipId);
}

void taskLed()
{
  if(param.bLedBlink)
  {
    if(LedStatusOff == param.uledValue){
      param.uledValue = LedStatusOn;
    }else{
      param.uledValue = LedStatusOff;
    }
  }
  digitalWrite(param.uledPin, param.uledValue);
}

void taskParseData()
{
  while(param.strCache.length())
  {
      int index = param.strCache.indexOf('\n');
      if(index < 0){
        return;
      }
      String strwhole = param.strCache.substring(0, index);
      param.strCache.remove(0, index+1);
      strwhole.trim();
      Serial.printf("whole len:%d ", strwhole.length());
      Serial.println(strwhole);
      // Serial.printf("equal ret:%d\n", ret);
      if(strwhole.equalsIgnoreCase(String('s')))
      {
          showStatus();
      }else if(strwhole.equalsIgnoreCase(String("reboot")))
      {
        ESP.restart();
      }
      else if(strwhole.startsWith("ssid="))
      {
        String ssid = strwhole.substring(5, strwhole.length());
        romflash.writeString(AddressSSID, ssid);
        romflash.commit();
        sendSerial1(ssid);
      }else if(strwhole.startsWith("password="))
      {
          String pwd = strwhole.substring(9, strwhole.length());
          romflash.writeString(AddressPassword, pwd);
          romflash.commit();
          sendSerial1(pwd);
      }else if(strwhole.startsWith("remhost="))
      {
        String ssid = strwhole.substring(8, strwhole.length());
        romflash.writeString(AddressPeerHost, ssid);
        romflash.commit();
        sendSerial1(ssid);
      }else if(strwhole.startsWith("remport="))
      {
        String ssid = strwhole.substring(8, strwhole.length());
        romflash.writeInt(AddressPeerPort, ssid.toInt());
        romflash.commit();
        sendSerial1(ssid);
      }
      else {
          showUsage();
      }
  }  
}

void taskSerialRecv()
{  
  while(Serial.available())
  {
      char nch = Serial.read();
      param.strCache += nch;      
  }
}

char buffUdpRecv[255] = {0};

void taskUDPRecv()
{
  if(!param.wifiConnected){
    return;
  }
  int len = udp.parsePacket();
  if(len <= 0){
    return;
  }
  // IPAddress phost = udp.remoteIP();
  // uint16_t pport = udp.remotePort();
  // param.udpPeerHost = phost;
  // param.udpPeerPort = pport;  
  udp.read(buffUdpRecv, len);
  buffUdpRecv[len] = '\n';
  buffUdpRecv[len+1] = 0;
  param.strCache += buffUdpRecv;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  sendSerial1("system start ...");

  initEEPRom();

  pb[TaskTypeParse].runLast = 0;
  pb[TaskTypeParse].runSlice = 20;
  pb[TaskTypeParse].func = taskParseData;

  pb[TaskTypeLed].runLast = 0;
  pb[TaskTypeLed].runSlice = 1000;
  pb[TaskTypeLed].func = taskLed;

  pb[TaskTypeSerialRecv].runLast = 0;
  pb[TaskTypeSerialRecv].runSlice = 40;
  pb[TaskTypeSerialRecv].func = taskSerialRecv;

  pb[TaskTypeUDPRecv].runLast = 0;
  pb[TaskTypeUDPRecv].runSlice = 40;
  pb[TaskTypeUDPRecv].func = taskUDPRecv;

  getChipInfo();

  param.uledPin = GPIO_ID_PIN(22);
  param.uledValue = LedStatusOff;
  pinMode(param.uledPin, OUTPUT);
  taskLed();
  connectToWIFI();
}

void loop() {
  // put your main code here, to run repeatedly:  
  param.uruntimemsec = millis();
  for(int t = 0; t < TaskTypeCount; t++)
  {
    if(param.uruntimemsec - pb[t].runLast > pb[t].runSlice)
    {
      pb[t].runLast = param.uruntimemsec;
      pb[t].func();
    }
  }
}

  

标签:ROM,arduino,esp32,param,str,println,Serial,eeprom,SIZE
From: https://www.cnblogs.com/larkin-cn/p/17478231.html

相关文章

  • Arduino学习—— 蜂鸣器
     voidsetup(){//putyoursetupcodehere,torunonce:Serial.begin(9600);pinMode(2,OUTPUT);pinMode(7,OUTPUT);analogWrite(9,255);}voidloop(){//putyourmaincodehere,torunrepeatedly://Serial.println(digitalRead(4));if(......
  • esp32_s3学习
    官方资料https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32s3/hw-reference/esp32s3/user-guide-devkitc-1.html......
  • ESP32-C3入门教程——导读
    文章目录一、环境篇二、基础篇三、系统篇四、WiFi篇五、蓝牙篇六、网络篇七、IoT篇八、问题篇九、ESP-IDF5.x篇十、开源代码十一、视频演示关于更新进度有超链接的文章是已经完成的,可以点击跳转直接看没有超链接的文章是计划要写的,暂时还没写的,我会尽快完成一、环境篇ESP32-C3入......
  • 7日玩转ESP32——(第1日) Hello World
    文章目录一、ESP32-C3开发板准备二、安装VisualStudioCode三、安装扩展四、配置ESP-IDF插件五、创建工程六、配置工程6.1选择目标芯片6.2项目配置七、编译工程八、固件下载九、今日作业十、参考答案十一、打卡~一、ESP32-C3开发板准备ESP32-C3是一款安全、低功耗、低成本的......
  • ESP32的JTAG调试
    JTAG使用手册准备工作:硬件:必备硬件ESP32-S3-DevKitC-1USB2.0数据线(标准A型转Micro-B型)电脑(Windows、Linux或macOS)硬件设置以ESP32-S3-DevKitC-1开发板举例:该开发板有两个USB接口,其中一个为USBPort,另一个为USB-to-UARTPortUSBPort(USB接口):ESP32-S3......
  • ESP32的JTAG调试方法
    ReportofJTAGIntroduction:Figuringoutabugthatiscausedbytwothreads,runningevensimultaneouslyontwodifferentCPUcores,cantakealongtimewhenallyouhaveareprintf()statements.Abetter(andinmanycasesquicker)waytodebugsuch......
  • 基于ESP32的TCP/IP传输实现
    TCP/IP协议原理TCP/IP协议是Internet互联网最基本的协议,TCP/IP协议的应用层的主要协议有HTTP、Telnet、FTP、SMTP等,是用来读取来自传输层的数据或者将数据传输写入传输层;传输层的主要协议有UDP、TCP,实现端对端的数据传输;网络层的主要协议有ICMP、IP、IGMP,主要负责网络中数据包的......
  • ESP32 IDF 读取双路AS5600
    白了少年头,空悲切。又有哥们生孩子了。他们有车有房有老婆还有孩子,也不知咋挣来的。搞得我聚会都不想去了。郁闷。没办法,自己只有努力。胸藏万卷凭吞吐,笔有千钧任翕张。前言之前用ESP32驱动两路AS5600没有成功,后面简单梳理了下IIC的逻辑,才反应过来。下面记录一下。ESP32......
  • DHT22湿度和温度传感器与Arduino连接电路图
    DHT22温湿度传感器是一款多功能且经济高效的传感器,用于测量环境温度和湿度,应用范围广泛。它基于数字信号输出,可以提供高精度测量,温度分辨率为0.1摄氏度,湿度分辨率为0.1%。该传感器采用电容式湿度传感元件和热敏电阻分别测量湿度和温度。DHT22传感器的功耗也相对较低,可在3.3V......
  • Arduino开发环境搭建
    软件下载地址:安装板级驱动:C:\Users\88429\Documents\Arduino\hardware\espressif\arduino-esp32-masteresp32wroom32板级驱动为github的链接:安装库文件:旧版(18)库位置:安装位置//aduino//librariesor//hardware或者:c盘的x86programfiles/arduinno/libraries新版的library......