Im making a choose your own adventure game in my Arduino esp8266 but I'm having a lot of trouble because sometimes pages won't load. I am using a SD card to hold my page so I don't take up too much memory. I'll give an example of one of my pages below.
Setup:
void setup() {
Serial.begin(115200);
connectToWifi("SSID", "PASS"); //Function I made elsewhere. Problem is not here.
//INIT SD
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
setupBegin_Intro(); //In this function there is server.on("/INTRO_3", INTRO_3) <- This means when I get a request for /SCENE_3, I run the function INTRO_3() (see below for function)
server.begin(); //Allows connection from clients
}
void loop() {
server.handleClient();//Handle URL requests from clients
}
INTRO_2()
void INTRO_2() {
String s = "";
fileToUse = "Begin_Intro/INTRO_2";
correctAns = "NEXT";
whereToCorrect = "INTRO_3";
s = "<html>";
s += "<head>";
s += "<style>";
s += "#main {font-family:'Lucida Console', monospace;}";
s += "</style>";
s += "<title>MY WEBSITE</title>";
s += "</head>";
s += "<body>";
s += "<center>";
s += "<div id=\"main\">";
s += copyImageToMemory(fileToUse);
s += "</div>";
s += "<a href=" + whereToCorrect + "><button>" + correctAns + "</button></a><br />";
s += "</center>";
s += "</body></html>"; //Read HTML contents
Serial.println("SENDING: ");
server.send(200, "text/html", s); //Send web page
s = "";
memset(charPic, 0, sizeof(charPic)); //Resets the array that I hold the pic in
}
Getting Pic (No problem here but I thought to add it)
String copyImageToMemory(String fileName) {
String pic;
int i = 0;
myFile = SD.open(fileName + ".txt");
if (myFile) {
Serial.println(fileName + ".txt");
while (myFile.available() && i < maxPixels) {
charPic[i] = myFile.read();
i++;
}
pic = charPic;
myFile.close();
} else {
Serial.println("error opening file");
}
Serial.println("Image copied to memory");
Serial.printf("using %d characters\n", i);
return pic;
}
Notes:
I am using ESP8266WebServer Library
This happens on other pages but others work fine
Pressing "view page source" shows me this:
<html>
<head>
<style>
#main {
font-family: 'Lucida Console', monospace;
}
</style>
<title>MY WEBSITE</title>
</head>
<body>
<center>
<div id="main"></div>
<a href=SCENE_1>
<button>NEXT</button>
</a>
<br/>
</center>
</body>
</html>
Any and all help appreciated :D.
ALL THE CODE THAT HAS ANYTHING TO DO WITH INTRO_3:
#include <SPI.h>
#include <SD.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server(80); //default server port is 80
File myFile;
String fileName = "INTRO_2";
const int maxPixels = 16000;
char curentChar;
char charPic[maxPixels];
String s = "";
int trust = 100;
int trustSubtract = 50;
String fileToUse = "";
String correctAns = "";
String whereToCorrect = "";
String wrongAns = "";
String whereToWrong = "";
String whereFrom = "";
String address;
void connectToWifi(String myssid, String mypass);
void setup() {
Serial.begin(115200);
connectToWifi("iPhone (40)", "FrischSyp");
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
setupBegin_Intro();
setupScenes1_to_5();
setupWrongs();
setupEndings();
server.begin(); //Allows connection from clients
}
void loop() {
server.handleClient();//Handle URL requests from clients
}
void INTRO_3() {
fileToUse = "Begin_Intro/INTRO_3";
correctAns = "NEXT";
whereToCorrect = "INTRO_4";
wrongAns = "NO";
whereToWrong = "INTRO_2";
s = "<html>";
s += "<head>";
s += "<style>";
s += "#main {font-family:'Lucida Console', monospace;}";
s += "</style>";
s += "<title>Date Peter The Great</title>";
s += "</head>";
s += "<body>";
s += "<center>";
s += "<div id=\"main\">";
s += copyImageToMemory(fileToUse);
s += "</div>";
s += "<a href=" + whereToCorrect + "><button>" + correctAns + "</button></a><br>";
s += "</center>";
s += "</body></html>"; //Read HTML contents
Serial.println("SENDING: ");
printf("%s", s);
server.send(200, "text/html", s); //Send web page
memset(charPic, 0, sizeof(charPic));
}
void setupBegin_Intro() {
server.on("/", BEGIN);
server.on("/INTRO", INTRO);
server.on("/INTRO_2", INTRO_2);
server.on("/INTRO_3", INTRO_3);
server.on("/INTRO_4", INTRO_4);
}
//**********************WIFI********************
#include <ESP8266HTTPClient.h>
#include "ESP8266WiFi.h"
void connectToWifi(String myssid, String mypass){
Serial.println("Start");
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(myssid);
WiFi.begin(myssid, mypass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(".");
Serial.print ("Use this URL to connect: http://");
Serial.println(WiFi.localIP());
}