# Mengambil Data Terakhir dari Server Antares dengan Protokol HTTP

## Pendahuluan

Pada project  ini Anda akan diarahkan untuk mengambil data dari **Antares IoT Platform** ke **Development Board Lynx-32** menggunakan konektivitas Wi-Fi dengan protokol HTTP.

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/4O4C5OxrZFypvknE8HDc/Mengambil%20Data%20Terakhir%20dari%20Server%20Antares%20dengan%20Protokol%20HTTP.png" alt=""><figcaption><p>Gambar Ilustrasi Project</p></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="../prasyarat-umum-esp32-wi-fi" %}
[prasyarat-umum-esp32-wi-fi](https://docs.antares.id/contoh-kode-dan-library/esp32-wi-fi/prasyarat-umum-esp32-wi-fi)
{% endcontent-ref %}

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

1. Library Antares ESP HTTP. Pada dokumentasi ini menggunakan library **Antares ESP HTTP versi 1.6.0.**

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

[antares-wi-fi-http](https://docs.antares.id/pendahuluan/instalasi-library-arduino/antares-wi-fi-http "mention")
{% endhint %}

## Langkah Kerja

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

### **2. Membuka Contoh Program**

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

Berikut adalah kode program contoh AntaresGetLatestData.

```arduino
/*
  This code will fetch the latest data from your antares project device.
  Your Antares project device must have a structure like this:
  (Note that nesting the JSON object can only be done up to 2 levels using this library)
  {
    "temperature": some-integer,
    "humidity": some-integer,
    "wind_speed": some-float,
    "rain_level": some-float,
    "location" : {
      "latitude": "static-string",
      "longitude": "static-string"
    }
  }
  For more information please visit https://antares.id/id/docs.html
*/

#include <AntaresESPHTTP.h>

#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-APPLICATION-NAME"   // Replace with the Antares application name that was created
#define deviceName "YOUR-DEVICE-NAME"     // Replace with the Antares device name that was created

AntaresESPHTTP antares(ACCESSKEY);

void setup() {
    Serial.begin(115200);
    antares.setDebug(true);
    antares.wifiConnection(WIFISSID, PASSWORD);
}

void loop() {
  // Get the latest data from your Antares device
  antares.get(projectName, deviceName);

  // Check if we're actually getting data
  if(antares.getSuccess()) {
    int temp = antares.getInt("temperature");
    int hum = antares.getInt("humidity");
    float windsp = antares.getFloat("wind_speed");
    float rainlv = antares.getFloat("rain_level");
    String lat = antares.getString("location", "latitude");
    String lon = antares.getString("location", "longitude");

    Serial.println("Temperature: " + String(temp));
    Serial.println("Humidity: " + String(hum));
    Serial.println("Wind speed: " + String(windsp));
    Serial.println("Rain level: " + String(rainlv));
    Serial.println("Latitude: " + lat);
    Serial.println("Longitude: " + lon);
  }
  delay(5000);
}

```

### **3. Set HTTP Parameter 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-APPLICATION-NAME"   // Replace with the Antares application name that was created
#define deviceName "YOUR-DEVICE-NAME"     // Replace with the Antares device name that was created
```

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

<figure><img src="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FOwjl0QLzoSAsGheYULv8%2Faccess%20key.png?alt=media&#x26;token=064f2e4c-ff48-43a2-a34a-bbf49cf0276c" alt=""><figcaption><p>Letak Access Key di Laman Akun Antares</p></figcaption></figure>

{% hint style="info" %}
Parameter **WIFISSID** didapat dari nama **Wi-Fi/Hotspot** yang nantinya akan digunakan oleh **Development Board Lynx-32**. Contohnya pada gambar di bawah ini.
{% endhint %}

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/TQ7UiLDzCjZ8agRwkuMy/2_wifi.png" alt=""><figcaption><p>WIFISSID</p></figcaption></figure>

{% hint style="info" %}
Parameter **\*PASSWORD** didapat dari **password Wi-Fi** 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="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2F3G3Pek1wflcHqrdfG5lp%2Flynx%2032%20app.png?alt=media&#x26;token=b580c2bc-544b-457f-99ca-d7ceea76fdad" alt=""><figcaption><p>Tampilan Application Name</p></figcaption></figure>

<figure><img src="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FOH0ltufXnZb7GqIFJlro%2Fantares%20shield%20dev.png?alt=media&#x26;token=6fff53e5-8e27-4639-9ef9-c617ac535262" alt=""><figcaption><p>Tampilan Device Name</p></figcaption></figure>

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

Hubungkan **Lynx-32** dengan komputer Anda dan pastikan **Communication Port** terbaca.

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

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/9vD3gHWKgUn7ylVNCzBg/4_comport.PNG" alt=""><figcaption><p>Tampilan Device Manager</p></figcaption></figure>

Atur board **ESP32** dengan klik **Tools > Board > esp32** pada Arduino IDE, kemudian pastikan yang digunakan adalah **ESP32 Dev Module.** Pilih port sesuai communication port yang terbaca (dalam kasus ini COM4). Hasilnya akan terlihat seperti gambar berikut.

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/GTcHcvDZYAVyBoeYquR1/5_menuport.png" alt="" width="464"><figcaption><p>Tampilan Spesifikasi Board dan Port yang Digunakan</p></figcaption></figure>

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

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/a2B6YLAfil2H97Uu7cIf/6_icon%20compile.PNG" alt=""><figcaption><p>Ikon Compile untuk Centang dan Ikon Upload untuk Tanda Panah</p></figcaption></figure>

{% 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="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/ie8scKH9CcoeFkUbpzCt/7_success.PNG" alt=""><figcaption><p>Tampilan Keluaran Program Berhasil Di Upload</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="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/d247exqYbGQf3bfRGTD5/8_serial.png" alt=""><figcaption><p>Ikon Serial Monitor</p></figcaption></figure>

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

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/SAdWOHn9LGX6SrIANQ9T/9_serialmon.PNG" alt=""><figcaption><p>Tampilan Serial Monitor</p></figcaption></figure>

{% 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 %}

### **5. Periksa Data di Antares**

Setelah upload program berhasil, selanjutnya buka halaman device antares kemudian lihat apakah data yang diambil sudah sesuai dengan data di Antares Console.

<figure><img src="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FUNfUG4tfYJnxEDjx5RcT%2Fapp%20dev%20lynx%2032%20-%20antares%20shield.png?alt=media&#x26;token=e3bd13e7-648a-46df-86d3-e5623d59eea7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FjUtbwq6C5QQkod4Gj6Bs%2F146.png?alt=media&#x26;token=469c1d6f-c14d-4fec-9dd7-777e4a6cbe0c" alt=""><figcaption><p>Tampilan Antares Console</p></figcaption></figure>

{% hint style="info" %}
Data yang dikirimkan dari **Development Board Lynx-32** dengan protokol HTTP berupa variabel temperature, humidity, wind\_speed, rain\_level, dan location yang berisi latitude dan longitude.
{% endhint %}
