前提・実現したいこと
WioLTEからSORACOMBeamを経由してAzureIoTHubにデータを送りたい
磁気スイッチの変化したというデータを送りたい
発生している問題・エラーメッセージ
### Turn on or reset. ### Connecting to "soracom.io". ### Connecting to MQTT server "beam.soracom.io" 5 ### mq ERROR! ###
該当のソースコード
c++
1#include <WioLTEforArduino.h> 2#include <WioLTEClient.h> 3#include <PubSubClient.h> // https://github.com/SeeedJP/pubsubclient 4#include <stdio.h> 5 6#define MAGNETIC_SWITCE_PIN (WIOLTE_D20) 7 8#define APN "soracom.io" 9#define USERNAME "sora" 10#define PASSWORD "sora" 11 12#define MQTT_SERVER_HOST "beam.soracom.io" 13#define MQTT_SERVER_PORT (1883) 14 15#define ID "wiolte" 16#define OUT_TOPIC "devices/wiolte/messages/events/" 17#define IN_TOPIC "devices/wiolte/messages/devicebound/#" 18 19#define INTERVAL (60000) 20 21WioLTE Wio; 22WioLTEClient WioClient(&Wio); 23PubSubClient MqttClient; 24 25void callback(char* topic, byte* payload, unsigned int length) { 26 SerialUSB.print("Subscribe:"); 27 for (int i = 0; i < length; i++) SerialUSB.print((char)payload[i]); 28 SerialUSB.println(""); 29} 30 31void setup() { 32 delay(200); 33 34 SerialUSB.println(""); 35 SerialUSB.println("--- START ---------------------------------------------------"); 36 37 SerialUSB.println("### I/O Initialize."); 38 Wio.Init(); 39 40 SerialUSB.println("### Power supply ON."); 41 Wio.PowerSupplyLTE(true); 42 delay(500); 43 44 SerialUSB.println("### Turn on or reset."); 45 if (!Wio.TurnOnOrReset()) { 46 SerialUSB.println("### ERROR! ###"); 47 return; 48 } 49 50 SerialUSB.println("### Connecting to \""APN"\"."); 51 if (!Wio.Activate(APN, USERNAME, PASSWORD)) { 52 SerialUSB.println("### ERROR! ###"); 53 return; 54 } 55 56 SerialUSB.println("### Connecting to MQTT server \""MQTT_SERVER_HOST"\""); 57 MqttClient.setServer(MQTT_SERVER_HOST, MQTT_SERVER_PORT); 58 MqttClient.setCallback(callback); 59 MqttClient.setClient(WioClient); 60 if (!MqttClient.connect(ID)) { 61 SerialUSB.println(MqttClient.state()); 62 SerialUSB.println("### mq ERROR! ###"); 63 return; 64 } 65 MqttClient.subscribe(IN_TOPIC); 66 67 SerialUSB.println("### Setup completed."); 68} 69 70int state=digitalRead(MAGNETIC_SWITCE_PIN); 71int next_state; 72void loop(void) { 73 next_state=digitalRead(MAGNETIC_SWITCE_PIN); 74 if(state != next_state){ 75 state = next_state; 76 char data[100]; 77 sprintf(data, "%d",state); 78 SerialUSB.println(data); 79 MqttClient.publish(OUT_TOPIC, data); 80 } 81 delay(1000); 82 83err: 84 unsigned long next = millis(); 85 while (millis() < next + INTERVAL) 86 { 87 MqttClient.loop(); 88 } 89}
あなたの回答
tips
プレビュー