-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadata.py
More file actions
130 lines (111 loc) · 2.95 KB
/
readata.py
File metadata and controls
130 lines (111 loc) · 2.95 KB
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
# -*- coding: utf-8 -*-
import os
import minimalmodbus
import time
from pymongo import MongoClient
import json
import datetime
def mod():
rs485 = minimalmodbus.Instrument('/dev/ttyUSB0', 1)
rs485.serial.baudrate = 9600
rs485.serial.bytesize = 8
rs485.serial.parity = minimalmodbus.serial.PARITY_NONE
rs485.serial.stopbits = 1
rs485.serial.timeout = 1
rs485.debug = False
rs485.mode = minimalmodbus.MODE_RTU
return rs485
##for i in mdir:
## print i,mdir[i][1]
mdir=[[0,"V"], [6,"A"], [12,"W"], [18,"VA"], [24,"VAr"], [30,"-"], [36,"°"], [70,"Hz"], [72,"kwh"], [74,"kwh"], [76,"kvarh"], [78,"kvarh"], [342,"kwh"], [344,"kvarh"]]
meudir=[{
"name": "Voltaje",
"unit": "0",
"value": 0},
{
"name": "Corriente",
"unit": "0",
"value": 0},
{
"name": "Potencia Activa",
"unit": "0",
"value": 0},
{
"name": "Potencia Aparente",
"unit": "0",
"value": 0},
{
"name": "Potencia Reactiva",
"unit": "0",
"value": 0},
{
"name": "Factor de Potencia",
"unit": "0",
"value": 0},
{
"name": "Angulo de fase",
"unit": "0",
"value": 0},
{
"name": "Frecuencia",
"unit": "0",
"value": 0},
{
"name": "I Energia Activa",
"unit": "0",
"value": 0},
{
"name": "E Energia Activa",
"unit": "0",
"value": 0},
{
"name": "I Energia Reactiva",
"unit": "0",
"value": 0},
{
"name": "E Energia Reactiva",
"unit": "0",
"value": 0},
{
"name": "Energia Activa Total",
"unit": "0",
"value": 0},
{
"name": "Energia Reactiva Total",
"unit": "0",
"value": 0}]
def reader(adress):
return mod().read_float(adress, functioncode=4, numberOfRegisters=2)
def rhis(namefile, data):
fd = open(namefile,'a')
fd.write(str(json.dumps(data))+'\n')
fd.close()
def history(start, data):
date = datetime.datetime.now()
if start.day == date.day:
filename = "lastday.json"
rhis(filename, data)
if start.month == date.month:
filename = "lastmonth.json"
rhis(filename, data)
if start.year == date.year:
filename = "lastyear.json"
rhis(filename, data)
start = datetime.datetime.now()
os.system("rm lastday.json && rm lastmonth.json && rm lastyear.json" )
while True:
try:
for i in range(13):
meudir[i]["value"]=reader(mdir[i][0])
meudir[i]["unit"]=mdir[i][1]
print meudir
fc = open("data.json",'w')
fc.write(json.dumps(meudir))
fc.close()
history(start, meudir)
except IOError:
mod()
print "Waiting for communication"
# fc = open("output.json",'w')
# fc.write(json.dumps([{'corriente':0, 'voltaje':analogicout, 'potencia':0, 'tp':0, 'tv':0, 'tc':0}]))
# fc.close()