ESP8266 based dmx receiver and sender
Posted: Sat Apr 25, 2020 10:02 am
Hi,
So I have a esp with two max485 shields to receive (alter) and send dmx data. However, first off i'd like to just receive and pass through the DMX signal so when i get that to work, i can start doing my if's and elses.
However, reading the dmx just doesn't seem to start working with my kind of experience. I based the sending (which actually already works) on the espdmx library found here:
The code i'm currently running:
I know somewhere i'm messing up RX pins, serial and the attachinterrupt, however i'm not very capable of sorting out what to do after days of desperate trial and error.
source: https://github.com/Rickgg/ESP-Dmx
So I have a esp with two max485 shields to receive (alter) and send dmx data. However, first off i'd like to just receive and pass through the DMX signal so when i get that to work, i can start doing my if's and elses.
However, reading the dmx just doesn't seem to start working with my kind of experience. I based the sending (which actually already works) on the espdmx library found here:
The code i'm currently running:
Code: Select all
#include <ESP8266WiFi.h>
#include <Ticker.h> //Ticker Library
#include "espDMX2/espDMX.h"
#define NUMBER_OF_CHANNELS 512
unsigned int dmxaddress = 1;
#define DEpinTX D5
#define DEpinRX D6
#define btnPin 7
DMXESPSerial dmx;
#define RX_PIN 3 // serial receive pin, which takes the incoming data from the MAX485.
#define TX_PIN 2 // serial transmission pin
/******************************* DMX variable declarations ********************************/
volatile byte i = 0; //dummy variable for dmxvalue[]
volatile byte dmxreceived = 0; //the latest received value
volatile unsigned int dmxcurrent = 0; //counter variable that is incremented every time we receive a value.
volatile int currentBit = 0;
volatile byte dmxvalue[NUMBER_OF_CHANNELS];
volatile int zeroCounter = 0;
volatile int dmxnewvalue = 0;
void setup() {
//pinMode(RX_PIN, FUNCTION_3);
WiFi.mode( WIFI_OFF );
WiFi.forceSleepBegin();
/******************************* Max485 configuration ***********************************/
Serial.begin(250000);
pinMode(DEpinTX, OUTPUT);
pinMode(DEpinRX, OUTPUT);
digitalWrite(DEpinRX, LOW);
digitalWrite(DEpinTX, HIGH);
dmx.init(512);
pinMode(RX_PIN, INPUT); //sets serial pin to receive data
pinMode(TX_PIN, OUTPUT);
timer1_attachInterrupt(onTimerISR); //function to call at 4us
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP); // 80 Mhz / 16 = 5 Mhz, 5 ticks / us -> 20 ticks = 4us,
timer1_write(20); //4 us
delay(2000);
} //end setup()
void loop() {
// the processor gets parked here while the ISRs are doing their thing.
if (dmxnewvalue == 1) { //when a new set of values are received, jump to action loop...
for (int y = 0; y < NUMBER_OF_CHANNELS; y++) {
dmx.write(y, interestingBytes[y]);
}
dmx.update();
dmxnewvalue = 0;
zeroCounter = 0; //and then when finished reset variables and enable timer2 interrupt
i = 0;
}
} //end loop()
void onTimerISR() {
if (digitalRead(RX_PIN)) {
zeroCounter = 0;
} else {
zeroCounter++;
if (zeroCounter == 20) {
//80 us break (20 zero's in a row)
attachInterrupt(digitalPinToInterrupt(RX_PIN), receptionISR, CHANGE);
}
}
}
void receptionISR() {
dmxreceived = Serial.read();
dmxcurrent++; //increment address counter
if (dmxcurrent > dmxaddress) { //check if the current address is the one we want.
dmxvalue[i] = dmxreceived;
i++;
if (i == NUMBER_OF_CHANNELS) {
detachInterrupt(digitalPinToInterrupt(RX_PIN));
dmxnewvalue = 1; //set newvalue, so that the main code can be executed.
}
}
}
I know somewhere i'm messing up RX pins, serial and the attachinterrupt, however i'm not very capable of sorting out what to do after days of desperate trial and error.
source: https://github.com/Rickgg/ESP-Dmx