Hereby the code causing problems Part of the sketch is based on the entering ssid and password sketch when connect with ssid and password in flash is not working. The HTTP requests are send with a button in this case. Please do not pay any attention to possible not defined parameters in the sketch, since it is a cut and paste to reduce the size and declarations of parameters is not the issue I think
//#define BLYNK_DEBUG // Optional, this enables lots of print
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SPI.h>
#include <SimpleTimer.h>
// nieuwe toevoeging weblogin
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
MDNSResponder mdns;
WiFiServer server(80);
const char* APssid = "SCOREBOARD1"; // Name of access point
String st;
String rsid;
String rpass;
boolean newSSID = false;
#include <TimeLib.h>
#include <WidgetRTC.h>
char auth[] = "xxxxxxxxx";
WidgetTerminal terminal(V0);
SimpleTimer timer;
WidgetLED led3(V25); //register to virtual pin 1
WidgetLED led4(V26); //register to virtual pin 1
WidgetLED led5(V38); //register to virtual pin 1
WidgetLED led6(V39); //register to virtual pin 1
byte instelling = 0;
char tijdelijkchar[100];
char* waarden[11] = {"0", "1234", "100", "100000", "Speler 1", "Club 1", "123456", "Speler 2", "Club 2", "654321", "2"};
String tussenw[11] = {"0", "1234", "100", "100000", "Speler 1", "Club 1", "123456", "Speler 2", "Club 2", "654321", "2"};
byte wedstrijdpunt[2] = {0, 0};
byte kaart[2] = {0, 0};
byte setstand[2];
byte game[2];
char* speler[2] = {"Speler 1", "Speler 2"};
boolean servicestart = true;
byte volgnummer = 1;
boolean spiegel = true;
byte g[5][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}};
boolean wissel = false;
byte timeout[2] ;
boolean service = true;
WiFiClient client;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V43);
// Utility function for digital clock display: prints preceding colon and leading 0
char bufferspecial[250];
char specials[] = "$ ";
static char hex_digit(char c)
{ return "01234567890ABCDEF"[c & 0x0F];
}
char *urlencode(char *dst, char *src)
{ char c, *d = dst;
while (c = *src++)
{ if (strchr(specials, c))
{ *d++ = '%';
*d++ = hex_digit(c >> 4);
c = hex_digit(c);
}
*d++ = c;
}
*d = '\0';
return dst;
}
// Digital clock display of the time
void clockDisplay()
{ String digitm = "";
String digits = "";
if (minute() < 10) {
digitm = "0";
} else {
digitm = "";
}
if (second() < 10) {
digits = "0";
} else {
digits = "";
}
String currentTime = String(hour()) + ":" + digitm + minute() + ":" + digits + second();
String currentDate = String(day()) + " " + month() + " " + year();
String totaltime = currentDate + " " + currentTime;
Blynk.virtualWrite(V44, totaltime);
}
void setup()
{
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
// if the stored SSID and password connected successfully, exit setup
if ( testWifi()) {
Blynk.config(auth);
//return;
}
// otherwise, set up an access point to input SSID and password
else {
Serial.println("");
Serial.println("Connect timed out, opening AP");
setupAP();
}
terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
Serial.println(F("Applicatie start"));
rtc.begin();
//Serial.println(F("RTC start"));
timer.setInterval(1000L, clockDisplay);
timer.setInterval(500L, checkdetijd);
timer.setInterval(60000L, timeupdate);
// Setup a function to be called every second
startupreset();
timeout[0] = 0;
timeout[1] = 0;
servicestart = service;
schermupdate();
}
void loop()
{
Blynk.run();
timer.run(); // Initiates SimpleTimer
yield();
}
void timeupdate() {
rtc.begin();
}
int testWifi(void) {
long Timeout = (millis() + 25000);
Serial.print("Waiting for Wifi to connect. ");
while (millis() < Timeout) {
delay(500);
Serial.print(".");
if (WiFi.status() == WL_CONNECTED) {
Serial.println("");
Serial.println("WiFi connected.");
return (1);
}
}
return (0);
}
void launchWeb(int webtype) {
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());
// Start the server
server.begin();
Serial.println("Server started");
int b = 20;
int c = 0;
while (b == 20) {
b = mdns1(webtype);
//If a new SSID and Password were sent, close the AP, and connect to local WIFI
if (newSSID == true) {
newSSID = false;
//convert SSID and Password sting to char
char ssid[rsid.length()];
rsid.toCharArray(ssid, rsid.length());
char pass[rpass.length()];
rpass.toCharArray(pass, rpass.length());
Serial.println("Connecting to local Wifi"); //Close the AP and connect with new SSID and P/W
WiFi.softAPdisconnect(true);
delay(500);
WiFi.begin(ssid, pass);
delay(1000);
if ( testWifi()) {
Blynk.config(auth);
return;
}
else {
Serial.println("");
Serial.println("New SSID or Password failed. Reconnect to server, and try again.");
setupAP();
return;
}
}
}
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
}
Serial.println("");
st = "<ul>";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
st += "<li>";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
st += "</li>";
}
st += "</ul>";
delay(100);
WiFi.softAP(APssid);
Serial.println("softAP");
Serial.println("");
launchWeb(1);
}
String urldecode(const char *src) { //fix encoding
String decoded = "";
char a, b;
while (*src) {
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
if (a >= 'a')
a -= 'a' - 'A';
if (a >= 'A')
a -= ('A' - 10);
else
a -= '0';
if (b >= 'a')
b -= 'a' - 'A';
if (b >= 'A')
b -= ('A' - 10);
else
b -= '0';
decoded += char(16 * a + b);
src += 3;
}
else if (*src == '+') {
decoded += ' ';
*src++;
}
else {
decoded += *src;
*src++;
}
}
decoded += '\0';
return decoded;
}
int mdns1(int webtype) {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return (20);
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while (client.connected() && !client.available()) {
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return (20);
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if ( webtype == 1 ) {
if (req == "/")
{
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>";
s += "<font face='arial,helvetica' size='7'>";
s += "<b><label>Hello from NODEMCU at ";
s += ipStr;
s += "</label></b><p>";
s += st;
s += "<form method='get' action='a'><label>SSID: </label><input name='ssid' style='width:200px; height:60px; font-size:50px;'> ";
s += "<label>Password: </label><input name='pass' style='width:200px; height:60px; font-size:50px;'>";
s += "<p><input type='submit' style='font-size:60px'></form>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else if ( req.startsWith("/a?ssid=") ) {
newSSID = true;
String qsid; //WiFi SSID
qsid = urldecode(req.substring(8, req.indexOf('&')).c_str()); //correct coding for spaces as "+"
Serial.println(qsid);
Serial.println("");
rsid = qsid;
String qpass; //Wifi Password
qpass = urldecode(req.substring(req.lastIndexOf('=') + 1).c_str()); //correct for coding spaces as "+"
Serial.println(qpass);
Serial.println("");
rpass = qpass;
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266 ";
s += "<p> New SSID and Password accepted</html>\r\n\r\n";
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
else
{
if (req == "/")
{
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP8266";
s += "<p>";
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
}
client.print(s);
Serial.println("Done with client");
return (20);
}
//Use virtual pin 48 to overwrite wifi credentials in flash
BLYNK_WRITE(V48) {
if (param.asInt()) {
Serial.println("Erasing SSID and restarting.");
delay(100);
WiFi.begin("FakeSSID", "FakePW"); //replace current WiFi credentials with fake ones
setupAP();
}
}
// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
if (isFirstConnect) {
// Request Blynk server to re-send latest values for all pins
startupreset();
Blynk.syncVirtual(V30);
Blynk.syncVirtual(V31);
Blynk.syncVirtual(V16);
Blynk.syncVirtual(V17);
Blynk.syncVirtual(V14);
Blynk.syncVirtual(V15);
Blynk.syncVirtual(V44);
Blynk.syncVirtual(V41);
Blynk.syncVirtual(V0);
Blynk.syncVirtual(V38);
Blynk.syncVirtual(V25);
Blynk.syncVirtual(V26);
Blynk.syncVirtual(V13);
Blynk.syncVirtual(V39);
// You can also update an individual Virtual pin like this:
//Blynk.syncVirtual(V0);
isFirstConnect = false;
}
}
void httpscoreboard() {
boolean webscore = true;
if (webscore) {
const char* host = "xxxxxxxxx.nl";
unsigned int port = 80;
String digitmonth = "";
String digitday = "";
if (month() < 10) {
digitmonth = "0";
}
if (day() < 10) {
digitday = "0";
}
String dt = year() + digitmonth + month() + digitday + day();
//Serial.println(dt);
Serial.print(F("Connecting to "));
Serial.print(host);
Serial.print(F(":"));
Serial.print(port);
Serial.print("... ");
boolean serve = true;
if (!wissel) {
serve = service;
} else {
serve = !service;
}
if (client.connect(host, port)) {
Serial.println(F("OK"));
Serial.println(F("Sending GET request"));
//the URLENCODE is to change unwanted characters to UNIcode such as SPACE to %20
String HTReq = String("GET /anonymous.php?datum=") + dt + "&clubnr=" + waarden[2] + "&boardid=" + waarden[10] + "&wedstrijdnr=" + waarden[3] + String(volgnummer - 1) + "&naam1=" + urlencode(bufferspecial, speler[0]);
HTReq = HTReq + String("&naam2=") + urlencode(bufferspecial, speler[1]) + "&team1=" + urlencode(bufferspecial, waarden[5]) + "&team2=" + urlencode(bufferspecial, waarden[8]) + "&service=" + serve + "&wissel=" + wissel;
HTReq = HTReq + String("&game1=") + g[0][0] + urlencode(bufferspecial, " - ") + g[0][1] + "&game2=" + g[1][0] + urlencode(bufferspecial, " - ") + g[1][1] + "&game3=" + g[2][0] + urlencode(bufferspecial, " - ") + g[2][1] + "&game4=" + g[3][0] + urlencode(bufferspecial, " - ") + g[3][1];
HTReq = HTReq + String("&game5=") + g[4][0] + urlencode(bufferspecial, " - ") + g[4][1] + "&sets=" + setstand[0] + urlencode(bufferspecial, " - ") + setstand[1] + "&wedstrijdpunt=" + 0 + urlencode(bufferspecial, " - ") + 0; //markeer2
HTReq = HTReq + String("&volgnr=") + volgnummer + "&bondsnr1=" + waarden[6] + "&bondsnr2=" + waarden[9] + "&actueel1=" + game[0] + "&actueel2=" + game[1] + "&status1=" + kaart[0] + "&status2=" + kaart[1] + "&timeout1=" + timeout[0] + "&timeout2=" + timeout[1] + "&stand1=" + wedstrijdpunt[0] + "&stand2=" + wedstrijdpunt[1];
Serial.println(HTReq.length());
//Serial.println(HTReq);
client.print (HTReq);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
client.flush();
client.stop();
Serial.println(F("Done."));
} else {
Serial.println(F("failed"));
}
}
}
void httpwedstrijd() {
if (webscore) {
const char* host = "xxxxxxxxxxxxx.nl";
unsigned int port = 80;
String digitmonth = "";
String digitday = "";
if (month() < 10) {
digitmonth = "0";
}
if (day() < 10) {
digitday = "0";
}
String dt = year() + digitmonth + month() + digitday + day();
//Serial.println(dt);
Serial.print(F("Connecting to "));
Serial.print(host);
Serial.print(F(":"));
Serial.print(port);
Serial.print("... HTTPWEDSTRIJD");
boolean serve = true;
if (!wissel) {
serve = service;
} else {
serve = !service;
}
if (client.connect(host, port)) {
Serial.println(F("OK"));
Serial.println(F("Sending GET request"));
//Serial.println(getReq);
client.print("GET /anonymous2.php?datum=");
client.print(dt);
client.print("&clubnr=");
client.print(waarden[2]);
client.print("&boardid=");
client.print(waarden[10]);
client.print("&wedstrijdnr=");
client.print(waarden[3]);
client.print("&team1thuis=");
client.print(urlencode(bufferspecial, waarden[5]));
client.print("&naam1=");
client.print(wedstrijdpunt[0]);
client.print("&team1uit=");
client.print(urlencode(bufferspecial, waarden[8]));
client.print("&naam2=");
client.print(wedstrijdpunt[1]);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(host);
client.println("Connection: close");
client.println();
client.flush();
client.stop();
Serial.println(F("Done."));
} else {
Serial.println(F("failed"));
}
}
}
void startupreset() {
vijfdegame = false;
service = true;
wissel = false;
game[wissel] = 0;
game[!wissel] = 0;
setstand[wissel] = 0;
setstand[!wissel] = 0;
speler[wissel] = waarden[4];
speler[!wissel] = waarden[7];
timeout[wissel] = 0;
timeout[!wissel] = 0;
webscore = false;
servicestart = service;
rtc.begin();
opslag();
schermupdate();
Blynk.virtualWrite(V33, String("Wedstrijd: ") + volgnummer);
Blynk.virtualWrite(V2, 2);
Blynk.virtualWrite(V3, 1);
Blynk.virtualWrite(V41, 1);
Blynk.virtualWrite(V10, 1);
Blynk.virtualWrite(V22, 1);
Blynk.virtualWrite(V13, 0);
regelstatus();
}
BLYNK_WRITE(V28) //Button Widget is writing to pin V1
{
int pinData = param.asInt();
if (pinData == 1) {
httpscoreboard();
}
}
BLYNK_WRITE(V29) //Button Widget is writing to pin V1
{
int pinData = param.asInt();
if (pinData == 1) {
httpwedstrijd();
}
}