-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-DHT22.js
146 lines (123 loc) · 5.13 KB
/
MMM-DHT22.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
Module.register('MMM-DHT22', {
defaults: {
gpioPin: 6,
fontSize: '16px',
fontFamily: 'Arial',
showThermometerIcon: true,
showDropletIcon: true,
showTemperatureText: true,
showHumidityText: true,
headerText: 'Local Environment',
updateInterval: 60,
temperatureIconColor: 'red',
humidityIconColor: 'blue',
temperatureFontSize: '',
humidityFontSize: '',
temperatureUnit: 'C', // Default temperature unit (Celsius)
humidityUnit: ' %', // Default humidity unit
temperatureOffset: 0, // Default temperature offset adjustment
humidityOffset: 0, // Default humidity offset adjustment
layout: 'horizontal', // Default layout style ('horizontal' or 'vertical')
},
start: function() {
this.temperature = '';
this.humidity = '';
this.sendSocketNotification('START_DHT_READING', { gpioPin: this.config.gpioPin });
const updateIntervalMillis = this.config.updateInterval * 1000;
setInterval(() => {
this.sendSocketNotification('START_DHT_READING', { gpioPin: this.config.gpioPin });
}, updateIntervalMillis);
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'DHT_DATA') {
if (payload.humidity >= 0 && payload.humidity <= 100) {
let temperature = payload.temperature + this.config.temperatureOffset;
temperature = this.convertTemperature(temperature);
let humidity = payload.humidity + this.config.humidityOffset;
this.temperature = temperature;
this.humidity = humidity.toFixed(1) + this.config.humidityUnit;
this.updateDom();
} else {
console.warn('Invalid humidity reading:', payload.humidity);
}
}
},
getStyles: function () {
return ['MMM-DHT22.css'];
},
convertTemperature: function(celsiusTemperature) {
// Conversion formula from Celsius to Fahrenheit
if (this.config.temperatureUnit === 'F') {
return ((celsiusTemperature * 9/5 + 32).toFixed(1) + ' °F');
}
return (celsiusTemperature.toFixed(1) + ' °C'); // Default to Celsius
},
getDom: function() {
const wrapper = document.createElement('div');
wrapper.style.fontSize = this.config.fontSize;
wrapper.style.fontFamily = this.config.fontFamily;
const header = document.createElement('div');
header.style.fontWeight = 'bold';
header.appendChild(document.createTextNode(this.config.headerText));
wrapper.appendChild(header);
const line = document.createElement('hr');
line.className = 'horizontal-line';
wrapper.appendChild(line);
if (this.config.layout === 'horizontal') {
// Horizontal layout
const dataContainer = document.createElement('div');
dataContainer.className = 'horizontal-container';
const temperatureDiv = document.createElement('div');
if (this.config.showTemperatureText) {
temperatureDiv.appendChild(document.createTextNode('Temperature: '));
}
if (this.config.showThermometerIcon) {
const thermometerIcon = document.createElement('span');
thermometerIcon.className = 'fa fa-thermometer-half';
thermometerIcon.style.color = this.config.temperatureIconColor;
thermometerIcon.style.marginRight = '10px'; // Add spacing after icon
temperatureDiv.appendChild(thermometerIcon);
}
temperatureDiv.appendChild(document.createTextNode(this.temperature));
if (this.config.showTemperatureUnit) {
temperatureDiv.appendChild(document.createTextNode(' ' + this.config.temperatureUnit));
}
// Set the font size for temperature independently
if (this.config.temperatureFontSize) {
temperatureDiv.style.fontSize = this.config.temperatureFontSize;
}
dataContainer.appendChild(temperatureDiv);
const humidityDiv = document.createElement('div');
if (this.config.showHumidityText) {
humidityDiv.appendChild(document.createTextNode('Humidity: '));
}
if (this.config.showDropletIcon) {
const dropletIcon = document.createElement('span');
dropletIcon.className = 'fa fa-tint';
dropletIcon.style.color = this.config.humidityIconColor;
dropletIcon.style.marginRight = '10px'; // Add spacing after icon
humidityDiv.appendChild(dropletIcon);
}
humidityDiv.appendChild(document.createTextNode(this.humidity));
// Set the font size for humidity independently
if (this.config.humidityFontSize) {
humidityDiv.style.fontSize = this.config.humidityFontSize;
}
dataContainer.appendChild(humidityDiv);
wrapper.appendChild(dataContainer);
} else if (this.config.layout === 'vertical') {
// Vertical layout
const temperatureDiv = document.createElement('div');
temperatureDiv.className = 'vertical-data';
// Add temperature data here with icons and spacing as before
// ...
const humidityDiv = document.createElement('div');
humidityDiv.className = 'vertical-data';
// Add humidity data here with icons and spacing as before
// ...
wrapper.appendChild(temperatureDiv);
wrapper.appendChild(humidityDiv);
}
return wrapper;
},
});