わいずふぁくとりいがプログラムの話題をウィキします。

(ファイルトランスポータプロトコル - 2)

 デバイス(ESP32)側(EspFileTransporter)の実装を掲載します。

1
EspFileTransporter.ino
FileTransporter.h
FTDemo.ino

2
FTServer.ino
FTSession.ino

3
FTReceive.ino
FTSender.ino
FTCommand.ino
Timer.h
Timer.ino

EspFileTransporter.ino

// EspFileTransporter
//  2020.10.17 - 2021.01.16 ysfactory

#include "FileTransporter.h"
#include "Timer.h"
#include <WiFi.h>
#include <time.h>
#include <limits.h>
#include <list>

using namespace std;

const char* ssid     = "08318BF6DAAC-2G";
const char* password = "55401109907521";

void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200);
    delay(500);
    Serial.println("FileTranporter started.");

    Serial.print("WiFi Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    FTServer_setup();
    timer_setup();
    FTDemo_setup();
}

void loop() {
  // put your main code here, to run repeatedly:
    timer_loop();
    FTServer_loop();
    FTSession_loop();
    FTDemo_loop();
}

FileTransporter.h

// FileTransporter.h
//  2020.10.24 - 2020.11.06 ysfactory

#pragma once
#include <WiFi.h>

#define FT_RESPONSE_OK      1
#define FT_RESPONSE_ERROR   (-1)
#define FT_RESPONSE_TIMEOUT (-2)

class ClientPath
{
public:
    WiFiClient client;
    std::string path;
};

FTDemo.ino

// FTDemo
//  2020.11.03 - 2021.01.16 ysfactory

const int DEMO_CYCLE = 10 * 10;
const char* const DEMO_PATH = "path_a";
const int JST = 3600 * 9;

ClientPath demo_client;
struct tm demo_timeinfo;
int FTDemo_step = 0;  
    
void FTDemo_setup(void)
{
    //NTPから時刻を取得する、ための準備。 
    configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");  
    //Serial.println("FTDemo: setup");
}

void FTDemo_step_0(void)
{
    //Serial.println("FTDemo: setep 0");
    Timer100ms[TIMER_DEMO] = DEMO_CYCLE;
    ++FTDemo_step;
}

void FTDemo_step_1(void)
{
    if( Timer100ms[TIMER_DEMO] > 0 )
    {
        return;
    }

    //Serial.println("FTDemo: setep 1");
    if( ! FTServer_GetClient(DEMO_PATH, &demo_client) )
	  {
      Serial.println("FTDemo: no client!");
      FTDemo_step = 0;
      return;
	  }

    //時刻を取得する。
    if( !getLocalTime(&demo_timeinfo) )
    {
      Serial.println("getLocalTime error");
    }
    //Serial.println(&demo_timeinfo, "%Y/%m/%d %H:%M:%S");

    //ファイル名を作る。
    char filename[30];
    strftime(filename, 30, "%Y-%m-%d.txt", &demo_timeinfo);
    //Serial.println(filename);

    //appendコマンド送信。
    FTSession_AppendStart(filename, demo_client);
    ++FTDemo_step;
}

void FTDemo_step_2(void)
{
    if( FTSession_IsBusy() )
    {
        return;
    }
    //Serial.println("FTDemo: setep 2");
    if( FTSession_GetStatus() != FT_RESPONSE_OK )
    {
        Serial.println("FTDemo: append command error!");
        FTDemo_step = 0;
        return;
    }

    //記録データを作る。
    char data[30];
    strftime(data, 30, "%Y/%m/%d %H:%M:%S\r\n", &demo_timeinfo);
    Serial.print(data);

    //データ送信。
    FTSession_BinaryStart((const unsigned char*)data, strlen(data), demo_client);
    ++FTDemo_step;
}

void FTDemo_step_3(void)
{
    if( FTSession_IsBusy() )
    {
        return;
    }
    if( FTSession_GetStatus() != FT_RESPONSE_OK )
    {
        Serial.println("FTDemo: binary data error!");
        FTDemo_step = 0;
        return;
    }
    //Serial.println("FTDemo: setep 3");

    //closeコマンド送信。
    FTSession_CloseStart(DEMO_PATH, demo_client);
    ++FTDemo_step;
}

void FTDemo_step_4(void)
{
    if( FTSession_IsBusy() )
    {
        return;
    }
    if( FTSession_GetStatus() != FT_RESPONSE_OK )
    {
        Serial.println("FTDemo: append command error!");
        FTDemo_step = 0;
        return;
    }
    //Serial.println("FTDemo: setep 4");

    //終了。
    FTDemo_step = 0;
}

void FTDemo_loop(void)
{
    switch(FTDemo_step)
    {
    case 0:
        FTDemo_step_0();
        break;  
    case 1:
        FTDemo_step_1();
        break;  
    case 2:
        FTDemo_step_2();
        break;  
    case 3:
        FTDemo_step_3();
        break;  
    case 4:
        FTDemo_step_4();
        break;  
    }
}

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu

管理人/副管理人のみ編集できます