esp-01s GPIO3的电位不知为何被loop函数改变-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 346|回复: 2

[未解决] esp-01s GPIO3的电位不知为何被loop函数改变

[复制链接]
发表于 2022-7-20 15:38 | 显示全部楼层 |阅读模式


使用esp-01s + mqtt + IRremoteesp8266 制作远程空调遥控器



出现了很奇怪的情况,以下为示例代码,烧录成功后,GPIO3(RX引脚)可以根据for中的语句,间歇性发射红外信号


  1. <div>#include <Arduino.h>
  2. #include <IRremoteESP8266.h>
  3. #include <IRac.h>
  4. #include <IRutils.h>

  5. const uint16_t kIrLed = 3;  // The ESP GPIO pin to use that controls the IR LED.
  6. IRac ac(kIrLed);  // Create a A/C object using GPIO to sending messages with.

  7. void setup() {
  8.   Serial.begin(115200);
  9.   delay(200);
  10.   ac.begin()
  11.   // Set up what we want to send.
  12.   // See state_t, opmode_t, fanspeed_t, swingv_t, & swingh_t in IRsend.h for
  13.   // all the various options.
  14.   ac.next.protocol = decode_type_t::DAIKIN;  // Set a protocol to use.
  15.   ac.next.model = 1;  // Some A/Cs have different models. Try just the first.
  16.   ac.next.mode = stdAc::opmode_t::kCool;  // Run in cool mode initially.
  17.   ac.next.celsius = true;  // Use Celsius for temp units. False = Fahrenheit
  18.   ac.next.degrees = 25;  // 25 degrees.
  19.   ac.next.fanspeed = stdAc::fanspeed_t::kMedium;  // Start the fan at medium.
  20.   ac.next.swingv = stdAc::swingv_t::kOff;  // Don't swing the fan up or down.
  21.   ac.next.swingh = stdAc::swingh_t::kOff;  // Don't swing the fan left or right.
  22.   ac.next.light = false;  // Turn off any LED/Lights/Display that we can.
  23.   ac.next.beep = false;  // Turn off any beep from the A/C if we can.
  24.   ac.next.econo = false;  // Turn off any economy modes if we can.
  25.   ac.next.filter = false;  // Turn off any Ion/Mold/Health filters if we can.
  26.   ac.next.turbo = false;  // Don't use any turbo/powerful/etc modes.
  27.   ac.next.quiet = false;  // Don't use any quiet/silent/etc modes.
  28.   ac.next.sleep = -1;  // Don't set any sleep time or modes.
  29.   ac.next.clean = false;  // Turn off any Cleaning options if we can.
  30.   ac.next.clock = -1;  // Don't set any current time if we can avoid it.
  31.   ac.next.power = false;  // Initially start with the unit off.

  32.   Serial.println("Try to turn on & off every supported A/C type ...");
  33. }

  34. void loop() {
  35.   // For every protocol the library has ...
  36.   for (int i = 1; i < kLastDecodeType; i++) {
  37.     decode_type_t protocol = (decode_type_t)i;
  38.     // If the protocol is supported by the IRac class ...
  39.     if (ac.isProtocolSupported(protocol)) {
  40.       Serial.println("Protocol " + String(protocol) + " / " +
  41.                      typeToString(protocol) + " is supported.");
  42.       ac.next.protocol = protocol;  // Change the protocol used.
  43.       ac.next.power = true;  // We want to turn on the A/C unit.
  44.       Serial.println("Sending a message to turn ON the A/C unit.");
  45.       ac.sendAc();  // Have the IRac class create and send a message.
  46.       delay(5000);  // Wait 5 seconds.
  47.       ac.next.power = false;  // Now we want to turn the A/C off.
  48.       Serial.println("Send a message to turn OFF the A/C unit.");
  49.       ac.sendAc();  // Send the message.
  50.       delay(1000);  // Wait 1 second.
  51.     }
  52.   }
  53.   Serial.println("Starting from the begining again ...");
  54. }</div>
复制代码


