# Create an indicator LED on GPIO pin 17 led = LED(17) # Create the Temperature Sensor on GPIO pin 4 temperatureSensor = TemperatureSensor(4) # Dictionary to hold Temperature Sensor data temperatureData = {} # Sit in a loop reading the sensors every sampleTime, convert result to JSON, and POST to the Web API while True: # Turn LED ON led.on() # Read the Temperature Sensor temperatureSensor.read() if temperatureSensor.valid: # Save the Temperature Sensor results in a Temperature Data Object temperatureData["deviceID"] = deviceID temperatureData["temperature"] = temperatureSensor.temperatureF temperatureData["humidity"] = temperatureSensor.humidity temperatureData["pressure"] = 0 # Print results to the console if in debug mode if debug: print("Sampled at %s Temperature: %.2f F Humidity: %.2f" % (str(datetime.datetime.now()), temperatureData["temperature"], temperatureData["humidity"])) # Convert the Temperature Data Object to JSON string strj = json.dumps(temperatureData, ensure_ascii=False) # POST the JSON results to the RESTful Web API using HTTP Basic Authentication
from time import sleep from Wifi import is_connected from Server import Server from TemperatureSensor import TemperatureSensor from config import config if not is_connected(): print('No connection found, exiting...') exit() sensor = TemperatureSensor(pin_id=config['sensor']['pin']) server = Server( host=config['server']['host'], port=config['server']['port'], ) while True: temperatures = sensor.read() if not temperatures: print('Error while reading temperatures, exiting...') exit() print('Temperatures read:', temperatures) server.post('sensor/' + config['sensor']['id'], {'temperature': temperatures[0]}) sleep(config['measure']['period'])