|
使用esp-01s + mqtt + IRremoteesp8266 制作远程空调遥控器
出现了很奇怪的情况,以下为示例代码,烧录成功后,GPIO3(RX引脚)可以根据for中的语句,间歇性发射红外信号
- <div>#include <Arduino.h>
- #include <IRremoteESP8266.h>
- #include <IRac.h>
- #include <IRutils.h>
- const uint16_t kIrLed = 3; // The ESP GPIO pin to use that controls the IR LED.
- IRac ac(kIrLed); // Create a A/C object using GPIO to sending messages with.
- void setup() {
- Serial.begin(115200);
- delay(200);
- ac.begin()
- // Set up what we want to send.
- // See state_t, opmode_t, fanspeed_t, swingv_t, & swingh_t in IRsend.h for
- // all the various options.
- ac.next.protocol = decode_type_t::DAIKIN; // Set a protocol to use.
- ac.next.model = 1; // Some A/Cs have different models. Try just the first.
- ac.next.mode = stdAc::opmode_t::kCool; // Run in cool mode initially.
- ac.next.celsius = true; // Use Celsius for temp units. False = Fahrenheit
- ac.next.degrees = 25; // 25 degrees.
- ac.next.fanspeed = stdAc::fanspeed_t::kMedium; // Start the fan at medium.
- ac.next.swingv = stdAc::swingv_t::kOff; // Don't swing the fan up or down.
- ac.next.swingh = stdAc::swingh_t::kOff; // Don't swing the fan left or right.
- ac.next.light = false; // Turn off any LED/Lights/Display that we can.
- ac.next.beep = false; // Turn off any beep from the A/C if we can.
- ac.next.econo = false; // Turn off any economy modes if we can.
- ac.next.filter = false; // Turn off any Ion/Mold/Health filters if we can.
- ac.next.turbo = false; // Don't use any turbo/powerful/etc modes.
- ac.next.quiet = false; // Don't use any quiet/silent/etc modes.
- ac.next.sleep = -1; // Don't set any sleep time or modes.
- ac.next.clean = false; // Turn off any Cleaning options if we can.
- ac.next.clock = -1; // Don't set any current time if we can avoid it.
- ac.next.power = false; // Initially start with the unit off.
- Serial.println("Try to turn on & off every supported A/C type ...");
- }
- void loop() {
- // For every protocol the library has ...
- for (int i = 1; i < kLastDecodeType; i++) {
- decode_type_t protocol = (decode_type_t)i;
- // If the protocol is supported by the IRac class ...
- if (ac.isProtocolSupported(protocol)) {
- Serial.println("Protocol " + String(protocol) + " / " +
- typeToString(protocol) + " is supported.");
- ac.next.protocol = protocol; // Change the protocol used.
- ac.next.power = true; // We want to turn on the A/C unit.
- Serial.println("Sending a message to turn ON the A/C unit.");
- ac.sendAc(); // Have the IRac class create and send a message.
- delay(5000); // Wait 5 seconds.
- ac.next.power = false; // Now we want to turn the A/C off.
- Serial.println("Send a message to turn OFF the A/C unit.");
- ac.sendAc(); // Send the message.
- delay(1000); // Wait 1 second.
- }
- }
- Serial.println("Starting from the begining again ...");
- }</div>
复制代码
一旦我更改这个代码,比如loop中进行别的操作,比如我这样
- <div>void loop()
- {
- // succeed in connecting server
- if (mqttClient.connected())
- { // keep client heartbeat
- mqttClient.loop();
- // only send led sign one time
- }
- else
- { // try connct server
- connectMQTTServer();
- }
- }</div>
复制代码 然后红外信号通过mqtt的消息回调触发发送,即根据不同消息类型触发对应操作
- void receiveCallback(char *topic, byte *payload, unsigned int length)
- {
- Serial.print("Message Received [");
- Serial.print(topic);
- Serial.print(",");
- Serial.print("] ");
- //引入Arduinojson解析json
- StaticJsonDocument<200> doc;
- //
- char message[length];
- for (int i = 0; i < length; i++)
- {
- message[i] = (char)payload[i];
- Serial.print(message[i]);
- }
- Serial.println();
- DeserializationError error = deserializeJson(doc, payload);
- // Test if parsing succeeds.
- if (error)
- {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.f_str());
- return;
- }
- const char *type = doc["type"];
- const char *operation = doc["operation"];
- const char *parameter = doc["parameter"];
- Serial.print("type:");
- Serial.println(type);
- Serial.print("operation:");
- Serial.println(operation);
- Serial.print("parameter:");
- Serial.println(parameter);
- // if equal return 0;
- if (!strcmp(type, acControl))
- {
- // control ac
- // if want to turn ac on or off
- if (!strcmp(operation, power))
- {
- turnOnOrOffAc(atoi(parameter));
- }
- // if want to switch ac's temperature
- else if (!strcmp(operation, temperature))
- {
- temperatureControl(atoi(parameter));
- }
- // if want to switch ac mode
- else if (!strcmp(operation, mode))
- {
- }
- else
- {
- }
- }
- // done
- else if (!strcmp(type, setProtocol))
- {
- // store protocol & model
- EEPROM.write(0, (atoi(operation)));
- EEPROM.commit();
- EEPROM.write(1, (atoi(parameter)));
- EEPROM.commit();
- initAc();
- }
- else if (!strcmp(type, matchProtocol))
- {
- matchAcProtocol(atoi(operation), atoi(parameter));
- }
- // set settings systemsettings
- else
- {
- }
- }
复制代码 receiveCallback 已经在setup中设置: mqttClient.setCallback(receiveCallback);
问题出现了,我自己的代码烧录成功后,GPIO3启动后就持续高电平,红外灯一直亮;mqtt消息解析完成调用发送信号成功,但是因为一直维持高电平,也无法发射信号
我非常不解,并没有对引脚状态做任何的改变,为什么会出现这种情况;尝试GPIO0和GPIO2也都是如此
我猜测可能是loop函数底层有其他操作,但并没有找到
请求大大们帮忙,非常感激!
|
|