top of page

NodeMCU ADXL345

Updated: Dec 6, 2021



#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <ESP8266WiFi.h> //The library we will use for the wifi connection
#include <PubSubClient.h> // The library we will use for the mqtt connection

//mqtt connection library settings
WiFiClient espClient;
PubSubClient client(espClient);
//


//settings for internet and mqtt connection
const char* ssid = ""; // wifi SSID
const char* password =  ""; // wifi password 
const char* mqttServer = ""; // mqtt server ip  to be sent by smartespiot by mail 
const int mqttPort = ; // mqtt port   to be sent by smartespiot by mail 
const char* mqttUser = ""; // mqtt username  to be sent by smartespiot by mail 
const char* mqttPassword = ""; // mqtt pass to be sent by smartespiot by mail 



//

/* Assign a unique ID to this sensor at the same time */
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);

void displaySensorDetails(void)
{
  sensor_t sensor;
  accel.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

void displayDataRate(void)
{
  Serial.print  ("Data Rate:    ");

  switch (accel.getDataRate())
  {
    case ADXL345_DATARATE_3200_HZ:
      Serial.print  ("3200 ");
      break;
    case ADXL345_DATARATE_1600_HZ:
      Serial.print  ("1600 ");
      break;
    case ADXL345_DATARATE_800_HZ:
      Serial.print  ("800 ");
      break;
    case ADXL345_DATARATE_400_HZ:
      Serial.print  ("400 ");
      break;
    case ADXL345_DATARATE_200_HZ:
      Serial.print  ("200 ");
      break;
    case ADXL345_DATARATE_100_HZ:
      Serial.print  ("100 ");
      break;
    case ADXL345_DATARATE_50_HZ:
      Serial.print  ("50 ");
      break;
    case ADXL345_DATARATE_25_HZ:
      Serial.print  ("25 ");
      break;
    case ADXL345_DATARATE_12_5_HZ:
      Serial.print  ("12.5 ");
      break;
    case ADXL345_DATARATE_6_25HZ:
      Serial.print  ("6.25 ");
      break;
    case ADXL345_DATARATE_3_13_HZ:
      Serial.print  ("3.13 ");
      break;
    case ADXL345_DATARATE_1_56_HZ:
      Serial.print  ("1.56 ");
      break;
    case ADXL345_DATARATE_0_78_HZ:
      Serial.print  ("0.78 ");
      break;
    case ADXL345_DATARATE_0_39_HZ:
      Serial.print  ("0.39 ");
      break;
    case ADXL345_DATARATE_0_20_HZ:
      Serial.print  ("0.20 ");
      break;
    case ADXL345_DATARATE_0_10_HZ:
      Serial.print  ("0.10 ");
      break;
    default:
      Serial.print  ("???? ");
      break;
  }
  Serial.println(" Hz");
}

void displayRange(void)
{
  Serial.print  ("Range:         +/- ");

  switch (accel.getRange())
  {
    case ADXL345_RANGE_16_G:
      Serial.print  ("16 ");
      break;
    case ADXL345_RANGE_8_G:
      Serial.print  ("8 ");
      break;
    case ADXL345_RANGE_4_G:
      Serial.print  ("4 ");
      break;
    case ADXL345_RANGE_2_G:
      Serial.print  ("2 ");
      break;
    default:
      Serial.print  ("?? ");
      break;
  }
  Serial.println(" g");
}

void setup(void)
{
  Wire.begin(14, 12);
  Serial.begin(9600);
  Serial.println("Accelerometer Test"); Serial.println("");

  /* Initialise the sensor */
  if (!accel.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
    while (1);
  }

  /* Set the range to whatever is appropriate for your project */
  accel.setRange(ADXL345_RANGE_16_G);
  // accel.setRange(ADXL345_RANGE_8_G);
  // accel.setRange(ADXL345_RANGE_4_G);
  // accel.setRange(ADXL345_RANGE_2_G);

  /* Display some basic information on this sensor */
  displaySensorDetails();

  /* Display additional settings (outside the scope of sensor_t) */
  displayDataRate();
  displayRange();
  Serial.println("");

  WiFi.begin(ssid, password); // start the wifi connection process

  while (WiFi.status() != WL_CONNECTED) { //
    delay(500); //wait 500ms
    Serial.println("Connecting to wifi..."); // wait until the wifi process connects
  }
  Serial.println("Wifi connection ready :)"); // print the connection made information to the serial port

  client.setServer(mqttServer, mqttPort);//start connecting to the mqtt server from the specified port

  while (!client.connected()) { //wait until we connect to mqtt
    Serial.println("Connecting to MQTT server...");

    if (client.connect("SmartEspIOT", mqttUser, mqttPassword )) {//login to mqtt server with username and password with smartespIOT name

      Serial.println("MQTT connection established :) ");

    } else {

      Serial.print("Unable to connect... ");// If the connection cannot be established, let's give a warning
      Serial.print(client.state());             // print the problem description
      // We can understand the problem according to the information returned from the clients state.
      //-4 : MQTT_CONNECTION_TIMEOUT - the server did not respond in the ongoing time
      //-3 : MQTT_CONNECTION_LOST - network connection lost
      //-2 : MQTT_CONNECT_FAILED - network connection failed
      //-1 : MQTT_DISCONNECTED - disconnected
      //0 : MQTT_CONNECTED - connection established
      //1 : MQTT_CONNECT_BAD_PROTOCOL - The server does not support the requested MQTT version
      //2 : MQTT_CONNECT_BAD_CLIENT_ID - The server did not accept your information
      //3 : MQTT_CONNECT_UNAVAILABLE - The server did not accept the connectionetmed
      //4 : MQTT_CONNECT_BAD_CREDENTIALS - username password not accepted
      //5 : MQTT_CONNECT_UNAUTHORIZED - you are not authorized to connect


      delay(2000);   // wait two seconds and try connecting again

    }
  }

}

void loop(void)
{
  /* Get a new sensor event */
  sensors_event_t event;
  accel.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print("  "); Serial.println("m/s^2 ");

  client.publish("devicename/xacc", String(event.acceleration.x).c_str());// send xacc
  client.publish("devicename/yacc", String(event.acceleration.y).c_str());// send yacc
  client.publish("devicename/zacc", String(event.acceleration.z).c_str());// send yacc
  delay(2000); // wait two seconds and send the message again
}


adxl345_smartespiot_web2
.zip
Download ZIP • 2KB


40 views0 comments

Recent Posts

See All