Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define STATE_START_PULSES 0
- #define STATE_FIRST_BIT 1
- #define STATE_DATA 2
- #define PIN 4
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <wiringSerial.h>
- #include <time.h>
- #include <sys/time.h>
- #include <string.h>
- #define TIME_THRES_HIGH 290
- #define TIME_THRES_LOW 430
- #define LOW_VALID -50
- #define HIGH_VALID 400
- unsigned int volatile start_pulse_counter = 0, detection_state = 0;
- unsigned int volatile last_interrupt_micros = 0, last_interrupt_millis;
- 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];
- signed int probe1=0, probe2=0;
- unsigned int probe1_array[6], probe2_array[6];
- unsigned long volatile last_valid_millis=0;
- time_t t;
- struct tm *ts;
- char buff[20];
- void updateThingspeak(int field1, int field2, int field3)
- {
- char str4[200];
- 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);
- system(str4);
- }
- // make the quarternary convertion
- unsigned int quart(unsigned int param)
- {
- param &= 0x0F;
- if (param==0x05)
- return(0);
- if (param==0x06)
- return(1);
- if (param==0x09)
- return(2);
- if (param==0x0A)
- return(3);
- }
- void outputData(void)
- {
- unsigned int i=0;
- unsigned long current_millis;
- // char str4[100];
- probe1 = probe2 = 0;
- if ( (save_array[0] == 0xAA) &&
- (save_array[1] == 0x99) &&
- (save_array[2] == 0x95) &&
- (save_array[3] == 0x59) )
- {
- probe2_array[0]= quart(save_array[8] & 0x0F);
- probe2_array[1]= quart(save_array[8] >> 4);
- probe2_array[2]= quart(save_array[7] & 0x0F);
- probe2_array[3]= quart(save_array[7] >> 4);
- probe2_array[4]= quart(save_array[6] & 0x0F);
- probe1_array[0]= quart(save_array[6] >> 4);
- probe1_array[1]= quart(save_array[5] & 0x0F);
- probe1_array[2]= quart(save_array[5] >> 4);
- probe1_array[3]= quart(save_array[4] & 0x0F);
- probe1_array[4]= quart(save_array[4] >> 4);
- for (i=0;i<=4;i++)
- {
- probe1 += probe1_array[i] * (1<<(2*i));
- probe2 += probe2_array[i] * (1<<(2*i));
- }
- probe1 -= 532;
- probe2 -= 532;
- if((probe1>LOW_VALID && probe1<HIGH_VALID || probe1==-532) && (probe2>LOW_VALID && probe2<HIGH_VALID || probe2==-532))
- {
- t=time(NULL);
- // ts=localtime(&t);
- current_millis=millis();
- strftime(buff, 20, "%H:%M:%S %d.%m.%Y", localtime(&t));
- printf("%d\t%d\t%d\t%s\n",probe1,probe2,(current_millis-last_valid_millis)/1000 ,buff);
- fflush(stdout);
- updateThingspeak(probe1, probe2, (current_millis-last_valid_millis)/1000);
- last_valid_millis=current_millis;
- }
- else
- {
- printf("!!!TEMPERATURE EXCEEDED VALID RANGE!!! (%d, %d)\n",probe1, probe2);
- }
- }
- }
- void myInterrupt (void)
- {
- unsigned int time_since_last = 0;
- unsigned int tsl_micros = 0;
- unsigned int bit_ok = 0, i;
- unsigned int pin_state=digitalRead(PIN);
- //get the time since last interrupt in milli and micro seconds
- time_since_last = (millis() - last_interrupt_millis);
- tsl_micros = (micros() - last_interrupt_micros);
- //store current interrupt time to calculate time since last (above)
- last_interrupt_micros = micros();
- last_interrupt_millis = millis();
- //here we're attempting to detect the Maverick's preamble - 8x pulses of ~5ms each, spaced at ~250us
- if (detection_state == STATE_START_PULSES)
- {
- // printf("millis: %d ms, micros: %d us, state %d\n",time_since_last, tsl_micros,digitalRead(PIN));
- //if last interrupt was seen between 3ms and 7ms ago
- if (((time_since_last > 3) && (time_since_last < 7)) && pin_state)
- {
- start_pulse_counter++;
- if (start_pulse_counter == 8)
- {
- // printf("Preamble detected @%d\n", millis());
- start_pulse_counter = 0;
- detection_state = STATE_FIRST_BIT;
- }
- //printf("*TRIGGER* Since last pulse:%dms, Time from start:%dms, Pulse count:%d \n",time_since_last, millis(), start_pulse_counter);
- }
- else if (tsl_micros > 400)
- {
- start_pulse_counter = 0;
- }
- }
- else if (detection_state == STATE_FIRST_BIT && pin_state)
- {
- detection_state = STATE_DATA;
- current_bit=1;
- current_byte=0;
- shift_value=0;
- data_array_index=0;
- bit_ok=0;
- short_bit=0;
- add_1st_bit = 1;
- bit_count = 1;
- //printf("1");
- }
- if (detection_state == STATE_DATA)
- {
- //this is a debugging parameter to determine the time between 2 interrupts
- // printf("tsl_micros %d, state %d, shift_value %d\n", tsl_micros, pin_state, shift_value);
- if ((tsl_micros > 90) && ((pin_state==0 && tsl_micros<TIME_THRES_HIGH) || (pin_state!=0 && tsl_micros<TIME_THRES_LOW )))
- {
- if (short_bit == 0)
- {
- short_bit = 1;
- }
- else
- {
- short_bit = 0;
- bit_ok = 1;
- // printf("%d",digitalRead(PIN));
- }
- }
- if (((pin_state ==0 && tsl_micros>= TIME_THRES_HIGH) || (pin_state!=0 && tsl_micros>= TIME_THRES_LOW)) && (tsl_micros < 650))
- {
- if (short_bit == 1)
- {
- //expected a short bit and something went wrong
- //start over at getting preamble
- detection_state = STATE_START_PULSES;
- printf("!!!PATTERN FAILURE!!! @%d\n",tsl_micros);
- }
- bit_count++;
- //printf("%d",digitalRead(PIN));
- current_bit=pin_state;
- bit_ok = 1;
- }
- if (bit_ok)
- {
- if (add_1st_bit)
- {
- current_byte = 0x01;
- shift_value = 1;
- add_1st_bit = 0;
- }
- current_byte = (current_byte << 1) + current_bit;
- shift_value++;
- nibble = current_byte;
- if (shift_value == 8)
- {
- data_array[data_array_index++] = current_byte;
- // printf("Byte %0d:0x%X @%d (%d)\n", data_array_index, current_byte, micros(), millis());
- bit_count=0;
- shift_value = 0;
- current_byte = 0;
- }
- if (data_array_index == 9)
- {
- start_pulse_counter = 0;
- // printf("Flushing @%d (%d)\n",micros(), millis());
- detection_state = STATE_START_PULSES;
- for (i=0;i<=9;i++)
- {
- save_array[i] = data_array[i];
- }
- // printf("Outputting @%d (%d)\n",micros(), millis());
- outputData();
- // printf("Done @%d (%d)\n", micros(), millis());
- }
- bit_ok = 0;
- }
- }
- }
- int main(int argc, char **argv)
- {
- wiringPiSetupSys();
- pinMode (PIN, INPUT);
- t=time(NULL);
- // ts=localtime(&t);
- strftime(buff, 20, "%H:%M:%S %d.%m.%Y", localtime(&t));
- printf("Starting on PIN %d at %s\n",PIN, buff);
- printf("%s\t%s\t%s\t%s\n","probe1", "probe2", "age", "timestamp");
- wiringPiISR (PIN, INT_EDGE_BOTH, &myInterrupt);
- // Process priorising function <-- activating this may lead to break the functionality of the program
- // piHiPri(50);
- for (;;)
- {
- // delay(0.01);
- delay(1000);
- // printf("Waiting ... \n"); fflush (stdout);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement