# Mengambil Data Terakhir dari Server Antares dengan Protokol HTTP

## Pendahuluan

Pada project  ini Anda akan diarahkan untuk mengambil data dari **Antares IoT Platform** ke **ESP8266** menggunakan konektivitas WiFi dengan protokol HTTP.

<figure><img src="/files/jOE0d0Biu109F49UwcGP" 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="/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 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](/pendahuluan/instalasi-library-arduino/antares-wi-fi-http.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 > Example > Antares ESP HTTP > GetLatestData.**
{% endhint %}

Berikut adalah kode program contoh GetLatestData.

```cpp
/*
  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>  // Include the AntaresESP32HTTP library

#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

AntaresESPHTTP antares(ACCESSKEY);  // Initialize AntaresESP32HTTP with the access key

void setup() {
  Serial.begin(115200);                        // Initialize serial communication
  antares.setDebug(true);                      // Enable Antares library debug mode
  antares.wifiConnection(WIFISSID, PASSWORD);  // Connect to WiFi using provided SSID and 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 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/JoyU711Cu1Z7tOGiBrrF" 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/K2SscswAgSIezUKuoVIc" 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/OxOdPgDVNRrZH4YoMlg5" 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/TuGg1JyMyqPDXtu2icjp" 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/zVKLUHWwAFd2VkigjO5H" 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/59LRJymuGUF2Qd8O8rPh" alt=""><figcaption><p>Gambar Halaman Arduino IDE Setelah Upload Berhasil.</p></figcaption></figure>

<figure><img src="/files/UTjeDmTo4EmpFghbLFbw" 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/DFK2nSRcNVYuKtadjlCk" alt=""><figcaption><p>Gambar Serial Monitor</p></figcaption></figure>

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

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

Bentuk data yang diambil oleh ESP8266 akan berbentuk JSON seperti format berikut.

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

<figure><img src="/files/g5gaT6SJSIGpylUlsLFz" alt=""><figcaption><p>Gambar Halaman Console 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.antares.id/contoh-kode-dan-library/esp8266-wi-fi/esp8266-wi-fi-protokol-http/mengambil-data-terakhir-dari-server-antares-dengan-protokol-http.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
