Thomas Chef %!s(int64=7) %!d(string=hai) anos
achega
6386672aef
Modificáronse 4 ficheiros con 310 adicións e 0 borrados
  1. 6 0
      README.md
  2. 18 0
      queue.h
  3. 80 0
      queue.ino
  4. 206 0
      wireless_temp_sensor_receiver.ino

+ 6 - 0
README.md

@@ -0,0 +1,6 @@
+#This is the Arduino 433MHz Wireless Receiver project used for the Raspberry project at HW&HMI-Dept in Eskilstuna
+
+It receives wireless temp-sensor data and sends it to the Raspberry with the uart as a string
+
+Board: (Arduino) SparkFun Pro Micro (Ali-Copy)
+CPU:   ATmega32U4 16 MHz

+ 18 - 0
queue.h

@@ -0,0 +1,18 @@
+#ifndef ____queue__
+#define ____queue__
+
+
+
+#define NEXA_QUEUE_SIZE 500	// Queue size from Nexa-remotes
+
+
+unsigned char rcvDeQueue( unsigned int *value );
+unsigned char rcvEnQueue( uint16_t value );
+unsigned char rcvRollbackQueue();
+unsigned int getQueueUsed();
+void  rcvInitQueue(  );
+
+
+
+#endif
+

+ 80 - 0
queue.ino

@@ -0,0 +1,80 @@
+#include "queue.h"
+
+static int read_d;				/*!< Read index points to the place in the queue that has already been read */
+static int write_d;				/*!< The write index points to the place where the next write in the queue will take place. */
+
+static uint16_t nexaQueueBuffer[NEXA_QUEUE_SIZE];
+
+
+void  rcvInitQueue(  ) {
+    
+	read_d = 0;
+	write_d = 1;
+}
+
+unsigned int getQueueUsed() {
+  
+  return (write_d-read_d);
+}
+
+unsigned char rcvEnQueue( uint16_t value ) {
+    
+	// Is the queue full ?
+	if( write_d == read_d )
+	{
+		return 0u;
+	}
+    
+	// Make the data copy
+	nexaQueueBuffer[write_d] = value;
+    
+	// Increment the write position
+	write_d++;
+	if( write_d == NEXA_QUEUE_SIZE ) write_d=0;
+    
+	return 1u;
+}
+
+unsigned char  rcvDeQueue( uint16_t *value ) {
+    
+	int writePos = write_d;
+    
+	// Pseudo writepos
+	if( writePos < read_d || writePos == read_d ) writePos += NEXA_QUEUE_SIZE;
+    
+	// The queue is empty ?
+	if( writePos == ( read_d + 1 ) )
+	{
+		return 0u;
+	}
+    
+	// Increment the read position
+	read_d++;
+	if( read_d == NEXA_QUEUE_SIZE ) read_d=0; 
+    
+	// Make the data copy
+	*value = nexaQueueBuffer[read_d];
+    
+	return 1u;
+}
+
+// This function rolls back the latest inserted entry
+unsigned char rcvRollbackQueue() {
+
+	// Is the queue empty ?
+	if( write_d == ( read_d + 1 ) )
+	{
+		return 0u; // No rollback
+	}
+	
+	if( write_d == 0 ) {
+		write_d = NEXA_QUEUE_SIZE - 1;
+	}
+	else {
+		write_d--;
+	}
+
+	return 1u;
+}
+
+

+ 206 - 0
wireless_temp_sensor_receiver.ino

@@ -0,0 +1,206 @@
+#include "queue.h"
+
+boolean disableRx = true;
+
+const int rxPinA      = 7;  // RX, Connected to pin 7
+const int enablePin   = 4;  // Enable, Connected to RTX pin 6: 0=Power down, 1=Active
+const int txEnablePin = 5;  // rx/TX, Connected to RTX pin 5: 0=Rx, 1=Tx
+const int txPin       = 6;  // TX, Connected to RTX pin 4
+
+
+//********************************************************************
+//                              SETUP
+//********************************************************************
+void setup() {
+  
+  disableRx = true;
+
+  pinMode(rxPinA, INPUT);
+  pinMode(enablePin, OUTPUT);
+  pinMode(txEnablePin, OUTPUT);  
+  pinMode(txPin, OUTPUT);  
+  
+  digitalWrite(enablePin, LOW);
+  digitalWrite(txEnablePin, LOW);
+  digitalWrite(txPin, LOW);  
+  
+  // ------------------ INIT THE TRANCEIVER -------------------------
+  // From powerdown mode (pin 4-5-6 low), 
+  // 1. Drive high pin 6 (ENABLE)
+  // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us 
+  // 3. Drive down 20us pin 6 (ENABLE).  
+  digitalWrite(enablePin, HIGH); delayMicroseconds( 20 ); 
+  digitalWrite(txEnablePin, HIGH); delayMicroseconds( 200 ); digitalWrite(txEnablePin, LOW);
+  delayMicroseconds( 40 ); 
+  digitalWrite(enablePin, LOW); delayMicroseconds( 20 ); digitalWrite(enablePin, HIGH);  
+  delayMicroseconds( 200 );
+  // ------------------ INIT THE TRANCEIVER -------------------------  
+  
+  Serial.begin(9600);  // USB Serial
+
+  rcvInitQueue();
+  
+  cli(); // Clear interrupt
+  setup_timer_interrupt();
+  sei(); // Enable interrupt
+  
+  disableRx = false;
+}
+
+//********************************************************************
+//                       INTERRUPT FUNCTIONS
+//********************************************************************
+
+void setup_timer_interrupt()
+{
+  //set timer1 interrupt at 10Hz
+  TCCR1A = 0;// set entire TCCR1A register to 0
+  TCCR1B = 0;// same for TCCR1B
+  TCNT1  = 0;//initialize counter value to 0
+  // set compare match register for 1hz increments
+  
+  //  OCR1A = 40. This results is 16MHz / 8 / 40 = 20 uS between interrupts
+  OCR1A = 40;
+
+  // turn on CTC mode
+  TCCR1B |= (1 << WGM12);
+
+  // Set CS12:CS10 to 010 = /8 prescaler
+  TCCR1B |= (1 << CS11);  
+ 
+  // enable timer compare interrupt
+  TIMSK1 |= (1 << OCIE1A);
+}  
+
+// 10uS timer interrupt function
+ISR(TIMER1_COMPA_vect,ISR_NOBLOCK) {
+  
+    static unsigned char currValue = 0;
+    static unsigned short samples = 0;
+    static unsigned short newSamples = 0;
+    
+    //return; // FOR DEBUG, Disables nexa receive
+
+    // Sample the pin value
+    unsigned char value = digitalRead(rxPinA);
+
+    if( value == currValue ) {
+        samples++;
+        samples+=newSamples;
+        newSamples=0;
+    }
+    else {
+        newSamples++;
+    }
+
+    if( newSamples == 3 ) {
+        uint16_t queSamples = (uint16_t)((samples*20 > 60000)? 60000 : samples*20);
+        rcvEnQueue(queSamples); // Max 46000
+        samples = newSamples;
+        newSamples = 0;
+        currValue = value;    
+    }
+}
+
+void stopInterrupts() {
+    EIMSK &= 0xFC;      // Clear bit 1:0 in Enable Interrupt register
+}
+
+//********************************************************************
+//                           LOOP
+//
+// Test-code: <NT1B5C1D83>
+//********************************************************************
+#define SER_RX_MODE_WAIT 0
+#define SER_RX_MODE_RCV 1
+#define SER_RX_MODE_END 2
+
+extern unsigned long x_data; // Receive data from Nexa
+extern unsigned long temp1_x_data; // Receive data from temp1
+
+void loop() {
+  
+  static int rxMode = SER_RX_MODE_WAIT;
+  static int rxNum = 0;
+  
+  static unsigned long last_rx_time = 0;
+  
+  static unsigned char txDataReady = 0;
+  
+  static unsigned long previous_nexa_x_data = 0;
+  static unsigned long nexa_old_time=0;
+  
+  static unsigned long previous_temp1_x_data = 0;
+  static unsigned long temp1_old_time=0;
+  
+  //static boolean firstRun = true;
+
+  static uint16_t width;
+  
+  static unsigned long txCode = 0;
+  
+  static unsigned long previous_now = 0; // Get the current time
+  unsigned long now = millis(); // Get the current time
+  
+  // Check millis() for wrapping (after 49 days)
+  if( previous_now > now ) {
+    last_rx_time = 0; // Reset last rx time    
+  }
+  
+  // Display version information 10 sec after startup
+  /* if( firstRun == true && now > 30000 ) {
+    Serial.println("433MHz Repeater module. (Arduino nano) Ver: 170211 14:46");  // **** VERSION ****
+    firstRun = false;
+  } */
+  
+  if( rcvDeQueue(&width) ) {
+    
+      // ********** NEXA ***********
+      if( nextPulseNexa(width) ) {
+        
+          last_rx_time = now;            // Set last rx time to now
+        
+          if( x_data != previous_nexa_x_data || now > (nexa_old_time+1000) ) {
+
+        	    unsigned long  remote_id = (x_data & 0xFFFFFFC0) >>6;
+        	    int group     = (x_data & 0x00000020) >>5;
+        	    int onOff     = (x_data & 0x00000010) >>4;
+        	    int channel   = (x_data & 0x0000000C) >>2;
+        	    int button    = (x_data & 0x00000003);
+
+              //Serial.println(remote_id);
+
+              if( ( remote_id == 11067169UL || remote_id == 20071093UL ) && txDataReady == 0 ) {
+              
+                  remote_id++;      // Increase the remote id with one ( = the real id of the nexa remote control )
+                  txDataReady = 1;
+                  
+    
+              		txCode  = ((remote_id << 6) & 0xFFFFFFC0 );
+              		txCode |= ((group     << 5) & 0x00000020 );
+              		txCode |= ((onOff     << 4) & 0x00000010 );
+              		txCode |= ((channel   << 2) & 0x0000000C );
+              		txCode |= ((button        ) & 0x00000003 );
+              
+              }
+
+              previous_nexa_x_data = x_data;
+          }
+          
+          nexa_old_time = now;
+          resetDecoder();
+      }
+  }
+  
+  if( txDataReady == 1 && (now > (last_rx_time+200)) ) { // Transmit if it has passed more than 100mS since last rx
+      txDataReady = 0;
+      sendNexaCode(txCode);
+  }
+  
+  previous_now = now;  // Store to check for wrapping of millis()
+}
+
+
+
+
+