OC
OceanRemote
Low-code IoT platform
← Back to Course

Sending Sensor Data to OceanRemote

Sending Sensor Data to OceanRemote

šŸ“¤ Sending Sensor Data to OceanRemote Cloud

šŸ“” What You'll Learn:

  • šŸ“¤ Send sensor data to OceanRemote using HTTP POST
  • šŸ” Get and use your unique device token
  • šŸ“Š View your farm data from anywhere in the world
  • 🚨 Set up alerts for critical thresholds

Now that you can read sensors and connect to WiFi, let's send data to OceanRemote where you can see it from anywhere!

šŸ”‘ Getting Your Device Token

  1. Log in to OceanRemote at oceanremote.net
  2. Go to "Your Device" page
  3. Select ESP32 board type
  4. Enter your WiFi credentials
  5. Click "Generate Code"
  6. Copy the token (starts with "oc_perm_...")
šŸ’” Token Format:

Your device token looks like: oc_perm_aB3dE5fG7hI9jK1lM3nO5pQ7rS9tU
Keep this token secret! Anyone with your token can send fake data to your device.

šŸ“” Complete OceanRemote Integration Code

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

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

// ========== OCEANREMOTE SETTINGS ==========
const char* deviceToken = "YOUR_DEVICE_TOKEN_FROM_OCEANREMOTE";
const char* serverUrl = "https://api.oceanremote.net/device/state";

// ========== SENSOR PINS ==========
#define SOIL_PIN 32
#define DHTPIN 16
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

// Calibration values (CALIBRATE THESE!)
const int DRY = 3800, WET = 1500;

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

void loop() {
    // Read sensors
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    
    int raw = analogRead(SOIL_PIN);
    int soil = map(raw, DRY, WET, 0, 100);
    soil = constrain(soil, 0, 100);
    
    if (!isnan(temp) && !isnan(hum)) {
        sendToOceanRemote(temp, hum, soil);
    } else {
        Serial.println("āŒ Sensor read failed!");
    }
    
    delay(60000);  // Send every minute
}

void sendToOceanRemote(float temp, float hum, int soil) {
    if (WiFi.status() != WL_CONNECTED) {
        Serial.println("WiFi disconnected!");
        return;
    }
    
    HTTPClient http;
    http.begin(serverUrl);
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");
    
    String postData = "token=" + String(deviceToken);
    postData += "&temperature=" + String(temp);
    postData += "&humidity=" + String(hum);
    postData += "&soil_moisture=" + String(soil);
    
    int httpCode = http.POST(postData);
    
    if (httpCode == 200) {
        Serial.println("āœ… Data sent to OceanRemote!");
        Serial.printf("   Temp: %.1f°C | Hum: %.0f%% | Soil: %d%%\n", temp, hum, soil);
    } else {
        Serial.printf("āŒ Failed to send (HTTP %d)\n", httpCode);
    }
    http.end();
}
    
āš ļø Troubleshooting:
  • HTTP 401: Invalid token - check your device token
  • HTTP -1: WiFi not connected - check credentials
  • HTTP 400: Malformed data - check your postData format
  • Nothing appears in dashboard: Wait 1-2 minutes, then refresh
āœ… After Setup:
  • šŸ“Š Your sensor data appears on OceanRemote dashboard
  • šŸŒ Viewable from anywhere in the world
  • šŸ“ˆ Historical graphs and data export available
  • 🚨 Set up alerts for low moisture or high temperature
šŸ’” 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.