temp sensorwith web page py

Solutions on MaxInterview for temp sensorwith web page py by the best coders in the world

showing results for - "temp sensorwith web page py"
Emmanuel
02 Jan 2019
1import RPi.GPIO as GPIO
2import time
3import json, requests
4import datetime
5
6channel =2 #GPIO2
7data = []
8j = 0
9
10GPIO.setmode(GPIO.BCM)
11
12time.sleep(1)
13
14GPIO.setup(channel, GPIO.OUT)
15GPIO.output(channel, GPIO.LOW)
16time.sleep(0.02)
17GPIO.output(channel, GPIO.HIGH)
18GPIO.setup(channel, GPIO.IN)
19
20while GPIO.input(channel) == GPIO.LOW:
21    continue
22while GPIO.input(channel) == GPIO.HIGH:
23    continue
24
25while j < 40:
26    k = 0
27    while GPIO.input(channel) == GPIO.LOW:
28        continue
29    while GPIO.input(channel) == GPIO.HIGH:
30        k+=1
31    if k > 100:
32        break
33    if k < 8:
34        data.append(0)
35    else:
36        data.append(1)
37    j += 1
38
39print ("sensor is working.")
40#print data
41
42humidity_bit = data[0:8]
43humidity_point_bit = data[8:16]
44temperature_bit = data[16:24]
45temperature_point_bit = data[24:32]
46check_bit = data[32:40]
47
48humidity = 0
49humidity_point = 0
50temperature = 0
51temperature_point = 0
52check = 0
53
54for i in range(8):
55    humidity += humidity_bit[i] * 2 ** (7-i)
56    humidity_point += humidity_point_bit[i] * 2 ** (7-i)
57    temperature += temperature_bit[i] * 2 ** (7-i)
58    temperature_point += temperature_point_bit[i] * 2 ** (7-i)
59    check += check_bit[i] * 2 ** (7-i)
60
61tmp = humidity + humidity_point + temperature + temperature_point
62
63if check == tmp:
64    print ("temperature :", temperature, "*C, humidity :", humidity, "%")
65    res='{value:%f}'% temperature
66
67    with open('/home/pi/Desktop/data.txt', 'a') as outfile:
68        json.dump(res, outfile)
69    outest=open('/home/pi/Desktop/data.txt','a')
70    outest.write(res)
71    outest.close
72    #print(res)
73else:
74    print ("wrong")
75  #print "temperature :", temperature, "*C, humidity :", humidity, "% check :", check, ", tmp :", tmp
76
77# Post Data to webpage
78      
79rqs_headers={'Content-Type': 'application/json'}
80requrl ='http://192.168.1.3:8000/temperature_api/'
81new_data = {
82    "captime": datetime.datetime.now(),
83    "captemperature": temperature
84}
85
86class ComplexEncoder(json.JSONEncoder):
87    def default(self, obj):
88        if isinstance(obj, datetime.datetime):
89            return obj.strftime('%Y-%m-%d %H:%M:%S')
90        elif isinstance(obj, datetime.date):
91            return obj.strftime('%Y-%m-%d')
92        else:
93            return json.JSONEncoder.default(self, obj)
94        
95test_data = json.dumps(new_data, cls=ComplexEncoder)
96
97response = requests.post(url=requrl, headers=rqs_headers, data=test_data)
98
99GPIO.cleanup()
100
101123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
similar questions
queries leading to this page
temp sensorwith web page py