test_fast.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright 2016 by MPI-SWS and Data-Ken Research.
  2. # Licensed under the Apache 2.0 License.
  3. """
  4. This isn't actually a unit test - it is for validating fast sampling on the
  5. actual ESP8266. It samples a dummy sensor at 10Hz and pushes to an
  6. MQTT queue. To run it:
  7. 0. Set up an mqtt broker (mosquitto)
  8. 1. Replace the CHANGE_ME values with the appropriate values for your environment
  9. 2. Copy the script to the ESP8266 (e.g. via mpfsheel) and import the module.
  10. """
  11. from thingflow import Scheduler
  12. from mqtt_writer import MQTTWriter
  13. from wifi import wifi_connect
  14. import uos
  15. import ustruct
  16. # Params to set
  17. WIFI_SID="CHANGE_ME"
  18. WIFI_PW="CHANGE_ME"
  19. SENSOR_ID="front-room"
  20. BROKER='CHANGE_ME'
  21. class DummySensor:
  22. def __init__(self, sensor_id):
  23. self.sensor_id = sensor_id
  24. def sample(self):
  25. return ustruct.unpack('@H', uos.urandom(2))[0]
  26. wifi_connect(WIFI_SID, WIFI_PW)
  27. sensor = DummySensor(sensor_id=SENSOR_ID)
  28. writer = MQTTWriter(SENSOR_ID, BROKER, 1883, 'remote-sensors')
  29. sched = Scheduler()
  30. sched.schedule_sensor(sensor, 0.1, writer)
  31. print("Starting sensor sampling")
  32. sched.run_forever()