Post Data DHT 11 dan ditampilkan pada Display OLED
Pendahuluan
Pada project ini, Anda akan menggunakan Shield Workshop Antares pada modul Development Board Lynx-32. Pada Shield Workshop Antares ini terdapat sensor suhu, kelembapan (DHT11), relay, LED dan push button. Anda akan melakukan monitoring suhu dan kelembapan sesuai periode interval yang ditentukan. Hasil data yang dikirim sensor dapat Anda pantau melalui console Antares dan ditampilkan pada OLED.

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 ESP32 Wi-FiAdapun tambahan material yang spesifik untuk project ini adalah sebagai berikut.
Shield Workshop Antares
Modul OLED SSD1306 0,96inch 128x64 pixel berbasis I2C

Library Antares ESP HTTP. Pada dokumentasi ini menggunakan library Antares ESP HTTP versi 1.6.0.
Library DHT11. Pada dokumentasi ini menggunakan DHT11 Sensor Library versi 1.4.4.
Library OLED SSD1306. Pada dokumentasi ini menggunakan Adafruit SSD1306 by Adafruit versi 2.5.7.
Langkah Kerja
1. Jalankan Aplikasi Arduino IDE
2. Membuka Contoh Program
Berikut adalah kode program contoh POST_DATA_DHT11_OLED.
// Library initialization
#include <AntaresESPHTTP.h>   // Load AntaresESP32HTTP library for connecting to the Antares platform
#include "DHT.h"               // Load DHT sensor library for reading temperature and humidity
#include <Adafruit_SSD1306.h>  // Load OLED display library
#define DHTPIN 14            // Define DHTPIN variable, pointing to pin 14
#define DHTTYPE DHT11        // Set DHT type to DHT11
#define SCREEN_WIDTH 128     // Define OLED screen width
#define SCREEN_HEIGHT 64     // Define OLED screen height
#define OLED_RESET -1        // Define OLED reset pin, set to -1 as it's not used
#define SCREEN_ADDRESS 0x3C  // Define OLED I2C address
#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
const unsigned long interval = 10000;    // 10 s interval to send message
unsigned long previousMillis = 0;  // will store last time message sent
AntaresESPHTTP antares(ACCESSKEY);   // Create an antares object for connecting to Antares
DHT dht(DHTPIN, DHTTYPE);             // Create a dht object for the DHT sensor
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);  // Create a display object for the OLED screen
void setup() {
  Serial.begin(115200);     // Initialize serial communication with baudrate 115200
  antares.setDebug(true);   // Turn on debug mode. Set to "false" if you don't want messages to appear in the serial monitor
  // Reset WiFi cache before connecting
  WiFi.disconnect();
  antares.wifiConnection(WIFISSID, PASSWORD);  // Attempt to connect to Wi-Fi with the specified SSID and password
  dht.begin();             // Initialize the DHT sensor object
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));   // Check if the OLED is successfully initialized
    for (;;);
  }
  display.clearDisplay();   // Clear the OLED display
  display.setTextColor(SSD1306_WHITE);   // Set text color to white
  display.setTextSize(1);   // Set text size to 1
  display.setCursor(0, 0);   // Set cursor position to (0, 0)
  display.println(F("Temperature & Humidity"));   // Display "Temperature & Humidity" text on the OLED
  display.display();         // Show the text on the OLED
  delay(2000);               // Delay for 2 seconds
}
void loop() {
  
  // Check interval overflow
  if (millis() - previousMillis > interval) {
  previousMillis = millis();
  
  float hum = dht.readHumidity();      // Read humidity value from the DHT sensor
  float temp = dht.readTemperature();  // Read temperature value from the DHT sensor
  if (isnan(hum) || isnan(temp)) {     // Check if the sensor reading is invalid
    Serial.println("Failed to read DHT sensor!");   // If the reading is invalid, print an error message
    return;   // Exit the loop function and wait for the next cycle
  }
  display.clearDisplay();   // Clear the OLED display
  display.setTextSize(1);   // Set text size to 1
  display.setCursor(0, 0);   // Set cursor position to (0, 0)
  display.print(F("Temperature: "));   // Display "Temperature: " text on the OLED
  display.print(temp);         // Display temperature value on the OLED
  display.println(F(" C"));    // Display " C" (for Celsius) on the OLED
  display.print(F("Humidity: "));   // Display "Humidity: " text on the OLED
  display.print(hum);          // Display humidity value on the OLED
  display.println(F(" %"));    // Display " %" (for percentage) on the OLED
  display.display();          // Show the text on the OLED
  Serial.println("Temperature: " + (String)temp + " *C");  // Print temperature value to the serial monitor with "*C" format
  Serial.println("Humidity: " + (String)hum + " %");       // Print humidity value to the serial monitor with "%" format
 
  // Add variable data to the storage buffer in Antares
  antares.add("temperature", temp);
  antares.add("humidity", hum);
  // Send data from the storage buffer to Antares
  antares.send(projectName, deviceName);
  }
}
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.
#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



4. Compile dan Upload Program
Hubungkan Lynx-32 dengan komputer Anda dan pastikan Communication Port terbaca.
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.

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 Antares
Setelah upload program berhasil, selanjutnya buka halaman device antares kemudian lihat apakah data sudah berhasil dikirim.


Berikut data yang ditampilkan OLED.

Last updated