Hey guys,
i currently have some issues with the WiFi.begin() function.
I want to send the SSID and the password via serial to the Wemos D1 mini Pro and then store them in the EEPROM. So far so good.
I get the correct char back from the eeprom but i didn´t get a connection to the network.
I tried it with char variables and with string variables but nothing worked.
Can somone please help me?
Here my code:
#include <EEPROM.h>;
#ifdef ESP8266
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <ModbusIP_ESP8266.h>
const int REG = 1001; // Modbus Coils Offset
const int COUNT = 1;
char mySSID[50];
char myPWD[50];
char myIP[50];
char mymIP[50];
char cSSID[50];
char cPWD[50];
char tSSID[50];
char tPWD[50];
char cIP[50];
char cmIP[50];
char mIP1[3];
char mIP2[3];
char mIP3[3];
char mIP4[3];
int nIP = 0;
int IP1;
int IP2;
int IP3;
int IP4;
String sSSID;
String sPWD;
String sIP;
String smIP;
int i;
int lasti;
ModbusIP mb; // ModbusIP object
IPAddress remote(IP1, IP2, IP3, IP4);
void getData(){
Serial.println("SSID eingeben...");
while(Serial.available() == 0){
}
sSSID = Serial.readString();
Serial.println("Passwort eingeben...");
while(Serial.available() == 0){
}
sPWD = Serial.readString();
Serial.println("IP eingeben...");
while(Serial.available() == 0){
}
sIP = Serial.readString();
/* Serial.println("Modul IP eingeben...");
while(Serial.available() == 0){
}
smIP = Serial.readString();*/
for (i=0; i <= sSSID.length(); i++){
mySSID[i] = sSSID[i];
mySSID[i+1] = '\0';
}
for (i=0; i <= sPWD.length(); i++){
myPWD[i] = sPWD[i];
myPWD[i+1] = '\0';
}
for (i=0; i <= sIP.length(); i++){
myIP[i] = sIP[i];
myIP[i+1] = '\0';
}
/*
for (i=0; i <= smIP.length(); i++){
mymIP[i] = smIP[i];
mymIP[i+1] = '\0';
}*/
}
void writeEEPROM(){
for(i=0; i<= sSSID.length(); i++){
EEPROM.put(i, mySSID[i]);
}
EEPROM.put(i+1, '\0');
for(i=0; i<= sPWD.length(); i++){
EEPROM.put(i+60, myPWD[i]);
}
EEPROM.put(i+61, '\0');
for(i=0; i<= sIP.length(); i++){
EEPROM.put(i+120, myIP[i]);
}
EEPROM.put(i+121, '\0');
}
void readEEPROM(){
for(i=0; i<=50; i++){
cSSID[i] = EEPROM.read(i);
if(cSSID[i] == '\0'){
break;
}
tSSID[i] = cSSID[i];
}
for(i=0; i<=50; i++){
cPWD[i] = EEPROM.read(i+60);
if(cPWD[i] == '\0'){
break;
}
tPWD[i] = cPWD[i];
}
for(i=0; i<=50; i++){
cIP[i] = EEPROM.read(i+120);
if(cIP[i] == '\0'){
break;
}
}
sSSID = String(cSSID);
sPWD = String(cPWD);
sIP = String(cIP);
}
void setup() {
Serial.begin(9600);
EEPROM.begin(240);
pinMode (0, INPUT_PULLUP);
if (!digitalRead(0)){
getData();
writeEEPROM();
}
readEEPROM();
EEPROM.end();
nIP = 0;
for (i=0; i<= sIP.length(); i++){
if(nIP == 0 && cIP[i] != '.'){
mIP1[i] = cIP[i];
}
else{
nIP = nIP +1;
lasti = i;
break;
}
}
IP1 = atoi(mIP1);
for (i=0; i<= sIP.length(); i++){
if(nIP == 1 && cIP[i+lasti] != '.'){
mIP2[i] = cIP[i+lasti];
}
else{
nIP = nIP +1;
lasti = lasti + i;
break;
}
}
IP2 = atoi(mIP2);
for (i=0; i<= sIP.length(); i++){
if(nIP == 2 && cIP[i + lasti] != '.'){
mIP3[i] = cIP[i+lasti];
}
else{
nIP = nIP +1;
lasti = lasti + i;
break;
}
}
IP3 = atoi(mIP3);
for (i=0; i<= sIP.length(); i++){
if(nIP == 3 && cIP[i + lasti] != '.'){
mIP4[i] = cIP[i+lasti];
}
else{
nIP = nIP +1;
lasti = lasti + i;
break;
}
}
IP4 = atoi(mIP4);
IPAddress remote(IP1, IP2, IP3, IP4); // Address of Modbus Slave
Serial.print("Connecting to: ");
Serial.println(cSSID);
Serial.print("with PWD: ");
Serial.print(cPWD);
WiFi.begin(cSSID, cPWD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
mb.master();
}
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Modbus Transaction callback
if (event != Modbus::EX_SUCCESS) // If transaction got an error
Serial.printf("Modbus result: %02X\n", event); // Display Modbus error code
if (event == Modbus::EX_TIMEOUT) { // If Transaction timeout took place
mb.disconnect(remote); // Close connection to slave and
mb.dropTransactions(); // Cancel all waiting transactions
}
return true;
}
bool res[COUNT] = {true};
//bool res1[COUNT] = {false};
void loop() {
if (!mb.isConnected(remote)) { // Check if connection to Modbus Slave is established
mb.connect(remote); // Try to connect if no connection
Serial.print(".");
}
if (!mb.writeCoil(remote, REG, res, COUNT, cb)) // Try to Write array of COUNT of Coils to Modbus Slave
Serial.print("#");
if (mb.writeCoil(remote, REG, res, COUNT, cb) || millis()>30000){
ESP.deepSleep(0);
}
mb.task(); // Modbus task
delay(50); // Pushing interval
}