Sending Accelerometer ADXL335 data to MQTT Server using ESP32 with Arduino IDE

Updated: Jun 27

This article covers the understading of ADXL335 Accelerometer, ESP32 Microcontroller, MQTT Server, Integration of all these three blocks using Arduino IDE.




Accelerometer

Accelerometer is an electromechanical device used for measuring the acceleration of an object on which the sensor is mounted. Accelerometer sensor is widely used in many applications nowadays starting from being present in all the smartphones, medical equipments, detecting fall or any object, detecting earthquacks, etc.

ADXL335 Accelerometer Module

ADXL335 Accelerometer Sensor

ADXL335 Accelerometer is an analog sensor which provides 3-axis acceleration in X, Y and Z axis. This works at 1.8V to 3.3V voltage level. This gives acceleration in the range of +/- 3g.


We will be using module of ADXL335 to integrate with ESP32.




ESP32


ESP32 is a single 2.4 GHz Wi-Fi-and-Bluetooth combo chip designed with the TSMC ultra-low-power 40 nm technology. (Technical Datasheet)



We will be using ESP32-WROOM32 development kit for our experiment which we can power-up using USB 5V power source.





MQTT Server

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. (Reference Link)



As you observe in the above picture,

- Device/Sensor in our application will be ADXL335 Acceleromter Sensor with ESP32

- MQTT Broker is where all the sensor data will be posted

- Mobile/Laptop is where you will see the data recieved on your MQTT broker


Design Approach for the System


For quick prototype system, most of the components are purchased off-the shelf which reduces the time to develop and print PCBs but increases the cost a bit.


Accelerometer:

1. ADXL335 is chosen for the detection of speed of movement where sensor will be mounted on the object itself.

2. It’s a 3-axis analog sensor which given output for X, Y, Z-axis.

3. Analog sensor is used because it’s easy to interface and easily available in the market.


Micro-controller with built-in Wi-Fi:

1. Ideal choice with this requirement is ESP32 which has multiple GPIOs with Wi-Fi Antenna on the module itself which serves both purposes.

2. ESP32 EVB is used for the development where the programming can be done using Arduino IDE which makes it more user-friendly to write the business logic.

3. RGB LED is connected to the ESP32 module for understanding of the state of system and

debug if any failure occurring.


Power Block:

1. Since independent power required, Li-po battery is good choice which meets the capacity and voltage specification.

2. Li-Po battery with 3S configuration is chosen for the design which gives nominal voltage of 11.1V with 1500mAh capacity.

3. XT60 end connector Is used between Li-Po Battery & the USB Adapter converter board for high current contact & ease in removal of battery source to replace/charge.

4. 2S-6S input rage is feasible for the USB Adapter board which makes it safer in terms of any fluctuations on the battery side should not impact the USB power supply.

5. USB A type is the output connector of the USB Adapter board, so we need to use a micro-USB standard cable to power the ESP32 module via battery source


Block Diagram






Schematic Diagram


Schematic is designed in Autodesk Eagle platform. Link to download the editor.


ADXL335 Block



ESP32 WROOM32 Block



Bill of Materials (BOM)



MQTT Server Setup Steps


Step 1: Visit http://www.hivemq.com/demos/websocket-client/ which will act as broker for our data server


Step 2: Do the setup at hivemq end. (Note: Following image shows connected connection)



Step 3: Subscribe the channel where we are publishing our data from ESP32

Part A – mytopic/kitchenMotion



Step 4: You will see your messages on the MQTT Subscribed channel.



Demo Video




Source Code


/*
 * Acceleromter based intensity (slow vs fast pickup) of Item picked up 
 * 
*/


#include "EspMQTTClient.h"

const int xpin = 32;                  // x-axis of the accelerometer
const int ypin = 35;                  // y-axis
const int zpin = 34;                  // z-axis (only on 3-axis models)
const int redled = 17;
const int greenled = 16;

int device_id = 4545;

