ā 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.
×