Ruchir Sharma
Published © GPL3+

How to Send Data to Google Cloud Database from RaspberryPi

I show how to send temp sensor data from Rpi database to Google Cloud database.

IntermediateFull instructions provided2 hours18,272
How to Send Data to Google Cloud Database from RaspberryPi

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1

Software apps and online services

Cloud IoT Core
Google Cloud IoT Core

Story

Read more

Code

Python Script

Python
import MySQLdb
import subprocess
import re
import sys
import time
import datetime
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4 # GPIO numbering (Pin # 7)

# Open database connection
dbconn = MySQLdb.connect("localhost","root","pass","tempsens") or die("could not connect to database")
cursor=dbconn.cursor()

# Continuously append data
while True:
timestamp = datetime.datetime.now()
today = timestamp.strftime("%d/%m/%Y %H:%M:%S")
print today

# Run the DHT program to get the humidity and temperature readings!
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity)

# MYSQL DATA Processing
print"SQL Injected!"
cursor.execute("INSERT INTO weatherDataa (ts,humidity,tempC ) VALUES (%s, %s, %s)",(timestamp, humidity, temperature))
dbconn.commit()

#cursor.close()
time.sleep(5)

Credits

Ruchir Sharma

Ruchir Sharma

12 projects • 182 followers

Comments