当面の目標


皮膚温と皮膚電位を同時に計測

  • Wifiに接続して,現在時刻を取得し,皮膚温と皮膚電位を同時に計測できるようにした。
#include <M5StickC.h>
#include <WiFi.h>
#include <WiFiUDP.h>
#include "time.h"

const char* ssid       = "XXXXXXXXXXX"; //ssidは秘密
const char* password   = "XXXXXXXXXXX"; //passwordも秘密
const char* ntpServer =  "ntp.jst.mfeed.ad.jp";
const char* saddr = "192.168.XXX.XXX"; //受け手の端末のIPアドレス
const int sport = 55999; //受け手の端末のポート番号 ポート番号は適当
const int kport = 5556;  //M5StickCのポート番号 ポート番号は適当

RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;

int PIN = 36;
uint8_t mac3[6];

WiFiUDP Udp;
char packetBuffer[255];


void setup() {
  // put your setup code here, to run once:
  M5.begin();
  M5.Lcd.setRotation(3);
  M5.Lcd.fillScreen(BLACK);
  pinMode(PIN, INPUT);
  esp_read_mac(mac3, ESP_MAC_WIFI_STA);
  M5.Lcd.setTextSize(1);
  M5.Lcd.setCursor(1, 0, 2);
  M5.Lcd.println("Welcome! \n If the connection fails, turn off and on the power again.");
 
  // connect to WiFi
  M5.Lcd.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.print(".");
  }

  M5.Lcd.println("CONNECTED");
  delay(1000);

  // Set ntp time to local
  configTime(9 * 3600, 0, ntpServer);
  // Get local time
  struct tm timeInfo;
  if (getLocalTime(&timeInfo)) {
    // Set RTC time
    RTC_TimeTypeDef TimeStruct;
    TimeStruct.Hours   = timeInfo.tm_hour;
    TimeStruct.Minutes = timeInfo.tm_min;
    TimeStruct.Seconds = timeInfo.tm_sec;
    M5.Rtc.SetTime(&TimeStruct);
 
    RTC_DateTypeDef DateStruct;
    DateStruct.WeekDay = timeInfo.tm_wday;
    DateStruct.Month = timeInfo.tm_mon + 1;
    DateStruct.Date = timeInfo.tm_mday;
    DateStruct.Year = timeInfo.tm_year + 1900;
    M5.Rtc.SetData(&DateStruct);
  }
  M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 1, 2);
    M5.Lcd.print("Connected \nto ");
    M5.Lcd.println(WiFi.localIP());
   uint8_t mac3[6];
   esp_read_mac(mac3, ESP_MAC_WIFI_STA);
  M5.Lcd.printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5]);
  Udp.begin(kport); 
  
}

void loop() {
  int mil = millis(); 
  // put your main code here, to run repeatedly:
  M5.Rtc.GetTime(&RTC_TimeStruct);
  M5.Rtc.GetData(&RTC_DateStruct);
  M5.Lcd.setCursor(0, 50);
  M5.Lcd.printf("%04d-%02d-%02d ", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date);
//  M5.Lcd.printf("millis: %6d\n",mil);
  M5.Lcd.printf("%02d:%02d:%02d\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
//  Serial.printf("%04d-%02d-%02d (%s) %02d : %02d : %02d\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

  int val = analogRead(33);
  M5.Lcd.printf("GSR: %4d ", val);

  float voutT = (float)analogRead(PIN) / 4095.0 * 3.6 + 0.1132;
  float tp = voutT / 0.01;
  M5.Lcd.printf("temp: %4.1f'C\n", tp);
  //UDP
  Udp.beginPacket(saddr, sport);
  Udp.printf("[%02X:%02X:%02X:%02X:%02X:%02X]%04d-%02d-%02d %02d:%02d:%02d, %6d, %4d, %4.1f", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5], RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds, mil, val, tp);
  Udp.endPacket();  
  delay(100);
}


ここで問題発生


2020/1/2現在の完成品

GSRと皮膚温のArduino IDEのスケッチ

#include <M5StickC.h>
#include <WiFi.h>
#include <WiFiUDP.h>
#include "time.h"

const char* ssid       = "ssid";
const char* password   = "password";
const char* ntpServer =  "ntp.jst.mfeed.ad.jp";
//const char* saddr = "XXX.XXX.XXX.XXX"; //これはおおうち
const char* saddr = "XXX.XXX.XXX.XXX"; //taeget PC address
const int sport = 55999;
const int kport = 5556;

RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;

int PIN = 36;
uint8_t mac3[6];

