Reputation: 15
so I tried to pogramm a AI but the problem is now the error message and the esp32 resetting. There is no problem with the sd reader bc it is working with my other prject and there is nothing else on the esp32 the connection are well soldered so that is no problem. I would appreciate any comment for helping! Thank you
Connections:
MOSI: GPIO 23
MISO: GPIO 19
SCK: GPIO 18
CS: GPIO 5
GND: GND VCC: 5V
#include <WiFi.h>
#include <WebServer.h>
#include <SPI.h>
#include <SD.h>
#include <vector>
#include <map>
#include <set>
#define SD_CS_PIN 5 // SD Card Chip Select pin
const char* ssid = "ESP32_KI";
const char* password = "12345678";
WebServer server(80);
// Structure to hold a question-answer pair
struct QAPair {
String question;
String answer;
};
std::vector<QAPair> knowledgeBase;
std::map<String, String> synonyms;
std::set<String> stopwords;
// Function declarations
void loadSynonyms();
void replaceSynonyms(String &sentence);
void loadStopwords();
void removeStopwords(String &sentence);
String findBestAnswer(String question);
void loadKnowledgeBase();
void saveToSD(String question, String answer);
void setup() {
Serial.begin(115200);
Serial.println("Booting...");
// Start WiFi in SoftAP mode
WiFi.softAP(ssid, password);
Serial.println("WiFi AP started");
// Explicitly initialize the SPI bus:
// SCK: GPIO 18, MISO: GPIO 19, MOSI: GPIO 23, CS: SD_CS_PIN (GPIO 5)
SPI.begin(18, 19, 23, SD_CS_PIN);
// Mimic Arduino Nano style for SD initialization (optional)
pinMode(10, OUTPUT);
delay(100);
// Initialize SD card using the defined CS pin
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD Card Error!");
while(1) { delay(1000); } // Halt if SD card fails to initialize
}
Serial.println("SD Card found!");
// Load knowledge base, synonyms, and stopwords from SD card
loadKnowledgeBase();
loadSynonyms();
loadStopwords();
// Define web server routes
server.on("/", HTTP_GET, []() {
File file = SD.open("/index.html");
if (file) {
server.streamFile(file, "text/html");
file.close();
} else {
server.send(404, "text/plain", "Missing index.html on SD card!");
}
});
server.on("/ask", HTTP_GET, []() {
String question = server.arg("question");
replaceSynonyms(question);
removeStopwords(question);
String answer = findBestAnswer(question);
server.send(200, "text/plain", answer);
});
server.on("/teach", HTTP_GET, []() {
String question = server.arg("question");
String answer = server.arg("answer");
replaceSynonyms(question);
removeStopwords(question);
saveToSD(question, answer);
knowledgeBase.push_back({question, answer});
server.send(200, "text/plain", "Learned!");
});
server.begin();
}
void loop() {
server.handleClient();
}
// A simple token matching algorithm to choose the best answer
String findBestAnswer(String question) {
int bestMatch = 0;
String bestAnswer = "I don't know.";
// Tokenize the question
std::vector<String> questionWords;
int start = 0;
int end = 0;
while ((end = question.indexOf(' ', start)) != -1) {
questionWords.push_back(question.substring(start, end));
start = end + 1;
}
questionWords.push_back(question.substring(start));
// Compare with each QA pair in the knowledge base
for (QAPair pair : knowledgeBase) {
int matchCount = 0;
for (String word : questionWords) {
if (pair.question.indexOf(word) >= 0) {
matchCount++;
}
}
if (matchCount > bestMatch) {
bestMatch = matchCount;
bestAnswer = pair.answer;
}
}
return bestAnswer;
}
// Load the knowledge base from the SD card (knowledge.json)
void loadKnowledgeBase() {
File file = SD.open("/knowledge.json", FILE_READ);
if (!file) {
Serial.println("No knowledge found!");
return;
}
while (file.available()) {
String line = file.readStringUntil('\n');
int splitIndex = line.indexOf("|");
if (splitIndex > 0) {
String q = line.substring(0, splitIndex);
String a = line.substring(splitIndex + 1);
knowledgeBase.push_back({q, a});
}
}
file.close();
}
// Save a new question-answer pair to the SD card (knowledge.json)
void saveToSD(String question, String answer) {
File file = SD.open("/knowledge.json", FILE_APPEND);
if (file) {
file.println(question + "|" + answer);
file.close();
} else {
Serial.println("Error saving to knowledge.json");
}
}
// Replace synonyms in the sentence based on synonyms.txt
void replaceSynonyms(String &sentence) {
for (auto const& pair : synonyms) {
sentence.replace(pair.first, pair.second);
}
}
// Load synonyms from the SD card (synonyms.txt)
void loadSynonyms() {
File file = SD.open("/synonyms.txt", FILE_READ);
if (!file) return;
while (file.available()) {
String line = file.readStringUntil('\n');
int splitIndex = line.indexOf("=");
if (splitIndex > 0) {
String key = line.substring(0, splitIndex);
String value = line.substring(splitIndex + 1);
synonyms[key] = value;
}
}
file.close();
}
// Load stopwords from the SD card (stopwords.txt)
void loadStopwords() {
File file = SD.open("/stopwords.txt", FILE_READ);
if (!file) return;
while (file.available()) {
String line = file.readStringUntil('\n');
line.trim();
stopwords.insert(line);
}
file.close();
}
// Remove stopwords from a sentence
void removeStopwords(String &sentence) {
std::vector<String> words;
int start = 0;
int end = 0;
while ((end = sentence.indexOf(' ', start)) != -1) {
String word = sentence.substring(start, end);
if (stopwords.find(word) == stopwords.end()) {
words.push_back(word);
}
start = end + 1;
}
String lastWord = sentence.substring(start);
if (stopwords.find(lastWord) == stopwords.end()) {
words.push_back(lastWord);
}
sentence = "";
for (String word : words) {
if (sentence.length() > 0) sentence += " ";
sentence += word;
}
}
Error Message:
rst:0x8 (TG1WDT_SYS_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4832
load:0x40078000,len:16460
load:0x40080400,len:4
load:0x40080404,len:3504
entry 0x400805cc
Booting...
WLAN gestartet
ets Jul 29 2019 12:21:46
rst:0x8 (TG1WDT_SYS_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:4832
load:0x40078000,len:16460
load:0x40080400,len:4
load:0x40080404,len:3504
entry 0x400805cc
Upvotes: -2
Views: 51