一旦我更改这个代码,比如loop中进行别的操作,比如我这样

  1. <div>void loop()
  2. {
  3.     // succeed in connecting server
  4.     if (mqttClient.connected())
  5.     { //  keep client heartbeat
  6.         mqttClient.loop();
  7.         // only send led sign one time
  8.     }
  9.     else
  10.     { // try connct server
  11.         connectMQTTServer();
  12.     }
  13. }</div>
复制代码
然后红外信号通过mqtt的消息回调触发发送,即根据不同消息类型触发对应操作
  1. void receiveCallback(char *topic, byte *payload, unsigned int length)
  2. {
  3.     Serial.print("Message Received [");
  4.     Serial.print(topic);
  5.     Serial.print(",");
  6.     Serial.print("] ");
  7.     //引入Arduinojson解析json
  8.     StaticJsonDocument<200> doc;

  9.     //
  10.     char message[length];
  11.     for (int i = 0; i < length; i++)
  12.     {
  13.         message[i] = (char)payload[i];
  14.         Serial.print(message[i]);
  15.     }
  16.     Serial.println();
  17.     DeserializationError error = deserializeJson(doc, payload);
  18.     // Test if parsing succeeds.
  19.     if (error)
  20.     {
  21.         Serial.print(F("deserializeJson() failed: "));
  22.         Serial.println(error.f_str());
  23.         return;
  24.     }

  25.     const char *type = doc["type"];
  26.     const char *operation = doc["operation"];
  27.     const char *parameter = doc["parameter"];
  28.     Serial.print("type:");
  29.     Serial.println(type);
  30.     Serial.print("operation:");
  31.     Serial.println(operation);
  32.     Serial.print("parameter:");
  33.     Serial.println(parameter);
  34.     // if equal return 0;
  35.     if (!strcmp(type, acControl))
  36.     {
  37.         // control ac
  38.         // if want to turn ac on or off
  39.         if (!strcmp(operation, power))
  40.         {
  41.             turnOnOrOffAc(atoi(parameter));
  42.         }
  43.         // if want to switch ac's temperature
  44.         else if (!strcmp(operation, temperature))
  45.         {
  46.             temperatureControl(atoi(parameter));
  47.         }
  48.         // if want to switch ac mode
  49.         else if (!strcmp(operation, mode))
  50.         {
  51.         }
  52.         else
  53.         {
  54.         }
  55.     }
  56.     // done
  57.     else if (!strcmp(type, setProtocol))
  58.     {
  59.         // store protocol & model
  60.         EEPROM.write(0, (atoi(operation)));
  61.         EEPROM.commit();
  62.         EEPROM.write(1, (atoi(parameter)));
  63.         EEPROM.commit();
  64.         initAc();
  65.     }
  66.     else if (!strcmp(type, matchProtocol))
  67.     {
  68.         matchAcProtocol(atoi(operation), atoi(parameter));
  69.     }
  70.     // set settings  systemsettings
  71.     else
  72.     {
  73.     }
  74. }
复制代码
receiveCallback 已经在setup中设置: mqttClient.setCallback(receiveCallback);

问题出现了,我自己的代码烧录成功后,GPIO3启动后就持续高电平,红外灯一直亮;mqtt消息解析完成调用发送信号成功,但是因为一直维持高电平,也无法发射信号

我非常不解,并没有对引脚状态做任何的改变,为什么会出现这种情况;尝试GPIO0和GPIO2也都是如此

我猜测可能是loop函数底层有其他操作,但并没有找到

请求大大们帮忙,非常感激!



发表于 2022-7-20 23:17 | 显示全部楼层
GPIO3是串口引脚,串口会调用,GPIO0内部自带上拉
 楼主| 发表于 2022-7-20 23:21 来自手机 | 显示全部楼层
XlinliY.Zhang 发表于 2022-7-20 23:17
GPIO3是串口引脚,串口会调用,GPIO0内部自带上拉

但是我按照上面的示例代码烧录进去是没问题的,gpio0和gpio3都没问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-1 01:46 , Processed in 0.162516 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表