-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (79 loc) · 2.88 KB
/
main.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
#!/usr/bin/python3
# Simple example of reading the MCP3008 analog input channels using its
# Author: Gabriel Rodriguez
# License: MIT
# To Add Delay
import time
# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Scale Values
from numpy import interp
# Import DateTime
import datetime
# Import Elasticsearch Libraries
from elasticsearch import Elasticsearch
# Import Requests library to figure out external IP address
import requests
# Import netifaces library to figure out the internal IP address
import netifaces as ni
# Import socket library to get figure out the hostname
import socket
# Import Pretty Printer for JSON & Python Dictionaries
# from prettyprinter import pprint
# Generate Random IDs
import uuid
# Localize the timestamp
import pytz
# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
def main():
# Build Elasticsearch client
es = Elasticsearch(
"IPADDRESS:PORT",
http_auth=("username","password")
)
# Get Internal
# IP Address and save it as the variable 'hip'
# ni.ifaddresses('wlan0')
# hip = ni.ifaddresses('wlan0')[ni.AF_INET][0]['addr']
# Get External IP address and save it as the variable 'exip'
# exip = requests.get('http://icanhazip.com/')
# Get the Hostname of this Raspberry Pi
hname = socket.gethostname()
# The read_adc function will get the value of the specific channel (0-7).
value0 = mcp.read_adc(0)
scale0 = interp(value0, [433, 1023], [100, 0])
output0 = int(scale0)
value1 = mcp.read_adc(1)
scale1 = interp(value1, [432, 1023], [100, 0])
output1 = int(scale1)
channel0_sample = int('{0}'.format(value0))
channel1_sample = int('{0}'.format(value1))
channel0_percent = int('{0}'.format(output0))
channel1_percent = int('{0}'.format(output1))
timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat()
doc = {
'hostname' : hname,
'channel 0 Sample' : channel0_sample,
'channel 0 Moisture Percentage' : channel0_percent,
'channel 1 Sample' : channel1_sample,
'channel 1 Moisture Percentage' : channel1_percent,
'timestamp': timestamp
}
random_id=uuid.uuid4()
res = es.index(index="soil-index", id=random_id, body=doc)
print(res['result'])
res = es.get(index="soil-index", id=random_id)
print(res['_source'])
# es.indices.refresh(index="soil-index")
# res = es.search(index="soil-index", body={"query": {"match_all": {}}})
# print("Got %d Hits:" % res['hits']['total']['value'])
# for hit in res['hits']['hits']:
# print("%(timestamp)s %(hostname)s: %(channel 0 Moisture Percentage)s" % hit["_source"])
# pause for 600 seconds (10 minutes)
# time.sleep(600)
if __name__ == "__main__":
main()