-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemperature_Sensor_(WorkingButLessComments).py
191 lines (179 loc) · 4.23 KB
/
Temperature_Sensor_(WorkingButLessComments).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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#Import the necessary libraries
import serial
import web3
from web3 import Web3
#Defines Constants
temp_threshold = 30
counter_threshold = 60
#Init Web3 Server
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
#Participats of Transactions
owner = w3.eth.accounts[0]
receiver = w3.eth.accounts[1]
#Properties of the Contract
contractAdress = "0xd5e49e2141b60681057432aa64851d7bd1b054a5"
contractAbi = '''[
{
"constant": false,
"inputs": [
{
"name": "_receiver",
"type": "address"
},
{
"name": "_amount",
"type": "uint256"
},
{
"name": "_credential",
"type": "uint256"
}
],
"name": "createTokens",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_receiver",
"type": "address"
},
{
"name": "_amount",
"type": "uint256"
}
],
"name": "transferTokens",
"outputs": [
{
"name": "success",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "checkBalance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "balance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "sender",
"type": "address"
},
{
"indexed": false,
"name": "receiver",
"type": "address"
},
{
"indexed": false,
"name": "amount",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
]'''
#Init Account
w3.eth.defaultAccount = w3.eth.accounts[0]
'''Contract = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
tx_hash = Greeter.constructor().transact()
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)'''
sustainergy_contract = w3.eth.contract(address=Web3.toChecksumAddress(contractAdress),abi=contractAbi)
#Defines function that calculates number of tokens earned
def compute_token(average):
#Calculates the difference between the average room temperature and a set threshold
diff = temp_threshold - average
#If the room temperature is higher than the set maximum no tokens will be generated
if diff < 0:
return 0
#The number of tokens received is half of the temperature difference squared
anz_token = 0.5 * diff ** 2
#The function returns the number of tokens earned
return anz_token
#Defines function that accesses the smart contract
def create_token(n):
sustainergy_contract.functions.createTokens(receiver,n,120).transact()
#Init connection to Arduino
try:
ser = serial.Serial('/dev/ttyACM0', 9600)
except:
ser = serial.Serial('/dev/ttyACM1', 9600)
#Defines variables
average = 0.0
counter = 0
tokenrest = 0
#Initiates infinite loop
while 1:
try:
#Saves data from the Arduino (which is sent every second) in the variable x
x = float(ser.readline())
#Filters obviously wrong measurements
if x > 0:
#Calculates the weighted average of the last average and the current measurement
average = (average*counter + x)/(counter+1)
#Increases the counter by 1
counter += 1
#Prints the counter and the average
print(counter)
print(average)
print(" ")
#If the counter exceeds a set threshold it is reset and the number of earned tokens is calculated
if counter >= counter_threshold:
#Compute amount of token
tokenfloat = compute_token(average) + tokenrest
#average and counter is reset to 0
average = 0
counter = 0
#The tokens are converted to an integer
tokenint = int(tokenfloat)
#The tokens earned are printed
print(tokenint)
#The leftover tokens are saved
tokenrest = tokenfloat - tokenint
#Writes the created tokens to the blockchain
create_token(tokenint)
print("success")
#If no data is received the try path is skipped
except:
pass