LoginSignup
4
7

More than 3 years have passed since last update.

2016-05-21 ESP8266 > udpLogger (with AccessPoint) > UDP受信文字列をSDに書込む

Last updated at Posted at 2016-05-21
動作確認
ESP-WROOM-02 + ユニバーサル基板実装

ユニバーサル基板は以下で実装したものを使用。
http://qiita.com/7of9/items/280758359f3d2c0c7fe8

実装内容

  • Access Pointとなる
  • UDPで受信した文字列をSDに追記する

pythonで実装した以下のものをESP8266+microSDで行おうとしている。
http://qiita.com/7of9/items/0f74ba674fa8dfe02a04

code v0.1

v0.1 @ github

esp8266_160521_udpLoggerWithAccessPoint.ino
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <SPI.h>
#include <SD.h>

/*
 * v0.1 2016 May 21
 *   - add SD_write()
 *   - add SD_setup()
 *   - add softAPConfig() to set IP address
 *   - processUdpReceive() does not have UDP echo back
 * ===== below as [eps8266_151230_udpEchoWithAccessPoint] =====
 * v0.2 2015 Dec. 30
 *   - add processUdpReceive()
 *   - WiFi_setup() has myWifiUDP.begin()
 *   - add kLocalPort
 *   - add receivedBuffer[]
 *   - include WiFiUDP.h
 * v0.1 2015 Dec. 30
 *   - add Serial_setup()
 *   - add WiFi_setup()
 *   - add handleRoot()
 *   - add loop()
 *   - add setup()
 */

// Access point related
static const char *kSsid = "esp8266";
static const char *kPassword = "12345678";

// UDP realated
static WiFiUDP myWifiUDP;
static char receivedBuffer[255];
static const int kLocalPort = 7000;

// SD related
#define SD_CS (4)
static File s_myFile;

bool SD_setup()
{
    Serial.print("Initializing SD card...");

    if (!SD.begin(SD_CS)) {
      Serial.println("initialization failed!");
      return false;
    }
    Serial.println("initialization done.");
    return true;
}

static void WiFi_setup()
{
    WiFi.softAPConfig(IPAddress(192, 168, 79, 2), IPAddress(192, 168, 79, 1), IPAddress(255, 255, 255, 0));
    WiFi.softAP(kSsid, kPassword);

    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
    myWifiUDP.begin(kLocalPort);
}

static void Serial_setup()
{
    Serial.begin(115200);
    Serial.println();
    Serial.print("Configuring access point...");  
}

void setup() {
    pinMode(4, OUTPUT);
    delay(1000);

    Serial_setup();
    WiFi_setup();
    SD_setup();
}

void SD_write(char *rcvdPtr)
{
    s_myFile = SD.open("test.txt", FILE_WRITE);

    if (s_myFile) {
        Serial.print("Writing to test.txt...");
        s_myFile.println(rcvdPtr);
        s_myFile.close();
        Serial.println("done.");
    } else {
        Serial.println("error opening test.txt");
    }
}


void processUdpReceive()
{
    int rcvdSize = myWifiUDP.parsePacket();
    if (rcvdSize == 0) {
        delay(100);
        return;
    }

    Serial.println("received");

    int len = myWifiUDP.read(receivedBuffer, 255);
    if (len == 0) {
        return;
    }
    receivedBuffer[len] = 0x00;
    SD_write(receivedBuffer);
}

void loop() {
    processUdpReceive();
}

実行

UDP送信側

androidスマホ 304SHを使用。
Unityで作ったudpSenderで文字列をUDP送信する ( udpSender @ github )。

手順
1. ESP8266側を起動
2. 304SHでESP8266のAPに接続 (パスワード12345678)
3. UnityソフトudpSenderで文字列をIP:192.168.79.2、UDPポート7000に送信 (例: "from 304SH")

結果

以下のファイルが作成できた。

TEST.txt
from 304SH

改行が1つ多いが今後修正とする。

udpSenderソフトはESP8266からの応答がないのでホールトしてしまうが、目的は達成した。

304SHからの送信の代わりにMacのターミナルから送信してもいい。
http://qiita.com/7of9/items/0fbea085ddb6bbadc9c2

今後

borgSensorSystem1としての今後としては、センサの通信プロトコルをきちんと定義して、複数のセンサーデータを整理してmicroSDに書込みしたい。

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7