山本?

組み込みセンサで無線通信 

1.ハード構築
・Arduinoとe-healthとwifiモジュールを接続する
・Arduinoに脈波センサと体温センサを接続しPCと接続
※ハードの画像
接続方法が分からない場合は以下のURLを参照
e-healthのチュートリアル
https://www.cooking-hacks.com/documentation/tutorials/ehealth-biometric-sensor-platform-arduino-raspberry-pi-medical
wifiモジュールのチュートリアル
https://www.cooking-hacks.com/blog/tutorial-wifi-module-for-arduino-roving-rn-xvee

2.PC内の環境構築
・ArduinoIDEのインストール 以下のURLからArduinoIDEのver1.0.5をそれぞれのPCに合わせた環境でインストールする
※既に別verでのIDEをインストールしている場合は追加でインストールする(アンインストールする必要はない)
https://www.arduino.cc/en/Main/OldSoftwareReleases#1.0.x

・e-healthライブラリの導入 以下のURLのUsing the library with Arduinoの項のV2.4のライブラリをダウンロード
https://www.cooking-hacks.com/documentation/tutorials/ehealth-biometric-sensor-platform-arduino-raspberry-pi-medical
その後解凍し,\Documents\Arduino\librariesに解凍したものを入れてArduinoIDEを起動する
起動するとスケッチのユーザ提供にeHealthとPinChangeIntというライブラリが追加されていればOK

ide.png



  1. xamppのインストール
    xamppとはApacheでサーバーを立てたりMySQLを使用できたりする便利なソフト
    https://www.apachefriends.org/jp/index.html
    上記のURLからそれぞれの環境に合わせてxamppをインストール
    インストールで不明な点があったら以下のURLを参考にしてください
    https://www.adminweb.jp/xampp/install/index1.html
  2. xamppの設定変更
    C:\xamppの位置にxampp-control.exeというものがあるのでそちらを起動,下記の画像のような画面が表示される
    xammp.png
    configからApache(http.conf)を選択すればApacheの設定ファイルの中身が表示される
    ・ローカルIP設定
    Apacheの設定ファイルの58行目に
    Listen 80
    とあるので
    Listen 8080
    と書き換えておく
    Apacheの設定ファイルの221行目に
    ServerName localhost:80
    とあるので
    ServerName localhost:8080
    と書き換えておく
    ・ディレクトリパス設定
    244・245行あたりの DocumentRoot と Directory のパスを下記のように書き換える
    DocumentRoot "C:/xampp/htdocs/changeroot"
    Directory "C:/xampp/htdocs/changeroot"
    その後にhtdocsの中にchangerootというフォルダを作っておく

  3. wifiモジュールの設定と動作確認
    ArduinoIDEを用いてwifiモジュールを設定する
    まずは下記のコードを実行する
    void setup(){
    }
    void loop(){
    }
    マイコンボードに書き込む際にXbeeシールドのスイッチがUSB側になっているのを確認する
    書き込み後シリアルモニタを起動
    改行なし,9600に設定した後$$$と入力しENTER
    その結果CMDと返答があればコマンドモードに入る
    CMDと返答が来ない場合はUSBの接続,Xbeeシールドのスイッチの位置,改行なしになっているか,9600bpsに設定しているかなどを再び確認する.
    その後"改行なし"から"CFおよびLF"に変更し以下のコマンドを実行,それぞれのコマンドでAOKと返答が返ってくるとok
    set wlan join 0
    set wlan phrase basestation←使用するルータのパスワード
    join AirMacTimeCapsule←使用するルータのIPアドレス
    set dns address 192.168.0.1
    set ip host 192.168.0.103←自分のPCのIPアドレスに変更
    set ip gateway 192.168.0.1
    set ip address 192.168.0.70
    set ip remote 8080
    set opt format 1
    get ip
    save
    コマンド実行後以下の画像のように表示されていればok
    ※実行結果の画像
    ※注意する点
    set ip host ←PC側のIP
    set ip adress ←モジュール側
    IF=UPになっていることを確認する,IF=DOWNになっていると通信が行えないので必ず確認しておく

