GR-SAKURA
GR-KURUMI
GR-COTTON
GR-CITRUS
GR-PEACH
GR-KAEDE
GR-ADZUKI
GR-LYCHEE
GR-ROSE
GR-MANGO(*)
SNShield
Web Compiler
IDE for GR
TOPPERS関連
女子美コラボ
その他
※プロデューサミーティング中
作り方使い方資料
イベント関連
作品記事
体験記事
ライブラリ
ツール
その他・過去ファイル
がじぇるね岡宮です。
GR-ROSEでのMQTTサンプルです。動作確認にはmosquittoを使いました。WiFiのssidやpassの他、ブローカーのIPは適当に変更してください。
GR-ROSEは、"inTopic"のトピックをListenし、コネクトする際には"outTopic"のトピックでhello worldを投げます。
以下はGR-ROSEに対してパブリッシュするときのmosquittoのコマンドです。
mosquitto_pub -t inTopic -m "Hello"
以下はサブスクライブ用です。
mosquitto_sub -t outTopic
下記フォーラムがMQTTの応用となる「IBM Watson IoT」の例です。
https://japan.renesasrulz.com/gr_user_forum_japanese/f/gr-rose/5890/ibm-watson-iot
#include <WiFiEsp.h>#include <WiFiEspClient.h>#include <WiFiEspUdp.h>#include <PubSubClient.h>char ssid[] = "Okamylab7"; // your network SSID (name)char pass[] = "oooooooo"; // your network passwordint status = WL_IDLE_STATUS; // the Wifi radio's statusIPAddress server(172,20,10,3);// Initialize the Ethernet client objectWiFiEspClient espClient;void callback(char* topic, byte* payload, unsigned int length);PubSubClient client(server, 1883, callback, espClient);void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i=0;i<length;i++) { Serial.print((char)payload[i]); } Serial.println();}void setup() { // initialize serial for debugging Serial.begin(115200); // initialize serial for ESP module Serial6.begin(115200); // initialize ESP module WiFi.init(&Serial6); // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); // don't continue while (true); } // attempt to connect to WiFi network while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network status = WiFi.begin(ssid, pass); } // you're connected now, so print out the data Serial.println("You're connected to the network");}void loop() { // put your main code here, to run repeatedly: if (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("GR-ROSE")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("outTopic","hello world"); client.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } else { static uint32_t last_time = millis(); if((millis()-last_time) > 100) client.loop(); } if(client.connected()){ static int count = 0; char buffer [50]; sprintf(buffer, "%d", count); client.publish("outTopic", buffer); count++; delay(500); }}