Go Down

Topic: (WeMos D1 ESP Wroom 02 Board ESP8266 Mini-WiFi Nodemcu Module) with ADXL345. (Read 1 time) previous topic - next topic

anurag2508

Hello Everyone,

I have interfaced wemos ESP8266 battery module with ADXL_345 and I am uploading the values on the server. I can see the values uploading over the server.

In the code I am giving SPI clock rate of 40 MHz and I have checked from different clock rate values from 1 MHz to 40 MHz. Actually ,I want the minimum 1K sample rate over wifi, But After uploading the code When i check the SPI clock rate on ADXL_345 over oscilloscope it comes in  around (90 - 160) hertz even if I comment serial section in the programming and Data rate on SDO pin also comes in Hertz.

But if I check the same thing on the device on the normal code without any wifi settings in that case i can see data rate  at around 20 K and SPI clock at specified clock rate in the code.

I am not able to understand the problem here. Is it wifi connectivity issue or coding issue?

PaulS

Quote
Is it wifi connectivity issue or coding issue?
Probably.

Since you have posted no code, or any links to any products, we can not see what you are doing in the code or with the hardware.

Are you communicating with the ADXL345 using SPI? That appears to be an I2C device, so using SPI to communicate with it would be strange.
The art of getting good answers lies in asking good questions.

anurag2508

Hello,

Thank you for your reply, Actually the one ADXL_345 board which I am using supports both the interfaces SPI and I2C I have attached the picture of it with this reply and Also I have posted the code below.

link for the esp8266 board which I am using for this project.

https://robu.in/product/wemos-d1-esp-wroom-02-board-esp8266-mini-wifi-nodemcu-module-18650-battery/

Code: [Select]



#define INTERVAL 10
#define SCLpin   14 //D5=
#define SDOpin   12 //D6
#define SDApin   13 //D7
#define SSpin     5 //D1

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;

int X_Data, Y_Data, Z_Data;


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(40000000,MSBFIRST,SPI_MODE3));//max 40 MHz

  setup_wifi();
  client.setServer(mqtt_server, 1883);
  
  pinMode(SSpin , OUTPUT);
  digitalWrite(SSpin , HIGH);
  
  writeRegister(POWER_CTL, 0x00);// Configured in Stand By Mode
  writeRegister(POWER_CTL, 0x08); // Configured in Measurement Mode
  writeRegister(DATA_FORMAT, 0x0B); // ±16g 10bit, 4mg/LSB.
  writeRegister(BW_RATE, 0x0F); // Baud Rate Register configuration. 3200 Hz
}

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

  long now = millis();
  if (now - lastMsg > INTERVAL)
  {
    lastMsg = now;
    read_345();
    client.publish("ADXL_345",str);
  }
   client.loop();
}
void read_345()
{
// DATAX0
  readRegister(DATAX0, TO_READ, str);
  X_Data= ((int)str[1] << 8) & (0x1F00) | (int)str[0];//(0xF00 for getting 13 bit output)
  Y_Data= ((int)str[3] << 8) & (0x1F00) | (int)str[2];
  Z_Data= ((int)str[5] << 8) & (0x1F00) | (int)str[4];
  
  sprintf(str,"%d %d %d",X_Data,Y_Data,Z_Data);
  Serial.println(str);
}


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

void readRegister(char registerAddress, int16_t numBytes,char * str)
{
    char address = (0x80 | registerAddress);
    if (numBytes > 1)address = address | 0x40;
    digitalWrite(SSpin , LOW);
    SPI.transfer(address);
    
       for (int i = 0; i < numBytes; i++)
       {
            str[i] = SPI.transfer(0x00);
       }
    SPI.endTransaction();
    digitalWrite(SSpin , HIGH);
    
}


Go Up