Hey folks,
I'm using a NodeMCU v3 with some stuff connected to it.
Long story short: I have to define my SD-reader-pins freely.
The only library I've found to be able to is mySD. (GitHub - nhatuan84/esp32-micro-sdcard)
But: It is outdated and also will it redefine File, so I have to use #define FS_NO_GLOBALS
first.
But this makes the ESP8266 Webserver unusable.
What I need is a simple server to download one single file from this NodeMCU's SD.
It's name is speed.txt and at the end of the day there are only two things needed:
Get this file from the SD via Wifi (What I use for NTP anyways) and, which is a second thing I'd like to do, empty the same file. (with whatever command).
However, the downloading is my #1 priority.
So can anyone suggest me a webserver / code that is small and easy for transferring this file?
Actually my talents in C are very limited (Any examples or whatever are highly appreciated!)
Regards,
Maeffjus
Here is my code, for your information:
#define FS_NO_GLOBALS
#include <NTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <mySD.h>
#include <FS.h>
#include <WiFiClient.h>
#include <time.h>
// VARIABLES
File root;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
float laenge;
String Geschwindigkeit_lang;
float Geschwindigkeit;
float max_geschwindigkeit;
String Richtung;
boolean ankommend;
unsigned long previousMillis = 0;
unsigned long currentMillis;
unsigned long old_speed_Millis;
const long speed_interval = 30000;
float totalmax;
float speed[200];
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 7200, 60000);
void setup() {
Serial.begin(115200);
Serial.swap();
Wire.begin(D2, D1); /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
WiFi.begin("NETWORK", "PASSWORD");
lcd.init();
lcd.backlight();
lcd.home();
lcd.clear();
lcd.setCursor ( 0, 3 );
lcd.print(WiFi.localIP());
lcd.setCursor ( 0, 3 );
delay (5000);
lcd.print(WiFi.localIP());
pinMode(D6, OUTPUT); //SDCard CS-Pin
SD.begin(D6, D4, 3, D5);
}
void loop() {
recvWithEndMarker();
umrechnung();
maximum();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
//Serial.println(receivedChars);
lcd.clear();
lcd.setCursor ( 0, 0 ); // go to the next line
lcd.print(max_geschwindigkeit);
lcd.setCursor ( 0, 1 );
lcd.print(Richtung);
lcd.setCursor ( 0, 2 );
lcd.print(ankommend);
lcd.print(" ");
lcd.print(totalmax);
lcd.print("km/h");
lcd.setCursor ( 0, 3 );
lcd.print(timeClient.getFormattedDate());
lcd.print(" ");
//lcd.setCursor ( 11, 3 );
lcd.print(timeClient.getFormattedTime());
lcd.print(" ");
//lcd.setCursor ( 17, 3 );
lcd.print(max_geschwindigkeit);
newData = false;
}
}
void umrechnung() {
if (newData == true) {
Geschwindigkeit_lang = receivedChars;
Geschwindigkeit_lang.remove(0, 12);
Geschwindigkeit_lang.remove(5);
Geschwindigkeit = Geschwindigkeit_lang.toFloat();
Richtung = receivedChars;
Richtung.remove(0, 19);
Richtung.remove(1);
if (Richtung == "+"){
ankommend = false;}
if (Richtung == "-"){
ankommend = true;}
}
}
void maximum(){
currentMillis = millis();
if ((newData == true)&(currentMillis - old_speed_Millis > speed_interval)){
old_speed_Millis = currentMillis;
if (max_geschwindigkeit >10){
sdwrite();
}
max_geschwindigkeit = 0;
return;
}
if ((newData == true)&(currentMillis - old_speed_Millis <= speed_interval)){
if (Geschwindigkeit > max_geschwindigkeit){
max_geschwindigkeit = Geschwindigkeit;
}
if (totalmax < max_geschwindigkeit){
totalmax = max_geschwindigkeit;
}
}
}
void sdwrite(){
root = SD.open("speed.txt", FILE_WRITE);
root.print(timeClient.getFormattedDate());
root.print(",");
root.print(timeClient.getFormattedTime());
root.print(",");
root.print(max_geschwindigkeit);
root.print(",");
root.println(ankommend);
root.flush();
root.close();
}