Arduino从不断更新的文件中读取

时间:2014-08-07 00:37:13

标签: python arduino processing weather yahoo-weather-api

我想知道你是否可以通过解决这个我不知道如何修复的问题帮助我正在教的一些高中生。这也有助于他们了解Stackoverflow的优秀资源。

我的学生正在尝试使用arduino创建一个实时检测的天气灯。 python程序使用Yahoo API读取邮政编码所具有的天气状况,并每隔15分钟左右将其附加到文件中。

同时,我们的Arduino正在使用Processing来访问文件,将数字推入串口,arduino读取串口以打开正确的灯光以显示"天气" (晴天会打开黄色LED)。

我们的处理和Arduino工作正常(它从文件中读取并显示正确的灯光)。它甚至可以在Processing和Arduino环境运行时运行,并且您手动将数字添加到文件中。通过向文件输出正确的天气,我们的Python文件也能正常工作。

问题......这两个脚本无法同时运行。如果Python正在进行真实世界更新(每15分钟检查一次天气),则处理将不会触及该文件。在完成Python脚本并且我们启动Processing环境(再次)之后,将不会读取该文件。如果它不能访问文件并且灯光会随着时间的推移而改变,那么这就破坏了真实世界更新的目的和这个项目的重点。

在相关的说明中,我知道为Arduino使用Wifi Shield会更好,但是我们没有时间也没有资源来获取它。这是80美元,从开始到结束他们有一个星期做这个项目。

这是代码。

文件的外观

2, 3, 4, 3, 2

2 - 晴天(打开引脚2)...... 3 - 下雨(打开第3针)......

Arduino的

 void setup() { 
     // initialize the digital pins as an output.
     pinMode(2, OUTPUT);
     pinMode(3, OUTPUT);
     pinMode(4, OUTPUT);
    // Turn the Serial Protocol ON
     Serial.begin(9600);
}

void loop() {
     byte byteRead;

     /* check if data has been sent from the computer: */
     if (Serial.available()) {
         /* read the most recent byte */
         byteRead = Serial.read();
         //You have to subtract '0' from the read Byte to convert from text to a number.
         byteRead=byteRead-'0';

         //Turn off all LEDs if the byte Read = 0
         if(byteRead==0){
             //Turn off all LEDS
             digitalWrite(2, LOW);
             digitalWrite(3, LOW);
             digitalWrite(4, LOW);

         }

         //Turn LED ON depending on the byte Read.
        if(byteRead>0){
             digitalWrite((byteRead), HIGH); // set the LED on
             delay(100);
         }
     }
 }

处理

import processing.serial.*;
import java.io.*;
int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;


void setup(){
     //Create a switch that will control the frequency of text file reads.
     //When mySwitch=1, the program is setup to read the text file.
     //This is turned off when mySwitch = 0
     mySwitch=1;

     //Open the serial port for communication with the Arduino
     //Make sure the COM port is correct
     myPort = new Serial(this, "COM3", 9600);
     myPort.bufferUntil('\n');
}

void draw() {
     if (mySwitch>0){
        /*The readData function can be found later in the code.
        This is the call to read a CSV file on the computer hard-drive. */
        readData("C:/Users/Lindsey/GWC Documents/Final Projects/lights.txt");

        /*The following switch prevents continuous reading of the text file, until
        we are ready to read the file again. */
        mySwitch=0;
     }
     /*Only send new data. This IF statement will allow new data to be sent to
     the arduino. */
     if(counter<subtext.length){
        /* Write the next number to the Serial port and send it to the Arduino 
        There will be a delay of half a second before the command is
        sent to turn the LED off : myPort.write('0'); */
        myPort.write(subtext[counter]);
         delay(500);
         myPort.write('0');
         delay(100);
         //Increment the counter so that the next number is sent to the arduino.
         counter++;
     } else{
         //If the text file has run out of numbers, then read the text file again in 5 seconds.
         delay(5000);
         mySwitch=1;
     }
} 


/* The following function will read from a CSV or TXT file */
void readData(String myFileName){

     File file=new File(myFileName);
     BufferedReader br=null;

     try{
        br=new BufferedReader(new FileReader(file));
        String text=null;

        /* keep reading each line until you get to the end of the file */
        while((text=br.readLine())!=null){
            /* Spilt each line up into bits and pieces using a comma as a separator */
            subtext = splitTokens(text,",");
        }
     }catch(FileNotFoundException e){
        e.printStackTrace();

     }catch(IOException e){
        e.printStackTrace();

     }finally{
        try {
            if (br != null){
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
     }
}

Python代码

import pywapi
import time

# create a file
file = open("weatherData.txt", "w+")
file.close()

for i in range(0,10):
    weather = pywapi.get_weather_from_weather_com("90210")['current_conditions']['text']
    print(weather)

    if "rain" or "showers" in weather:
        weatherValue = "4"

    if "Sun" in weather:
        weatherValue = "2"
    if "thunder" in weather:
        weatherValue = "5"
    #elif "Cloud" in weather:
    if "Cloudy" in weather: 
        weatherValue = "3"
        print(weatherValue)

    # append the file with number
    # append with a comma after and space
    file = open("weatherData.txt","a")
    file.write(weatherValue + ", ")
    file.close()

    time.sleep(10)

1 个答案:

答案 0 :(得分:4)

这实际上不一定是三部分编程挑战 - 因为您可以使用PySerial模块。这是我过去用来检索在线数据并通过串口直接传递给arduino的模块。首先,您必须按照我给您的链接中的说明安装模块。在python程序中,您可以将代码更改为:

import pywapi
import time
import serial #The PySerial module

# create a file
file = open("weatherData.txt", "w+")
file.close()

ser = serial.Serial("COM3", 9600) #Change COM3 to whichever COM port your arduino is in

for i in range(0,10):
    weather = pywapi.get_weather_from_weather_com("90210")['current_conditions']['text']
    print(weather)

    if "rain" or "showers" in weather:
        weatherValue = "4"

    if "Sun" in weather:
        weatherValue = "2"
    if "thunder" in weather:
        weatherValue = "5"
    #elif "Cloud" in weather:
    if "Cloudy" in weather: 
        weatherValue = "3"
        print(weatherValue)

    # append the file with number
    # append with a comma after and space
    file = open("weatherData.txt","a")
    file.write(weatherValue + ", ")
    file.close()

    #Sending the file via serial to arduino
    byte_signal = bytes([weatherValue])
    ser.write(byte_signal)

    time.sleep(10)

您甚至不需要将其写入文件,但如果您打算以其他方式使用该文件,程序仍应创建相同的文件。

您的arduino代码可能如下所示:

int weather_pin = 0;
void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  if(weather_pin>0){
    digitalWrite(weather_pin, HIGH);
  }
}

void serialEvent() {
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  weather_pin = Serial.read();
}

这应该有效!我有它在我的系统上工作,但每个系统都不同,所以如果它没有工作随时给我发表评论。如果您对代码有任何疑问,请同样处理。作为一名高中生,我自己知道stackoverflow的美丽,我认为你为孩子们所做的事情绝对令人敬畏。祝你好好制作天气灯!