Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- #include <WiFiClient.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiMulti.h>
- ESP8266WiFiMulti WiFiMulti;
- //String cofig[4];
- char cofig[4];
- char a;
- const char * rede;
- const char * minhaSenha;
- int pos=0;
- int x, b, c ;
- boolean y, z ;
- String qsid;
- String esid;
- String ssid, senha, ip, porta;
- String ssiD, senhA, iP, portA;
- boolean t=0;
- char thisChar;
- byte Flag =0;
- String MySSID = "";
- String MyPW = "";
- const char* port;
- const char* host;
- void APmode()
- {
- if(digitalRead(D1) == 0)
- {
- const char *ssid = "IOTechConfig";
- String dados;
- int status = WL_IDLE_STATUS;
- boolean alreadyConnected = false;
- WiFiServer server(23);
- WiFiClient client;
- delay(1000);
- Serial.begin(115200);
- Serial.println();
- Serial.print("Configuring access point...");
- WiFi.softAP(ssid);
- delay(5000);
- IPAddress myIP = WiFi.softAPIP();
- Serial.print("AP IP address: ");
- Serial.println(myIP);
- Serial.println("\nStarting server...");
- // start the server:
- server.begin();
- while(true)
- {
- if (!client)
- {
- client = server.available();
- }else
- {
- if (client.status() == CLOSED)
- {
- client.stop();
- Serial.println("connection closed !");
- }
- if (!alreadyConnected) // when the client sends the first byte, say hello:
- {
- client.flush();
- Serial.println("We have a new client");
- client.println("Hello, client!");
- alreadyConnected = true;
- }
- if(client.available() > 0)
- {
- if(pos == 0)
- {
- thisChar = client.read();
- if(thisChar != '\n')
- {
- qsid+=thisChar;
- }else
- {
- int charLength=qsid.length();
- Serial.println("writing eeprom ssid:");
- for (int i = 0; i < qsid.length(); ++i)
- {
- EEPROM.write(i, qsid[i]);
- Serial.print("Wrote: ");
- Serial.println(qsid[i]);
- int qtdChar=qsid.length();
- EEPROM.write(100,qtdChar);
- delay(10);
- }
- Serial.println(EEPROM.read(100));
- delay(10);
- EEPROM.commit();
- }
- }
- }
- }
- }
- }
- }
- void dadosConfig()
- {
- if(z ==0)
- {
- for (int i = 0; i<EEPROM.read(100); ++i)
- {
- if (EEPROM.read(i) == '£')
- Flag = 1;
- if (Flag == 0)
- {
- MySSID += char(EEPROM.read(i));
- Serial.println(MySSID);
- }
- if (Flag ==1)
- {
- MyPW += char(EEPROM.read(i));
- Serial.println(MyPW);
- }
- Serial.println(esid);
- Serial.println(i);
- //Serial.println("Passou por aqui");
- delay(10);
- }
- z=1;
- }
- while(y == 0)
- {
- a=esid.charAt(x);
- if(a != '£')
- {
- cofig[b]+=a;
- }else{
- b++;
- }
- x++;
- if(b == 4)
- {
- Serial.println(cofig[0]);
- Serial.println(cofig[1]);
- Serial.println(cofig[2]);
- Serial.println(cofig[3]);
- y=1;
- // cofig[0].toCharArray(rede,16);
- // cofig[1].toCharArray(minhaSenha,16);
- }
- }
- }
- void setup() {
- pinMode(D1, INPUT_PULLUP);
- Serial.begin(115200);
- delay(10);
- APmode();
- dadosConfig();
- // We start by connecting to a WiFi network
- WiFiMulti.addAP(rede, minhaSenha);
- Serial.println();
- Serial.println();
- Serial.print("Wait for WiFi... ");
- while(WiFiMulti.run() != WL_CONNECTED) {
- Serial.print(".");
- delay(500);
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- delay(500);
- }
- void loop() {
- port = MySSID;
- host = MyPW;
- //cofig[2].toCharArray(host,16); // ip or dns
- if(digitalRead(D1) == 0)
- {
- Serial.print("connecting to ");
- // Serial.println(host);
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- if (!client.connect(host, port)) {
- Serial.println("connection failed");
- Serial.println("wait 5 sec...");
- delay(5000);
- return;
- }
- // This will send the request to the server
- client.print("rele1");
- //read back one line from server
- String line = client.readStringUntil('\r');
- client.println(line);
- Serial.println("closing connection");
- client.stop();
- Serial.println("wait 5 sec...");
- delay(5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement