ESP32 проблемы с шиной I2C и реконектом wifi

C Sharp > ESP32 проблемы с шиной I2C и реконектом wifi
20.04.2018 17:33:20


Наиболее часто встречающиеся слова в статье:

[println] [include] [readPressure] [available] [request] [blank_line] [Presure] [s=static_cast] [s/60/60] [WL_CONNECTED]


Статья:

esp32 , в отличии от ESP826 , в среде arduino славится  странными глюками с i2c wire шиной и реконектом wifi . например в режиме клиента, при пропадании сети wifi происходит отваливание  от сети wifi и не возможность подключиться  к сети wifi

для  решения  проблем с i2c wire  я нашёл такое  простое решение как Wire.reset();

  if(!Wire.available()) {
                Wire.reset();
                s=static_cast<int>(millis()/1000);
                Serial.println("-------------------Wire.reset()-----------" + String(s)+ " sec| Uptime:"+String(s/60/60)+" hours, "+String(s/60%60)+" minuts ");
                };

для решения проблем с wifi реконетом :

if (WiFi.status() != WL_CONNECTED)
      {Serial.println(WiFi.status()); delay(4000);
       //esp_restart_noos();
       WiFi.reconnect();
       delay(10000);
  Serial.println("");
  Serial.println("WiFi connected");
       }

 

Вот пример скетча для  arduino ESP32 на примере веб сервера и датчика bme280 на основе беблиотек от Adafruit_Sensor и Adafruit_BME280

/*********
  Project: 
VIN--3.3V
GND--GND
SCL--22
SDA--21
*********/
 
 
#include <WiFi.h> //библиотека
#include "esp_system.h"
#include <Wire.h>
#include <SPI.h>
#include <D:\arduino\ESP32\Adafruit_Sensor.h>
#include <D:\arduino\ESP32\Adafruit_BME280.h>
 
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
 
#define SEALEVELPRESSURE_HPA (1013.25)
 
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
// Replace with your network details
const char* ssid = "iocsha";
const char* password = "1122";
char *mess;
byte mac[6]; 
unsigned long delayTime;
int s;
// Web Server on port 80
WiFiServer server(80);
 
 
 
void setup() {
    Serial.begin(9600);
  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.disconnect(true);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Starting the web server
  server.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);
 
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
 
    Serial.println(F("BME280 test"));
 
    bool status;
 
    // default settings
    // (you can also pass in a Wire library object like &Wire2)
    status = bme.begin();  
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
    }
 
    Serial.println("-- Default Test --");
    delayTime = 50000;
 
    Serial.println();
}
 
 
void loop() { 
 
  // Listenning for new clients www
  WiFiClient client = server.available();
  // proverim status wifi and reconect if no connect
  if (WiFi.status() != WL_CONNECTED)
      {Serial.println(WiFi.status()); delay(4000);
       //esp_restart_noos();
       WiFi.reconnect();
       delay(10000);
  Serial.println("");
  Serial.println("WiFi connected");
       }
  String request;
  if (client) {
    Serial.println("New client");
    printValues();
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        request=request+c;
 
        if (c == ''\n'' && blank_line) {
          // вывод датчиков и самой  страницы
          // Получение  значений  датчиков
            if (request.indexOf("GET /LED=OFF") != -1){
              Serial.println("esp i2c restart....");
              bool status;
              //delete &Wire;
              Wire.flush();
              delay(4000);
              // default settings
              // (you can also pass in a Wire library object like &Wire2)
              status = bme.begin();  
              delay(4000);
              //esp_restart_noos();
              if (!status) {
                  Serial.println("Could not find a valid BME280 sensor, check wiring!");
                  while (1);
                   }
           }
           if (request.indexOf("GET /LED=REBOOT") != -1){
              Serial.println("esp  reboot....");
              bool status;
              //delete &Wire;
              delay(4000);
              status = bme.begin();  
              delay(4000);
              esp_restart_noos();
           }
             //wire reset if wire not !!!!!
             if(!Wire.available()) {
                Wire.reset();
                s=static_cast<int>(millis()/1000);
                Serial.println("-------------------Wire.reset()-----------" + String(s)+ " sec| Uptime:"+String(s/60/60)+" hours, "+String(s/60%60)+" minuts ");
                };
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head><META HTTP-EQUIV=\"refresh\" CONTENT=\"30\"></head>");
            client.println("<body><h1>ESP32 Weather Web Server</h1>");
            client.println("<table border=\"2\" width=\"456\" cellpadding=\"10\"><tbody><tr><td>");
            client.println("<h3>Temperature = ");
            client.println(bme.readTemperature());
            client.println("&deg;</h3><h3>Humidity = ");
            client.println(bme.readHumidity());
            client.println("%</h3>");
            client.println("<h3>Presure h0 = ");
            client.println(bme.readPressure()* 0.0075006375541921+5.31);
            client.println("mmHg</h3>");
            client.println("<h3>Presure h0 = ");
            client.println((bme.readPressure()* 0.0075006375541921+5.31)*0.00133322*1000);
            client.println("mbar</h3>");
            client.println("<h3>Presure = ");
            client.println(bme.readPressure()* 0.0075006375541921);
            client.println("mmHg</h3>");
            client.println("<h3>Presure = ");
            client.println(bme.readPressure() / 100.0F);
            client.println("hPa</h3>");
            client.println("<h3>Alt(m) = ");
            client.println(bme.readAltitude(SEALEVELPRESSURE_HPA));
            client.println("m</h3>");
            client.println("<h3>Free memory = ");
            client.println(ESP.getFreeHeap());
            client.println("byte</h3><h3>Wifi status:");
              client.print("SSID: ");
             client.println(WiFi.SSID());
 
             // print your WiFi shield''s IP address:
            IPAddress ip = WiFi.localIP();
            client.print("IP Address: ");
             client.println(ip);
          // print the received signal strength:
            long rssi = WiFi.RSSI();
            client.print("signal strength (RSSI):");
            client.print(rssi);
            client.println(" dBm </h3>");
            s=static_cast<int>(millis()/1000);
            client.println("<h3>Uptime:"+String(s/60/60)+" hours, "+String(s/60%60)+" minuts ");
            client.println("</h3></td></tr></tbody></table><h3>@2018 Ioksha S. compile 20.04.2018</h3>");
            client.println("<button style=\"color:red;\" onclick=\"location.href=''/LED=OFF''\" type=\"button\">I2C rest</button><button style=\"color:red;\" onclick=\"location.href=''/LED=REBOOT''\" type=\"button\">Reboot</button><br>");
            client.println("</body></html>");
            request="";
            break;
        }
        if (c == ''\n'') {
          // when starts reading a new line
          blank_line = true;
 
        }
        else if (c != ''\r'') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Client disconnected.");
 
  }
}
 
 
void printValues() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");
 
    Serial.print("Pressure = ");
 
    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");
 
    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");
 
    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");
 
    Serial.println();
}