如何在 Arduino Sketch 中移动 C 定义

问题描述 投票:0回答:1

我是一名 Java 开发人员,在 Arduino 中编写 C 语言。我的C技能已经生锈了。下面发布的针对 ESP8266 的代码在当前形式下运行良好,但我需要将

Pushsafer pushsafer(pushsaferKey, wifiClient);
定义移至
loop()
函数。在当前形式中,
pushsaferKey
位于定义语句中。我想从 EEPROM 中读取它(我知道该怎么做),而不是对其进行硬编码。我该如何移动它?

谢谢!

  #include <ESP8266WiFi.h>
  #include <WiFiClient.h>
  #include <Pushsafer.h>

  char ssid[] = "my_ssid";
  char password[] = "my_pwd"; 

  boolean notificationSent = false;

  #define pushsaferKey "my_pushsafer_key"  // REPLACE THIS
  //String pushsaferKey = "";              // WITH THIS

  WiFiClient wifiClient;
  Pushsafer pushsafer(pushsaferKey, wifiClient);  //<<< problem, how to move to loop()?

  void sendNotification() {

    // See https://www.pushsafer.com/en/pushapi

    struct PushSaferInput input;
    input.message = "This is a test message";
    input.title = "This is a Test Title";
    input.sound = "8";
    input.vibration = "1";
    input.icon = "1";
    input.iconcolor = "#FFCCCC";
    input.priority = "1";
    input.device = "a";
    input.url = "https://www.pushsafer.com";
    input.urlTitle = "Open Pushsafer.com";
    input.picture = "";
    input.picture2 = "";
    input.picture3 = "";
    input.time2live = "";
    input.retry = "";
    input.expire = "";
    input.confirm = "";
    input.answer = "";
    input.answeroptions = "";
    input.answerforce = "";

    Serial.println(pushsafer.sendEvent(input));
    Serial.println("Notification Sent");
  }

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

    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);

    Serial.print("Connecting Wifi: ");
    Serial.println(ssid);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
      Serial.print(".");
      delay(500);
    }

    Serial.println("\nWiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }

  void loop() {
    //read pushsaferKey from EEPROM
  //  pushsaferKey = "pushsafer_key_from_eeprom";  
  //  Pushsafer pushsafer(pushsaferKey, wifiClient); //<<< move to here

    while(!notificationSent) {  
      sendNotification();  
      notificationSent = true;
    }
  }
c arduino
1个回答
0
投票
  1. ESP32 没有 EEPROM 内存

  2. 您可以使用Arduino EEPROM库。 ESP32 Arduino 端口使用 512 个 FLASH 存储器来模拟 EEPROM。

  3. #include <EEPROM.h>

  4. setup

    EEPROM.begin(100);
    (100 是您要使用的模拟 EEPROM 的大小)

  5. 模拟EEPROM的读写方法

void readStringFromEEPROM(char *buff)
{
    for(unsigned address = 0; buff[address] = EEPROM.read(address); address++);
}

void writeStringToEEPROM(const char *buff)
{
    for(unsigned address = 0;; address++)
    {
        EEPROM.write(address, buff[address]);
        if(!buff[address]) break;
    }
    EEPROM.commit();
}

当您需要读取或写入 eeprom 时使用它们。记得先写点东西再看。

© www.soinside.com 2019 - 2024. All rights reserved.