Esp8266 ile veri tabanından hızlıca veri okuma ve yazma

segoman99

Üye
Katılım
11 Ağu 2021
Mesajlar
5
Puanları
1
Yaş
44
Merhabalar. Arkadaşımla bir proje üzerine çalışıyoruz. Amacımız Nodemcu ESP8266 kullanarak karşıya ses dosyası yollamak. Öncelikle elimizdeki ses dosyasını bir programla çözümledik (encode etme) . Bu çözümlediğimiz ses dosyası örneklerini (sample) XAMPP server kullanarak oluşturduğumuz MySQL veritabanına attık. Ses örneklerini (sample) oldukça hızlı bir şekilde veritabanına atabildik ancak örnekleri veritabanından yavaş indirebiliyoruz. Elimizdeki ses örneklerinin indirilme hızını nasıl arttırabiliriz veya daha önce webserver üzerinden Nodemcu kullanarak ses dosyası atmış varsa yardımcı olabilir mi? İlginiz için teşekkürler.
Bu kısım ses örneklerini indirmek için kullandığımız kod. #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266HTTPClient.h> #include "LittleFS.h" const char* ssid = "****"; //--> Your wifi name or SSID. const char* password = "****"; //--> Your wifi password. WiFiClient wifiClient; IPAddress ip1 = ipaddr_addr("192.168.1.103"); IPAddress ip2 = ipaddr_addr("192.168.1.1"); IPAddress ip3 = ipaddr_addr("255.255.255.0"); IPAddress ip4 = ipaddr_addr("8.8.8.8");//dns 1 IPAddress ip5 = ipaddr_addr("8.8.4.4");//dns 2 const char *host = "192.168.1.106"; String host2 = "192.168.1.103"; HTTPClient http; //--> Declare object of class HTTPClient File file1; File file2; File file3; File file4; File file5; File file6; File file7; File file8 ; File file9 ; File file10 ; int id = 1; int sampleAmount=100; void connectionRestarting(); String payloadGet(String GetAddress); void endofFetching(); void setup() { Serial.begin(115200); LittleFS.begin(); File file1 = LittleFS.open("/audiosample1.txt","w"); File file2 = LittleFS.open("/audiosample2.txt","w"); File file3 = LittleFS.open("/audiosample3.txt","w"); File file4 = LittleFS.open("/audiosample4.txt","w"); File file5 = LittleFS.open("/audiosample5.txt","w"); File file6 = LittleFS.open("/audiosample6.txt","w"); File file7 = LittleFS.open("/audiosample7.txt","w"); File file8 = LittleFS.open("/audiosample8.txt","w"); File file9 = LittleFS.open("/audiosample9.txt","w"); File file10 = LittleFS.open("/audiosample10.txt","w"); delayMicroseconds(100); file1.println("OPEN"); file2.println("OPEN"); file3.println("OPEN"); file4.println("OPEN"); file5.println("OPEN"); file6.println("OPEN"); file7.println("OPEN"); file8.println("OPEN"); file9.println("OPEN"); file10.println("OPEN"); delayMicroseconds(100); file1.close(); file2.close(); file3.close(); file4.close(); file5.close(); file6.close(); file7.close(); file8.close(); file9.close(); file10.close(); delayMicroseconds(100); WiFi.mode(WIFI_OFF); WiFi.config(ip1,ip2, ip3,ip4,ip5); Serial.setTimeout(1); delay(5); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.setOutputPower(20.5); 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"); Serial.print(WiFi.localIP()); delay(10); Serial.println("connecting..."); wifiClient.setNoDelay(1); } void loop() { if (wifiClient.connect(host,80)){ String GetAddress1,GetAddress2,GetAddress3,GetAddress4,GetAddress5,GetAddress6,GetAddress7,GetAddress8,GetAddress9,GetAddress10,ID,payload; for(int i=1;i<=(sampleAmount);i++){ ID=String(id); GetAddress1 = "http://192.168.1.106:80/getData/getData1.php?ID="+ID; payload=payloadGet(GetAddress1); File file1 = LittleFS.open("/audiosample1.txt","a"); delayMicroseconds(10); file1.println(payload); GetAddress2 = "http://192.168.1.106:80/getData/getData2.php?ID="+ID; payload=payloadGet(GetAddress2); File file2 = LittleFS.open("/audiosample2.txt","a"); delayMicroseconds(10); file2.println(payload); GetAddress3 = "http://192.168.1.106:80/getData/getData3.php?ID="+ID; payload=payloadGet(GetAddress3); File file3 = LittleFS.open("/audiosample3.txt","a"); delayMicroseconds(10); file3.println(payload); GetAddress4 = "http://192.168.1.106:80/getData/getData4.php?ID="+ID; payload=payloadGet(GetAddress4); File file4 = LittleFS.open("/audiosample4.txt","a"); delayMicroseconds(10); file4.println(payload); GetAddress5 = "http://192.168.1.106:80/getData/getData5.php?ID="+ID; payload=payloadGet(GetAddress5); File file5 = LittleFS.open("/audiosample5.txt","a"); delayMicroseconds(10); file5.println(payload); GetAddress6 = "http://192.168.1.106:80/getData/getData6.php?ID="+ID; payload=payloadGet(GetAddress6); File file6 = LittleFS.open("/audiosample6.txt","a"); delayMicroseconds(10); file6.println(payload); GetAddress7 = "http://192.168.1.106:80/getData/getData7.php?ID="+ID; payload=payloadGet(GetAddress7); File file7 = LittleFS.open("/audiosample7.txt","a"); delayMicroseconds(10); file7.println(payload); GetAddress8 = "http://192.168.1.106:80/getData/getData8.php?ID="+ID; payload=payloadGet(GetAddress8); File file8 = LittleFS.open("/audiosample8.txt","a"); delayMicroseconds(10); file8.println(payload); GetAddress9 = "http://192.168.1.106:80/getData/getData9.php?ID="+ID; payload=payloadGet(GetAddress9); File file9 = LittleFS.open("/audiosample9.txt","a"); delayMicroseconds(10); file9.println(payload); GetAddress10 = "http://192.168.1.106:80/getData/getData10.php?ID="+ID; payload=payloadGet(GetAddress10); File file10 = LittleFS.open("/audiosample10.txt","a"); delayMicroseconds(10); file10.println(payload); id+=1; Serial.println(id); connectionRestarting(); } file1.close(); file2.close(); file3.close(); file4.close(); file5.close(); file6.close(); file7.close(); file8.close(); file9.close(); file10.close(); endofFetching(); } else{ connectionRestarting(); } } void connectionRestarting(){ http.end(); //--> Close connection delayMicroseconds(100); WiFi.config(ip1,ip2, ip3,ip4,ip5); WiFi.setOutputPower(20.5); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(""); Serial.println("WiFi connected"); wifiClient.setNoDelay(1); Serial.println(WiFi.localIP()); } String payloadGet(String GetAddress){ http.begin(wifiClient,GetAddress.c_str()); //--> Specify request destination http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header http.addHeader("Content-Length", "3"); //Specify content-type header int httpCodeGet = http.GET(); //--> Send the request String payloadGet = http.getString(); //--> Get the response payload from server delayMicroseconds(10); Serial.print("Returned data from Server : "); Serial.println(payloadGet); //--> Print request response payload if (payloadGet==""){ WiFi.disconnect(); WiFi.setAutoConnect(false); WiFi.mode(WIFI_OFF); delayMicroseconds(100); connectionRestarting(); } else{ return payloadGet; } } void endofFetching(){ Serial.println("Audio file has been fetched."); File filenew1 = LittleFS.open("/audiosample1.txt","r"); File filenew2 = LittleFS.open("/audiosample2.txt","r"); File filenew3 = LittleFS.open("/audiosample3.txt","r"); File filenew4 = LittleFS.open("/audiosample4.txt","r"); File filenew5 = LittleFS.open("/audiosample5.txt","r"); File filenew6 = LittleFS.open("/audiosample6.txt","r"); File filenew7 = LittleFS.open("/audiosample7.txt","r"); File filenew8 = LittleFS.open("/audiosample8.txt","r"); File filenew9 = LittleFS.open("/audiosample9.txt","r"); File filenew10 = LittleFS.open("/audiosample10.txt","r"); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew1.read()); } filenew1.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew2.read()); } filenew2.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew3.read()); } filenew3.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew4.read()); } filenew4.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew5.read()); } filenew5.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew6.read()); } filenew6.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew7.read()); } filenew7.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew8.read()); } filenew8.close(); delay(10); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew9.read()); } filenew9.close(); Serial.println(""); for(int i=0;i<=(sampleAmount-1);i++){ Serial.write(filenew10.read()); } filenew10.close(); }

