leon-thomm / nus-cs3237-iot-buoys

0 stars 1 forks source link

Phone - Cloud communication #3

Open leon-thomm opened 2 years ago

cheehongw commented 2 years ago

https://pub.dev/packages/mqtt_client

Tino3141 commented 2 years ago

General Description how to setup the phone to communicate with the cloud

Example will be in the repo

Mqtt-Client

Create Client

MqttServerClient mqttclient = MqttServerClient.withPort(address, 'iot_device', 1883); // 1883 standard port for Mqtt

Setup


void connected() {
    print("Connected");
}

mqttclient.onConnected = connected;
final connMessage = MqttConnectMessage()
          .authenticateAs('username', 'password')
          .withWillTopic('willtopic')
          .withWillMessage('Will message')
          .startClean()
          .withWillQos(MqttQos.atLeastOnce);
mqttclient.keepAlivePeriod = 60;
mqttclient.connectionMessage = connMessage;
try {
          await mqttclient.connect();
          print("Connected to Mqtt Server");
} catch (e) {
          print('Error Connecting');
}

How to publish

const pubTopic = 'topic/test';
final builder = MqttClientPayloadBuilder();
String data2SendStr = "";
builder.addString(data2SendStr);
mqttclient.publishMessage(pubTopic, MqttQos.atLeastOnce, builder.payload!);
exetr commented 2 years ago

JSON Format

[
  {
    "time": 5555,
    "temp": 26.0,
    "light": 512,
    "x": 512,
    "y": 512,
    "z": 512
  },
  {
    "time": 6666,
    "temp": 26.0,
    "light": 512,
    "x": 512,
    "y": 512,
    "z": 512
  }
]
leon-thomm commented 2 years ago
[
  {
    "time": 5555,
    "temp": 26.0,
    "light": 512,
    "acc": [<x>, <y>, <z>, <g_res>],
    "gyro": [<pitch>, <yaw>, <roll>]
  },
  ...
]

g_res is the resulting g force; already computed on the WeMOS

cheehongw commented 2 years ago

I couldn't get the app to publish arbitrary packets to the cloud. I've currently pushed my changes into a41e79c9c075bcf075aa75ad46631ecb7d4bc26b, which i'll work on after the checkin.