简体   繁体   English

在Raspberry Pi Weather Station的JavaScript中读取(从保管箱).txt文件的问题

[英]Issue reading .txt files (from dropbox) in JavaScript for Raspberry Pi Weather station

How exactly would I go by reading .txt files in JavaScript from a dropbox file? 我如何从Dropbox文件中读取JavaScript中的.txt文件? I recently got a weather sensor for my raspberry pi, and I want to save the current weather data every hour to a .txt file in dropbox (code seen below). 最近,我为树莓派安装了一个天气传感器,我希望每小时将当前的天气数据保存到Dropbox中的.txt文件中(如下所示)。 However, I am a bit stuck on linking the data from a dropbox file to my web page so I can chart graphs of weather patterns. 但是,我在将数据从保管箱文件链接到我的网页时有些卡住,因此可以绘制天气图的图表。

I have tried using the Dropbox API but to no avail. 我尝试使用Dropbox API,但无济于事。

Below is my code in Python: 以下是我在Python中的代码:

# Weather Station App by Rob

# Will get current temperatures every minute, then will write data to a seperate file, which will then be added to dropbox.

from time import *
from random import randint

def writeTemperature():
    output = open("output.txt", "w")

    # Instead of using a variable, get the temperature from the Raspberry Pi.
    # Fahrenheight or Celcius
    temperature = randint(1, 32)

    listOfTemperatures.append(temperature)

    averageTotal = 0

    for temp in listOfTemperatures:
        averageTotal = averageTotal + temp

    average = averageTotal / len(listOfTemperatures)

    output.write(("Temperature: " + str(temperature)) + '\n')
    output.write("Average: " + str(average)) 
    output.write(listOfTemperatures)

    print "Added!"

    output.close()

listOfTemperatures = []
average = 0    

while True:
    writeTemperature()
    sleep(1)

The function saves the weather data to a file. 该功能将天气数据保存到文件中。 (Ignore the temperature variable and the sleep function, was just debugging) (忽略温度变量和睡眠功能,只是调试)

The output.txt file will look like: output.txt文件如下所示:

Temperature: 22
Average: 12

After this, my only trouble is linking it to JavaScript. 此后,我唯一的麻烦就是将其链接到JavaScript。 Just confused on this part. 只是对这部分感到困惑。 Pointers in the right direction would be well appreciated. 正确方向的指针将不胜感激。

Thank you. 谢谢。

linking the data from a dropbox file to my web page 将数据从保管箱文件链接到我的网页

As far as i know you cannot simply load a local file into your browser javascript. 据我所知,您不能简单地将本地文件加载到浏览器javascript中。 What you can do is setup a simple webserver like pythons SimpleHTTPServer or a simple nodejs server with express.js . 您可以执行的操作是使用pythons SimpleHTTPServer设置一个简单的Web服务器,或者使用express.js设置一个简单的nodejs服务器。 Then deliver your html, js and define a route for loading your text file via an ajax for instance. 然后提供您的html,js并定义通过ajax加载文本文件的路径。

Express.js example: Express.js示例:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.sendFile('index.html')
})

app.use(express.static('public')) // now you can load any file from your public folder (e.g. public/weather_data.txt)

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

In your browsers javascript you can then do (using jquery): 然后,您可以在浏览器中使用javascript执行操作(使用jquery):

$.ajax({
  url: "./public/weather_data.txt",
  async: false,
  success: function (data){
    // Do whatever you want with the data
    document.getElementById('myElement').innerHTML = data
  }
})

Was this helpful? 这个有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM