-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
211 lines (181 loc) · 5.13 KB
/
index.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const express = require('express')
const cors = require('cors')
const app = express()
const server = require('http').createServer(app)
const io = require('socket.io')(server)
const ip = require('ip')
const PORT = 3030
const serverIP = `${ip.address()}:${PORT}`
const path = require('path')
const bodyParser = require('body-parser')
const five = require("johnny-five")
const saveMeasurement = require('./database/saveMeasurement')
const getSampleMeasurements = require('./database/getSampleMeasurements')
const getSparklines = require('./database/getSparklines')
const emptyMisterLevel = 30
const emptyDrainLevel = 23
var misterWaterLevel = 0,
drainWaterLevel = 0,
lastReading,
lastSaveTime,
misterRelayValue,
lightsRelayValue
new five.Board({ repl: false }).on("ready", function() {
console.log('Johnny-Five up, Board Ready!')
// Initialize water level sensors using proximity sensors
let misterWaterLevelSensor = new five.Proximity({ controller: "HCSR04", pin: "A0" })
let drainWaterLevelSensor = new five.Proximity({ controller: "HCSR04", pin: "A1" })
const lightsRelay = new five.Relay(2)
const misterRelay = new five.Relay(3)
// start off with the lights and mister off
// relay is active low
lightsRelay.on()
misterRelay.on()
lightsRelayValue = false
misterRelayValue = false
misterWaterLevelSensor.on('change', function() {
let level = Math.round((emptyMisterLevel - this.cm + 1) / emptyMisterLevel * 100)
misterWaterLevel = level > 0 ? level : 0
})
drainWaterLevelSensor.on('change', function() {
let level = Math.round((emptyDrainLevel - this.cm + 1) / emptyDrainLevel * 100)
drainWaterLevel = level > 0 ? level : 0
})
io.on('connection', function(socket){
socket.on('toggleMister', function(value){
// relay is active low
if (!value) {
misterRelay.on()
} else {
misterRelay.off()
}
misterRelayValue = value
socket.emit('misterValue', value);
})
socket.on('toggleLights', function(value){
// relay is active low
if (!value) {
lightsRelay.on()
} else {
lightsRelay.off()
}
lightsRelayValue = value
socket.emit('lightsValue', value);
})
// emit current values
socket.emit('misterValue', misterRelayValue);
socket.emit('lightsValue', lightsRelayValue);
})
app.use(cors())
app.use(bodyParser.urlencoded({
extended: true
}))
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'views'))
// route for main dashboard
app.get('/', (req, res, next) => {
res.render('index')
next()
})
// get last measurement
app.get('/measurement', (req, res, next) => {
res.json(lastReading || {})
next()
})
app.get('/measurements', (req, res, next) => {
getSampleMeasurements()
.then(results => {
let temp = results.reduce((measurements, m, index) => {
if (index % 10 === 0) { measurements.push(m) }
return measurements
}, [])
if (lastReading) {
results.push(lastReading);
}
res.json(temp)
next()
})
.catch(err => {
console.log(err)
next()
})
})
app.get('/sparklines', (req, res, next) => {
getSparklines()
.then(results => {
if (lastReading) {
results.push(lastReading);
}
res.json(results)
next()
})
.catch(err => {
console.log(err)
next()
})
})
// POST temperatures
app.post('/temperatures', (req, res, next) => {
let body = JSON.parse(JSON.stringify(req.body))
let reading = {
date: new Date(),
probeA: body.probeA,
probeB: body.probeB,
probeC: body.probeC,
probeD: body.probeD,
probeE: body.probeE,
rig_name: body.rig_name,
uvIndex: body.uvIndex,
humidity: body.humidity,
soilMoisture: body.soilMoisture,
temperature: body.temperature,
misterWaterLevel,
drainWaterLevel
}
lastReading = reading
//console.log(lastReading)
//io.sockets.emit('reading', reading)
res.sendStatus(200)
next()
})
// Toggle Mister
app.post('/toggleMister', (req, res, next) => {
// mister relay is active low
if (misterRelayValue === true) {
misterRelay.on()
misterRelayValue = false
} else {
misterRelay.off()
misterRelayValue = true
}
io.sockets.emit('misterValue', misterRelayValue);
res.sendStatus(200)
next()
})
// Toggle Lights
app.post('/toggleLights', (req, res, next) => {
// lights relay is active low
if (lightsRelayValue === true) {
lightsRelay.on()
lightsRelayValue = false
} else {
lightsRelay.off()
lightsRelayValue = true
}
io.sockets.emit('lightsValue', lightsRelayValue);
res.sendStatus(200)
next()
})
setInterval(() => {
if (!lastReading) { return }
saveMeasurement(lastReading)
.then(results => {
lastSaveTime = new Date().getTime()
console.log('Reading saved.')
})
.catch(error => {
console.log(error)
})
}, (1000 * 60))
})
server.listen(PORT, () => console.log(`API listening on ${serverIP}`))