← Back to Course
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:
- Log in to OceanRemote dashboard
- Go to Devices → Add Device
- Copy your unique device token
- 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.
×