repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mqtt_publisher.py 855B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import paho.mqtt.client as mqtt
  2. import software.backend.defines
  3. from software.backend.defines import MQTT_BROKER_LOCAL
  4. from random import randrange, uniform
  5. import time
  6. import json
  7. from software.backend.defines import Topics, PLANTDATA
  8. mqttBroker = software.backend.defines.MQTT_BROKER_GLOBAL
  9. def on_connect(client, userdata, flags, rc):
  10. if rc == 0:
  11. print("Connected")
  12. else:
  13. print("Connection failed")
  14. client = mqtt.Client()
  15. client.on_connect = on_connect
  16. client.connect(mqttBroker)
  17. plantdata = {
  18. "AirTemperature": 1.0,
  19. "AirHumidity": 1.0,
  20. "SoilMoisture": 1.0,
  21. "Brightness": 1,
  22. "PlantID": 2,
  23. "Timestamp": "",
  24. "MeasurementID": 0,
  25. "PlantName": "test"
  26. }
  27. print(type(PLANTDATA))
  28. client.publish(Topics['BACKEND_ACTION_NEWPLANT'], json.dumps(plantdata))
  29. print(json.dumps(plantdata))
  30. time.sleep(2)