5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MilkcocoaをESP32(NefryBT)で使ってみる

Last updated at Posted at 2018-08-22

Nefry BTでMilkcocoa使ってみたいという声があったので試してみました。

前提としてESP32向けのMilkcocoaライブラリが存在しないのでESP8266向けのライブラリをいじって試してみます。

前半は@306_sanさんの記事をもとに試してみています。

参考: ESP32(ESP-WROOM-32)でMilkcocoaSDKを使う

MilkcocoaのESP8266ライブラリ

https://github.com/milk-cocoa/Milkcocoa_ESP8266_SDK からダウンロードしてArduino IDEで読み込みます。

ライブラリの中を変更

/Users/foo/Documents/Arduino/libraries/Milkcocoa_Arduino_SDK-master/Adafruit_MQTT.cpp

# include "include/Adafruit/Adafruit_MQTT.h"

# if defined(ARDUINO_ARCH_AVR)
# define strncasecmp strncasecmp_P
# define strncpy strncpy_P
# else
//#include "avr/dtostrf.h" //=>ここをコメントアウト
# endif

Nefry BTで使ってみる

大元は部谷さんの記事のコードです。

milkcocoa.ino
# include <Milkcocoa.h>
# include <WiFi.h>

# define MILKCOCOA_APP_ID      "APPID"
# define MILKCOCOA_DATASTORE   "DATASTORE_NAME"
# define MILKCOCOA_SERVERPORT  1883

const char MQTT_SERVER[] PROGMEM    = MILKCOCOA_APP_ID ".mlkcca.com";
const char MQTT_CLIENTID[] PROGMEM  = __TIME__ MILKCOCOA_APP_ID;

WiFiClient client;
Milkcocoa milkcocoa = Milkcocoa(&client, MQTT_SERVER, MILKCOCOA_SERVERPORT, MILKCOCOA_APP_ID, MQTT_CLIENTID);

void setup(){
  Serial.begin(115200);
}

void loop(){
  // 以下をloopの中で必ず実行します
  milkcocoa.loop();

  // データ格納用のオブジェクトです
  DataElement elem = DataElement();

  // 以下のようにkey, valueを設定していきます。
  elem.setValue("name", "Milk");
  elem.setValue("age", 35);

  // データストア名を指定して、データをpushもしくはsendします
  //milkcocoa.push(MILKCOCOA_DATASTORE, &elem);
  // or
  milkcocoa.send(MILKCOCOA_DATASTORE, &elem);
}

ここでは

# define MILKCOCOA_APP_ID      "APPID"
# define MILKCOCOA_DATASTORE   "DATASTORE_NAME"

の2箇所を各自のものに修正します。

フロントのJSで確認

index.html
<html>
    <script src="https://cdn.mlkcca.com/v0.6.0/milkcocoa.js"></script>
    <script>
        var milkcocoa = new MilkCocoa('APPID.mlkcca.com');
        var ds = milkcocoa.dataStore('DATASTORE');
        // setInterval(()=>{
        //     ds.push({v:1}, (err, sended) => {
        //         console.log('送信:', sended);
        //     });
        // },2000);

        ds.on('send', (send) => {
            console.log(send.value);
        });

    </script>
</html>

Node.jsで受け取る

npm i milkcocoa
send.js
'use strict'

const MilkCocoa = require('milkcocoa');
const milkcocoa = new MilkCocoa('APPID.mlkcca.com');
const ds = milkcocoa.dataStore('DATASTORE');

ds.on('send', (err, send) => {
  console.log(err,send);
});

このコードでNefry BTから送られてくるSendメッセージを受信できます。

onpushとonsendが...

試そうとするとNefry BTが無限ループになってしまいました。
再度調べて試してみよう。

5
1
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
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?