> For the complete documentation index, see [llms.txt](https://docs.antares.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.antares.id/contoh-kode-dan-library/esp8266-wi-fi/esp8266-wi-fi-protokol-mqtt/mengambil-data-terakhir-dari-server-antares-dengan-protokol-mqtt.md).

# 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.

<figure><img src="/files/9vQhREanuzKfYPoDlBfk" alt=""><figcaption></figcaption></figure>

## Prasyarat

Material yang dibutuhkan mengikuti **Prasyarat Umum** pada laman sebelumnya. Jika Anda belum menyiapkan kebutuhan pada laman tersebut, maka Anda dapat mengunjungi laman berikut.

{% content-ref url="/pages/FfvYH4773vShlmvxo6Gj" %}
[Prasyarat Umum ESP8266 Wi-Fi](/contoh-kode-dan-library/esp8266-wi-fi/prasyarat-umum-esp8266-wi-fi.md)
{% endcontent-ref %}

Adapun tambahan material yang spesifik untuk project ini adalah sebagai berikut.

1. Library Antares ESP MQTT. Pada dokumentasi ini menggunakan library **Antares ESP MQTT versi 1.0.**

{% hint style="info" %}
Jika anda belum menginstall **Antares ESP MQTT versi 1.0** dapat mengikuti langkah berikut.

[Antares Wi-Fi MQTT](/pendahuluan/instalasi-library-arduino/antares-wi-fi-mqtt.md)
{% endhint %}

## Langkah Kerja

### **1. Jalankan Aplikasi Arduino IDE**

### **2. Membuka Contoh Program**&#x20;

{% hint style="info" %}
Kode program dapat Anda buka pada Arduino IDE melalui **File > Examples > Antares ESP MQTT > AntaresMQTTRetrieveLatestData.**
{% endhint %}

Berikut adalah kode program contoh GetLatestData.

```cpp
/*
    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.&#x20;

```arduino
#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 
```

{% hint style="info" %}
Parameter **\*Access key** didapat dari laman akun Antares Anda.
{% endhint %}

<figure><img src="/files/xfmf0ZwiWkeinNu95sAR" alt=""><figcaption><p>Letak Access Key di Laman Akun Antares</p></figcaption></figure>

{% hint style="info" %}
Parameter **WIFISSID** didapat dari nama **Wifi/Hotspot** yang sedang digunakan oleh Anda. contohnya pada gambar di bawah ini.
{% endhint %}

<div align="center" data-full-width="true"><figure><img src="/files/LKNC8EjsEKlmR7wXqk7g" alt="" width="375"><figcaption><p>WiFi SSID yang digunakan.</p></figcaption></figure></div>

{% hint style="info" %}
Parameter **\*PASSWORD** didapat dari **password WiFi** yang sedang anda gunakan.
{% endhint %}

{% hint style="info" %}
Parameter **\*projectName** dan **\*deviceName** didapat dari **Application Name** dan **Device Name** yang sudah dibuat dalam akun Antares.
{% endhint %}

<figure><img src="/files/GLkIaEnWogU4BQCnecYI" alt=""><figcaption><p>Tampilan Application Name</p></figcaption></figure>

<figure><img src="/files/D8dMS0xfMZL1Wec1JyuC" alt=""><figcaption><p>Tampilan Device Name</p></figcaption></figure>

### **4. Compile dan Upload Program**

Hubungkan **ESP8266 WEMOS D1R2** dengan komputer Anda dan pastikan **Communication Port** terbaca.

{% hint style="info" %}
Pada sistem operasi Windows pengecekan dapat dilakukan melalui **Device Manager.** Jika **ESP8266 WEMOS D1R2** Anda terbaca maka tampil **USB-Serial CH340** dengan port menyesuaikan ketersediaan port (pada kasus ini terbaca **COM4**).
{% endhint %}

<figure><img src="/files/URJ5Djglrgwng6x5zcoT" alt=""><figcaption><p>Gambar Device Manager pada Windows.</p></figcaption></figure>

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.

<figure><img src="/files/LT1ySxzMnLa7xIYXSbGf" alt="" width="563"><figcaption><p>Gambar Menu Tools pada Arduino IDE</p></figcaption></figure>

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

<div data-full-width="true"><figure><img src="/files/pyhcbN25hMvaw5zfTGo2" alt=""><figcaption><p>Gambar Icon Verify dan Upload pada Arduino IDE.</p></figcaption></figure></div>

{% hint style="info" %}
**Icon Centang** pada Arduino IDE hanyalah proses verify. Biasanya digunakan untuk **Compile** program untuk mengetahui apakah terdapat error atau tidak.

**Icon Panah** pada Arduino IDE adalah proses verify and upload. Biasanya digunakan untuk **Compile** program sekaligus **Flash program** kedalam target board.
{% endhint %}

Jika upload program berhasil maka akan terlihat seperti gambar berikut.

<figure><img src="/files/tuNF5jzPruJwBnk49WYP" alt=""><figcaption><p>Gambar Halaman Arduino IDE Setelah Upload Berhasil.</p></figcaption></figure>

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

<figure><img src="/files/Hu3EF4VAjztNkxfriIOK" alt=""><figcaption><p>Gambar Icon Serial Monitor pada Arduino IDE.</p></figcaption></figure>

Atur **serial baud rate** menjadi 115200 dan pilih BothNL & CR. Hasilnya akan terlihat seperti gambar berikut.

{% hint style="danger" %}
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.
{% endhint %}

<figure><img src="/files/g72j04OT6vo0TP6hMMXA" alt=""><figcaption><p>Gambar Serial Monitor</p></figcaption></figure>

### **5. Periksa Data di Serial Monitor**

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

<figure><img src="/files/FbiumdF49Nt453QmvAvS" alt=""><figcaption><p>Gambar Halaman Serial Monitor Ketika Data Berhasil Diambil.</p></figcaption></figure>

<figure><img src="/files/zd4sCoCWiHtsWdBADk3P" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/xLDZPQ1oc4PQ7JamGMAv" alt=""><figcaption><p>Gambar Halaman Console Antares Ketika Data Berhasil Diterima.</p></figcaption></figure>

{% hint style="info" %}
Data yang diambil dari **Antares IOT Platform** diambil menggunakan Board **ESP8266** dengan protokol HTTP berupa variabel temperature, humidity, wind\_level, rain\_level, dan location yang berisi latitude dan longitude.
{% endhint %}
