Hey All,
I've got some code that works as intended on a generic esp8266 board. I want to use this code on a Feather Huzzah.
The pinouts are different between the two boards so I update that before uploading. I change the board in the board manager and upload. As far as I can tell that's all I need to change.
The Huzzah starts up and connects to the wifi network but after a second or two it drops and the board resets/restarts. Before it crashes the serial monitor sends out a line of gibberish. I tried looking at it at different baud rates but it kept coming as gibberish.
How can I make this work on the Huzzah?
My code is in tabs. I've pasted each one separately
// coded for Feather Huzzah
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <LEDFader.h>
#include <stdlib.h>
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
// The following pins are PWM for the Feather Huzzah
#define LED_PIN 3
#define LED_PIN2 12
#define LED_PIN3 13
#define fan 14
// The following pins are PWM for the generic esp8266
// #define LED_PIN 4
// #define LED_PIN2 14
// #define LED_PIN3 15
// #define fan 12
char message_buff[100];
#define FADE_UP_TIME 10
#define FADE_DOWN_TIME 3000
#define FADE_TIME 3000
#define DIR_UP 1
#define DIR_DOWN -1
LEDFader led;
LEDFader led2;
LEDFader led3;
// #define LED_NUM 3
int direction = DIR_UP;
float bookshelfTemp = 0;
int brightness = 0;
int lastBrightness = 0;
unsigned long currentMillisTemps = 0;
unsigned long previousMillisTemps = 0; // will store last time LED was updated
const long intervalTemps = 10000;
// Update these with values suitable for your network.
const char* ssid = "XXX";
const char* password = "XXX";
const char* mqtt_server = "192.168.1.217";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(ledControl); //callback
pinMode(3, OUTPUT); // Initialize the BUILTIN_LED pin as an output
pinMode(10, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);
pinMode(14, OUTPUT);
led = LEDFader(LED_PIN);
led.fade(0, 255);
led2 = LEDFader(LED_PIN2);
led2.fade(0, 255);
led3 = LEDFader(LED_PIN3);
led3.fade(0, 255);
Serial.println("Bookshelf 8/8/2023 - this script is configured for generic esp8266");
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.hostname("BOOKSHELF");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("/bookshelf/motionSensor", "Reconnected to Hanging Lamp");
client.subscribe("/bookshelf/lamp1");
client.subscribe("/bookshelf/lamp2");
client.subscribe("/bookshelf/lamp3");
client.subscribe("/bookshelf/lamp4");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void ledControl(char* topic, byte* payload, unsigned int length) {
StaticJsonDocument<256> doc; // Allocate memory for the doc array.
deserializeJson(doc, payload, length);
// Serial.print("Message arrived: ["); Serial.print(topic); Serial.println("]"); // Prints out anything that's arrived from broker and from the topic that we've subscribed to.
int i;
for (i = 0; i < length; i++) message_buff[i] = payload[i];
message_buff[i] = '\0'; // We copy payload to message_buff because we can't make a string out of payload.
String modeBrightness = String(message_buff);
modeBrightness = modeBrightness.toInt();
if (strcmp(topic, "/bookshelf/lamp1") == 0) { // Returns 0 if the strings are equal, so we have received our topic.
uint16_t modeBrightness = doc["lamp1Brightness"];
Serial.print("lamp1ModeBrightness: ");
Serial.println(modeBrightness);
led.fade(255, 500);
// if (modeBrightness == 0) { digitalWrite(4, LOW); }
}
if (strcmp(topic, "/bookshelf/lamp2") == 0) { // Returns 0 if the strings are equal, so we have received our topic.
uint16_t modeBrightness = doc["lamp2Brightness"];
Serial.print("lamp2ModeBrightness: ");
Serial.println(modeBrightness);
led2.fade(modeBrightness, 500);
}
if (strcmp(topic, "/bookshelf/lamp3") == 0) { // Returns 0 if the strings are equal, so we have received our topic.
uint16_t modeBrightness = doc["lamp3Brightness"];
Serial.print("lamp3ModeBrightness: ");
Serial.println(modeBrightness);
led3.fade(modeBrightness, 0);
}
if ((strcmp(topic, "/bookshelf/fan") == 0) && ((char)payload[0] == '0')) {
digitalWrite(fan, LOW);
Serial.println("FAN OFF");
client.publish("/bookshelf/fan", "off");
}
if ((strcmp(topic, "/bookshelf/fan") == 0) && ((char)payload[0] == '1')) {
digitalWrite(fan, HIGH);
Serial.println("FAN ON");
client.publish("/bookshelf/fan", "on");
}
}
void loop() {
led.update();
led2.update();
led3.update();
if (!client.connected()) { reconnect(); }
client.loop();
}```