Skip to content

Instantly share code, notes, and snippets.

@alejo4373
Last active September 30, 2018 00:11
Show Gist options
  • Save alejo4373/c5a11a8241337688f46bbf883c72fd79 to your computer and use it in GitHub Desktop.
Save alejo4373/c5a11a8241337688f46bbf883c72fd79 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from __future__ import print_function
import boto3
import time
import datetime
import sys
import Adafruit_DHT
from decimal import Decimal
# Type of sensor, can be Adafruit_DHT.DHT11, Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
DHT_TYPE = Adafruit_DHT.AM2302 #quals to just 22
# GPIO data pins connected to Raspberry Pi pin in an array
DHT_PINS_ARRAY = [2,3,4]
#Sensors IDs
SENSORS_IDS = ['sensorA', 'sensorB', 'sensorC']
#Variables
temperatureA = temperatureB = temperatureC = 0
humidityA = humidityB = humidityC = 0
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
# Instantiate a table resource object without actually
# creating a DynamoDB table using an already creaded one.
table = dynamodb.Table('ladfyu-sensors')
# index
i = 0
while True:
humidity, temperature = Adafruit_DHT.read(22, DHT_PINS_ARRAY[i]) #Attempt a reading
if humidity is not None and temperature is not None:
humidity = Decimal(temperature).quantize(Decimal('0.01'))
temperature = Decimal(temperature * 1.8 + 32).quantize(Decimal('0.01')) #converted to 2 decimal points and farenth
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") # datetime.datetime to string
with open('output.csv', 'a') as f: #Keep a local copy in output.csv of the data
print(current_time, SENSORS_IDS[i], temperature, humidity, sep=',', file=f)
try:
#Send data to table
table.put_item(
Item={ 'reading_id': current_time,
'sensor_id': SENSORS_IDS[i],
'humidity': humidity,
'temperature': temperature
}
)
print ("logged")
except:
# Error appending data, most likely because of a network error
print('Append error, check network connection')
time.sleep(30)
continue
else:
time.sleep(5)
print ('temp or hum is None')
continue
print(SENSORS_IDS[i], DHT_PINS_ARRAY[i], temperature, humidity)
time.sleep(5)
if i == 2: i = -1 #restart index for pins and sensors array
i = i + 1
#Tutorial followed http://boto3.readthedocs.io/en/latest/guide/dynamodb.html?highlight=dynamo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment