Toll ... immer gegen die Deutschen ... ![]()
#include <VirtualWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define RX433DATAPIN 2
#define TX433DATAPIN 4
// hardware interrupt connected to the pin
// with Arduino UNO interrupt-0 belongs to pin-2, interrupt-1 to pin-3
#define RX433INTERRUPT 0
// Set speed of serial in Arduino IDE to the following value
#define SERIALSPEED 115200
// Now make some suggestions about pulse lengths that may be detected
// minimum duration (microseconds) of the start pulse
#define MINSTARTPULSE 4500
// minimum duration (microseconds) of a short bit pulse
#define MINBITPULSE 450
// minimum duration (microseconds) of a HIGH pulse between valid bits
#define MINHITIME 50
// variance between pulses that should have the same duration
#define PULSEVARIANCE 250
// minimum count of data bit pulses following the start pulse
#define MINPULSECOUNT 20
// maximum count of data bit pulses following the start pulse
#define MAXPULSECOUNT 50
// buffer sizes for buffering pulses in the interrupt handler
#define PBSIZE 216
//----------------------TXstarten---------------------------
unsigned long tx_millis=0;
void setup()
{
Serial.begin(115200);
lcd.begin ();
lcd.clear ();
Serial.println();
Serial.println("Start!");
lcd.setCursor(1,0);
lcd.print("AndalinhoPearl");
lcd.setCursor(1,1);
lcd.print("warte auf PT-250");
pinMode(RX433DATAPIN, INPUT);
pinMode(TX433DATAPIN, OUTPUT);
attachInterrupt(RX433INTERRUPT, rx433Handler, CHANGE);
//----------------------tx sender
vw_set_ptt_inverted(true); // Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(TX433DATAPIN);
pinMode(13, OUTPUT); // Internal LED will blink on transmit
tx_millis=millis();
}
volatile unsigned int pulsbuf[PBSIZE]; // ring buffer storing LOW pulse lengths
volatile unsigned int hibuf[PBSIZE]; // ring buffer storing HIGH pulse lengths
unsigned int validpulsbuf[MAXPULSECOUNT]; // linear buffer storing valid LOW pulses
unsigned int validhibuf[MAXPULSECOUNT]; // linear buffer storing valid HIGH pulses
volatile byte pbread,pbwrite; // read and write index into ring buffer
unsigned long Sequenz, LastSequenz;
unsigned int CRC, LastCRC;
unsigned int SequenzCounter;
unsigned long LastSequenzMillis;
void rx433Handler()
{
static long rx433LineUp, rx433LineDown;
long LowVal, HighVal;
int rx433State = digitalRead(RX433DATAPIN); // current pin state
if (rx433State) // pin is now HIGH
{
rx433LineUp=micros(); // line went HIGH after being LOW at this time
LowVal=rx433LineUp - rx433LineDown; // calculate the LOW pulse time
if (LowVal>MINBITPULSE)
{ // store pulse in ring buffer only if duration is longer than MINBITPULSE
// To be able to store startpulses of more than Maxint duration, we dont't store the actual time,
// but we store MINSTARTPULSE+LowVal/10, be sure to calculate back showing the startpulse length!
if (LowVal>MINSTARTPULSE) LowVal=MINSTARTPULSE+LowVal/10; // we will store this as unsigned int, so do range checking
pulsbuf[pbwrite]=LowVal; // store the LOW pulse length
pbwrite++; // advance write pointer in ringbuffer
if (pbwrite>=PBSIZE) pbwrite=0; // ring buffer is at its end
}
}
else
{
rx433LineDown=micros(); // line went LOW after being HIGH
HighVal=rx433LineDown - rx433LineUp; // calculate the HIGH pulse time
if (HighVal>31999) HighVal=31999; // we will store this as unsigned int
hibuf[pbwrite]=HighVal; // store the HIGH pulse length
}
}
boolean counting;
byte i,counter;
int startBitDurationL,startBitDurationH,shortBitDuration,longBitDuration;
String aStr = "";
int BinStrToInt(String aStr);
void showBuffer()
// this function will show the results on the serial monitor
// output will be shown if more bits than MINPULSECOUNT have been collected
{
long sum;
int avg;
sum=0;
if (counter>=MINPULSECOUNT)
{ // only show buffer contents if it has enough bits in it
/*
Serial.println();
Serial.print("Start Bit L: "); Serial.print((startBitDurationL-MINSTARTPULSE)*10L);
Serial.print(" H: ");Serial.println(startBitDurationH);
Serial.print("Data Bits: ");Serial.println(counter);
Serial.print("L: ");*/
for (i=0;i<counter;i++)
{
//Serial.print(validpulsbuf[i]);Serial.print(" ");
sum+=validpulsbuf[i];
}
avg = sum / counter; // calculate the average pulse length
// then assume that 0-bits are shorter than avg, 1-bits are longer than avg
for (i=0;i<counter;i++)
{
if (validpulsbuf[i] < avg) {
// Serial.print('0');
if (i > 1 && i < 34) {
Sequenz <<= 1; // eine 0 von rechts rein
}
else if (i >= 34) {
CRC <<= 1; // eine 0 von rechts rein
}
}
else {
// Serial.print('1');
if (i > 1 && i < 34) {
Sequenz <<= 1;
Sequenz |= 1; // eine 1 von rechts rein
}
else if (i >= 34) {
CRC <<= 1;
CRC |= 1; // eine 1 von rechts rein
}
}
}
}
if (Sequenz != 0 ) {
if (Sequenz == LastSequenz) {
SequenzCounter++;
}
else SequenzCounter = 0;
LastSequenz = Sequenz;
LastCRC = CRC;
Sequenz = 0;
CRC = 0;
LastSequenzMillis = millis();
}
counting = false;
counter = 0;
}
void loop()
{
long lowtime, hitime;
if (pbread!=pbwrite) // check for data in ring buffer
{
lowtime=pulsbuf[pbread]; // read data from ring buffer
hitime=hibuf[pbread];
cli(); // Interrupts off while changing the read pointer for the ringbuffer
pbread++;
if (pbread>=PBSIZE) pbread=0;
sei(); // Interrupts on again
if (lowtime>MINSTARTPULSE) // we found a valid startbit!
{
if (counting) showBuffer(); // new buffer starts while old is still counting, show it first
startBitDurationL=lowtime;
startBitDurationH=hitime;
counting=true; // then start collecting bits
counter=0; // no data bits yet
}
else if (counting && (counter==0)) // we now see the first data bit
{ // this may be a 0-bit or a 1-bit, so make some assumption about max/min lengths of data bits that will follow
shortBitDuration=lowtime/2;
if (shortBitDuration<MINBITPULSE+PULSEVARIANCE)
shortBitDuration=MINBITPULSE;
else
shortBitDuration-=PULSEVARIANCE;
longBitDuration=lowtime*2+PULSEVARIANCE;
validpulsbuf[counter]=lowtime;
validhibuf[counter]=hitime;
counter++;
}
else if (counting&&(lowtime>shortBitDuration)&&(lowtime<longBitDuration))
{
validpulsbuf[counter]=lowtime;
validhibuf[counter]=hitime;
counter++;
if ((counter==MAXPULSECOUNT) || (hitime<MINHITIME))
{
showBuffer();
}
}
else // Low Pulse is too short
{
if (counting) showBuffer();
counting=false;
counter=0;
}
}
if (millis() > LastSequenzMillis + 1000 && SequenzCounter > 3) { // enought time since last conversion
StoreMeas();
}
}
void StoreMeas() {
// start conversion
Serial.print(SequenzCounter);
Serial.print(" ");
Serial.print(LastSequenz, BIN);
Serial.print(" ");
Serial.print(LastCRC, BIN);
SequenzCounter = 0;
Serial.print("\t");
Serial.print(millis()/1000);
Serial.print("\tSensor ID: ");
int SensorID = LastSequenz >> 24;
Serial.print(SensorID);
int Kanal = (LastSequenz >> 18) & 0b11;
int Kanal_d = (Kanal + 1);
Serial.print("\tKanal: ");
Serial.print(Kanal_d, DEC);
int Temp_i = (LastSequenz>>6) & 0b1111111111;
Serial.print("\tTemp: ");
Serial.print(Temp_i);
lcd.clear();
lcd.print("Temperatur ");
lcd.print(Temp_i, DEC);
lcd.setCursor(1,1),
lcd.print("Kanal "),
lcd.print(Kanal_d, DEC);
vw_send((uint8_t *) &Kanal, sizeof(Kanal));
vw_send((uint8_t *) &Temp_i, sizeof(Temp_i));
vw_wait_tx();
Serial.print("\tübertragen "); Serial.print(Temp_i );
Serial.print("\tübertragen "); Serial.print(Kanal_d );
delay(700);
Serial.println();
}
hab mich jetzt für rc switch entschieden ist schneller als virtuel wire
#include <RCSwitch.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define RX433DATAPIN 2
#define TX433DATAPIN 4
// hardware interrupt connected to the pin
// with Arduino UNO interrupt-0 belongs to pin-2, interrupt-1 to pin-3
#define RX433INTERRUPT 0
// Set speed of serial in Arduino IDE to the following value
#define SERIALSPEED 115200
// Now make some suggestions about pulse lengths that may be detected
// minimum duration (microseconds) of the start pulse
#define MINSTARTPULSE 4500
// minimum duration (microseconds) of a short bit pulse
#define MINBITPULSE 450
// minimum duration (microseconds) of a HIGH pulse between valid bits
#define MINHITIME 50
// variance between pulses that should have the same duration
#define PULSEVARIANCE 250
// minimum count of data bit pulses following the start pulse
#define MINPULSECOUNT 20
// maximum count of data bit pulses following the start pulse
#define MAXPULSECOUNT 50
// buffer sizes for buffering pulses in the interrupt handler
#define PBSIZE 216
//----------------------TXstarten---------------------------
RCSwitch mySwitch = RCSwitch();
unsigned long tx_millis=0;
void setup()
{
Serial.begin(115200);
lcd.begin ();
lcd.clear ();
Serial.println();
Serial.println("Start!");
lcd.setCursor(1,0);
lcd.print("AndalinhoPearl");
lcd.setCursor(1,1);
lcd.print("warte auf PT-250");
pinMode(RX433DATAPIN, INPUT);
mySwitch.enableTransmit(4);
attachInterrupt(RX433INTERRUPT, rx433Handler, CHANGE);
//----------------------tx sender
pinMode(13, OUTPUT); // Internal LED will blink on transmit
tx_millis=millis();
}
volatile unsigned int pulsbuf[PBSIZE]; // ring buffer storing LOW pulse lengths
volatile unsigned int hibuf[PBSIZE]; // ring buffer storing HIGH pulse lengths
unsigned int validpulsbuf[MAXPULSECOUNT]; // linear buffer storing valid LOW pulses
unsigned int validhibuf[MAXPULSECOUNT]; // linear buffer storing valid HIGH pulses
volatile byte pbread,pbwrite; // read and write index into ring buffer
unsigned long Sequenz, LastSequenz;
unsigned int CRC, LastCRC;
unsigned int SequenzCounter;
unsigned long LastSequenzMillis;
void rx433Handler()
{
static long rx433LineUp, rx433LineDown;
long LowVal, HighVal;
int rx433State = digitalRead(RX433DATAPIN); // current pin state
if (rx433State) // pin is now HIGH
{
rx433LineUp=micros(); // line went HIGH after being LOW at this time
LowVal=rx433LineUp - rx433LineDown; // calculate the LOW pulse time
if (LowVal>MINBITPULSE)
{ // store pulse in ring buffer only if duration is longer than MINBITPULSE
// To be able to store startpulses of more than Maxint duration, we dont't store the actual time,
// but we store MINSTARTPULSE+LowVal/10, be sure to calculate back showing the startpulse length!
if (LowVal>MINSTARTPULSE) LowVal=MINSTARTPULSE+LowVal/10; // we will store this as unsigned int, so do range checking
pulsbuf[pbwrite]=LowVal; // store the LOW pulse length
pbwrite++; // advance write pointer in ringbuffer
if (pbwrite>=PBSIZE) pbwrite=0; // ring buffer is at its end
}
}
else
{
rx433LineDown=micros(); // line went LOW after being HIGH
HighVal=rx433LineDown - rx433LineUp; // calculate the HIGH pulse time
if (HighVal>31999) HighVal=31999; // we will store this as unsigned int
hibuf[pbwrite]=HighVal; // store the HIGH pulse length
}
}
boolean counting;
byte i,counter;
int startBitDurationL,startBitDurationH,shortBitDuration,longBitDuration;
String aStr = "";
int BinStrToInt(String aStr);
void showBuffer()
// this function will show the results on the serial monitor
// output will be shown if more bits than MINPULSECOUNT have been collected
{
long sum;
int avg;
sum=0;
if (counter>=MINPULSECOUNT)
{ // only show buffer contents if it has enough bits in it
/*
Serial.println();
Serial.print("Start Bit L: "); Serial.print((startBitDurationL-MINSTARTPULSE)*10L);
Serial.print(" H: ");Serial.println(startBitDurationH);
Serial.print("Data Bits: ");Serial.println(counter);
Serial.print("L: ");*/
for (i=0;i<counter;i++)
{
//Serial.print(validpulsbuf[i]);Serial.print(" ");
sum+=validpulsbuf[i];
}
avg = sum / counter; // calculate the average pulse length
// then assume that 0-bits are shorter than avg, 1-bits are longer than avg
for (i=0;i<counter;i++)
{
if (validpulsbuf[i] < avg) {
// Serial.print('0');
if (i > 1 && i < 34) {
Sequenz <<= 1; // eine 0 von rechts rein
}
else if (i >= 34) {
CRC <<= 1; // eine 0 von rechts rein
}
}
else {
// Serial.print('1');
if (i > 1 && i < 34) {
Sequenz <<= 1;
Sequenz |= 1; // eine 1 von rechts rein
}
else if (i >= 34) {
CRC <<= 1;
CRC |= 1; // eine 1 von rechts rein
}
}
}
}
if (Sequenz != 0 ) {
if (Sequenz == LastSequenz) {
SequenzCounter++;
}
else SequenzCounter = 0;
LastSequenz = Sequenz;
LastCRC = CRC;
Sequenz = 0;
CRC = 0;
LastSequenzMillis = millis();
}
counting = false;
counter = 0;
}
void loop()
{
long lowtime, hitime;
if (pbread!=pbwrite) // check for data in ring buffer
{
lowtime=pulsbuf[pbread]; // read data from ring buffer
hitime=hibuf[pbread];
cli(); // Interrupts off while changing the read pointer for the ringbuffer
pbread++;
if (pbread>=PBSIZE) pbread=0;
sei(); // Interrupts on again
if (lowtime>MINSTARTPULSE) // we found a valid startbit!
{
if (counting) showBuffer(); // new buffer starts while old is still counting, show it first
startBitDurationL=lowtime;
startBitDurationH=hitime;
counting=true; // then start collecting bits
counter=0; // no data bits yet
}
else if (counting && (counter==0)) // we now see the first data bit
{ // this may be a 0-bit or a 1-bit, so make some assumption about max/min lengths of data bits that will follow
shortBitDuration=lowtime/2;
if (shortBitDuration<MINBITPULSE+PULSEVARIANCE)
shortBitDuration=MINBITPULSE;
else
shortBitDuration-=PULSEVARIANCE;
longBitDuration=lowtime*2+PULSEVARIANCE;
validpulsbuf[counter]=lowtime;
validhibuf[counter]=hitime;
counter++;
}
else if (counting&&(lowtime>shortBitDuration)&&(lowtime<longBitDuration))
{
validpulsbuf[counter]=lowtime;
validhibuf[counter]=hitime;
counter++;
if ((counter==MAXPULSECOUNT) || (hitime<MINHITIME))
{
showBuffer();
}
}
else // Low Pulse is too short
{
if (counting) showBuffer();
counting=false;
counter=0;
}
}
if (millis() > LastSequenzMillis + 1000 && SequenzCounter > 3) { // enought time since last conversion
StoreMeas();
}
}
void StoreMeas() {
// start conversion
Serial.print(SequenzCounter);
Serial.print(" ");
Serial.print(LastSequenz, BIN);
Serial.print(" ");
Serial.print(LastCRC, BIN);
SequenzCounter = 0;
Serial.print("\t");
Serial.print(millis()/1000);
Serial.print("\tSensor ID: ");
int SensorID = LastSequenz >> 24;
Serial.print(SensorID);
int Kanal = (LastSequenz >> 18) & 0b11;
int Kanal_d = (Kanal + 1);
Serial.print("\tKanal: ");
Serial.print(Kanal_d, DEC);
int Temp_i = (LastSequenz>>6) & 0b1111111111;
Serial.print("\tTemp: ");
Serial.print(Temp_i);
lcd.clear();
lcd.print("Temperatur ");
lcd.print(Temp_i, DEC);
lcd.setCursor(1,1),
lcd.print("Kanal "),
lcd.print(Kanal_d, DEC);
mySwitch.send(Temp_i, DEC);
Serial.print("\tübertragen "); Serial.print(Temp_i );
delay(1000);
Serial.println();
}
und das bau ich in den Mega mit Ethernetshield ein
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup()
{
Serial.begin(115200);
mySwitch.enableReceive(0); // Empfänger ist an Interrupt-Pin "0" - Das ist am UNO der Pin2,3 am Mega 2, 3, 18, 19, 20, 21
}
void loop() {
if (mySwitch.available()) // Wenn ein Code Empfangen wird...
{
int value_pearl = mySwitch.getReceivedValue(); // Empfangene Daten werden unter der Variable "value_pearl" gespeichert.
if (value_pearl == 0) // Wenn die Empfangenen Daten "0" sind, wird "Unbekannter Code" angezeigt.
{
Serial.println("Unbekannter Code");
}
else // Wenn der Empfangene Code brauchbar ist, wird er hier an den Serial Monitor gesendet.
{
Serial.print("\t");
Serial.print("\tEmpfangen: ");
Serial.print(value_pearl);
}
mySwitch.resetAvailable(); // Hier wird der Empfänger "resettet"
Serial.println();
}
}