# Mengirim Data Sederhana ke Antares dengan Protokol HTTP

## Pendahuluan

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

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/OlN3KYMIYP31t4djHaoB/Mengirim%20Data%20Sederhana%20ke%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-esp8266-wi-fi" %}
[prasyarat-umum-esp8266-wi-fi](https://docs.antares.id/contoh-kode-dan-library/esp8266-wi-fi/prasyarat-umum-esp8266-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**&#x20;

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

Berikut adalah kode program contoh AntaresStoreData.

```cpp
/*
  This code will deploy data to your Antares project device with the following structure:
  (Note that nesting the JSON object can only be done up to 2 levels using this library)
  {
    "temperature": random-int,
    "humidity": random-int,
    "wind_speed": random-float,
    "rain_level": random-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() {
    // Variables
  int temp = random(25,30) ;  //Create a random integer value for temperature
  int hum = random(75,90); // Create a random integer value for humidity
  float windsp = float(random(20, 30))/3.33; //Create a random float value for windspeed
  float rainlv = float(random(0, 20))/6.99; //Create a random float value for rainlevel
  String lat = "-6.8718189"; //Create a string value for latitude
  String lon = "107.5872477"; //Create a string value for longitude

  // Add variable data to storage buffer
  antares.add("temperature", temp); // Add temperature data to the Antares payload
  antares.add("humidity", hum); // Add humidity data to the Antares payload
  antares.add("wind_speed", windsp); // Add wind_speed data to the Antares payload
  antares.add("rain_level", rainlv); // Add rain_level data to the Antares payload
  antares.add("location", "latitude", lat); // Add location latitude data to the Antares payload
  antares.add("location", "longitude", lon); // Add location longitude data to the Antares payload

  // Send from buffer to Antares
  antares.send(projectName, deviceName); // Send  data to Antares
  delay(10000); // Transfer data for every 10 seconds
}
```

### **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="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FrsGvkyelfI1aoqF9yYms%2Faccess%20key.png?alt=media&#x26;token=a84dc662-a16c-4917-aaca-31639bd17a00" 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="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/n5KRN5bFMLvHeFT2xmxB/WIFI%20SSID.jpeg" 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="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FkoS88rfgokK60zcarXdV%2Fapp.png?alt=media&#x26;token=b98641ec-ebb6-4992-aeee-3d3f856615b9" alt=""><figcaption><p>Tampilan Application Name</p></figcaption></figure>

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/OBl0ixMJyZQET4vkwvxD/deive%20wemos%20http.png" 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="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/RUgfSy4A8ZwAdCEKH4c8/Device%20Manager%20Windows.jpeg" 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="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/FT0hiUhOImC6COBFLrHT/Menu%20Tools%20Arduino%20IDE.jpeg" 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

<figure><img src="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/F6q1UrLUKGqTww1880WL/Icon%20Verify%20dan%20Compile.jpeg" alt=""><figcaption><p>Gambar Icon Verify dan Upload pada Arduino IDE.</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/59Bg5srVjUnlYNoBoK1U/Upload%20Berhasil%20Halaman%20Arduino%20IDE.jpeg" 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="https://content.gitbook.com/content/rRkxjRLeqOm0pNhLfsus/blobs/CcyJQ7qcai5lXxKy0Ta4/Icon%20Serial%20Monitor%20Arduino%20IDE.jpeg" 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="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FeUb4bcr2R202ClQyQSYj%2Fserial%20monitor.png?alt=media&#x26;token=61bdda88-58d1-4f3c-bbe6-da4cc4b33745" alt=""><figcaption><p>Gambar Serial Monitor</p></figcaption></figure>

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

Setelah upload program berhasil, selanjutnya buka halaman device antares kemudian lihat apakah data sudah berhasil dikirim.

<figure><img src="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FPVRmu0pERBwOlfD7hL5I%2Fapp%20dev.png?alt=media&#x26;token=55de5c4a-f6d8-4388-88c4-48ae57af7fd5" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3995702122-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRkxjRLeqOm0pNhLfsus%2Fuploads%2FbBkHHqkB66YqY0DKU2gJ%2Fesp8266.png?alt=media&#x26;token=92c61ab5-68cc-4612-95a7-b51b42d5963b" alt=""><figcaption><p>Gambar Halaman Console Ketika Data Berhasil Diterima</p></figcaption></figure>

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