我正在使用ESP32接收原始红外信号,并使用mqtt协议将其发布到服务器。服务器收到IR信号后,会将其发送回ESP进行传输。
我能够在收到信号后立即成功发送信号(控制设备)。但是,当从服务器接收到IR数据时,它不会成功传输。我从https://github.com/curlyz/ir-remote-upython-esp32/blob/master/remote_v2.py
处获取了IR代码Remote类的一部分:(注释掉的行发送正确的信号)
def _routine(self):
while True:
sleep_ms(200)
if ticks_diff(ticks_us(),self.prev_irq) > 200000 and self.length > 0 :
print('DECODED = [{},{}] '.format(self.decode()[1],self.length))
payload = {"buffer":self.buffer, "length":self.length,"prev_irq":self.prev_irq}
client.publish(b"remote/payload/123", json.dumps(payload))
#self.send ( self.buffer , self.length , 40 )
self.prev_irq = 0
self.length = 0
for x in range(len(self.buffer)):
self.buffer[x] = 0
def send(self, packet, length = None , mul = 40):
try:
length = length or len(packet)
self.recv.irq(trigger = 0 , handler = None) # Disable IRQ Receiving Channel
sleep = sleep_us
duty = self.pwm.duty
n = 0 # prev
b = ticks_diff
c = ticks_us
for x in range(length):
if x%2 == 0 :
duty(512)
else :
duty(0)
sleep( packet[x])
duty(0)
sleep_ms(3) # magic
self.recv.irq(trigger = Pin.IRQ_RISING|Pin.IRQ_FALLING , handler = self._handler) # Re-enable IRQ
except Exception as e:
print('Error occurred : ' + repr(e))
Mqtt回调函数:(在Remote类之外)
def sub_cb(topic, msg):
try:
global remote
data = json.loads(msg)
remote.send(data['buffer'], data['length'], 40)
except Exception as e:
print('Error occurred : ' + repr(e))
启动了一个新线程,以便mqtt可以在红外接收器等待信号的同时从服务器检查发布的方法。
def main():
global remote
s = startup()
client.set_callback(sub_cb)
client.connect()
client.subscribe(b"remote/receive/12345")
_thread.start_new_thread(mqtt_thread, ())
remote._routine()
remote.send(data['buffer'], data['length'], 40)
仍然会传输信号,但是我希望在给定相同参数的情况下,它会以与self.send ( self.buffer , self.length , 40 )
相同的方式工作。我使用另一个ESP32记录了两个信号,但找不到两者之间的差异。
任何帮助将不胜感激!