OC
OceanRemote
Low-code IoT platform
← Back to Course

Sending Sensor Data to OceanRemote

Sending Sensor Data to OceanRemote

šŸ“¤ Sending Data to OceanRemote Cloud - C++ for ESP32/ESP8266

šŸ“¤ What You'll Learn:

  • šŸ“” Send sensor data to OceanRemote cloud using HTTP POST
  • šŸ”Œ Connect ESP32/ESP8266 to WiFi
  • šŸ“Š Send soil moisture, temperature, and humidity data
  • šŸŒ View data from anywhere in the world

šŸ”Œ Wiring (Soil Moisture Sensor)

Soil Moisture Sensor → ESP32
VCC                 → 3.3V
GND                 → GND
AO                  → GPIO32
    

šŸ“– Complete Arduino Code

#include <WiFi.h>
#include <HTTPClient.h>

// ========== WIFI CREDENTIALS ==========
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";

// ========== OCEANREMOTE TOKEN ==========
const char* token = "YOUR_DEVICE_TOKEN";

// ========== SENSOR PIN ==========
#define SOIL_PIN 32

// ========== CALIBRATION ==========
const int DRY = 3800, WET = 1500;

void setup() {
    Serial.begin(115200);
    pinMode(SOIL_PIN, INPUT);
    
    // Connect to WiFi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nāœ… WiFi connected!");
}

void loop() {
    // Read soil moisture
    int raw = analogRead(SOIL_PIN);
    int moisture = map(raw, DRY, WET, 0, 100);
    moisture = constrain(moisture, 0, 100);
    
    Serial.printf("Soil moisture: %d%%\n", moisture);
    
    // Send to OceanRemote
    sendData(moisture);
    
    delay(60000);  // Send every minute
}

void sendData(int moisture) {
    if (WiFi.status() != WL_CONNECTED) return;
    
    HTTPClient http;
    http.begin("https://api.oceanremote.net/device/state");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    String data = "token=" + String(token);
    data += "&soil_moisture=" + String(moisture);
    
    int code = http.POST(data);
    
    if (code == 200) {
        Serial.println("āœ… Data sent to OceanRemote!");
    } else {
        Serial.printf("āŒ Failed: HTTP %d\n", code);
    }
    
    http.end();
}
    

šŸ“– With DHT22 (Temperature + Humidity)

#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>

#define DHTPIN 16
#define DHTTYPE DHT22
#define SOIL_PIN 32

const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";
const char* token = "YOUR_TOKEN";

const int DRY = 3800, WET = 1500;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(115200);
    dht.begin();
    pinMode(SOIL_PIN, INPUT);
    
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) delay(500);
    Serial.println("āœ… WiFi connected!");
}

void loop() {
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    int raw = analogRead(SOIL_PIN);
    int soil = constrain(map(raw, DRY, WET, 0, 100), 0, 100);
    
    sendData(temp, hum, soil);
    delay(60000);
}

void sendData(float temp, float hum, int soil) {
    if (WiFi.status() != WL_CONNECTED) return;
    
    HTTPClient http;
    http.begin("https://api.oceanremote.net/device/state");
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    String data = "token=" + String(token);
    data += "&temperature=" + String(temp);
    data += "&humidity=" + String(hum);
    data += "&soil_moisture=" + String(soil);
    
    int code = http.POST(data);
    
    if (code == 200) {
        Serial.println("āœ… Data sent!");
        Serial.printf("   Temp: %.1f°C | Hum: %.0f%% | Soil: %d%%\n", temp, hum, soil);
    } else {
        Serial.printf("āŒ Failed: %d\n", code);
    }
    
    http.end();
}
    
šŸ’” Getting Your Token:
  1. Log in to OceanRemote dashboard
  2. Go to Devices → Add Device
  3. Copy your unique device token
  4. Replace "YOUR_DEVICE_TOKEN" with your token
āœ… After Setup:
  • šŸ“Š Your data appears on OceanRemote dashboard
  • šŸŒ Viewable from anywhere in the world
  • šŸ“… Historical graphs and exportable data
  • 🚨 Set alerts for critical thresholds
šŸ’” Key Takeaways:
  • Apply these concepts directly to your farm or project.
  • Take notes on important details for the quiz.
  • Use the button below to track your progress.