質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

MQTT

MQTT(Message Queue Telemetry Transport)とは、TCP/IPネットワークで利用可能な通信プロトコルの一つで、IoT/M2M向けに開発された軽量なプロトコルです。ヘッダ部分は最小2バイトと小さく、通信量・CPU負荷・電力消費量などを抑えることができます。

Q&A

解決済

1回答

2227閲覧

取得したデータをMQTTに送りたい。

ysmd

総合スコア17

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

MQTT

MQTT(Message Queue Telemetry Transport)とは、TCP/IPネットワークで利用可能な通信プロトコルの一つで、IoT/M2M向けに開発された軽量なプロトコルです。ヘッダ部分は最小2バイトと小さく、通信量・CPU負荷・電力消費量などを抑えることができます。

0グッド

0クリップ

投稿2019/02/11 07:36

前提・実現したいこと

NTPから取得した時間をMQTTに送れるようにしたいです。送る際は1つの.publishでできれば理想です。
使用エディターはarduno IDEです。
.publish1つでなくともMQTTに送る手段が知りたいです。

加えて、MACアドレスを.publishするときに、分割しないで1つの.publishで送信する手段があればご教授いただきたいです。

よろしくお願いします。

該当のソースコード

/*************************************************** Adafruit MQTT Library ESP8266 Example Must use ESP8266 Arduino from: https://github.com/esp8266/Arduino Works great with Adafruit's Huzzah ESP board & Feather ----> https://www.adafruit.com/product/2471 ----> https://www.adafruit.com/products/2821 Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Tony DiCola for Adafruit Industries. MIT license, all text above must be included in any redistribution ****************************************************/ #include <ESP8266WiFi.h> #include <Adafruit_MQTT.h> #include <Adafruit_MQTT_Client.h> #include <DHT.h> #include <time.h> const int PIN_DHT = 2; DHT dht(PIN_DHT,DHT11); byte mac[6]; #define JST 3600*9 /************************* WiFi Access Point *********************************/ #define WLAN_SSID "Buffalo-G-0150" #define WLAN_PASS "adestu75vkhbk" /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "13.66.159.35" #define AIO_SERVERPORT 1883 // use 8883 for SSL #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." #define AIO_KEY "...your AIO key..." /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // or... use WiFiFlientSecure for SSL //WiFiClientSecure client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); /****************************** Feeds ***************************************/ // Setup a feed called 'photocell' for publishing. // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> Adafruit_MQTT_Publish testPublisher = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); // Setup a feed called 'onoff' for subscribing to changes. //Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff"); /*************************** Sketch Code ************************************/ // Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). void MQTT_connect(); void setup() { Serial.begin(9600); delay(10); Serial.println(F("Adafruit MQTT demo")); // Connect to WiFi access point. Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("WiFi hostname: "); Serial.println(WiFi.hostname()); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Setup MQTT subscription for onoff feed. //mqtt.subscribe(&onoffbutton); configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); } uint32_t x=0; void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); // this is our 'wait for incoming subscription packets' busy subloop // try to spend your time here // Adafruit_MQTT_Subscribe *subscription; // while ((subscription = mqtt.readSubscription(5000))) { // if (subscription == &onoffbutton) { // Serial.print(F("Got: ")); // Serial.println((char *)onoffbutton.lastread); // } // } // Now we can publish stuff! Serial.print(F("\nSending photocell val ")); Serial.print(x); Serial.print("..."); if (! testPublisher.publish(x++)) { Serial.println(F("Failed")); } else { Serial.println(F("OK!")); } float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); String wifi = WiFi.macAddress(); time_t t; struct tm *tm; t = time(NULL); tm = localtime(&t); String hum = "%\t"; String tem = "*C";  //以下のように送信しようとしても送ることが出来ません。 testPublisher.publish("%04d/%02d/%02d %02d:%02d:%02d\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);  //以下のMACアドレスの送信方法を1つの.publishで送信できるようにしたいです。 WiFi.macAddress(mac); testPublisher.publish("MAC: "); testPublisher.publish(mac[5],HEX); testPublisher.publish(":"); testPublisher.publish(mac[4],HEX); testPublisher.publish(":"); testPublisher.publish(mac[3],HEX); testPublisher.publish(":"); testPublisher.publish(mac[2],HEX); testPublisher.publish(":"); testPublisher.publish(mac[1],HEX); testPublisher.publish(":"); testPublisher.publish(mac[0],HEX); testPublisher.publish(temperature); testPublisher.publish(humidity); Serial.printf("%04d/%02d/%02d %02d:%02d:%02d\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec); Serial.println(wifi + ", " + temperature + tem + ", " + humidity + hum); delay(3000); // ping the server to keep the mqtt connection alive // NOT required if you are publishing once every KEEPALIVE seconds /* if(! mqtt.ping()) { mqtt.disconnect(); } */ } // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); Serial.println(ret); mqtt.disconnect(); delay(5000); // wait 3 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); }

ここにより詳細な情報を記載してください。

######以下のように送信しようとしても送ることが出来ません。
testPublisher.publish("%04d/%02d/%02d %02d:%02d:%02d\n",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);

 ######以下のMACアドレスの送信方法を1つの.publishで送信できるようにしたいです。
WiFi.macAddress(mac);
testPublisher.publish("MAC: ");
testPublisher.publish(mac[5],HEX);
testPublisher.publish(":");
testPublisher.publish(mac[4],HEX);
testPublisher.publish(":");
testPublisher.publish(mac[3],HEX);
testPublisher.publish(":");
testPublisher.publish(mac[2],HEX);
testPublisher.publish(":");
testPublisher.publish(mac[1],HEX);
testPublisher.publish(":");
testPublisher.publish(mac[0],HEX);

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ysmd

