Allora fatto tutto ok. C’è solo un problema. In pratica quando tento di avviare il tutto, ci sono dei problemi in fase di inizializzazione, vedo i led della scheda RELE che blinka, il WiFi non è disponibile. Per ovviare a questo problema ho scoperto che, se do la corrente solo all ESP8266, attendo che si collega al WiFi, quindi do la corrente alla scheda Rele, non ho alcun problema. Secondo voi cosa potrebbe essere ? Vi posto un pò di codice:
// Load Wi-Fi library
#include <ESP8266WiFi.h>
#include <Wire.h>
// Replace with your network credentials
const char* ssid = "iPhone";
const char* password = "m1234567";
const int relayD0 = D0;
const int relayD1 = D1;
const int relayD2 = D2;
const int relayD3 = D3;
const int relayD5 = D5;
const int relayD6 = D6;
const int relayD7 = D7;
const int relayD8 = D8;
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
int conta =0;
// Assign output variables to GPIO pins
#define ESP8266_LED 3
//const int relay2Pin = D8; // FOR GREEN LED
const int output4 = 4;
void setup() {
//il pin 6,7,11 non va toccato
Serial.begin(115200);
ConnectWiFi();
delay(2000);
pinMode(relayD0, OUTPUT);
pinMode(relayD1, OUTPUT);
pinMode(relayD2, OUTPUT);
pinMode(relayD3, OUTPUT);
pinMode(relayD5, OUTPUT);
pinMode(relayD6, OUTPUT);
pinMode(relayD7, OUTPUT);
pinMode(relayD8, OUTPUT);
digitalWrite(relayD0, HIGH);
delay(100);
digitalWrite(relayD1, HIGH);
delay(100);
digitalWrite(relayD2, HIGH);
delay(100);
digitalWrite(relayD3, HIGH);
delay(100);
digitalWrite(relayD5, HIGH);
delay(100);
digitalWrite(relayD6, HIGH);
delay(100);
digitalWrite(relayD7, HIGH);
delay(100);
Serial.println("Setup PIN terminato");
delay(200);
delay(1000);
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
digitalWrite(relayD8, HIGH);
delay(100);
if (WiFi.status() != WL_CONNECTED){
Serial.println("riconnessione in corso");
ConnectWiFi();
delay(1000);
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
sendPageHtml(client);// If a new client connects,
// Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
closeConnection(client);
String readString = getValue(header, ' ', 1);
Serial.println("+++++++++++++++");
Serial.println(readString);
Serial.println("+++++++++++++++");
/*if (header.indexOf("ON") >= 0) {
String _PIN = readString.substring(4);
int pinOn = _PIN.toInt();
digitalWrite(pinOn, LOW);
//Serial.println("LUCE ACCESA PIN " +pinOn);
} else if (header.indexOf("OFF") >= 0) {
String _PIN = readString.substring(5);
int pinOn = _PIN.toInt();
digitalWrite(pinOn, HIGH);
//Serial.print("LUCE SPENTA "+pinOn);
}else*/
if (header.indexOf("RELE") >= 0) {
//RECUPERO LA CORRENTE PASSATA DAL PIN
String _PIN = readString.substring(6);
int pinOn = getPinToMove(_PIN.toInt());
digitalWrite(pinOn, LOW);
//Serial.println("RELE ACCESA "+pinOn);
//ATTENDO DUE SECONDI, POI SPENGO IL PIN
delay(250);
digitalWrite(pinOn, HIGH);
digitalWrite(pinOn, HIGH);
delay(50);
}
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
// Serial.println("Client disconnected.");
// Serial.println("");
}
}
void sendPageHtml(WiFiClient client)
{
//client.println("HTTP/1.1 200 OK");
//client.println("Content-type:text/html");
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
/* client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// Web Page Heading
client.println("<body><h1>Domotica di Michele Castriotta</h1>");
// Display current state, and ON/OFF buttons for GPIO 2
client.println("<p>RELE 1</p>");
client.println("<p><a href=\"/RELE-1\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 2</p>");
client.println("<p><a href=\"/RELE-2\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 3</p>");
client.println("<p><a href=\"/RELE-3\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 4</p>");
client.println("<p><a href=\"/RELE-4\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 5</p>");
client.println("<p><a href=\"/RELE-5\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 6</p>");
client.println("<p><a href=\"/RELE-6\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 7</p>");
client.println("<p><a href=\"/RELE-7\"><button class=\"button\">ON</button></a></p>");
client.println("<p>RELE 8</p>");
client.println("<p><a href=\"/RELE-8\"><button class=\"button\">ON</button></a></p>");
client.println("</body>");
*/
client.println("</html>");
// The HTTP response ends with another blank line
client.println();
}
void closeConnection(WiFiClient client){
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
}
bool ConnectWiFi() {
// Serial.println("testo lcd" +testo);
WiFi.begin(ssid, password);
IPAddress ip(192,168,1,201);
IPAddress subnet(255,255,255,0);
IPAddress gateway(192,168,1,1);
WiFi.config(ip, gateway, subnet);
delay(10000);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println(WiFi.status());
Serial.println(WL_CONNECTED);
WiFi.disconnect();
delay(5000);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
delay(10000);
}
Serial.println("");
Serial.print ("WiFi connected ");
return true;
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}//fine metodo getvalue
int getPinToMove(int idPin)
{
switch (idPin) {
case 1: return relayD0;
case 2: return relayD1;
case 3: return relayD2;
case 4: return relayD3;
case 5: return relayD5;
case 6: return relayD6;
case 7: return relayD7;
case 8: return relayD8;
// Qui sai che x è > 0 && < 4
break;
}
}