プログラムの実行 

  1. ArduinoIDEのコード(ファイル名はなんでもよい)
    #include <PinChangeInt.h>
    #include <eHealth.h>
    //Enter here your data
    const char wifi_ssid[] = "AirMacTimeCapsuleKO";←用いるルータのSSID
    const char wifi_password[] = "basestation";←用いるルータのパスワード
    const char host[] = "192.168.0.103";←用いるPCのIP
    const char gateway[] = "192.168.0.1";
    const char server[] = "192.168.0.70";
    const char host_port[] = "8080";
    //const char GET[] = "test.php?Temperature=20&PRbpm=45";
    const char GET[] = "test.php?Temperature=20&PRbpm=45";
    int8_t answer;
    char response[300];
    char response2[100];
    int cont;
    char aux_str[100];
    int s=1;
    
    char value[50];
    
    void setup()
    {
      //Write here you correct baud rate
      Serial.begin(9600);
      //Serial.begin(115200);
     
      wificonfig();
        eHealth.initPulsioximeter();
    
      //Attach the inttruptions for using the pulsioximeter.
      PCintPort::attachInterrupt(6, readPulsioximeter, RISING);
    }
    
    void loop()
    {
     
      Serial.println(F("Sending HTTP GET"));
      
      delay(5000);
      while(s>0){
      Serial.print("PRbpm : ");
      Serial.print(eHealth.getBPM());
    
      Serial.print("    %SPo2 : ");
      Serial.print(eHealth.getOxygenSaturation());
    
      Serial.print("\n");
      Serial.println("=============================");
      delay(500);
      
      float temperature = eHealth.getTemperature();
    
      Serial.print("Temperature (ºC): ");
      Serial.print(temperature, 2);
      Serial.println("");
      
      sendGET(temperature,eHealth.getBPM());
      delay(1000);	// wait for a second
      }
    }
    
    
    
    void wificonfig() {
     
      Serial.println(F("Setting Wifi parameters"));
      sendCommand("exit\r","EXIT",2000);
      delay(2000);
     
      enterConfig(2000);
      sendCommand("leave\r","DeAuth",2000);
      delay(1000);
      // Sets DHCP and TCP protocol
      sendCommand("set ip dhcp 1\r","AOK",2000);
      delay(1000);   
      sendCommand("set ip protocol 18\r","AOK",2000);
      delay(1000); 
    
      // Configures the way to join the network AP, sets the encryption of the
      // network and joins it
      sendCommand("set wlan join 0\r","AOK",2000); //The auto-join feature is  disabled
      delay(1000);
    
      snprintf(aux_str, sizeof(aux_str), "set wlan phrase %s\r", wifi_password);
      sendCommand(aux_str,"AOK",2000);
      delay(1000);   
      snprintf(aux_str, sizeof(aux_str), "join %s\r", wifi_ssid);
      answer = sendCommand(aux_str,"Associated",10000);
    
    
      if (answer == 1){
    
        snprintf(aux_str, sizeof(aux_str), "Connected to \"%s\"", wifi_ssid);
        Serial.println(aux_str);
        delay(5000);
      } 
    
      else {
        snprintf(aux_str, sizeof(aux_str), "Error connecting to: \"%s\"", wifi_ssid);
        Serial.println(aux_str);
        delay(1000);
      }
    
    
      Serial.println(F("Wifi succesfully configured"));
      delay(1000);
    }
    
    
    
    void sendGET(float Temperature,int PRbpm) {
     
      enterConfig(2000);
      //sendCommand("set i h 0\r","AOK",2000);
      delay(1000);
      //snprintf(aux_str, sizeof(aux_str), "set d n %s\r", server);
      //sendCommand(aux_str,"AOK",2000);
      delay(1000);
      snprintf(aux_str, sizeof(aux_str), "set dns address %s\r", gateway);
      sendCommand(aux_str,"AOK",2000);
      delay(1000);
      snprintf(aux_str, sizeof(aux_str), "set ip host %s\r", host);
      sendCommand(aux_str,"AOK",2000);
      delay(1000);
      snprintf(aux_str, sizeof(aux_str), "set ip gateway %s\r", gateway);
      sendCommand(aux_str,"AOK",2000);
      
      delay(1000);
      snprintf(aux_str, sizeof(aux_str), "set ip address %s\r", server);
      sendCommand(aux_str,"AOK",2000);
      delay(1000);
      //Configures HTTP connection
      snprintf(aux_str, sizeof(aux_str), "set ip remote %s\r", host_port);
      sendCommand(aux_str,"AOK",2000);
      delay(1000); 
    
      sendCommand("set opt format 1\r","AOK",2000);
      delay(1000);
      
      dtostrf(Temperature,5,2,value);
      
      snprintf(aux_str, sizeof(aux_str), "set comm remote GET$/test.php?Temperature=%s&PRbpm=%d\r", value,PRbpm);
      sendCommand(aux_str,"AOK",10000);
      delay(1000);
    
      // Calls open to launch the configured connection.
      sendCommand("open\r","*CLOS*",10000);
      delay(1000);
    
      findResponse();
    
    }
    
    
    void findResponse(){
      Serial.println(response);
      boolean go_On = true;
      uint16_t counter = 0;
    
      while (go_On){
    
        if (response[counter] == '\r'){
    
          if (response[counter+1] == '\n'){
    
            if (response[counter+2] == '\r'){
    
              if (response[counter+3] == '\n'){
    
                go_On = false;
              }
    
            }   
    
          }      
    
        }
    
        counter++;
      }
      counter = counter + 3;
    
      for (int i=0;response[i+counter]!='*'; i++){
        response2[i] = response[i+counter];
        delay(100);
    
    
      }
    
      snprintf(aux_str, sizeof(aux_str), "GET response:\"%s\"", response2);
      Serial.println(aux_str);
    
    
    }
    
    
    int8_t sendCommand(const char* Command, const char* expected_answer, unsigned int timeout){
    
      uint8_t x=0,  answer=0;
      unsigned long previous;
    
      memset(response, 0,300);    // Initialize the string
    
      delay(100);
    
      while( Serial.available() > 0) Serial.read();    // Clean the input buffer
    
      Serial.println(Command);    // Send Command
    
        x = 0;
      previous = millis();
    
      // this loop waits for the answer
      do{
        if(Serial.available() != 0){   
          // if there are data in the UART input buffer, reads it and checks for the asnwer
          response[x] = Serial.read();
          x++;
          // check if the desired answer  is in the response of the module
          if (strstr(response, expected_answer) != NULL)   
          {
            answer = 1;
          }
        }
      }
      // Waits for the asnwer with time out
      while((answer == 0) && ((millis() - previous) < timeout));   
    
      return answer;
    }
    
    
    int8_t enterConfig(unsigned int timeout){
    
      uint8_t x=0,  answer=0;
      unsigned long previous;
    
      memset(response, 0,300);    // Initialize the string
    
      delay(100);
    
      while( Serial.available() > 0) Serial.read();    // Clean the input buffer
    
      Serial.print("$$$");    // Send Command
    
    
      x = 0;
      previous = millis();
    
      // this loop waits for the answer
      do{
        if(Serial.available() != 0){   
          // if there are data in the UART input buffer, reads it and checks for the asnwer
          response[x] = Serial.read();
          x++;
          // check if the desired answer  is in the response of the module
          if (strstr(response, "CMD") != NULL)   
           {
            answer = 1;
          }
        }
      }
      // Waits for the asnwer with time out
      while((answer == 0) && ((millis() - previous) < timeout));   
    
      return answer;
    }
    
    void readPulsioximeter(){
    
      cont ++;
    
      if (cont == 50) { //Get only of one 50 measures to reduce the latency
        eHealth.readPulsioximeter();
        cont = 0;
      }
    }	 
  2. php側の記述(ファイル名test.php)
    ファイルの保存場所 C:\xampp\htdocs\changeroot
    <?php
       $filename = "data.txt";
       $data1 = $_GET['Temperature'];
       $data2 = $_GET['PRbpm'];
       if($data1=='a'){
       $fp = fopen($filename, "r");
       $contents = fread($fp, filesize($filename));
       print($contents);
       fclose($fp);
     }
       else{
       $fp = fopen($filename, 'a');
       date_default_timezone_set('Asia/Tokyo');
       fwrite($fp,  date("Y-m-d H:i:s")."\t".$data1."\t".$data2."\r\n");
       fclose($fp);
     }
    ?>
  3. 実行
    ①xamppでapacheをstartで起動
    ②次にArduinoIDEで書き込みを行う,書き込みを行う際はXveeのシールドのスイッチがUSBになっていることを必ず確認する.書き込みを終えた後はスイッチをXveeに切り替えておく
    ③シリアルモニタを起動,念のためにRESETボタンを一度押す,以下シリアルモニタの実行結果
    ※実行結果画像
    ④C:\xampp\htdocs\changerootファルダを確認しdata.txtファイルが作成されていることを確認する
    その中身が以下の画像のように時刻,センサ1の値,センサ2の値となっているならok
    data.png
    ⑤ブラウザ側での結果確認
    ブラウザのURLに
    localhost:8080/test.php?Temperature=a&PRbpm=a~
    と入力するとブラウザにdata.txtの結果が表示される

