This guide gets a reading from an ESP32 onto a chart you can open from anywhere. It uses a plain web request, so there is no broker to run and no extra library to install. The example reads a soil moisture probe, but the same code works for any value you can measure.
What you need
- An ESP32 board and a USB cable
- The Arduino IDE with ESP32 support installed. In the IDE, open Boards Manager and install the package called esp32 by Espressif Systems.
- A sensor. The example uses a capacitive soil moisture probe on pin 34. If you have no sensor yet, replace the reading with a fixed number to test the connection.
- A free SensorConsole account
Step 1: create the device
- Sign up and choose Connect a sensor.
- Name it, pick a category that fits, and choose Generic/DIY push as the connection.
- Save. The device page now shows a web address that contains your device token. The token is a long string of letters and numbers that identifies this device. Copy the token.
Step 2: the sketch
Fill in your Wi-Fi name, your Wi-Fi password and the token, then upload.
#include <WiFi.h>
#include <HTTPClient.h>
const char* WIFI_NAME = "your-wifi-name";
const char* WIFI_PASSWORD = "your-wifi-password";
const char* TOKEN = "paste-your-device-token-here";
const int SENSOR_PIN = 34; // use an ADC1 pin (32 to 39), ADC2 stops working with Wi-Fi on
const int DRY_VALUE = 2900; // reading in dry air, measure your own
const int WET_VALUE = 1300; // reading in a glass of water, measure your own
void connectWifi() {
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
int readMoisturePercent() {
long total = 0;
for (int i = 0; i < 10; i++) {
total += analogRead(SENSOR_PIN);
delay(20);
}
int raw = total / 10;
return constrain(map(raw, DRY_VALUE, WET_VALUE, 0, 100), 0, 100);
}
void sendReading(int moisture) {
HTTPClient http;
String url = String("http://sensorconsole.com/ingest-generic-push.php?token=")
+ TOKEN + "&soil_pct=" + moisture;
http.begin(url);
int code = http.GET();
Serial.print("Server replied: ");
Serial.println(code);
http.end();
}
void setup() {
Serial.begin(115200);
connectWifi();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
connectWifi();
}
sendReading(readMoisturePercent());
delay(300000); // one reading every 5 minutes
}
Open the Serial Monitor at 115200 baud. A reply of 200 means the reading was stored. Open the device page and it appears within a moment.
How the request works
The sketch calls one address with the token and the reading in the query. Everything after the question mark is a name and a value joined by an equals sign, with an ampersand between pairs. You can send several values at once:
http://sensorconsole.com/ingest-generic-push.php?token=YOUR_TOKEN&temp_c=21.4&humidity=55&soil_pct=38
The rules are short.
- Every name you send becomes its own reading and its own chart. Use letters, digits, underscores, dots or hyphens, up to 40 characters.
- Numbers are charted. Text values are stored and shown but not charted.
- You can send up to 20 values per request.
- Each device accepts one request every 10 seconds. A faster request gets reply code 429 and is not stored. A history point is saved at most once every 5 minutes per device, so sending every 5 minutes is enough for almost everything. If you send faster, the live value on the device page updates each time, the daily lowest and highest still count every reading, and the saved history stays one point per 5 minutes.
- A reply of 401 means the token is wrong. A reply of 400 means the request held no usable values.
Why plain http and not https
The address starts with http on purpose. Many small boards cannot make encrypted connections without extra code and memory, and this endpoint accepts plain requests so they work out of the box. The trade-off is that someone on the network path could read the token and send false readings to that device. They cannot reach your account or any other data. If your board supports HTTPS, you can change the address to https and nothing else needs to change.
Note on the analog pin
On an ESP32 the analog converter called ADC2 stops working while Wi-Fi is on. Use a pin on ADC1 for sensors, which means pins 32 to 39. Pin 34 in the example is one of them.
Running on a battery: deep sleep
An ESP32 that stays awake uses far more power than one that sleeps between readings. For a battery, do all the work in setup and then sleep. Replace the end of the sketch with this:
void setup() {
Serial.begin(115200);
connectWifi();
sendReading(readMoisturePercent());
esp_sleep_enable_timer_wakeup(10ULL * 60ULL * 1000000ULL); // 10 minutes
esp_deep_sleep_start();
}
void loop() {
// never reached, the board restarts from setup after each sleep
}
The board wakes every 10 minutes, connects, sends one reading and goes back to sleep. A couple of AA cells can last a long time this way, though the exact time depends on the board and how fast it connects.
ESP8266 differences
For an ESP8266 board, the includes and the way you start the request change slightly. The analog input is pin A0 and reads 0 to 1023, and most boards limit it to a voltage lower than 3.3 volts, so check your board before wiring a sensor to it.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
WiFiClient client;
void sendReading(int moisture) {
HTTPClient http;
String url = String("http://sensorconsole.com/ingest-generic-push.php?token=")
+ TOKEN + "&soil_pct=" + moisture;
http.begin(client, url);
int code = http.GET();
Serial.println(code);
http.end();
}
If it does not work
- Print the reply code. No number at all usually means Wi-Fi never connected. A negative number means the board could not reach the server.
- Check there are no spaces around the token.
- Make sure the board uses a 2.4 GHz network.
- Work through the sensor stopped reporting checklist.