Advertisement
tMh

Maverick 733 receiver on raspberry pi (v6)

tMh
Jan 29th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.17 KB | None | 0 0
  1. #define STATE_START_PULSES      0
  2. #define STATE_FIRST_BIT         1
  3. #define STATE_DATA              2
  4. #define PIN                     4
  5.  
  6. #include <wiringPi.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <wiringSerial.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <string.h>
  13.  
  14. #define TIME_THRES_HIGH     290
  15. #define TIME_THRES_LOW      430
  16. #define LOW_VALID       -50
  17. #define HIGH_VALID      400
  18.  
  19. unsigned int volatile start_pulse_counter = 0, detection_state = 0;
  20. unsigned int volatile last_interrupt_micros = 0, last_interrupt_millis;
  21. unsigned int nibble = 0, fd, data_array_index = 0, data_array[13], shift_value = 0, short_bit = 0, add_1st_bit = 1, current_byte = 0, current_bit = 1, bit_count = 0, ISR_status, save_array[13];
  22. signed int probe1=0, probe2=0;
  23. unsigned int probe1_array[6], probe2_array[6];
  24. unsigned long volatile last_valid_millis=0;
  25.  
  26. time_t t;
  27. struct tm *ts;
  28.  
  29. char buff[20];
  30.  
  31. void updateThingspeak(int field1, int field2, int field3)
  32. {
  33.     char str4[200];
  34.  
  35.     sprintf(str4, "echo \"key=[insert key here]&field1=%d&field2=%d&field3=%d\" | lynx -post-data https://thingspeak.com/update 1>/dev/null 2>/dev/null",field1,field2, field3);
  36.     system(str4);
  37.  
  38. }
  39.  
  40.  
  41. // make the quarternary convertion
  42. unsigned int quart(unsigned int param)
  43. {
  44.         param &= 0x0F;
  45.         if (param==0x05)
  46.                 return(0);
  47.         if (param==0x06)
  48.                 return(1);
  49.         if (param==0x09)
  50.                 return(2);
  51.         if (param==0x0A)
  52.                 return(3);
  53. }
  54.  
  55. void outputData(void)
  56. {
  57.         unsigned int i=0;
  58.     unsigned long current_millis;
  59. //  char str4[100];
  60.  
  61.         probe1 = probe2 = 0;
  62.  
  63.         if (    (save_array[0] == 0xAA) &&
  64.                 (save_array[1] == 0x99) &&
  65.                 (save_array[2] == 0x95) &&
  66.                 (save_array[3] == 0x59) )
  67.         {
  68.                 probe2_array[0]= quart(save_array[8] & 0x0F);
  69.                 probe2_array[1]= quart(save_array[8] >> 4);
  70.                 probe2_array[2]= quart(save_array[7] & 0x0F);
  71.                 probe2_array[3]= quart(save_array[7] >> 4);
  72.                 probe2_array[4]= quart(save_array[6] & 0x0F);
  73.  
  74.                 probe1_array[0]= quart(save_array[6] >> 4);
  75.                 probe1_array[1]= quart(save_array[5] & 0x0F);
  76.                 probe1_array[2]= quart(save_array[5] >> 4);
  77.                 probe1_array[3]= quart(save_array[4] & 0x0F);
  78.                 probe1_array[4]= quart(save_array[4] >> 4);
  79.  
  80.                 for (i=0;i<=4;i++)
  81.                 {
  82.                         probe1 += probe1_array[i] * (1<<(2*i));
  83.                         probe2 += probe2_array[i] * (1<<(2*i));
  84.                 }
  85.  
  86.                 probe1 -= 532;
  87.                 probe2 -= 532;
  88.  
  89.         if((probe1>LOW_VALID && probe1<HIGH_VALID || probe1==-532) && (probe2>LOW_VALID && probe2<HIGH_VALID || probe2==-532))
  90.         {
  91.             t=time(NULL);
  92.     //      ts=localtime(&t);
  93.             current_millis=millis();
  94.                     strftime(buff, 20, "%H:%M:%S %d.%m.%Y", localtime(&t));
  95.             printf("%d\t%d\t%d\t%s\n",probe1,probe2,(current_millis-last_valid_millis)/1000 ,buff);
  96.             fflush(stdout);
  97.  
  98.             updateThingspeak(probe1, probe2, (current_millis-last_valid_millis)/1000);
  99.             last_valid_millis=current_millis;
  100.         }
  101.         else
  102.         {
  103.             printf("!!!TEMPERATURE EXCEEDED VALID RANGE!!! (%d, %d)\n",probe1, probe2);
  104.         }
  105.         }
  106. }
  107.  
  108. void myInterrupt (void)
  109. {
  110.         unsigned int time_since_last = 0;
  111.         unsigned int tsl_micros = 0;
  112.         unsigned int bit_ok = 0, i;
  113.     unsigned int pin_state=digitalRead(PIN);
  114. //get the time since last interrupt in milli and micro seconds
  115.         time_since_last = (millis() - last_interrupt_millis);
  116.         tsl_micros = (micros() - last_interrupt_micros);
  117.  
  118.         //store current interrupt time to calculate time since last (above)
  119.         last_interrupt_micros = micros();
  120.         last_interrupt_millis = millis();
  121.         //here we're attempting to detect the Maverick's preamble - 8x pulses of ~5ms each, spaced at ~250us
  122.         if (detection_state == STATE_START_PULSES)
  123.         {
  124. //      printf("millis: %d ms, micros: %d us, state %d\n",time_since_last, tsl_micros,digitalRead(PIN));
  125.                 //if last interrupt was seen between 3ms and 7ms ago
  126.                 if (((time_since_last > 3) && (time_since_last < 7)) && pin_state)
  127.                 {
  128.                         start_pulse_counter++;
  129.                         if (start_pulse_counter == 8)
  130.                         {
  131. //                                printf("Preamble detected @%d\n", millis());
  132.                                 start_pulse_counter = 0;
  133.                                 detection_state = STATE_FIRST_BIT;
  134.                         }
  135.  
  136.                         //printf("*TRIGGER* Since last pulse:%dms, Time from start:%dms, Pulse count:%d \n",time_since_last, millis(), start_pulse_counter);
  137.                 }
  138.                 else if (tsl_micros > 400)
  139.                 {
  140.                         start_pulse_counter = 0;
  141.                 }
  142.    
  143.     }
  144.     else if (detection_state == STATE_FIRST_BIT && pin_state)
  145.         {
  146.                 detection_state = STATE_DATA;
  147.                 current_bit=1;
  148.                 current_byte=0;
  149.                 shift_value=0;
  150.                 data_array_index=0;
  151.                 bit_ok=0;
  152.                 short_bit=0;
  153.                 add_1st_bit = 1;
  154.                 bit_count = 1;
  155.                 //printf("1");
  156.         }
  157.         if (detection_state == STATE_DATA)
  158.         {
  159. //this is a debugging parameter to determine the time between 2 interrupts
  160. //                printf("tsl_micros %d, state %d, shift_value %d\n", tsl_micros, pin_state, shift_value);
  161.  
  162.                 if ((tsl_micros > 90) && ((pin_state==0 && tsl_micros<TIME_THRES_HIGH) || (pin_state!=0 && tsl_micros<TIME_THRES_LOW )))
  163.                 {
  164.                         if (short_bit == 0)
  165.                         {
  166.                                 short_bit = 1;
  167.                         }
  168.                         else
  169.                         {
  170.                                 short_bit = 0;
  171.                                 bit_ok = 1;
  172.                                // printf("%d",digitalRead(PIN));
  173.                         }
  174.                 }
  175.  
  176.                 if (((pin_state ==0 && tsl_micros>= TIME_THRES_HIGH) || (pin_state!=0 && tsl_micros>= TIME_THRES_LOW)) && (tsl_micros < 650))
  177.                 {
  178.                         if (short_bit == 1)
  179.                         {
  180.                                 //expected a short bit and something went wrong
  181.                                 //start over at getting preamble
  182.                                 detection_state = STATE_START_PULSES;
  183.                                 printf("!!!PATTERN FAILURE!!! @%d\n",tsl_micros);
  184.                         }
  185.                         bit_count++;
  186.                         //printf("%d",digitalRead(PIN));
  187.                         current_bit=pin_state;
  188.                         bit_ok = 1;
  189.                 }
  190.  
  191.                 if (bit_ok)
  192.                 {
  193.  
  194.                         if (add_1st_bit)
  195.                         {
  196.                                 current_byte = 0x01;
  197.                                 shift_value = 1;
  198.                                 add_1st_bit = 0;
  199.                         }
  200.  
  201.                         current_byte = (current_byte << 1) + current_bit;
  202.                         shift_value++;
  203.             nibble = current_byte;
  204.  
  205.  
  206.                         if (shift_value == 8)
  207.                         {
  208.                                 data_array[data_array_index++] = current_byte;
  209. //                                printf("Byte %0d:0x%X @%d (%d)\n", data_array_index, current_byte, micros(), millis());
  210.                 bit_count=0;
  211.                                 shift_value = 0;
  212.                                 current_byte = 0;
  213.                         }
  214.  
  215.                         if (data_array_index == 9)
  216.                         {
  217.                                 start_pulse_counter = 0;
  218. //                              printf("Flushing @%d (%d)\n",micros(), millis());
  219.                                 detection_state = STATE_START_PULSES;
  220.  
  221.                                 for (i=0;i<=9;i++)
  222.                                 {
  223.                                         save_array[i] = data_array[i];
  224.                                 }
  225. //                              printf("Outputting @%d (%d)\n",micros(), millis());
  226.                                 outputData();
  227. //                              printf("Done @%d (%d)\n", micros(), millis());
  228.                         }
  229.                         bit_ok = 0;
  230.                 }
  231.  
  232.         }
  233. }
  234.  
  235.  
  236. int main(int argc, char **argv)
  237. {
  238.         wiringPiSetupSys();
  239.         pinMode (PIN, INPUT);
  240.  
  241.         t=time(NULL);
  242. //        ts=localtime(&t);
  243.     strftime(buff, 20, "%H:%M:%S %d.%m.%Y", localtime(&t));
  244.         printf("Starting on PIN %d at %s\n",PIN, buff);
  245.         printf("%s\t%s\t%s\t%s\n","probe1", "probe2", "age", "timestamp");
  246.    
  247.         wiringPiISR (PIN, INT_EDGE_BOTH, &myInterrupt);
  248. // Process priorising function <-- activating this may lead to break the functionality of the program
  249. //  piHiPri(50);
  250.  
  251.         for (;;)
  252.         {
  253. //      delay(0.01);
  254.         delay(1000);
  255. //                printf("Waiting ... \n"); fflush (stdout);
  256.         }
  257.         return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement