Nodemcu with ADXL345

Hello Everyone,

I have interfaced Nodemcu with ADXL345 Accelerometer using SPI and the code is working fine after it gets uploaded. I am facing a problem and looking for a solution. Actually the problem is Once the code is uploaded to Nodemcu ESP8266 it works till it is powered on through USB. But if it is disconnected and repowered through USB or any other source again, I need to reupload the code to the nodemcu everytime. Please let me know if there is any solution.

Thank you

Post your code so it is clear what pins you are using. Certain pins have a special significance at boot time (0,2 and 15).

// Here’s my code please let me know tge changes

#include <SPI.h>
#include<ESP8266WiFi.h>
#include <PubSubClient.h>
//ADXL345

#define SCLpin D5//GPIO 14 Serial clock pin
#define SDOpin D6//GPIO 12 MISO pin
#define SDApin D7//GPIO 13 MOSI pin
#define SSpin D8//GPIO 15 CHip Select
//Nodemcu pin 3.3 -----> pin VCC ADXL345
//Nodemcu pin GND -----> pin GND ADXL345

char BW_RATE = 0x2C; // Data Rate and power mode control
char POWER_CTL=0x2D; // Power Control Register
char DATA_FORMAT=0x31; // Data Format Register
char DATAX0=0x32;
char DATAX1=0x33;
char DATAY0=0x34;
char DATAY1=0x35;
char DATAZ0=0x36;
char DATAZ1=0x37;

int16_t X_Data, Y_Data, Z_Data;
const char* ssid = “Senpronics”;
const char* password = “Senpronics@123”;
const char* mqtt_server = “broker.mqtt-dashboard.com”;
//const char* mqtt_server = “192.168.0.17”;

char str[10];
long lastMsg=0;
byte TO_READ = 6;

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi()
{
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(”.”);
}
randomSeed(micros());
Serial.println("");
Serial.println(“WiFi connected”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
}
void reconnect()
{
while (!client.connected())
{
Serial.print(“Attempting MQTT connection…”);
String clientId = “ESP8266Client-”;
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str()))
{
Serial.println(“connected”);
client.publish(“outTopic”, “My First MQTT Code”);
client.subscribe(“Welcome ESP8266”);
}
else
{
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(” try again in 5 seconds”);
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
SPI.begin();
SPI.beginTransaction(SPISettings(5000000,MSBFIRST,SPI_MODE3));
setup_wifi();
client.setServer(mqtt_server, 1883);
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
//pinMode(togglepin,OUTPUT);

writeRegister(POWER_CTL, 0x00);// Configured in Stand By Mode
writeRegister(POWER_CTL, 0x08); // Configured in Measurement Mode
// writeRegister(DATA_FORMAT, 0x03); // ±16g 10bit
writeRegister(DATA_FORMAT, 0x0B); // ±16g 10bit, 4mg/LSB.
// writeRegister(BW_RATE, 0x0F); // Baud Rate Register configuration. 3200 Hz
writeRegister(BW_RATE, 0x0C);
}

void loop()
{
if (!client.connected())
{
reconnect();
}

long now = micros();
if (now - lastMsg > 1)
{
lastMsg = now;
read_345();
client.publish(“ADXL_345”,str);
}
client.loop();
}
void read_345()
{
// DATAX0
readRegister(DATAX0, TO_READ, str);
X_Data= ((int16_t)str[1] << 8) | (int16_t)str[0];
Y_Data= ((int16_t)str[3] << 8) | (int16_t)str[2];
Z_Data= ((int16_t)str[5] << 8) | (int16_t)str[4];

sprintf(str,"%d %d %d",X_Data,Y_Data,Z_Data);
Serial.println(str);

}

void writeRegister(char registerAddress, char value)
{
digitalWrite(SS, LOW);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(SS, HIGH);
SPI.endTransaction();
}

void readRegister(char registerAddress, int16_t numBytes,char * str)
{
char address = (0x80 | registerAddress);
if (numBytes > 1)address = address | 0x40;
digitalWrite(SS, LOW);
SPI.transfer(address);

for (int16_t i = 0; i < numBytes; i++)
{
str = SPI.transfer(0x00);

  • }*

  • digitalWrite(SS, HIGH);*

  • SPI.endTransaction();*
    }

Maybe edit your previous post to (a) use code tags so it displays correctly here and (b) remove the credentials (password etc.) in the code.

My suggestion is to change this:

#define SSpin   D8//GPIO 15 CHip Select

to use D0 (GPIO 16) instead and then change the wiring to match.

If that doesn't work, then also post a circuit diagram which shows how you've wired it all up.

attached is the circuit design
b) code after change it prints 0 0 0 continuously

#include <SPI.h>
#include<ESP8266WiFi.h>
#include <PubSubClient.h>
//ADXL345

#define SCLpin D5//GPIO 14 Serial clock pin
#define SDOpin D6//GPIO 12 MISO pin
#define SDApin D7//GPIO 13 MOSI pin
#define SSpin D0// CS pin
//#define SSpin D8//GPIO 15 CHip Select
//Nodemcu pin 3.3 -----> pin VCC ADXL345
//Nodemcu pin GND -----> pin GND ADXL345

char BW_RATE = 0x2C; // Data Rate and power mode control
char POWER_CTL=0x2D; // Power Control Register
char DATA_FORMAT=0x31; // Data Format Register
char DATAX0=0x32;
char DATAX1=0x33;
char DATAY0=0x34;
char DATAY1=0x35;
char DATAZ0=0x36;
char DATAZ1=0x37;

int16_t X_Data, Y_Data, Z_Data;

char str[10];
long lastMsg=0;
byte TO_READ = 6;

WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
Serial.begin(115200);
SPI.begin();
SPI.beginTransaction(SPISettings(5000000,MSBFIRST,SPI_MODE3));
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);

writeRegister(POWER_CTL, 0x00);// Configured in Stand By Mode
writeRegister(POWER_CTL, 0x08); // Configured in Measurement Mode
writeRegister(BW_RATE, 0x0C);
}

void loop()
{
readRegister(DATAX0, TO_READ, str);
X_Data= ((int16_t)str[1] << 8) | (int16_t)str[0];
Y_Data= ((int16_t)str[3] << 8) | (int16_t)str[2];
Z_Data= ((int16_t)str[5] << 8) | (int16_t)str[4];

Serial.println(X_Data);
Serial.println(Y_Data);
Serial.println(Z_Data);
Serial.println();
}

void writeRegister(char registerAddress, char value)
{
digitalWrite(SS, LOW);
SPI.transfer(registerAddress);
SPI.transfer(value);
digitalWrite(SS, HIGH);
SPI.endTransaction();
}

void readRegister(char registerAddress, int16_t numBytes,char * str)
{
char address = (0x80 | registerAddress);
if (numBytes > 1)address = address | 0x40;
digitalWrite(SS, LOW);
SPI.transfer(address);

for (int16_t i = 0; i < numBytes; i++)
{
str = SPI.transfer(0x00);

hi

sorry it was mistaake in the code, it works with D0 pin, but I NEED to press reset button for nodemcu to reconnect to wifi again.

anurag2508: hi

sorry it was mistaake in the code, it works with D0 pin, but I NEED to press reset button for nodemcu to reconnect to wifi again.

can you please share the updated code?