ESP32-S2 QT Py arduino code "bricking" purple led flashes

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
mariobro_3
 
Posts: 7
Joined: Wed Jan 04, 2023 3:51 pm

ESP32-S2 QT Py arduino code "bricking" purple led flashes

Post by mariobro_3 »

I have been working on a node red project with esp8266's for long time. I switched to the esp32-s2 qt py's to make life easier with hardware.

I am trying to talk to a 4 channel relay with one board qt py that receives the mqtt messages, and then another board connected to the neokey 1x4 to send those messages to my Pi 4 node red broker.

When I flash the Arduino code for the neokey1x4, nothing works, and no serial. the neoBlink example works perfectly.
After i flash the factory reset uf2 then go to Arduino serial, it I2C scans and wifi scans, so I know its working.
I wonder if wire.h or my i2c is messing stuff up?

Any input is greatly appreciated. I am new to the qt py and I am not fluent in python, I know all of this would work with circuit python, but I'm not about to start over, unless this just isn't possible to do with Arduino ide and this equipment.

When I flash this, it uploads successfully, then goes to purple flashing led and is must be factory reset.

Code: Select all

#include <Wire.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>

const char* ssid = "******";
const char* password = "******";

//RPI4 MQTT NODE RED BROKER
#define mqtt_server "******"
#define mqtt_port "1883"
#define mqtt_user "******"
#define mqtt_password "******"

// FOR STATIC IP ADDRESS CONFIG AT ******
IPAddress local_IP(**, *, *, ***);
IPAddress gateway(**, *, *, ***);
IPAddress subnet(**, *, *, ***);
IPAddress primaryDNS(**, *, *, ***);   //optional
IPAddress secondaryDNS(**, *, *, ***); //optional


WiFiClient espClient;
PubSubClient client(espClient);

#define NUMPIXELS        1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

const int relayAddress = 0x20;

void setup_wifi() {
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }

  // Connect to WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
   WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  ArduinoOTA.setHostname("ESP169"); // set name base on unique ID of esp module
  ArduinoOTA
    .onStart([]() {
      String type;
      if (ArduinoOTA.getCommand() == U_FLASH)
        type = "sketch";
      else // U_SPIFFS
        type = "filesystem";

      // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
      Serial.println("Start updating " + type);
    })
    .onEnd([]() {
      Serial.println("\nEnd");
    })
    .onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    })
    .onError([](ota_error_t error) {
      Serial.printf("Error[%u]: ", error);
      if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
      else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
      else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
      else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
      else if (error == OTA_END_ERROR) Serial.println("End Failed");
    });

  ArduinoOTA.begin();

#if defined(NEOPIXEL_POWER)
  pinMode(NEOPIXEL_POWER, OUTPUT);
  digitalWrite(NEOPIXEL_POWER, HIGH);
