Mengambil Data Terakhir dari Server Antares dengan Protokol MQTT
Pendahuluan
Pada project ini Anda akan diarahkan untuk mengambil data dari Antares IoT Platform ke ESP8266 menggunakan konektivitas WiFi dengan protokol MQTT.

Prasyarat
Material yang dibutuhkan mengikuti Prasyarat Umum pada laman sebelumnya. Jika Anda belum menyiapkan kebutuhan pada laman tersebut, maka Anda dapat mengunjungi laman berikut.
Prasyarat Umum ESP8266 Wi-FiAdapun tambahan material yang spesifik untuk project ini adalah sebagai berikut.
Library Antares ESP MQTT. Pada dokumentasi ini menggunakan library Antares ESP MQTT versi 1.0.
Langkah Kerja
1. Jalankan Aplikasi Arduino IDE
2. Membuka Contoh Program
Berikut adalah kode program contoh GetLatestData.
/*
This is an example sketch to subscribe to MQTT data on ESP8266
via the Antares IoT Platform.
MQTT server & port:
platform.antares.id, port 1338
MQTT topic:
/oneM2M/req/your-access-key/antares-cse/json
The main function in this sketch is the callback function,
which will be fired every time a new message is published
to the topic.
For more information, please visit https://antares.id/id/docs.html
*/
#include <AntaresESPMQTT.h>
#define ACCESSKEY "YOUR-ACCESS-KEY" // Antares account access key
#define WIFISSID "YOUR-WIFI-SSID" // Wi-Fi SSID to connect to
#define PASSWORD "YOUR-WIFI-PASSWORD" // Wi-Fi password
#define projectName "YOUR-APPLICATION-NAME" // Name of the application created in Antares
#define deviceName "YOUR-DEVICE-NAME" // Name of the device created in Antares
AntaresESPMQTT antares(ACCESSKEY);
unsigned long previousMillis=0;
unsigned long interval =5000;
void callback(char topic[], byte payload[], unsigned int length) {
/*
Get the whole received data, including the topic,
and parse the data according to the Antares data format.
*/
antares.get(topic, payload, length);
Serial.println("New Message!");
// Print topic and payload
Serial.println("Topic: " + antares.getTopic());
Serial.println("Payload: " + antares.getPayload());
// Print individual data
}
void setup() {
Serial.begin(115200);
antares.setDebug(true);
antares.wifiConnection(WIFISSID, PASSWORD);
antares.setMqttServer();
antares.setCallback(callback);
}
void loop() {
/*
Check if we're still connected to the MQTT broker/server.
If disconnected, the device will try to reconnect.
*/
antares.checkMqttConnection();
while(millis()-previousMillis > interval)
{
previousMillis = millis();
antares.retrieveLastData(projectName,deviceName);
}
}
3. Set Credential WiFi dan Credential Antares pada Kode Program
Ubah parameter Protokol HTTP pada variabel berikut *ACCESSKEY, *WIFISSID, *PASSWORD, *projectName, dan *deviceName. Sesuaikan dengan parameter di console Antares.
#define ACCESSKEY "your-access-key" // Replace with your Antares account access key
#define WIFISSID "your-wifi-ssid" // Replace with your Wi-Fi SSID
#define PASSWORD "your-wifi-password" // Replace with your Wi-Fi password
#define projectName "your-project-name" // Antares project name
#define deviceName "your-project-name" // Name of the device




4. Compile dan Upload Program
Hubungkan ESP8266 WEMOS D1R2 dengan komputer Anda dan pastikan Communication Port terbaca.

Atur board ESP8266 WEMOS D1R2 dengan klik Tools > Board > esp8266 pada Arduino IDE, kemudian pastikan yang digunakan adalah LOLIN(WEMOS) D1 R2 & mini. Pilih port sesuai communication port yang terbaca (dalam kasus ini COM4). Hasilnya akan terlihat seperti gambar berikut.

Setelah semua setup selesai, lakukan upload program dengan menekan icon panah seperti gambar berikut. Tunggu hingga selesai proses compile dan upload

Jika upload program berhasil maka akan terlihat seperti gambar berikut.

Setelah selesai upload program, Anda dapat melihat serial monitor untuk melakukan debug program. Icon serial monitor terlihat pada gambar berikut.

Atur serial baud rate menjadi 115200 dan pilih BothNL & CR. Hasilnya akan terlihat seperti gambar berikut.
Pastikan serial baud rate sesuai dengan nilai yang terdefinisi di kode program. Jika serial baud rate tidak sama, antara kode program dan serial monitor maka karakter ASCII tidak akan terbaca dengan baik.

5. Periksa Data di Serial Monitor
Setelah upload program berhasil, selanjutnya buka halaman Serial Monitor kemudian lihat apakah data sudah berhasil diambil.



Last updated