Arduino adlx345 + esp8266 | Arduino | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Arduino adlx345 + esp8266
No permission to create posts
September 17, 2020
8:15 pm
Avatar
Drakan01
New Member
Members
Forum Posts: 1
Member Since:
September 17, 2020
sp_UserOfflineSmall Offline

hi, i'm doing a project and i need really help with this.

so using adlx345 i want send data to esp8266 and then, see that data in thingspeak, the problem is i can't make the right conexion with adlx345 and esp8266

can anyone helpme?

#include "ThingSpeak.h"
#include "secrets2.h"
#include

//adlx

#include // Wire library - used for I2C communication
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
float X_out, Y_out, Z_out;
float roll,pitch,rollF,pitchF=0;

//esp8266
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
void setup() {

Serial.begin(115200); // Initiate serial communication for printing the results on the Serial monitor
pinMode(9, OUTPUT);
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345); // Start communicating with the device
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(8); // Bit D3 High for measuring enable (8dec -> 0000 1000 binary)
Wire.endTransmission();
delay(10);
//Off-set Calibration
//X-axis
Wire.beginTransmission(ADXL345);
Wire.write(0x1E);
Wire.write(1);
Wire.endTransmission();
delay(10);
//Y-axis
Wire.beginTransmission(ADXL345);
Wire.write(0x1F);
Wire.write(-2);
Wire.endTransmission();
delay(10);
//Z-axis
Wire.beginTransmission(ADXL345);
Wire.write(0x20);
Wire.write(-9);
Wire.endTransmission();
delay(10);
//esp
Serial.begin(115200); // Initialize serial
//esp
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}

void loop() {
//adlx345
// === Read acceleromter data === //
Wire.beginTransmission(ADXL345);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read() | Wire.read() << 8); // X-axis value
X_out = X_out / 256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read() | Wire.read() << 8); // Y-axis value
Y_out = Y_out / 256;
Z_out = ( Wire.read() | Wire.read() << 8); // Z-axis value
Z_out = Z_out / 256;
// Calculate Roll and Pitch (rotation around X-axis, rotation around Y-axis)
roll = atan(Y_out / sqrt(pow(X_out, 2) + pow(Z_out, 2))) * 180 / PI;
pitch = atan(-1 * X_out / sqrt(pow(Y_out, 2) + pow(Z_out, 2))) * 180 / PI;
// Low-pass filter
rollF = 0.94 * rollF + 0.06 * roll;
pitchF = 0.94 * pitchF + 0.06 * pitch;
Serial.print(" X= ");
Serial.print(rollF);
Serial.print(" Y= ");
Serial.print(pitchF );//ln na ultima linha
Serial.print(" Z= ");
Serial.println(Z_out);

//esp8266
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("
Connected.");
}
// set the fields with the values
ThingSpeak.setField(1, X_out);
ThingSpeak.setField(2, Y_out);
ThingSpeak.setField(3, Z_out);

// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}

// change the value
rollF = X_out;
pitchF = Y_out;
Z_out = Z_out;

delay(20000); // Wait 20 seconds to update the channel again
}

September 21, 2020
1:26 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 863
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I suggest you separate ThingSpeak from the problem. See if you can get the accelerometer data to write to the serial monitor. Then see if you can get an fixed number or string to write to ThingSpeak. Then write the accelerometer output to ThingSpeak (if that is your goal).

What do you expect to see? What are you actually seeing?

FYI, MATLAB answers is the preferred location for ThingSpeak Community support.

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
22 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

daner, Tito Silveira, HomeImprove123, Bryan, kenwood1701, dosh2684

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457