-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
145 lines (126 loc) · 3.7 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
var raspi = require('raspi-io'),
five = require("johnny-five"),
dhtSensor = require('node-dht-sensor'),
Gpio = require('onoff').Gpio,
waterPump = new Gpio(18, 'out'),
awsDevice = require('./device');
var ioPi = new raspi();
var waterLevelValue, soilValue, lcd, temperature, humidity;
// 11 is dht11, 4 is pi gpio pin
// sensorLib.initialize(11, 4);
var tempSensor = {
initialize: function() {
return dhtSensor.initialize(11, 4);
},
read: function() {
var readOut = dhtSensor.read();
temperature = readOut.temperature.toFixed(2);
humidity = readOut.humidity.toFixed(2)
console.log('Temperature: ' + temperature + 'C, ' +
'humidity: ' + humidity + '%');
// return readOut;
setTimeout(function(){
tempSensor.read();
}, 4000)
}
}
var lcdScreen = {
initialize: function(lcd) {
this.lcdVal = lcd;
},
update: function() {
setInterval(function(){
lcdScreen.lcdVal.clear();
printWaterAndSoil(lcdScreen.lcdVal);
setTimeout(function(){
lcdScreen.lcdVal.clear();
printTempAndHumidity(lcdScreen.lcdVal);
}, 2000)
}, 4000)
}
}
var boards = new five.Boards([
{id: 'pi', io: ioPi, debug: true },
{id: 'uno', port: "/dev/ttyACM0", debug: true }
]);
boards.on("ready", function() {
console.log('ready');
if (tempSensor.initialize()) {
tempSensor.read();
} else {
console.warn('Failed to initialize sensor');
}
// initialize lcd screen to print debug info
lcd = new five.LCD({
controller: "LCM1602",
board: this.byId('pi')
});
// test purpose | blink led on uno
var led = new five.Led({
pin: 13,
board: this.byId("uno")
});
led.blink(500);
// initialize water level sensor power pin
var waterPower = five.Pin({pin: 'D4', board: this.byId("uno")});
// A0 is used for water level sensor
var waterLevelSensor = new five.Sensor({ pin: 'A0', enabled: false, board: this.byId("uno") });
waterLevelSensor.on("data", function() {
if(waterPower.isHigh) {
waterLevelValue = this.value;
console.log('water lev: '+this.value);
}
});
// initialize soil moisture power pin
var power = five.Pin({pin: 'D7', board: this.byId("uno")});
// A1 is used for water level sensor
var soilMoisture = new five.Sensor({ pin: "A1", enabled: false, board: this.byId("uno") });
soilMoisture.on("data", function() {
if (power.isHigh) {
soilValue = this.value;
console.log('soil lev: '+this.value);
}
});
this.byId("uno").loop(15000, function() {
if (!power.isHigh) {
power.high();
soilMoisture.enable();
}
if(!waterPower.isHigh) {
waterPower.high();
waterLevelSensor.enable();
}
this.wait(500, function() {
waterPower.low();
waterLevelSensor.disable();
power.low();
soilMoisture.disable();
});
});
lcdScreen.initialize(lcd);
lcdScreen.update();
awsUpdater();
});
awsDevice.device.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
payloadData = JSON.parse(payload);
if(payloadData['state']['pump'] == 'start') {
waterPump.writeSync(1);
} else {
waterPump.writeSync(0);
}
});
printWaterAndSoil = function(lcd) {
lcd.home().print('water: ' + waterLevelValue)
lcd.cursor(1,0).print(' soil: ' + soilValue);
},
printTempAndHumidity = function(lcd) {
lcd.home().print('temp: ' + temperature);
lcd.cursor(1,0).print('hum: ' + humidity);
}
awsUpdater = function() {
setInterval(function() {
data = {'temp': parseFloat(temperature), 'hum': parseFloat(humidity), 'wat': parseFloat(waterLevelValue), 'sol': parseFloat(soilValue)}
awsDevice.device.publish('sensor/data', JSON.stringify(data));
}, 20000);
}