Hello Everyone,
I use a Node MCU / SD Card Reader-Writer / Accelerometer LIS3DH.
I have a problem because I can turn on or turn of the sensor by the webpage, but when I want to write the the data on the SD card by the webpage button...It writes always the same values.
Here my code :
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
// Used for software SPI
#define LIS3DH_CLK 13
#define LIS3DH_MISO 12
#define LIS3DH_MOSI 11
// Used for hardware & software SPI
#define LIS3DH_CS 10
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
#include <SPI.h>
#include <SD.h>
const char* ssid = "-------";
const char* password = "--------";
int T;
int ledPin = 13; // GPIO13
WiFiServer server(80);
File myFile;
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
#ifndef ESP8266
while (!Serial); // will pause Zero, Leonardo, etc until serial console opens
#endif
Serial.println("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1);
}
Serial.println("LIS3DH found!");
lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
delay(1000);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
myFile = SD.open("test.txt", FILE_WRITE);
Serial.print("Writing to test.txt...");
for (int t =0 ; t<5000 ; t++ ){
sensors_event_t event;
lis.getEvent(&event);
myFile.print("\t\tX: "); myFile.print(event.acceleration.x);
myFile.print(" \tY: "); myFile.print(event.acceleration.y);
myFile.print(" \tZ: "); myFile.print(event.acceleration.z);
myFile.println(" m/s^2 ");
value = HIGH;
t=t+1;
delay(1);
myFile.flush();
}
myFile.close();
while (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
break;
}
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<h1 style=text-align:center;font-size:300%;color:blue;font-family:britannic bold;>North Sails</h1>");
client.println("<TEXTAREA name="" rows=4 cols=8>Time (seconds)</TEXTAREA>");
client.println("<a href=\"\"\"><button>Send </button></a>");
client.println("
");
client.print("Accelerometer is now: ");
if(value == LOW) {
client.print("Off");
}
client.println("
");
client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a>");
client.println("</html>");
Serial.println("Client disonnected");
Serial.println("");
}
Thank you for your help!
U.D