Now the sketch does not wait for GPIO 0, to trigger low to initiate the On Demand connection.
This is the sketch:
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>
//#include <TFT_eSPI.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
// Pin definitions
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
#define TRIGGER_PIN 0
// Use hardware SPI
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
//TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
int x=10;
int y=20;
#define TFT_GREY 0x5AEB
int timeout = 120; // seconds to run for
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
Serial.begin(115200);
delay(2000); // wait for Serial
Serial.println("\n Starting");
pinMode(TRIGGER_PIN, INPUT_PULLUP);
Serial.println("starting Display ili9341 ");
tft.begin();
// Set display rotation (optional, 1 is landscape for some displays)
tft.setRotation(2);
// Set background color
tft.fillScreen(ILI9341_YELLOW);
// Set text size
tft.setTextSize(3); // Size 1 is 6x8 pixels, Size 2 is 12x16, etc.
// Set text color
tft.setTextColor(ILI9341_BLACK);
tft.print("Press the Connect button");
// is configuration portal requested?
if (digitalRead(TRIGGER_PIN) == LOW) {
WiFiManager wm;
//reset settings - for testing
wm.resetSettings();
// set configportal timeout
wm.setConfigPortalTimeout(timeout);
if (!wm.startConfigPortal("OnDemandAP")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
// Set background color
tft.fillScreen(ILI9341_RED);
// Set text size
tft.setTextSize(3); // Size 1 is 6x8 pixels, Size 2 is 12x16, etc.
// Set text color
tft.setTextColor(ILI9341_WHITE);
tft.print("WIFI connected");
delay(10000);
}
void loop() {
for (int y=20; y <= 300; y=y+20){
// Set cursor position (X, Y)
tft.setCursor(x, y);
// Print the message
tft.print("Hello world");
delay(1000);
tft.fillScreen(ILI9341_YELLOW);
//y=y+20;
}
}
The sketch is above. The sketch compiles and uploads.
Just that is not doing as it is supposed to do. It is supposed to stop waiting for GPIO 0 to go low, after start the WIFI on Demand and once connected to go to the display “Hello world”
Nothing in your code tells it to wait for that input to go LOW. It simply checks if it is LOW. If so, it configures the WiFi. Otherwise it just moves on display text on the screen.
Put a debug statement immediately after the following to see if the trigger pin is being manipulated by the ESP32 immediately following the boot process.
This behaviour is ESP board dependent.
If your debug message is shown then try increasing the delay at the beginning of setup.
If the debug message is not shown but the Wifi managers starts anyway, then you have to check its documentation to see if your configuration could trigger an auto start of the Wifi manager. I don't use Wifi manager so I can't help with it.
Anyway, show what appears on the serial console during a start up.
The Task Watchdog Timer for Core 1 fired. It's not easy to get it to do that on Core 1. To debug, I'd rip out all the TFT stuff and try just the WiFi using the Serial Monitor for debug prints. Once that works, add the TFT back in.
/**
* OnDemandConfigPortal.ino
* example of running the configPortal AP manually, independantly from the captiveportal
* trigger pin will start a configPortal AP for 120 seconds then turn it off.
*
*/
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
String Oledtext;
String y;
String x;
// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 0
int timeout = 120; // seconds to run for
void setup() {
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
Serial.begin(115200);
delay(2000); // wait for Serial
Serial.println("\n Starting");
pinMode(TRIGGER_PIN, INPUT_PULLUP);
Serial.println("starting OLED ");
Serial.println("Waiting for Connect button");
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Show initial display buffer contents on the screen --
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Here, waiting for the OnDemand display the message "Press connect button"
x = "Push connect button";
showoled (x);
}
void loop() {
// is configuration portal requested?
if (digitalRead(TRIGGER_PIN) == LOW) {
WiFiManager wm;
//reset settings - for testing
wm.resetSettings();
// set configportal timeout
wm.setConfigPortalTimeout(timeout);
if (!wm.startConfigPortal("OnDemandAP")) {
Serial.println("failed to connect and hit timeout");
delay(3000);
//reset and try again, or maybe put it to deep sleep
ESP.restart();
delay(5000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
x = "Wifi connected";
showoled(x);
delay(5000);
Serial.print("Next thing to do");
x = "Now we go on";
showoled(x);
}
}
void showoled(String x) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print(x);
display.display();
}
The logic of the working oled code in post #38 is different. Button GPIO0 (trigger_pin) is tested in the loop.
Does the repeating stream of hex codes (mentioned in post #35) that emerge on the serial console from the code in post #22 also happen when GPIO0 is not pressed?
The timing of the press of the button is important because its pin GPIO0 must be high at boot time .
Incidentally, code like this with a blocking for loop can cause continuous crashes:
Not with normal Arduino code running on a Dual Core ESP32. By default, no Core 1 tasks are subscribed to the Task Watchdog Timer (TWDT), not even the Idle Task. You can crash Core 1 using a blocking loop like that if you first disabled interrupts. Then the Interrupt Watchdog Timer (IWDT) will fire.
Check if any library is missing. I can see that you included some libraries at the beginning of your code. Make sure that those are properly installed in your device. If it is your first project with the ESP32, make sure that you have performed the board installation part properly.