Skip to content

Instantly share code, notes, and snippets.

@milesburton
Created January 20, 2019 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milesburton/1028dba934e7433cac144470039752ba to your computer and use it in GitHub Desktop.
Save milesburton/1028dba934e7433cac144470039752ba to your computer and use it in GitHub Desktop.
ADXL345 with ESP8266
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// ADXL345
// This code is designed to work with the ADXL345_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/content/Accelorometer?sku=ADXL345_I2CS#tabs-0-product_tabset-2
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
// ADXL345 I2C address is 0x53(83)
#define Addr 0x53
const uint8_t scl = 14; //D5
const uint8_t sda = 12; //D6
const char* ssid = "XXXXXX";
const char* password = "XXXXX";
float xAccl, yAccl, zAccl;
ESP8266WebServer server(80);
void handleroot()
{
unsigned int data[6];
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select bandwidth rate register
Wire.write(0x2C);
// Normal mode, Output data rate = 100 Hz
Wire.write(0x0A);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select power control register
Wire.write(0x2D);
// Auto-sleep disable
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data format register
Wire.write(0x31);
// Self test disabled, 4-wire interface, Full resolution, Range = +/-2g
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
for (int i = 0; i < 6; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((50 + i));
// Stop I2C transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
// Read 6 bytes of data
// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
if (Wire.available() == 1)
{
data[i] = Wire.read();
}
}
// Convert the data to 10-bits
int xAccl = (((data[1] & 0x03) * 256) + data[0]);
if (xAccl > 511)
{
xAccl -= 1024;
}
int yAccl = (((data[3] & 0x03) * 256) + data[2]);
if (yAccl > 511)
{
yAccl -= 1024;
}
int zAccl = (((data[5] & 0x03) * 256) + data[4]);
if (zAccl > 511)
{
zAccl -= 1024;
}
// Output data to serial monitor
Serial.print("Acceleration in X-Axis : ");
Serial.println(xAccl);
Serial.print("Acceleration in Y-Axis : ");
Serial.println(yAccl);
Serial.print("Acceleration in Z-Axis : ");
Serial.println(zAccl);
delay(300);
// Output data to web server
server.sendContent
("<html><head><meta http-equiv='refresh' content='3'</meta>"
"<h1 style=text-align:center;font-size:300%;color:blue;font-family:britannic bold;>CONTROL EVERYTHING</h1>"
"<h3 style=text-align:center;font-family:courier new;><a href=http://www.controleverything.com/ target=_blank>www.controleverything.com</a></h3><hr>"
"<h2 style=text-align:center;font-family:tahoma;><a href= https://www.controleverything.com/content/Accelorometer?sku=ADXL345_I2CS#tabs-0-product_tabset-2/ \n"
"target=_blank>AXDL345 Sensor I2C Mini Module</a></h2>");
server.sendContent
("<h3 style=text-align:center;font-family:tahoma;>Acceleration in X-Axis : " + String(xAccl));
server.sendContent
("<h3 style=text-align:center;font-family:tahoma;>Acceleration in Y-Axis : " + String(yAccl));
server.sendContent
("<h3 style=text-align:center;font-family:tahoma;>Acceleration in Z-Axis : " + String(zAccl));
}
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin(sda, scl);
// Initialise serial communication, set baud rate = 115200
Serial.begin(115200);
// Connect to WiFi network
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
// Get the IP address of ESP8266
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.on("/", handleroot);
server.begin();
Serial.println("HTTP server started");
}
void loop()
{
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment