ā Back to Course
Sending Data to OceanRemote
š¤ Sending Data to OceanRemote Cloud - ESP8266 HTTP POST
š¤ What You'll Learn:
- š” Send sensor data to OceanRemote cloud using HTTP POST
- š Connect ESP8266 to WiFi
- š Send temperature, humidity, and soil moisture data
- š View your farm data from anywhere in the world
š Wiring (Soil Moisture + DHT22)
Soil Moisture Sensor ESP8266 (NodeMCU)
āāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāā
VCC ā 3.3V
GND ā GND
AO ā A0
DHT22 ESP8266
VCC ā 3.3V
GND ā GND
DATA ā GPIO4 (D2) + 10kĪ© pull-up
ā ļø ESP8266 ADC Warning:
ESP8266 A0 only accepts 0-1.0V! Most sensors output 0-3.3V. Use voltage divider:
Sensor AO ā 10kĪ© ā A0
Sensor AO ā 5.6kĪ© ā GND
š Complete Code (Soil + Temp + Humidity)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>
// ========== WIFI CREDENTIALS ==========
const char* ssid = "YOUR_WIFI";
const char* password = "YOUR_PASSWORD";
// ========== DEVICE TOKEN ==========
const char* deviceToken = "YOUR_DEVICE_TOKEN";
// ========== SENSOR PINS ==========
#define DHTPIN 4
#define DHTTYPE DHT22
#define SOIL_PIN A0
DHT dht(DHTPIN, DHTTYPE);
// Calibration values (CALIBRATE THESE!)
const int DRY = 950; // Value in dry air
const int WET = 350; // Value in water
void setup() {
Serial.begin(115200);
dht.begin();
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 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)) {
sendData(temp, hum, soil);
} else {
Serial.println("ā DHT22 read error!");
}
delay(60000); // Send every minute
}
void sendData(float temp, float hum, int soil) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi disconnected!");
return;
}
HTTPClient http;
http.begin("https://api.oceanremote.net/device/state");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String data = "token=" + String(deviceToken);
data += "&temperature=" + String(temp);
data += "&humidity=" + String(hum);
data += "&soil_moisture=" + String(soil);
int httpCode = http.POST(data);
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: HTTP %d\n", httpCode);
}
http.end();
}
š” Getting Your Device 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 data export
- šØ Set up alerts for low moisture or high temp
š” 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.
×