2019/02/11 08:51

同じような質問内容になってしまっているのは否めません。 ただ、また嵌ってしまったので質問を投稿した次第です。
iwanote

2019/02/11 13:58

本質問の核心はMQTTに関係なく、文字列操作できないことですよね? なにができなくて困っているかを明確にしないと、いつまでもわかるようにならないですよ
ysmd

2019/02/13 07:02

すみません、質問の仕方を大雑把にしすぎてしまいました。
ysmd

2019/02/13 07:02

すみません、質問の仕方を大雑把にしすぎてしまいました。
guest

回答1

0

自己解決

NTPとMACアドレスをMQTTに送れるようになったかもしれません。

/*************************************************** Adafruit MQTT Library ESP8266 Example Must use ESP8266 Arduino from: https://github.com/esp8266/Arduino Works great with Adafruit's Huzzah ESP board & Feather ----> https://www.adafruit.com/product/2471 ----> https://www.adafruit.com/products/2821 Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Tony DiCola for Adafruit Industries. MIT license, all text above must be included in any redistribution ****************************************************/ #include <ESP8266WiFi.h> #include <Adafruit_MQTT.h> #include <Adafruit_MQTT_Client.h> #include <DHT.h> #include <time.h> const int PIN_DHT = 2; DHT dht(PIN_DHT, DHT11); byte mac[6]; #define JST 3600*9 /************************* WiFi Access Point *********************************/ //#define WLAN_SSID "Buffalo-G-0150" //#define WLAN_PASS "adestu75vkhbk" #define WLAN_SSID "m3" #define WLAN_PASS "barhonda006" /************************* Adafruit.io Setup *********************************/ #define AIO_SERVER "13.66.159.35" #define AIO_SERVERPORT 1883 // use 8883 for SSL #define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..." #define AIO_KEY "...your AIO key..." /************ Global State (you don't need to change this!) ******************/ // Create an ESP8266 WiFiClient class to connect to the MQTT server. WiFiClient client; // or... use WiFiFlientSecure for SSL //WiFiClientSecure client; // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); /****************************** Feeds ***************************************/ // Setup a feed called 'photocell' for publishing. // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> Adafruit_MQTT_Publish testPublisher = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell"); // Setup a feed called 'onoff' for subscribing to changes. //Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff"); /*************************** Sketch Code ************************************/ // Bug workaround for Arduino 1.6.6, it seems to need a function declaration // for some reason (only affects ESP8266, likely an arduino-builder bug). void MQTT_connect(); void setup() { Serial.begin(9600); delay(10); Serial.println(F("Adafruit MQTT demo")); // Connect to WiFi access point. Serial.println(); Serial.print("Connecting to "); Serial.println(WLAN_SSID); WiFi.begin(WLAN_SSID, WLAN_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected"); Serial.print("WiFi hostname: "); Serial.println(WiFi.hostname()); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // Setup MQTT subscription for onoff feed. //mqtt.subscribe(&onoffbutton); configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); } uint32_t x = 0; void loop() { // Ensure the connection to the MQTT server is alive (this will make the first // connection and automatically reconnect when disconnected). See the MQTT_connect // function definition further below. MQTT_connect(); // this is our 'wait for incoming subscription packets' busy subloop // try to spend your time here // Adafruit_MQTT_Subscribe *subscription; // while ((subscription = mqtt.readSubscription(5000))) { // if (subscription == &onoffbutton) { // Serial.print(F("Got: ")); // Serial.println((char *)onoffbutton.lastread); // } // } // Now we can publish stuff! // Serial.print(F("\nSending photocell val ")); // Serial.print(x); // Serial.print("..."); // if (! testPublisher.publish(x++)) { // Serial.println(F("Failed")); // } else { // Serial.println(F("OK!")); // } float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); String hum = "%\t"; String tem = "*C"; time_t t; struct tm *tm; t = time(NULL); tm = localtime(&t); String timestamp = String(tm->tm_year + 1900); timestamp.concat(String("/")); timestamp.concat(String(tm->tm_mon + 1)); timestamp.concat(String("/")); timestamp.concat(String(tm->tm_mday)); timestamp.concat(String("/")); timestamp.concat(String(tm->tm_hour)); timestamp.concat(String(":")); timestamp.concat(String(tm->tm_min)); timestamp.concat(String(":")); timestamp.concat(String(tm->tm_sec)); //時間を.publishする。 char ts[21]; timestamp.toCharArray(ts, timestamp.length() + 1); testPublisher.publish(ts); //MACアドレスを.publishする。 String wifi = WiFi.macAddress(); char macad[21]; wifi.toCharArray(macad, wifi.length() + 1); testPublisher.publish(macad); testPublisher.publish(temperature); testPublisher.publish(humidity); //Serial.println(timestamp + ", " + wifi + ", " + temperature + tem + ", " + humidity + hum); delay(5000); // ping the server to keep the mqtt connection alive // NOT required if you are publishing once every KEEPALIVE seconds /* if(! mqtt.ping()) { mqtt.disconnect(); } */ } // Function to connect and reconnect as necessary to the MQTT server. // Should be called in the loop function and it will take care if connecting. void MQTT_connect() { int8_t ret; // Stop if already connected. if (mqtt.connected()) { return; } Serial.print("Connecting to MQTT... "); uint8_t retries = 3; while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected Serial.println(mqtt.connectErrorString(ret)); Serial.println("Retrying MQTT connection in 5 seconds..."); Serial.println(ret); mqtt.disconnect(); delay(5000); // wait 3 seconds retries--; if (retries == 0) { // basically die and wait for WDT to reset me while (1); } } Serial.println("MQTT Connected!"); }

投稿2019/02/13 07:00

ysmd

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問