WiFiUDP Udp;
char packetBuffer[255];


void setup() {
  // put your setup code here, to run once:
  M5.begin();
  M5.Lcd.setRotation(3);
  M5.Lcd.fillScreen(BLACK);
  pinMode(PIN, INPUT);
  esp_read_mac(mac3, ESP_MAC_WIFI_STA);
  M5.Lcd.setTextSize(1);
  M5.Lcd.setCursor(1, 0, 2);
  M5.Lcd.println("Welcome \n If the connection fails, turn off and on the power again.");
 
  // connect to WiFi
  M5.Lcd.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.print(".");

  }
  M5.Lcd.println(" CONNECTED");
//  Serial.println(" CONNECTED");
  delay(1000);

  // Set ntp time to local
  configTime(9 * 3600, 0, ntpServer);
  // Get local time
  struct tm timeInfo;
  if (getLocalTime(&timeInfo)) {
    // Set RTC time
    RTC_TimeTypeDef TimeStruct;
    TimeStruct.Hours   = timeInfo.tm_hour;
    TimeStruct.Minutes = timeInfo.tm_min;
    TimeStruct.Seconds = timeInfo.tm_sec;
    M5.Rtc.SetTime(&TimeStruct);
 
    RTC_DateTypeDef DateStruct;
    DateStruct.WeekDay = timeInfo.tm_wday;
    DateStruct.Month = timeInfo.tm_mon + 1;
    DateStruct.Date = timeInfo.tm_mday;
    DateStruct.Year = timeInfo.tm_year + 1900;
    M5.Rtc.SetData(&DateStruct);
  }
  M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 1, 2);
    M5.Lcd.print("Connected \nto ");
    M5.Lcd.println(WiFi.localIP());
   uint8_t mac3[6];
   esp_read_mac(mac3, ESP_MAC_WIFI_STA);
  M5.Lcd.printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5]);
  Udp.begin(kport); 
  
}

void loop() {
  int mil = millis(); 
  // put your main code here, to run repeatedly:
  M5.Rtc.GetTime(&RTC_TimeStruct);
  M5.Rtc.GetData(&RTC_DateStruct);
  M5.Lcd.setCursor(0, 50);
  M5.Lcd.printf("%04d-%02d-%02d ", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date);
//  M5.Lcd.printf("millis: %6d\n",mil);
  M5.Lcd.printf("%02d:%02d:%02d\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
//  Serial.printf("%04d-%02d-%02d (%s) %02d : %02d : %02d\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

  int val = analogRead(33);
  M5.Lcd.printf("GSR: %4d ", val);

  float voutT = (float)analogRead(PIN) / 4095.0 * 3.6 + 0.1132;
  float tp = voutT / 0.01;
  M5.Lcd.printf("temp: %4.1f'C\n", tp);
  //UDP
  Udp.beginPacket(saddr, sport);
  Udp.printf("gsrtemp![%02X:%02X:%02X:%02X:%02X:%02X], %04d-%02d-%02d %02d:%02d:%02d, %6d, %4d, %4.1f", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5], RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds, mil, val, tp);
  Udp.endPacket();  
  delay(100);
}

加速度センサのArduino IDEのスケッチ

#include <M5StickC.h>
#include <WiFi.h>
#include <WiFiUDP.h>
#include "time.h"

const char* ssid       = "ssid";
const char* password   = "password";
const char* ntpServer =  "ntp.jst.mfeed.ad.jp";
//const char* saddr = "XXX.XXX.XXX.XXX"; //これはおおうち
const char* saddr = "XXX.XXX.XXX.XXX"; //taeget PC address

const int sport = 55998;
const int kport = 5556;

RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;

float accX = 0.0F;
float accY = 0.0F;
float accZ = 0.0F;

uint8_t mac3[6];

WiFiUDP Udp;
char packetBuffer[255];


void setup() {
  // put your setup code here, to run once:
  M5.begin();
  M5.Lcd.setRotation(3);
  M5.Lcd.fillScreen(BLACK);

  M5.IMU.Init();
  
  esp_read_mac(mac3, ESP_MAC_WIFI_STA);
  M5.Lcd.setTextSize(1);
  M5.Lcd.setCursor(1, 0, 2);
  M5.Lcd.println("Welcome \n If the connection fails, turn off and on the power again.");
 
  // connect to WiFi
  M5.Lcd.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.print(".");

  }
  M5.Lcd.println("CONNECTED");
