Using Python to Send Data to the Cloud

Make sure you have created your sensor in the dashboard and have received the SSL certificate key pairs. If you need to do this process please refer to the Introduction to Sensors on FarmHub.

The system will send the SSL certificate, that is specific to the sensor you just created, to the project admin. The email will contain all the certificate information in order to connect securely with our system.

Here is an example of how to send your data to the dashboard using javascript, but you can use whatever language you would like as long as it follows the same basic principals and uses the SSL certificate to connect.

from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
import json
import datetime
# For certificate based connection
myMQTTClient = AWSIoTMQTTClient("myClientID"
myMQTTClient.configureEndpoint("YOUR.ENDPOINT", 8883)
myMQTTClient.configureCredentials("YOUR/ROOT/CA/PATH", "PRIVATE/KEY/PATH", "CERTIFICATE/PATH")
payload = {
  "dt": datetime.datetime.now().isoformat(),
  "v": 4.8
}
myMQTTClient.publish("TOPIC", json.dump(payload), 0)
1
2
3
4
5
6
7
8
9
10
11
12

You will experience a slight delay with the publishing if your data as your data is aggregated to a 5 minute interval. Our enterprise clients have more fine-grained exploration of the data. If you're interested in this level of analysis, contact us today.

For more information and options on setting up a MQTT client you can check out the GitHub respositoryopen in new window. We recommend these libraries because they have great features like offline-queuing and intermittent connection handling for those of you who have weaker internet connections.

Last Updated: 10/23/2022, 1:45:24 PM
Contributors: Jonathan Reyes