peterhinch / micropython-mqtt

A 'resilient' asynchronous MQTT driver. Recovers from WiFi and broker outages.
MIT License
550 stars 117 forks source link

esp8266 keep restarting #27

Closed alirezaimi closed 4 years ago

alirezaimi commented 4 years ago

Hi I cross compiled mqtt_as.py with mpy_cross and use it in my project on esp8266, this is my code and my problem is after i deploy main.py to board and restart it, keep restarting untile i kill it !

async def conn_han(client):
    print(">>>>>>>>> inside conn_han")
    await client.subscribe('foo_topic', 1)

async def mqtt_connector(client):
    print(">> connecting to MQTT Broker ...")
    await client.connect()
    print(">> connected MQTT Broker .")

if __name__ == "__main__":
...
config = {
        'client_id': mqtt_client_id,
        'server': mqtt_server_host,
        'port': mqtt_server_port,
        'user': mqtt_server_username,
        'password': mqtt_server_password,
        'keepalive': 60,
        'ping_interval': 0,
        'ssl': True,
        'ssl_params': {},
        'response_time': 10,
        'clean_init': True,
        'clean': True,
        'max_repubs': 4,
        'will': None,
        'subs_cb': lambda *_: None,
        'wifi_coro': "",
        'connect_coro': conn_han,
        'wifi_pw': None,
    }
    mqtt_client = MQTTClient(config)
    try:
        loop.run_until_complete(mqtt_connector(client=mqtt_client))
    finally:
        mqtt_client.close()  # Prevent LmacRxBlk:1 errors
>> connecting to MQTT Broker ...

 ets Jan  8 2013,rst cause:2, boot mode:(3,0)

load 0x40100000, len 31024, room 16 
tail 0
chksum 0xb4
load 0x3ffe8000, len 1104, room 8 
tail 8
chksum 0x74
load 0x3ffe8450, len 824, room 0 
tail 8
chksum 0x45
csum 0x45
���2�o�{��n|�
             $
              l$l b���
                      �|s��$�N��o�l`��r�p��l�

                                             $`��r�p��l�
                                                        $`��r�l���
                                                                  l$`rl��o���B
                                                                              ��B|
                                                                                  $b���#|����ll#��o�n�WebREPL daemon started 

I'm connected to wifi through boot.py and there is no need to do it in mqtt_as because of this i not fill wifi setting in config .

peterhinch commented 4 years ago

This type of question is best raised in the forum where more people are likely to see it and respond. My suggestions are:

  1. Connect in main.py rather than boot.py or use the config. It is generally best to keep boot.py to an absolute minimum.
  2. Implement mqtt_as as frozen bytecode rather than as a cross-compiled file. The ESP8266 is extremely RAM limited. All testing on ESP8266 has been done in this way.
alirezaimi commented 4 years ago

@peterhinch thanks for your respnse, but process of forzening to byte code take couple of my hours and not work! is there any other way !? i'l ask my question in forum too.

peterhinch commented 4 years ago

I would start out by running one of the test programs e.g. range.py. That would prove your hardware and that your broker is running properly. I use mosquitto as the broker, and mosquitto_sub and mosquitto_pub to exercise the test program.

These test program has been heavily tested by myself and others on various hardware including esp8266. On the latter, owing to RAM limitations, I freeze mqtt_as and uasyncio.

alirezaimi commented 4 years ago

I'm totally stuck in freezing process and so far can't get any result ! Is it really the right solution for my problem ? Is any good documentation about freezing micropython for eps8266 ?

peterhinch commented 4 years ago

Is it really the right solution for my problem ?

I can't guarantee your code is bug free. However the test programs should run if you freeze mqtt_as and uasyncio. From that starting point you should be able to develop your own code.

Is any good documentation about freezing micropython for eps8266 ?

Build instructions are here. Once you've successfully built and run the standard firmware, place the modules you want to freeze in ports/esp8266/modules and re-build.

Please ask further questions in the forum. GitHub is for bug reports.