{"size": 941, "ext": "js", "max_stars_repo_path": "teste07-gpio-mqtt-adafruit.js", "max_stars_repo_name": "andreibosco/sensor_caixa_dagua", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "teste07-gpio-mqtt-adafruit.js", "max_issues_repo_name": "andreibosco/sensor_caixa_dagua", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "teste07-gpio-mqtt-adafruit.js", "max_forks_repo_name": "andreibosco/sensor_caixa_dagua", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.8837209302, "max_line_length": 59, "alphanum_fraction": 0.6811902232}
require('dotenv').config();

var gpio = require('chip-gpio').Gpio;
var mqtt = require('mqtt');

var sensor = new gpio(0, 'in', 'both', {
  debounceTimeout: 5000
});

var adafruit_key = process.env.ADAFRUIT_KEY;
var adafruit_username = 'andreibosco';
var feed = 'sensorCaixaDagua';
var topicWill = adafruit_username+'/feeds/'+feed+'_status';
var topic = adafruit_username+'/feeds/'+feed;

var mqttServer = 'mqtt://io.adafruit.com';
var mqttClient = mqtt.connect(mqttServer, {
  username: adafruit_username,
  password: adafruit_key,
  will: {
    topic: topicWill,
    payload: 'Falha'
  }
});

sensor.watch(function (err, value) {
  if (err) {
    throw err;
  }
  sensorValor = sensor.read();
  console.log('Leitura: ' + sensorValor);
  mqttClient.publish(topicWill, 'OK');
  mqttClient.publish(topic, sensorValor.toString());
});

function exit() {
  sensor.unexport();
  mqttClient.end();
  process.exit();
}

process.on('SIGINT', exit);
