Skip to main content
Documentation v2.0
Connecting a device/docs/getting-started/connect-device

Quick start

Stable

Connecting a device

Connect an ESP32 or a compatible board to Ruang IoT with the official library and MQTT v2 topics.

On this page4 sections

Prerequisites

  • The project, device, and variables have been created.
  • The Project ID and Device ID are available.
  • The board and data cable are detected.
  • The Wi-Fi network has internet access.
  • The RuangIoT library is built into Firmware Studio; for the Arduino IDE or PlatformIO, download the official zip from the Downloads page.

Identity, Wi-Fi, and Connection.h

The internal MQTT credentials stay sealed inside the RuangIoT library. Your sketch stores only the Project ID (const char* AUTH_TOKEN), the Device ID (const char* DEVICE_ID), and the Wi-Fi details (ssid, pass). Connection.h — the same file as in the library examples — provides net, ruangiot, and setupRuangIoT().

CPP Connection.h
WiFiClient net;
RuangIoT ruangiot;

void setupRuangIoT() {
  Serial.println("RuangIoT https://ruangiot.id");
  Serial.println("Menghubungkan ke WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nTerhubung ke WiFi!");
  Serial.print("Menghubungkan ke server");
  while (!ruangiot.connect()) {
    Serial.print(".");
    delay(1000);
  }

  Serial.println("\nTerhubung ke server!");
}

Minimal telemetry sketch

CPP main.ino
#include <RuangIoT.h>
#include "Connection.h"

// Set your Project ID and device.
const char* AUTH_TOKEN = "YOUR_PROJECT_ID";
const char* DEVICE_ID = "YOUR_DEVICE_ID";

RuangIoTTimer timer;       // Timer so periodic sends never block the program.

// Set these to your network.
const char ssid[] = "WIFI_NAME";
const char pass[] = "WIFI_PASSWORD";

void send() {
  float temp = 28.5;                       // Replace with a sensor reading.
  ruangiot.send("temperature", temp);      // send data to the "temperature" variable.
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  ruangiot.begin(net);

  timer.setInterval(3000, send);           // Run send every 3000 ms.

  setupRuangIoT();
}

void loop() {
  ruangiot.loop();                         // Required: keeps the connection alive and processes commands.
  timer.run();

  if (!ruangiot.connected()) {
    setupRuangIoT();
  }
}

Testing

  1. Verify the source until there are no blocking errors.
  2. Upload the firmware to the board.
  3. Open the Serial Monitor at the matching baud rate.
  4. Make sure the Wi-Fi and Ruang IoT connections succeed.
  5. Check Data History and the device status.

Documentation feedback

Was this article helpful?

Documentation search

What do you want to learn?

Type at least two characters to search the documentation.
Code copied.