File: http://svn.grimore.org/cool-iot/trunk/main.js -

#!/usr/bin/env nodejs
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2018 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////
 
const Gpio = require('onoff').Gpio
const mqtt = require('mqtt')
const YAML = require('yamljs');
const winston = require('winston')
 
// Load configuration file.
const config = YAML.load('config.yml');
 
// Generate GPIO pins for configuration.
var COOL = {};
for(var i in config.GPIO) {
  if(!config.GPIO.hasOwnProperty(i))
    continue;
 
  if(config.GPIO[i] === -1)
    continue;
 
  COOL[i] = new Gpio(config.GPIO[i], 'out')
}
 
// Set up logger.
winston.add(winston.transports.File, {filename: config.log})
 
// Initiate connection to MQTT.
const client  = mqtt.connect(config.mqtt.url, {queueQoSZero: false})
 
client.on('connect', function () {
  winston.info('Connected to MQTT server')
  client.subscribe(config.mqtt.topic)
})
 
client.on('message', function (topic, message) {
  if(message.length === 0)
    return;
 
  // Remove any retained message.
  client.publish(topic, "", {retain: true})
 
  message.
  message = message.toString()
  winston.info('Received message: ' + message)
  if(!(message in config.GPIO)) {
    winston.warn('Request to toggle unknown GPIO: ' + message)
    return;
  }
 
  var pin = config.GPIO[message]
  if(pin === -1) {
    winston.warn('GPIO pin for "' + message + '" is not configured')
    return;
  }
 
  winston.info('Toggling pin ' + pin + ' for ' + message)
  COOL[message].write(1, (err) => {
    if(err) {
      winston.err('Unable to toggle pin ' + pin + ' error message received is: ' + err.message)
      return;
    }
 
    setTimeout(function() {
      winston.info('Toggled pin ' + pin + ' for ' + message)
      COOL[message].write(0)
    }, 1000)
  })
})

iot/programatically_controlling_wall_mounted_air_conditioner/software/cool-iot/main.js.txt ยท Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.