esp32accelerometerwifitutorialarduino

Stream Your Phone's Accelerometer to an ESP32 in 10 Minutes

By Shihab Sikder

Stream Your Phone's Accelerometer to an ESP32 in 10 Minutes

Getting real motion data into your microcontroller project usually means buying an IMU, soldering headers, and fighting with I2C wiring. With SensorCast you can skip all of that and stream your Android phone's built-in accelerometer straight to an ESP32 over WiFi in about ten minutes.

What you'll need

  • An Android phone with the SensorCast app installed
  • An ESP32 development board
  • The Arduino IDE (or PlatformIO)
  • Both devices on the same WiFi network

Step 1: Configure the stream in the app

Open SensorCast, sign in to your free account, and select the accelerometer from the sensor list. Set the sampling rate to 50Hz to start — this is fast enough for most motion projects while keeping the data manageable. Choose WiFi as your connection method and note the port the app displays.

Step 2: Connect the ESP32

On the ESP32 side, connect to the same network and open a UDP or TCP socket to the phone's IP address. SensorCast sends each reading as a small JSON payload containing the x, y, and z axis values plus a timestamp.

Step 3: Parse the data

Read the incoming packet, parse the three axis values, and you're done. From here you can drive a servo based on tilt, log motion to an SD card, or trigger an alert when the board is bumped.

Where to go next

Once you have the accelerometer working, try adding the gyroscope for full orientation tracking, or bump the sample rate up toward 200Hz for high-speed applications like vibration analysis. Because the sensors are already calibrated by the phone manufacturer, you get clean readings without the drift you'd fight with a cheap breakout board.

The ESP32 code

Here's a minimal WiFi UDP listener for the ESP32 that receives SensorCast packets and prints the three accelerometer axes to the serial monitor. Replace the network credentials and the listening port with the values shown in the app.

#include <WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>

const char* ssid     = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const uint16_t LISTEN_PORT = 5000;   // match the port shown in SensorCast

WiFiUDP udp;
char packetBuffer[512];

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
    Serial.print(".");
  }
  Serial.printf("\nConnected. IP: %s\n", WiFi.localIP().toString().c_str());
  udp.begin(LISTEN_PORT);
}

void loop() {
  int size = udp.parsePacket();
  if (size) {
    int len = udp.read(packetBuffer, sizeof(packetBuffer) - 1);
    packetBuffer[len] = '\0';

    StaticJsonDocument<256> doc;
    if (deserializeJson(doc, packetBuffer) == DeserializationError::Ok) {
      float ax = doc["x"];
      float ay = doc["y"];
      float az = doc["z"];
      Serial.printf("ax=%.3f ay=%.3f az=%.3f\n", ax, ay, az);
    }
  }
}

Upload this sketch, open the serial monitor at 115200 baud, and start the stream in the app — you'll see live accelerometer values scroll past. From here you can map any axis to a servo, threshold it for shake detection, or log it to storage.

Comments (0)

Sign in to join the conversation.

No comments yet. Be the first.