Hi,
this is my first post here, so I am sorry if I miss something. I really like this platform and Arduino in general. The help which is provided here is really useful and nice.
So what I am trying to do is that I have built and two Ambilights. I previously controlled them with an Arduino Nano until I got to know the ESP32. When I tried to control the Amblights with the ESP32, to have an IoT device, I got some problems with flickering, which only occurred when the Wi-Fi on the ESP32 was on. When googling I found out that this is a known Problem and that there is not really a solution to it. Now I am trying to send some data over Serial communication to the Arduino Nano over the ESP32 to have both an Ambilight and an IoT device. The Problem I am getting is that even when the ESP32 is not supposed to send data (so basically after Serial.end() has been called), the Data from Ambibox is not getting to the Arduino Nano. When I manually remove the wire between the ESP32 and the Arduino Nano it works. I don't even have to restart the Arduino Nano or anything. What I also found out while testing was that when the ESP32 had no power and the wire was still connected it worked. This is why my conclusion is that for some reason the ESP32 still sends data to the Arduino Nano even if you write Serial.end().
I am attaching the code for the ESP32 and the Arduino Nano. Also, I will attach a wiring diagram. If you guys have any questions, especially regarding the code, please ask.
Thanks in advance!
Arduino Nano:
#include "FastLED.h"
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 98
#define DATA_PIN 6
// Baudrate, higher rate allows faster refresh rate and more LEDs (defined in /etc/boblight.conf)
#define serialRate 115200
// Adalight sends a "Magic Word" (defined in /etc/boblight.conf) before sending the pixel data
uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i;
// Initialise LED-array
CRGB leds[NUM_LEDS];
Adafruit_NeoPixel strip(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// Use NEOPIXEL to keep true colors
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
strip.begin();
strip.show();
strip.setBrightness(255);
strip.show();
Serial.begin(serialRate);
// Send "Magic Word" string to host
Serial.print("Ada\n");
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255));
strip.show();
delay(5);
}
delay(1500);
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, color);
strip.show();
delay(wait);
}
}
void loop() {
int msg = Serial.read();
if (msg != 10 && msg != 13) {
Serial.println(msg);
if (msg == 49) {
normal();
Serial.println("normal");
}
if (msg == 50) {
anders();
Serial.println("anders");
}
if (msg == -1) {
Serial.end();
delay(1000);
Serial.begin(serialRate);
ambilight();
Serial.println("ambilight");
}
}
}
void ambilight() {
while (true) {
for (i = 0; i < sizeof prefix; ++i) {
waitLoop: while (!Serial.available()) ;;
// Check next byte in Magic Word
if (prefix[i] == Serial.read()) continue;
// otherwise, start over
i = 0;
goto waitLoop;
}
// Hi, Lo, Checksum
while (!Serial.available()) ;;
hi = Serial.read();
while (!Serial.available()) ;;
lo = Serial.read();
while (!Serial.available()) ;;
chk = Serial.read();
// If checksum does not match go back to wait
if (chk != (hi ^ lo ^ 0x55)) {
i = 0;
goto waitLoop;
}
memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
// Read the transmission data and set LED values
for (uint8_t i = 0; i < NUM_LEDS; i++) {
byte r, g, b;
while (!Serial.available());
r = Serial.read();
while (!Serial.available());
g = Serial.read();
while (!Serial.available());
b = Serial.read();
leds[i].r = r;
leds[i].g = g;
leds[i].b = b;
}
// Shows new values
FastLED.show();
}
}
int u = 0;
void anders() {
for (int j = 0; j <= 255; j++) {
for (int i = 0; i < strip.numPixels(); i++) {
int msg = Serial.read();
if (msg != 10 && msg != 13 && msg != 52) {
if (u == 1) {
goto anfang;
}
u = u++;
strip.setPixelColor(i, strip.Color(0, j, 0));
}
strip.setPixelColor(i, strip.Color(0, j, 0));
}
strip.show();
delay(10);
}
anfang: u = 0;
}
void normal() {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(255, 255, 0));
}
strip.show();
}
ESP32:
#include <WiFi.h>
int k = 0;
const char* ssid = "FRITZ!Box 7590 UL";
const char* password = "blumenstr6";
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
k = k + 1;
if (k >= 5) {
ESP.restart();
}
delay(250);
}
server.begin();
}
int value = 0;
int p = 0;
boolean drei = false;
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> normal.
");
client.print("Click <a href=\"/L\">here</a> anders.
");
client.print("Click <a href=\"/A\">here</a> ambilight.
");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
if (!drei) {
p = 1;
} else {
p = 1;
drei = false;
Serial.begin(115200);
delay(10);
}
}
if (currentLine.endsWith("GET /L")) {
if (!drei) {
p = 2;
} else {
p = 2;
drei = false;
Serial.begin(115200);
delay(10);
}
}
if (currentLine.endsWith("GET /A")) {
p = 3;
drei = true;
Serial.flush();
Serial.end();
}
}
}
// close the connection:
client.stop();
}
if (p != 3) Serial.println(p);
}