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");
}