mqtt_ws_example_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. import re
  3. import sys
  4. from threading import Event, Thread
  5. import paho.mqtt.client as mqtt
  6. import ttfw_idf
  7. from tiny_test_fw import DUT
  8. event_client_connected = Event()
  9. event_stop_client = Event()
  10. event_client_received_correct = Event()
  11. message_log = ''
  12. # The callback for when the client receives a CONNACK response from the server.
  13. def on_connect(client, userdata, flags, rc):
  14. print('Connected with result code ' + str(rc))
  15. event_client_connected.set()
  16. client.subscribe('/topic/qos0')
  17. def mqtt_client_task(client):
  18. while not event_stop_client.is_set():
  19. client.loop()
  20. # The callback for when a PUBLISH message is received from the server.
  21. def on_message(client, userdata, msg):
  22. global message_log
  23. payload = msg.payload.decode()
  24. if not event_client_received_correct.is_set() and payload == 'data':
  25. client.publish('/topic/qos0', 'data_to_esp32')
  26. if msg.topic == '/topic/qos0' and payload == 'data':
  27. event_client_received_correct.set()
  28. message_log += 'Received data:' + msg.topic + ' ' + payload + '\n'
  29. @ttfw_idf.idf_example_test(env_tag='ethernet_router')
  30. def test_examples_protocol_mqtt_ws(env, extra_data):
  31. broker_url = ''
  32. broker_port = 0
  33. """
  34. steps: |
  35. 1. join AP and connects to ws broker
  36. 2. Test connects a client to the same broker
  37. 3. Test evaluates it received correct qos0 message
  38. 4. Test ESP32 client received correct qos0 message
  39. """
  40. dut1 = env.get_dut('mqtt_websocket', 'examples/protocols/mqtt/ws', dut_class=ttfw_idf.ESP32DUT)
  41. # check and log bin size
  42. binary_file = os.path.join(dut1.app.binary_path, 'mqtt_websocket.bin')
  43. bin_size = os.path.getsize(binary_file)
  44. ttfw_idf.log_performance('mqtt_websocket_bin_size', '{}KB'.format(bin_size // 1024))
  45. # Look for host:port in sdkconfig
  46. try:
  47. value = re.search(r'\:\/\/([^:]+)\:([0-9]+)', dut1.app.get_sdkconfig()['CONFIG_BROKER_URI'])
  48. broker_url = value.group(1)
  49. broker_port = int(value.group(2))
  50. except Exception:
  51. print('ENV_TEST_FAILURE: Cannot find broker url in sdkconfig')
  52. raise
  53. client = None
  54. # 1. Test connects to a broker
  55. try:
  56. client = mqtt.Client(transport='websockets')
  57. client.on_connect = on_connect
  58. client.on_message = on_message
  59. print('Connecting...')
  60. client.connect(broker_url, broker_port, 60)
  61. except Exception:
  62. print('ENV_TEST_FAILURE: Unexpected error while connecting to broker {}: {}:'.format(broker_url, sys.exc_info()[0]))
  63. raise
  64. # Starting a py-client in a separate thread
  65. thread1 = Thread(target=mqtt_client_task, args=(client,))
  66. thread1.start()
  67. try:
  68. print('Connecting py-client to broker {}:{}...'.format(broker_url, broker_port))
  69. if not event_client_connected.wait(timeout=30):
  70. raise ValueError('ENV_TEST_FAILURE: Test script cannot connect to broker: {}'.format(broker_url))
  71. dut1.start_app()
  72. try:
  73. ip_address = dut1.expect(re.compile(r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]'), timeout=30)[0]
  74. print('Connected to AP with IP: {}'.format(ip_address))
  75. except DUT.ExpectTimeout:
  76. print('ENV_TEST_FAILURE: Cannot connect to AP')
  77. raise
  78. print('Checking py-client received msg published from esp...')
  79. if not event_client_received_correct.wait(timeout=30):
  80. raise ValueError('Wrong data received, msg log: {}'.format(message_log))
  81. print('Checking esp-client received msg published from py-client...')
  82. dut1.expect(re.compile(r'DATA=data_to_esp32'), timeout=30)
  83. finally:
  84. event_stop_client.set()
  85. thread1.join()
  86. if __name__ == '__main__':
  87. test_examples_protocol_mqtt_ws()