チーム173A

課題名

Weather Forecast

研究者名

2-15-21 Tomoki Sakata
2-15-22 Airi Sato

ソースコード

ex.) python weather_forecast.py

import pywapi
import serial
import threading
import time

def call():
  result=pywapi.get_weather_from_weather_com('JAXX0036')
   # out="The temperature in this room is ..... "
   # out+="The weather is "+result['current_conditions']['text'].lower()
   # out+=" and "+result['current_conditions']['temperature']+"C now in Kawasaki. "
   # out+="And tomorrow will be "+result['forecasts'][1]['day']['text']+".\r\n"

  weather=result['current_conditions']['text'].lower()


  out=result['current_conditions']['text'].lower()+" "+result['current_conditions']['temperature']#+"C\r\n"
   out="".join(out.split(" "))+"C\n\r"

  bps=38400

  ser=serial.Serial(port="COM3",baudrate=bps,bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE)

  print(out);

  ser.write(bytes(out,'ascii'))

  print(ser.readline().decode('utf-8'))


while(True):
  call()
   time.sleep(60*5)





#include <m8c.h> // part specific constants and macros
#include "PSoCAPI.h" // PSoC API definitions for all User Modules
#include<string.h>

void main(void)
{

  char input_from_python[128];
   char * strPtr; // Parameter pointer <=Char型変数の宣言
   int index;
   char enter[]="\n";
   
   LCD_Start();
   
   
   Counter8_WritePeriod(155); // Set up baud rate generator
   Counter8_WriteCompareValue(77);
   Counter8_Start(); // Turn on baud rate generator


  UART_CmdReset(); // Initialize receiver/cmd buffer <=コマンドバッファのリセット
   UART_IntCntl(UART_ENABLE_RX_INT); // Enable RX interrupts <=RX割込のイネーブル
   UART_Start(UART_PARITY_NONE); // Enable UART <=UARTのモード設定

  M8C_EnableGInt ; // Turn on interrupts

  
   
   while(1) {
       if(UART_bCmdCheck()) { // Wait for command <=有効な文字コード受信をチェック
           if(strPtr = UART_szGetParam()) { // More than delimiter? <=受信バッファから1文字読みこみ
           
               LCD_PrCString("                                       ");
               LCD_Position(0,0);
               memset(input_from_python,'\0',sizeof(input_from_python));
               strcat(input_from_python,strPtr);
           
               while(strPtr = UART_szGetParam()) { // loop on each param <=受信バッファから順次1文字読みこみ
   
                   strcat(input_from_python,strPtr);
               }
               LCD_PrString(input_from_python);
               strcat(input_from_python,enter);
               UART_PutString(input_from_python);
           }
           UART_CmdReset(); // Reset command buffer <=受信バッファをリセット
       }
   } 
}

使用したもの

・PSoC 1個
・シリアルポート通信ケーブル 1本
・LCD display
・ジャンパ線 2本


説明

(注意)
pythonのスクリプト内で通信するCOM Portを選択する必要がある。
PSocとwindows pcを接続した後、デバイスマネージャ->ポートからポート番号を確認する。
serial.Serial(port="COMX", ... )
コンストラクタに引数として確認したポートに書き換える。
ソースでは例として"COM3"とする。

device.png

このプログラムはpywapiを用いて神奈川県川崎市の天気予報を取得し、
PSocのLCD displayに現在の天気と気温を表示し、一定時間ごとに更新することを目的としている。
ソースコードでは例として5分に設定している。

プログラム内で神奈川県川崎市のロケーションコードを指定し、weather.comからapiを通して天気予報を取得。
現在の天気と気温のみを文字列として抽出し、asciiにエンコードした後、ビット列に変換。
シリアル通信でPSoc に送信。
PSocでは受信した文字列をLCD displayに表示させる。

以上の操作を一定時間ごとに実行し、天気の情報を更新する。


sample.jpg

cmd.png

designer.png

考察

この作品の欠点
PSoC側の処理は文字列を受信して表示するだけの簡単なものになっている。
したがってこの作品の出来はどんな文字列を表示するかに大きく依存してしまう。
結果pythonでの一発芸になってしまい、PSoCの良いところを上手く活かせていない。

他の例
(Oh-meijiで自分の時間割をスクレイピングし、現在の時刻から授業名を取得、表示する)

参考にしたラボ:uart_1

制作にあたって以下のドキュメントを参照した。

  • 最終更新:2017-07-18 10:57:47

このWIKIを編集するにはパスワード入力が必要です

認証パスワード