//  Serial.println("CONNECTED");
  delay(1000);

  // Set ntp time to local
  configTime(9 * 3600, 0, ntpServer);
  // Get local time
  struct tm timeInfo;
  if (getLocalTime(&timeInfo)) {
    // Set RTC time
    RTC_TimeTypeDef TimeStruct;
    TimeStruct.Hours   = timeInfo.tm_hour;
    TimeStruct.Minutes = timeInfo.tm_min;
    TimeStruct.Seconds = timeInfo.tm_sec;
    M5.Rtc.SetTime(&TimeStruct);
 
    RTC_DateTypeDef DateStruct;
    DateStruct.WeekDay = timeInfo.tm_wday;
    DateStruct.Month = timeInfo.tm_mon + 1;
    DateStruct.Date = timeInfo.tm_mday;
    DateStruct.Year = timeInfo.tm_year + 1900;
    M5.Rtc.SetData(&DateStruct);
  }
  M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 1, 2);
    M5.Lcd.print("Connected \nto ");
    M5.Lcd.println(WiFi.localIP());
   uint8_t mac3[6];
   esp_read_mac(mac3, ESP_MAC_WIFI_STA);
  M5.Lcd.printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5]);
  Udp.begin(kport); 
  
}

void loop() {
  int mil = millis(); 
  // put your main code here, to run repeatedly:
  M5.Rtc.GetTime(&RTC_TimeStruct);
  M5.Rtc.GetData(&RTC_DateStruct);
  M5.Lcd.setCursor(0, 50);
  M5.Lcd.printf("%04d-%02d-%02d ", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date);
//  M5.Lcd.printf("millis: %6d\n",mil);
  M5.Lcd.printf("%02d:%02d:%02d\n", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
//  Serial.printf("%04d-%02d-%02d (%s) %02d : %02d : %02d\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

  // 加速度データ取得
  M5.IMU.getAccelData(&accX, &accY, &accZ);
  M5.Lcd.printf("%5.2f, %5.2f, %5.2f\n", accX, accY, accZ);

  //UDP
  Udp.beginPacket(saddr, sport);
  Udp.printf("acc![%02X:%02X:%02X:%02X:%02X:%02X], %04d-%02d-%02d %02d:%02d:%02d, %6d, %5.2f, %5.2f, %5.2f", mac3[0], mac3[1], mac3[2], mac3[3], mac3[4], mac3[5], RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds, mil, accX, accY, accZ);
  Udp.endPacket();  
  delay(100);
}

2つのM5StickCの測定結果からCSVファイルを出力するProcessingのスケッチ

スケッチ→ライブラリをインポート→ライブラリを追加→UDPを追加

// import UDP library
import hypermedia.net.*;


UDP udp1;  // define the UDP object
UDP udp2;  // define the UDP object

PrintWriter output1;
PrintWriter output2;
PrintWriter output3;
  int loga = 0;
  int logb = 0;
  String mesa;
  String mesb;


void setup() {
  String filename1 = nf(year(),2)+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+nf(second(),2)+"_log1.csv";
  String filename2 = nf(year(),2)+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+nf(second(),2)+"_log2.csv";
  String filename3 = nf(year(),2)+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+nf(second(),2)+"_log3.csv";

  output1 = createWriter(filename1);
  output2 = createWriter(filename2);
  output3 = createWriter(filename3);

  udp1 = new UDP( this, 55998 );
  udp1.listen( true );
  udp2 = new UDP( this, 55999 );
  udp2.listen( true );

}

//process events
void draw() {;}


void keyPressed() {
    
    String message  = str( key );   // the message to send
    String ip       = "XXX.XXX.XXX.XXX";    // the remote IP address
    int port1        = 55998;       // the destination port
    int port2        = 55999;    // the destination port
    

    message = message+";\n";
    udp1.send( message, ip, port1 );
    udp2.send( message, ip, port2 );

      if( key == 'e' ){
  output1.flush();
  output1.close();
  output2.flush();
  output2.close();
  output3.flush();
  output3.close();
  delay(1000);
  exit();
  }

}



void receive( byte[] data, String ip, int port ) {  // <-- extended handler
  
  String message = new String( data );
  println( "receive: \""+message+"\" from "+ip+" on port "+port );
  
  // print the result
  String dmessage[] = message.split("!", 0);
  if( dmessage[0].length() == 3 ){
  output1.println(dmessage[1]);
  mesa = dmessage[1];
  loga += 1;
  }else{
  output2.println(message);
  mesb = message;
  logb += 1;
}
  if(loga > 0 && logb > 0){
    output3.println(mesa + ", " + mesb);
    loga = 0;
    logb = 0;
  }
}
.
デバイス Processing

.

出力例