#endif

  pixels.begin(); 
  pixels.setBrightness(20);

  Wire.setPins(SDA1, SCL1);
  Wire.beginTransmission(0x20);
  Wire.write(0x00); // Set all channels to output mode
  Wire.endTransmission();
  
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

}

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();

  if(topic=="manager1/red"){
      Serial.print("Changing neopixel to red ");
      if(messageTemp == "on"){
        pixels.fill(0xFF0000);
        pixels.show();
       Wire.beginTransmission(relayAddress);
       Wire.write(0x01);
       Wire.endTransmission();
        Serial.print("On");
      }
      else if(messageTemp == "off"){
         pixels.fill(0x000000);
         pixels.show();
        Wire.beginTransmission(relayAddress);
        Wire.write(0x02);
        Wire.endTransmission();        
        Serial.print("Off");
      }
  }
  else if (topic=="material1/yellow"){
      Serial.print("Changing neopixel to red ");
      if(messageTemp == "on"){
        pixels.fill(0xFFFF00);
        pixels.show();
       Wire.beginTransmission(relayAddress);
       Wire.write(0x01);
       Wire.endTransmission();
        Serial.print("On");
      }
      else if(messageTemp == "off"){
         pixels.fill(0x000000);
         pixels.show();
        Wire.beginTransmission(relayAddress);
        Wire.write(0x02);
        Wire.endTransmission();        
        Serial.print("Off");
      }
  }
    else if (topic=="quality1/green"){
      Serial.print("Changing neopixel to red ");
      if(messageTemp == "on"){
        pixels.fill(0x00FF00);
        pixels.show();
       Wire.beginTransmission(relayAddress);
       Wire.write(0x01);
       Wire.endTransmission();
        Serial.print("On");
      }
      else if(messageTemp == "off"){
         pixels.fill(0x000000);
         pixels.show();
        Wire.beginTransmission(relayAddress);
        Wire.write(0x02);
        Wire.endTransmission();        
        Serial.print("Off");
      }
  }
    else if (topic=="maintenance1/blue"){
      Serial.print("Changing neopixel to red ");
      if(messageTemp == "on"){
        pixels.fill(0x0000FF);
        pixels.show();
       Wire.beginTransmission(relayAddress);
       Wire.write(0x01);
       Wire.endTransmission();
        Serial.print("On");
      }
      else if(messageTemp == "off"){
         pixels.fill(0x000000);
         pixels.show();
        Wire.beginTransmission(relayAddress);
        Wire.write(0x02);
        Wire.endTransmission();        
        Serial.print("Off");
      }
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    
    if (client.connect("ESP169")) {
      Serial.println("connected");  

      client.subscribe("manager1/red");
      client.subscribe("material1/yellow");
      client.subscribe("quality1/green");
      client.subscribe("maintenance1/blue");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}



// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {
  ArduinoOTA.handle();
  
  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP169");
} 

User avatar
mariobro_3
 
Posts: 7
Joined: Wed Jan 04, 2023 3:51 pm

Re: ESP32-S2 QT Py arduino code "bricking" purple led flashes

Post by mariobro_3 »

I just realized a wifi and pubsub client issue. Fixing that right now. I also realize the I2C adresses are not correct, those are just place holders, I need to know that its working and talking to my broker first.

User avatar
mariobro_3
 
Posts: 7
Joined: Wed Jan 04, 2023 3:51 pm

Re: ESP32-S2 QT Py arduino code "bricking" purple led flashes

Post by mariobro_3 »

Believe it or not, the Arduino OTA was "bricking" this module.
Do not use the example provided in the IDE, its wrong. Instead go here:
https://lastminuteengineers.com/esp32-o ... duino-ide/
I also modified it to work for this board to flash the neopixel instead of the built in led.

Code: Select all

#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Adafruit_NeoPixel.h>

const char* ssid = "***";
const char* password = "***";


#define NUMPIXELS        1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

void setup() {

#if defined(NEOPIXEL_POWER)
  // If this board has a power control pin, we must set it to output and high
  // in order to enable the NeoPixels. We put this in an #if defined so it can
  // be reused for other boards without compilation errors
  pinMode(NEOPIXEL_POWER, OUTPUT);
  digitalWrite(NEOPIXEL_POWER, HIGH);
#endif

  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels.setBrightness(20); // not so bright
  
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }

  // Port defaults to 3232
  // ArduinoOTA.setPort(3232);

  // Hostname defaults to esp3232-[MAC]
  // ArduinoOTA.setHostname("myesp32");

  // No authentication by default
  // ArduinoOTA.setPassword("admin");

  // Password can be set with it's md5 value as well
  // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
  // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");

  ArduinoOTA
    .onStart([]() {
      String type;
      if (ArduinoOTA.getCommand() == U_FLASH)
        type = "sketch";
      else // U_SPIFFS
        type = "filesystem";

      // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
      Serial.println("Start updating " + type);
    })
    .onEnd([]() {
      Serial.println("\nEnd");
    })
    .onProgress([](unsigned int progress, unsigned int total) {
      Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
    })
    .onError([](ota_error_t error) {
      Serial.printf("Error[%u]: ", error);
      if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
      else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
      else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
      else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
      else if (error == OTA_END_ERROR) Serial.println("End Failed");
    });

  ArduinoOTA.begin();

  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  ArduinoOTA.handle();  
  
  Serial.println("Hello!");
  
  // set color to red
  pixels.fill(0xFF0000);
  pixels.show();
  delay(500); // wait half a second

  // turn off
  pixels.fill(0x000000);
  pixels.show();
  delay(500); // wait half a second
  }

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”