EspMQTTClient client(
  "DM_hotspot",
  "abcd1234",
  "broker.hivemq.com",  // MQTT Broker server ip
  "TestClientTest",     // Client name that uniquely identify your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);


void setup() {
  // initialize the serial communications:
  Serial.begin(115200);

  pinMode(greenled, OUTPUT);
  pinMode(redled, OUTPUT);
  digitalWrite(greenled, LOW);
  digitalWrite(redled, LOW);

  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
}

void onConnectionEstablished()
{
  //client.subscribe("mytopic/kitchenMotion", [](const String & payload) {
    //Serial.println(payload);
  //});

  client.publish("mytopic/kitchenMotion", "Device booted"); // You can activate the retain flag by setting the third parameter to true
}

float gx = 0;
float gy = 0;
float gz = 0;

float gx_prev = 0, gy_prev = 0, gz_prev = 0;

int xadc = 0; int yadc = 0; int zadc = 0;
int intensity = 0;
int action = 0;
int action_ts = 0;
int last_millis = 0;

void loop() {
  xadc = 0;
  yadc = 0;
  zadc = 0;
  for (int i = 0; i < 100; i++)
  {
    xadc += analogRead(xpin);
    yadc += analogRead(ypin);
    zadc += analogRead(zpin);
    delayMicroseconds(100);
  }
  xadc /= 100;
  yadc /= 100;
  zadc /= 100;

  gx_prev = gx;
  gy_prev = gy;
  gz_prev = gz;
  gx = (xadc - 2048) / 330.0;
  gy = (yadc - 2048) / 330.0;
  gz = (zadc - 2048) / 330.0;

  float g = sqrt(gx * gx + gy * gy + gz * gz);
  float g_prev = sqrt(gx_prev * gx_prev + gy_prev * gy_prev + gz_prev * gz_prev);
  //  Serial.printf("g: %.3f,\t gx: %.3f,\t gy: %.3f,\t gz: %.3f \n", g, gx, gy, gz);

  int intensity_prev = intensity;
  Serial.printf("delta: %.1f \t", abs(g - g_prev));
  if (abs(g - g_prev) <= 0.1) {
    //No motion
    Serial.println("");
  }
  else if (abs(g - g_prev) <= 0.75) {
    //Slow motion
    if (intensity_prev <= 1) {
      action = 1;
      intensity = 1;
      action_ts = millis();
      digitalWrite(greenled, LOW);
      digitalWrite(redled, HIGH);
      Serial.println("LOW");
    }
    else Serial.println("LOW skipped");
  }
  else if (abs(g - g_prev) <= 2.5) {
    //Med motion
    if (intensity_prev <= 2) {
      intensity = 2;
      action = 1;
      action_ts = millis();
      digitalWrite(greenled, LOW);
      digitalWrite(redled, LOW);
      Serial.println("MED");
    }
    else Serial.println("MID skipped");
  }
  else if (abs(g - g_prev) > 2.5 ) {
    //Fast motion
    intensity = 3;
    action = 1;
    action_ts = millis();
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
    Serial.println("HIGH");
  }

  if (millis() - last_millis >= 5000) {
    String message_string = "{\"TS\":" + String(millis()) + "," \
                            + "\"Device ID\":" + String(device_id) + "," \
                            + "\"Intensity\":" +  String(intensity) + "," \
                            + "\"Status\":" +  String(intensity > 0) + "," \
                            + "\"xadc\":" + String(xadc) + "," + "\"yadc\":" + String(yadc) + "," + "\"zadc\":" + String(zadc) + "}" ;
    client.publish("mytopic/kitchenMotion", message_string);
    last_millis = millis();
    intensity = 0;
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, HIGH);
  }
  delay(100);
  client.loop();

}


For more details, Reach out to us. You can fill the form available on the website (www.allaboutprojects.in) or directly call/WhatsApp us on +91-9545954177.


-

Technical Team Member

#accelerometer #adxl335 #esp32 #wifi #arduino #mqtt #server #technical #allaboutprojects

COMPANY

Careers

G-20, Cosmos Square,

Rustomjee Global City,

Virar (West), MH - 401303

RESOURCES

CAPABILITIES

Always be the first to know

WEBSITE

+91 - 9545954177

FOLLOW US:

  • Facebook
  • LinkedIn
  • Instagram
  • Twitter

©2018 by All About Projects. Made with ❤️ in India!
All Rights Reserved.