ā Back to Course
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
- Log in to OceanRemote at oceanremote.net
- Go to "Your Device" page
- Select ESP32 board type
- Enter your WiFi credentials
- Click "Generate Code"
- 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.
×