Bu kısım ise ses örneklerini veritabanına yüklemeek için kullandığımız kod. #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <SPI.h> const char* ssid = "****";// const char* password = "****"; //WiFiClient client; char server[] = "****"; int sampleAmount=3000; int i=0; const PROGMEM byte sample1[]= {***}; const PROGMEM byte sample2[]= {**}; const PROGMEM byte sample3[]= {**}; const PROGMEM byte sample4[]= {***}; const PROGMEM byte sample5[]= {***}; const PROGMEM byte sample6[]= {***}; const PROGMEM byte sample7[]= {***}; const PROGMEM byte sample8[]= {***}; const PROGMEM byte sample9[]= {***}; const PROGMEM byte sample10[]= {***}; WiFiClient client; void setup() { Serial.begin(115200); Serial.setTimeout(1); delay(10); Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.setOutputPower(20.5); 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"); Serial.print(WiFi.localIP()); delay(1000); Serial.println("connecting..."); client.setNoDelay(1); } void loop(){ if (client.connect(server, 80)){ Serial.println("connected"); // Make a HTTP request: client.print("GET /multidata/multisample1.php?audiosample1="); HTTPRequest(pgm_read_byte(&sample1[i])); client.print("GET /multidata/multisample2.php?audiosample2="); HTTPRequest(pgm_read_byte(&sample2[i])); client.print("GET /multidata/multisample3.php?audiosample3="); HTTPRequest(pgm_read_byte(&sample3[i])); client.print("GET /multidata/multisample4.php?audiosample4="); HTTPRequest(pgm_read_byte(&sample4[i])); client.print("GET /multidata/multisample5.php?audiosample5="); HTTPRequest(pgm_read_byte(&sample5[i])); client.print("GET /multidata/multisample6.php?audiosample6="); HTTPRequest(pgm_read_byte(&sample6[i])); client.print("GET /multidata/multisample7.php?audiosample7="); HTTPRequest(pgm_read_byte(&sample7[i])); client.print("GET /multidata/multisample8.php?audiosample8="); HTTPRequest(pgm_read_byte(&sample8[i])); client.print("GET /multidata/multisample9.php?audiosample9="); HTTPRequest(pgm_read_byte(&sample9[i])); client.print("GET /multidata/multisample10.php?audiosample10="); HTTPRequestLast(pgm_read_byte(&sample10[i])); if(i!=(sampleAmount-1)){ i+=1; Serial.println(i); connectionRestarting(); } else{ endofUploading(); } } } void HTTPRequest(byte sample){ Serial.println(sample); client.print(sample); client.print(" "); //SPACE BEFORE HTTP/1.1 client.print("HTTP/1.1"); client.println(); client.println("Host: 192.168.1.106"); client.println("Connection: keep-alive\r\n\r\n\r\n"); client.println(); } void HTTPRequestLast(byte sample){ Serial.println(sample); client.print(sample); client.print(" "); //SPACE BEFORE HTTP/1.1 client.print("HTTP/1.1"); client.println(); client.println("Host: 192.168.1.106"); client.println("Connection: close"); client.println(); } void connectionRestarting(){ delayMicroseconds(100); WiFi.setOutputPower(20.5); WiFi.begin(ssid, password); Serial.println("WiFi connected"); Serial.println("Server started"); Serial.print(WiFi.localIP()); delayMicroseconds(100); client.setNoDelay(1); } void endofUploading() { Serial.println("Audio file has been sent."); //All of samples have been sent. endofUploading(); }
 
Birincisi server olarak raspberry pi kullanıyorsanız yavaş olması normaldir. Ucuz yollu bi hosting paketi alıp büyük bir servera yüklemeyi deneyin. İkincisi mysql veritabanı yerine doğrudan dosya olarak kaydetmeyi ve geri çağırmayı deneyin.
 
Birincisi server olarak raspberry pi kullanıyorsanız yavaş olması normaldir. Ucuz yollu bi hosting paketi alıp büyük bir servera yüklemeyi deneyin. İkincisi mysql veritabanı yerine doğrudan dosya olarak kaydetmeyi ve geri çağırmayı deneyin.
Hocam geç gördüm mesajınızı, teşekkür ederim. Sorunu hallettik.
 

Forum istatistikleri

Konular
129,834
Mesajlar
930,598
Kullanıcılar
452,662
Son üye
Yılmaz Çetiner

Yeni konular

Geri
Üst