gnuplot 

以下のURLよりgnuplotをインストールする
https://sourceforge.net/projects/gnuplot/files/gnuplot/
C:\Program Files\gnuplot\binよりwgnuplot.exeを起動,以下のコマンドを実行

set xdata time
set timefmt "%H:%M:%S"
set xrange ["16:30:00":"17:00:00"]
set grid
set xlabel 'Time'
set ylabel 'Temperature [deg C]'
set title  'Yamamoto'
p "data.txt" u 2:3 w l lc rgb "#FF0000" notitle,"data.txt" u 2:3 w p pt 4 ps 0.5 notitle

2:3の2は時刻,3は体温を指定しているので3を4に変更すると脈拍のグラフを表示できる
以下実行結果画像

temp.png

参考URL 

e-Health Sensor Platform V2.0 for Arduino and Raspberry Pi
https://www.cooking-hacks.com/documentation/tutorials/ehealth-biometric-sensor-platform-arduino-raspberry-pi-medical
Tutorial: Wifi Module for Arduino "Roving RN-XVee"
https://www.cooking-hacks.com/blog/tutorial-wifi-module-for-arduino-roving-rn-xvee
wifiモジュールのコマンド集
http://www.cooking-hacks.com/skin/frontend/default/cooking/pdf/WiFly-RN-UM.pdf
e-healthに関する質問はこちらのプラットホームで
https://www.cooking-hacks.com/forum/index.php?sid=5acefe808eaee4df9c2a93b60c718a0a
ArduinoIDEの参考にしたコード
https://www.cooking-hacks.com/forum/viewtopic.php?f=20&t=10124&sid=10f3bb6d33397da94b6e5d01e114deb5
xamppの使い方
https://www.adminweb.jp/xampp/
phpの参考サイト
http://nn-hokuson.hatenablog.com/entry/2017/04/09/091148


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS