We have come to the end of my project to design a motorbike wireless key, based on wifi + sd card, where we will combine the previous discussion, first part is esp8266 as an asynchronous webserver and the second discussion regarding the SD Card. So this 2 parts needs to be understood first so that it is not too confusing for our readers.
Broadly speaking, the objectives to be achieved from this practice series are:
- Using ESP8266 as an access point and webserver to be accessed by smartphones and then controlling the relay connected to the electric motor lock system
- The SD card is used as a storage medium for wifi parameters and the answer key / challenge code that the user will solve via a smartphone
The circuit that I designed as follows:
But this time I will focus only on the important part and have never been discussed in the previous 2 series of articles, namely the wifi setting from sd card and questions for passwords, where users can write their own wifi name on the SD card via PC/Laptop and make this device can be changed as desired.
Here I use a settings file called lock.txt which contains the data:
wifi,mybike,iamverycool
1,motorbike,suzuki
2,year of purchase,2012
3,city,london
4,your cat name,pussy
5,hobby,fishing
The top line of wifi, mybike, iamverycool is the setting for the wifi name and password (at least 8 characters). While the next is a sequence of 5 questions that can be solved to turn on the motor contact relay.
The script for reading the contents of the file per line is then parsed as follows:
dataFile = SD.open("lock.txt");
if (dataFile) {
while (dataFile.available()) {
buffer = dataFile.readStringUntil('\r\n');
//parsing text for wifi and password
if(getStringPartByNr(buffer,',',0) == "wifi")
{
Serial.println(buffer);
ssid = getStringPartByNr(buffer,',',1);
password = getStringPartByNr(buffer,',',2);
}
//open the question and answer as the number of sequence
else if(getStringPartByNr(buffer,',',0) == String(sequ))
{
Serial.print("question : ");
key = getStringPartByNr(buffer,',',1);
Serial.print(key);
Serial.print(" - answer : ");
chal =getStringPartByNr(buffer,',',2);
Serial.println(chal);
}
}
dataFile.close();
Meanwhile, to make changes to the questions so that they are not always the same as those asked, it is necessary to carry out a process of adding sequences that are stored in the seq.txt file. It is similar to a random process / choosing questions at randomly order.
SD.remove("seq.txt"); //erase the file then make a new one
File myFile = SD.open("seq.txt", FILE_WRITE);
if (myFile) // it opened OK
{
Serial.println("Writing to seq.txt");
sequ++; //add sequence
if(sequ > maxsequ) sequ= 1; //back to start
//write to SD card for new sequence
myFile.print(String(sequ));
myFile.close();
Serial.print("current sequence :");
Serial.println(String(sequ));
}
else
Serial.println("Error opening seq.txt");
To compare whether the answer is correct or not, a comparison is made in the input form that is responded to by the form submit asynchronously.
rver.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
String inputParam;
// GET input1 value on <ESP_IP>/get?input=<inputMessage>
if (request->hasParam(PARAM_INPUT) ) {
inputMessage = request->getParam(PARAM_INPUT)->value();
inputParam = PARAM_INPUT;
}
else {
inputMessage = "No challenge ";
inputParam = "none";
}
Serial.print("Answer : ");
Serial.println(inputMessage);
//if compared and correct
if(inputMessage == chal)
{
request->send(200, "text/html", SendCorrect());
}
else{
olkey = key ;
request->send(200, "text/html", SendWrong(olkey,inputMessage));
}
writeToFile(); // add sequence to sd card
});
The results are quite satisfactory and the questions can be changed as desired.
Because it's still in the form of a web and the format is a regular GET, then for development it can be wrapped into an Android APK which is quite simple, you only need to change the HTML web to an Android application. Also, if you want to be cool, you can include a second relay to start it via the button on the smartphone screen.
0 komentar:
Posting Komentar