无法使用javascript

时间:2018-03-03 06:34:20

标签: javascript json ruby amazon-dynamodb

我正在制作一个自动浇水系统,并且有一个nodejs应用程序将数据推送到dynamodb,但是我需要将这些数据解析为json以使其进入highcharts。我认为它完全不会超过我的有效载荷。有关如何获取我的json文件的任何建议。

这是index.js

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, temperature, humidity;





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 + '%');

    setTimeout(function(){

      tempSensor.read();

    }, 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');

  }



  var led = new five.Led({

    pin: 13,

    board: this.byId("uno")

  });

  led.blink(500);





  var waterPower = five.Pin({pin: 'D4', board: this.byId("uno")});



  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);

    }

  });



  var power = five.Pin({pin: 'D7', board: this.byId("uno")});



  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();

    }

  });

});



awsDevice.device.on('message', function(topic,payload) {

  console.log('message', topic, payload.toString('utf-8'));

  console.log(payload);

  payloadData = JSON.parse(payload);

  if(payloadData['state']['pump'] == 'start') {

    waterPump.writeSync(1);

  } else {

    waterPump.writeSync(0);

  }

});



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);

}

这是device.js

var awsIot = require('aws-iot-device-sdk');



var device = awsIot.device({

  keyPath: '/home/pi/waterpi-web-master/waterpi-node/0f027e888e-private.pem.key',

  keyPath: '/home/pi/waterpi-web-master/waterpi-node/0f027e888e-private.pem.key',

  certPath: '/home/pi/waterpi-web-master/waterpi-node/0f027e888e-certificate.pem.crt',

  caPath: '/home/pi/waterpi-web-master/waterpi-node/rootCA.pem',

  host: "a9512mth4mf1f.iot.us-east-2.amazonaws.com",

  port: 8883,

  clientId: "iotconsole-1520057176406-1",

  region: 'us-east-2'

});



device.on('connect', function() {

  console.log('connect');

  device.subscribe('pump/data');

});



module.exports.device = device

我也制作了一个红宝石网络应用程序,不确定是否上传,如果是,请告诉我。我正在尝试按照本指南https://www.hackster.io/demirhanaydin/waterpi-houseplant-remote-watering-and-monitoring-system-340400#toc-step-1--setup-dynamodb-0进行操作,并参考高级图和红宝石部分。一切都显示在我的数据库中,我只是无法获取json文件,也许我只是感到困惑。

0 个答案:

没有答案