forked from futureshocked/RaspberryPiFullStack_Raspbian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rf24_receiver.py
111 lines (91 loc) · 3.9 KB
/
rf24_receiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
#
# 1120 - The Python nRF24 receiver sketch
#
# From Raspberry Pi Full Stack Raspbian
# https://app.techexplorations.com/courses/raspberry-pi-full-stack-raspbian/
#
# This program will use the nRF24 module to receive data from the remote Arduino node.
#
# RECEIVER NODE
# Listens for messages from the transmitter and prints them out.
#
# Script name: rf24_receiver.py
from __future__ import print_function
import time
from struct import *
from RF24 import *
from RF24Network import *
import sqlite3
import sys
from time import gmtime, strftime
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import requests
import RPi.GPIO as GPIO ## Import GPIO Library
def log_values(sensor_id, temp, hum):
GPIO.output(pin, GPIO.HIGH) ## Turn on GPIO pin (HIGH)
conn=sqlite3.connect('/var/www/lab_app/lab_app.db') #It is important to provide an
#absolute path to the database
#file, otherwise Cron won't be
#able to find it!
curs=conn.cursor()
print("Update database...")
curs.execute("INSERT INTO temperatures values(datetime(CURRENT_TIMESTAMP, 'localtime'), ?, ?)", (sensor_id,float(temp)))
curs.execute("INSERT INTO humidities values(datetime(CURRENT_TIMESTAMP, 'localtime'), ?, ?)", (sensor_id,float(hum)))
conn.commit()
conn.close()
# Create a new record in the Google Sheet
print("Update Google Sheet...")
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('/var/www/lab_app/raspberry-pi-full-stack-1-f37054328cf5.json', scope)
client = gspread.authorize(creds)
sheet = client.open('Temperature and Humidity').sheet1
row = [strftime("%Y-%m-%d %H:%M:%S", gmtime()),sensor_id,temp,hum] # Not using round because temp and hum are already strings
sheet.append_row(row)
GPIO.output(pin, GPIO.LOW) ## Turn off GPIO pin (LOW)
GPIO.setwarnings(False)
pin = 7 ## GPIO 7 connected is nRF24 CE
GPIO.setmode(GPIO.BOARD) ## Use BOARD pin numbering
GPIO.setup(pin, GPIO.OUT) ## Set pin 7 to OUTPUT
# CE Pin, CSN Pin, SPI Speed
radio = RF24(RPI_V2_GPIO_P1_26, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ)
network = RF24Network(radio)
# millis = lambda: int(round(time.time() * 1000))
octlit = lambda n: int(n, 8)
# Address of our node in Octal format (01, 021, etc)
this_node = octlit("00")
radio.begin()
time.sleep(0.1)
network.begin(90, this_node) # channel 90
radio.printDetails()
packets_sent = 0
last_sent = 0
while 1:
network.update()
while network.available():
header, payload = network.read(12) #8
print("payload length ", len(payload))
print(payload.decode())
values = payload.decode().split(",")
#print("--------")
#print("Temperature: ", values[1], "°C")
#print("Humidity: ", values[0], " %")
#print("Sensor ID: ", header.from_node)
#print("Header Type: ", str((chr(header.type))))
# Save these values in the database
# To make sure we have proper numbers, we'll convert the strings to numbers.
try:
print("--------")
print("Temperature: ", values[1])
print("Humidity: ", values[0])
print("Sensor ID: ", header.from_node)
print("Header Type: ", str((chr(header.type))))
print("--------")
temperature = float(values[1][0:5])
humidity = float(values[0][0:5])
log_values(header.from_node, temperature, humidity)
except:
print("Unable to process the data received from node ", header.from_node, ". Data: ", values )
print("---------")
time.sleep(1)