Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« on: January 15, 2013, 09:28:57 am » |
Hi! I'm glad I've successfully joined the Arduino community last week. Now it's time for first problem to get on, as my ambitious project to make something a'la home automation is getting on. At the moment I stopped on reading the measurements of weather sensor. It's a simple, no-name temperature and humidity sensor, which is used together with meteo-station inside home. (sensor: https://www.hurt.com.pl/prods/x/czujnik_sp26n.jpg, whole station: http://img01.allegroimg.pl/photos/oryginal/28/42/65/78/2842657864) It's popular in Poland, but I didn't find any working 'decoder' on the internet. As I was looking for working code to get the data from it, I've seen that it sends every 30-60 seconds the same binary code few times. As I googled I also assume that's because it doesn't use the check-sum (so it sends it repeatedly to make sure that station gets the measurements). It supports 3 channel and have a "force" button to send 'report' on demand (used while pairing with device). Could you please provide a working code to get the measurements from RF433MHz reciever properly? Or it's better to solder straight to sender before the RF module? The next problem probably would be to find a pattern in which the data is hidden. It's much simplier than other weather stations because it only have temperature & humid data, not rain, wind etc. If I manage, with your help hopefully, to get it working I'll surely like to share it for author of the library ( https://bitbucket.org/fuzzillogic/433mhzforarduino) and community. Just to clarify: I've spent whole night on googling and threads all around this forum. None of the sensors used here (even on Deutsch forum, even if it was hard to understand) worked. That's why I start this thread. It's not a complicated or secured transmission, the hardware inside looks simple. It just uses different pattern of data I guess. Regards, Andrew
|
|
|
|
« Last Edit: January 15, 2013, 09:35:41 am by andriej »
|
Logged
|
|
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #2 on: January 15, 2013, 11:44:21 am » |
I've found his thread with working code, but it has a bug. Here's the code: /* * Modified from "Thermor" DG950R Weather Station receiver v 0.3 * * Receives data from a Weather Station receiver, via * a 433Mhz RF receiver connected to pin 8 of the arduino, and outputs * to serial. * * Based on the Practical Arduino Weather Station Receiver project * (http://www.practicalarduino.com/projects/weather-station-receiver). * For more info: * http://kayno.net/2010/01/15/arduino-weather-station-receiver-shield/ * * */
#define INPUT_CAPTURE_IS_RISING_EDGE() ((TCCR1B & _BV(ICES1)) != 0) #define INPUT_CAPTURE_IS_FALLING_EDGE() ((TCCR1B & _BV(ICES1)) == 0) #define SET_INPUT_CAPTURE_RISING_EDGE() (TCCR1B |= _BV(ICES1)) #define SET_INPUT_CAPTURE_FALLING_EDGE() (TCCR1B &= ~_BV(ICES1))
#define WEATHER_RX_LED_ON() ((PORTD &= ~(1<<PORTD6))) #define WEATHER_RX_LED_OFF() ((PORTD |= (1<<PORTD6)))
#define WEATHER_RESET() { short_count = packet_bit_pointer = long_count = 0; weather_rx_state = RX_STATE_IDLE; current_bit = BIT_ZERO; WEATHER_RX_LED_OFF(); packet_start = false; }
#define TIMER_PERIOD_US 4 #define WEATHER_PACKET_BIT_LENGTH 19
// pulse widths. short pulses ~500us, long pulses ~1000us. 50us tolerance #define SHORT_PULSE_MIN_WIDTH 420/TIMER_PERIOD_US #define SHORT_PULSE_MAX_WIDTH 570/TIMER_PERIOD_US #define LONG_PULSE_MIN_WIDTH 1420/TIMER_PERIOD_US #define LONG_PULSE_MAX_WIDTH 1570/TIMER_PERIOD_US #define BIG_PULSE_MIN_WIDTH 2920/TIMER_PERIOD_US #define BIG_PULSE_MAX_WIDTH 3070/TIMER_PERIOD_US
// number of shorts in a row before the stream is treated as valid #define SHORT_COUNT_SYNC_MIN 3 #define LONG_COUNT_SYNC_MIN 4
// states the receiver can be #define RX_STATE_IDLE 0 // waiting for incoming stream #define RX_STATE_RECEIVING 1 // receiving valid stream #define RX_STATE_PACKET_RECEIVED 2 // valid stream received
#define BIT_ZERO 0 #define BIT_ONE 1
//byte locations of generic weather data in weather_packet[] array #define WEATHER_STATION_ID 0 #define WEATHER_PACKET_TYPE 1
//types of packets #define PACKET_TYPE_HUM 0 #define PACKET_TYPE_TEMP 1
#define DEBUG
// Type aliases for brevity in the actual code typedef unsigned int uint; //16bit typedef signed int sint; //16bit
uint captured_time; uint previous_captured_time; uint captured_period; uint current_bit; uint last_bit; uint packet_bit_pointer; uint short_count; uint long_count; uint weather_rx_state;
volatile bool packet_start = false; volatile bool packet_end = false; volatile bool ignore = false;
// byte arrays used to store incoming weather data byte weather_packet[(WEATHER_PACKET_BIT_LENGTH)]; byte last_weather_packet[(WEATHER_PACKET_BIT_LENGTH)];
// packet counter - 4 identical packets in a row means the packet is valid int packet_count = 0;
/* Overflow interrupt vector */ ISR(TIMER1_OVF_vect){ // here if no input pulse detected }
/* ICR interrupt vector */ ISR(TIMER1_CAPT_vect){
// Immediately grab the current capture time in case it triggers again and // overwrites ICR1 with an unexpected new value captured_time = ICR1;
//immediately grab the current capture polarity and reverse it to catch all the subsequent high and low periods coming in if(INPUT_CAPTURE_IS_RISING_EDGE()) { SET_INPUT_CAPTURE_FALLING_EDGE(); //previous period was low and just transitioned high } else { SET_INPUT_CAPTURE_RISING_EDGE(); //previous period was high and transitioned low }
// calculate the current period just measured, to accompany the polarity now stored captured_period = (captured_time - previous_captured_time);
// Analyse the incoming data stream. If idle, we need to detect the start of an incoming weather packet. // Incoming packet starts with a big pulse and the sequence 101010. if(weather_rx_state == RX_STATE_IDLE) { if( ((captured_period >= BIG_PULSE_MIN_WIDTH) && (captured_period <= BIG_PULSE_MAX_WIDTH))) { // received a big pulse - indicating the start of a packet packet_start = true;
} else {
if(((captured_period >= LONG_PULSE_MIN_WIDTH) && (captured_period <= LONG_PULSE_MAX_WIDTH)) && packet_start) { long_count++; } else if(((captured_period >= SHORT_PULSE_MIN_WIDTH) && (captured_period <= SHORT_PULSE_MAX_WIDTH)) && packet_start) { short_count++; } else { // not a long or short pulse, therefore not a valid bitstream WEATHER_RESET(); } } if((short_count >= SHORT_COUNT_SYNC_MIN) && (long_count >= LONG_COUNT_SYNC_MIN)) { weather_rx_state = RX_STATE_RECEIVING; } } else if(weather_rx_state == RX_STATE_RECEIVING) { if(((captured_period >= SHORT_PULSE_MIN_WIDTH) && (captured_period <= SHORT_PULSE_MAX_WIDTH))) { weather_packet[packet_bit_pointer] = 0; packet_bit_pointer++; } else if(((captured_period >= LONG_PULSE_MIN_WIDTH) && (captured_period <= LONG_PULSE_MAX_WIDTH))) { weather_packet[packet_bit_pointer] = 1; packet_bit_pointer++; }
}
if(packet_bit_pointer > WEATHER_PACKET_BIT_LENGTH) { // full packet received, switch state to RX_STATE_PACKET_RECEIVED weather_rx_state = RX_STATE_PACKET_RECEIVED; }
// save the current capture data as previous so it can be used for period calculation again next time around previous_captured_time = captured_time;
}
void setup() { Serial.begin(115200);
DDRB = 0x2F; // B00101111 DDRB &= ~(1<<DDB0); // PBO(ICP1) input PORTB &= ~(1<<PORTB0); // ensure pullup resistor is also disabled DDRD |= B11000000; // (1<<PORTD6); //DDRD |= (1<<PORTD7); (example of B prefix)
//--------------------------------------------------------------------------------------------- //ICNC1: Input Capture Noise Canceler On, 4 successive equal ICP1 samples required for trigger (4*4uS = 16uS delayed) //ICES1: Input Capture Edge Select 1 = rising edge to begin with, input capture will change as required //CS12,CS11,CS10 TCNT1 Prescaler set to 0,1,1 see table and notes above TCCR1A = B00000000; //Normal mode of operation, TOP = 0xFFFF, TOV1 Flag Set on MAX //This is supposed to come out of reset as 0x00, but something changed it, I had to zero it again here to make the TOP truly 0xFFFF TCCR1B = ( _BV(ICNC1) | _BV(CS11) | _BV(CS10) ); SET_INPUT_CAPTURE_RISING_EDGE(); //Timer1 Input Capture Interrupt Enable, Overflow Interrupt Enable TIMSK1 = ( _BV(ICIE1) | _BV(TOIE1) );
// attachInterrupt(0, rise, RISING); WEATHER_RESET();
Serial.println("\"joshhawk\" Weather Station receiver v0.01"); Serial.println("Ready to receive weather data"); }
/* * main loop */
void loop() {
// weather packet ready to decode if(weather_rx_state == RX_STATE_PACKET_RECEIVED) {
//#ifdef DEBUG Serial.println();
for(int i = 0; i < ((WEATHER_PACKET_BIT_LENGTH)); i++) { Serial.print(weather_packet[i]); Serial.print(" "); }
Serial.println(); //#endif packet_start = true; WEATHER_RESET();
} } Riva, you've worked out where was the bug in his code and he got it to work @ that thread. Can you point what's wrong with it (or even fix it)? I don't have a clue... You mentioned at that thread ( http://arduino.cc/forum/index.php?topic=110662.15) this thing: The code is a bit beyond me though I still think you only need to read rising or falling edge only and the timing between edges would determine start bit, 0 & 1 bits. . Ok, I did some more reading of the thread - he was getting packets recieved, I don't. Yet. Or they doesnt show up on Serial. Can anyone share working code to get more easy-readable readings from RF reciever?
|
|
|
|
« Last Edit: January 15, 2013, 12:28:03 pm by andriej »
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #3 on: January 15, 2013, 12:42:28 pm » |
I helped/hindered in working out the decoding of the signal but the code that the person managed to find was (and still is) beyond me. I have done PIC16 software to decode Sony longitudinal time-code that is similar in format  but am still relatively new to ATmega architecture & programming and have not attempted anything like that (yet) using arduino. The program that the original OP found in that thread seems to monitor both rising and then falling edge interrupts but I think it could have been done with just falling edge. I could not help any further as I don't have a RBxx receiver or a weather station to test any code with. I could maybe write some code if I someone supplied me with data from the RBxx receiver.
|
|
|
|
|
Logged
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #4 on: January 15, 2013, 12:56:38 pm » |
I've just soldered into "Data" and "Gnd" cables of RF transmitter. Using code like: int rfdataPin = 2;
void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps pinMode(rfdataPin, INPUT); // sets the digital pin as input to read Serial.println("RF433 Carrin arduino receiver startup");
} void loop() // run over and over again { int i;
for (i = 0; i < 100; i++) { // Used to create a CR point after 100 counts. Serial.print(digitalRead(rfdataPin)); } Serial.println(" "); // Read the pin and display the value //delay(100); } i get too much 0's on output when nothing is sent by device so I can't really measure proper bits. Is there a code for a nicer output? So I could collect binary and readings from LCD about temp and hum.?
|
|
|
|
|
Logged
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #5 on: January 16, 2013, 07:53:01 am » |
I've found a China producer of the sensor, maybe they will send me some info. Anyway, at the moment - please, someone help me with code that detects "high" pulse starting with "1010", so I dont recieve all the zeroe's on the input. I can't manage to do that... I'd really like to share some more info about this device for everyone, as the producer is in China so there will be probably more people interested in getting it working.
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #6 on: January 16, 2013, 08:11:07 am » |
Is there a code for a nicer output? So I could collect binary and readings from LCD about temp and hum.? I think in the original post the OP was capturing the signal using PC audio software like Audacity and hand decoding the results.
|
|
|
|
|
Logged
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #7 on: January 16, 2013, 08:16:27 am » |
Yeah, the problem is I'm afraid of connecting 'data' pin to my soundcard without circuit for it. And I couldn't fine any circuit except oscilloscope one (which I don't have all parts for).
Do you have any of these available? I have some old laptop with linux that I could play with.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 45
|
 |
« Reply #8 on: January 22, 2013, 01:23:10 pm » |
Hi, I've done this job a year ago and I used arduino for decoding First you have to prepare the hardware: - open your sensor - find the modulator input - feed it into arduino with proper level change (I assume 0-3v => 0-5v) The code I used was something like: #define samples 700 //max 700 byte ms[samples]; boolean values[samples]; void setup() { pinMode(13, OUTPUT); pinMode(2, INPUT); Serial.begin(2400); } long lastChange, maxTime = 0; bool previousValue; void loop() { for (int i = 0; i < samples; i++) { values[i] = false; ms[i] = 0; } long start = micros(); for (int i = 0; i <= samples; i++) { if (i == samples) i = 0; digitalWrite(13, (values[i] = digitalRead(2))); ms[i] = (byte)((micros() - start)/10); start = micros(); while (values[i] == digitalRead(2)) {} if((micros()-start) > 50000) { for (int k = i + 1; k < samples; k++) { Serial.print(k); Serial.print(":"); Serial.print((int)ms[k]); Serial.print(":"); Serial.println((values[k]?"true":"false")); } for (int k = 0; k <= i; k++) { Serial.print(k); Serial.print(":"); Serial.print((int)ms[k]); Serial.print(":"); Serial.println((values[k]?"true":"false")); } break; } } digitalWrite(13, LOW); delay(1000); digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(10000); } After you manage to isolate one packet, take 2 consecutive readings with just one unit (in the least significant digit) difference in temperature or humidity. If your streams have one difference you don't have CRC, if you have 2 or mode differences you might have something like 011+1=100 or CRC or both. Here you need some luck. After some hours of reverse engineering I come with the following code: #define nobits 36 #define initialstring "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #define syncmin 6400 #define syncmax 9600 #define separatormin 256 #define separatormax 384 #define lowmin 768 #define lowmax 1152 #define highmin 1536 #define highmax 2304 #define baudspeed 115200 #define receiverpin 2 #define ledpin 13 #define sensorid "10001000" #define rflisteninterval 50 float temperature = 0; int humidity = 0; unsigned long lastReadMills = 0; boolean lowBatt = false; boolean requestedTransmission = false; boolean invalidTransmission = false; String lastReceivedCode = "";
void setup() { pinMode(ledpin, OUTPUT); pinMode(receiverpin, INPUT); digitalWrite(receiverpin, HIGH); Serial.begin(baudspeed); }
void loop() { if(RFListened()) { //other processing } } boolean RFListened() { if(((millis()-lastReadMills)/1000>rflisteninterval)||(millis()<lastReadMills)) { GetWeatherData(); if((millis()==lastReadMills)&&(lastReadMills!=0)) { if (lowBatt) Serial.print("Battery low; "); if (requestedTransmission) Serial.print("Requested transmission; "); if (invalidTransmission) { Serial.println("Non temperature/humidity data; "); return false; } else { Serial.print(lastReadMills/1000); Serial.print("s; "); Serial.print(temperature); Serial.print("C; "); Serial.print(humidity); Serial.println("%"); return true; } } return false; } else { return true; } }
void GetWeatherData() { String receivedCode = ReceiveCode(); if (receivedCode!="") { if (receivedCode==lastReceivedCode) { lowBatt = (receivedCode.charAt(8)=='1'); requestedTransmission = (receivedCode.charAt(11)=='1'); invalidTransmission = !((receivedCode.charAt(9)=='0')||(receivedCode.charAt(10)=='0')); if (!invalidTransmission) { temperature = (receivedCode.charAt(24)=='1'?1:0)*2048 + (receivedCode.charAt(25)=='1'?1:0)*1024 + (receivedCode.charAt(26)=='1'?1:0)*512 + (receivedCode.charAt(27)=='1'?1:0)*256 + (receivedCode.charAt(28)=='1'?1:0)*128 + (receivedCode.charAt(29)=='1'?1:0)*64 + (receivedCode.charAt(30)=='1'?1:0)*32 + (receivedCode.charAt(31)=='1'?1:0)*16 + (receivedCode.charAt(32)=='1'?1:0)*8 + (receivedCode.charAt(33)=='1'?1:0)*4 + (receivedCode.charAt(34)=='1'?1:0)*2 + (receivedCode.charAt(35)=='1'?1:0); temperature = temperature /10; humidity = (receivedCode.charAt(16)=='1'?1:0)*128 + (receivedCode.charAt(17)=='1'?1:0)*64 + (receivedCode.charAt(18)=='1'?1:0)*32 + (receivedCode.charAt(19)=='1'?1:0)*16 + (receivedCode.charAt(20)=='1'?1:0)*8 + (receivedCode.charAt(21)=='1'?1:0)*4 + (receivedCode.charAt(22)=='1'?1:0)*2 + (receivedCode.charAt(23)=='1'?1:0); lastReadMills = millis(); } } else lastReceivedCode = receivedCode; } }
String ReceiveCode() { String receivedCode = initialstring; long startMicros = micros(), endMicros; if (digitalRead(receiverpin)) return ""; while(!digitalRead(receiverpin)) { if ((micros()-startMicros)>syncmax) return ""; } if ((micros()-startMicros)<syncmin) return ""; startMicros = micros(); while(digitalRead(receiverpin)) { if ((micros()-startMicros)>separatormax) return ""; } if ((micros()-startMicros)<separatormin) return ""; for(int i = 0; i < nobits; i++) { startMicros = micros(); while(!digitalRead(receiverpin)) { if ((micros()-startMicros)>highmax) return receivedCode; } endMicros = micros(); if(((endMicros-startMicros)>lowmin)&&((endMicros-startMicros)<lowmax)) receivedCode.setCharAt(i,'0'); else if(((endMicros-startMicros)>highmin)&&((endMicros-startMicros)<highmax)) receivedCode.setCharAt(i,'1'); else return ""; startMicros = micros(); while(digitalRead(receiverpin)) { if ((micros()-startMicros)>separatormax) return ""; } if ((micros()-startMicros)<separatormin) return ""; } //Serial.println(receivedCode); if (receivedCode.substring(0,8)==sensorid) return receivedCode; else return ""; }
This will not fit your sensor since, as I see in the image it has a configurable channel. Mine has a random signature generated at power on.
|
|
|
|
|
Logged
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #9 on: January 22, 2013, 09:29:28 pm » |
Thank you very much for your post! Just tell me please - where is the 'modulator input' on this picture? Do you mean output for the 433 transciever?  
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #10 on: January 27, 2013, 12:56:13 pm » |
Did you manage to get this working? I did a quick hunt online for a similar looking wireless sender so I could hack it but found nothing suitable & cheap. I'm in the process of converting the audio captures joshhawk did for me in his thread into logic level samples so I can attempt to write an interrupt based receiver routine. I'm getting bit-streams from the one sample I have converted but time will tell if it's correct.
|
|
|
|
|
Logged
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #11 on: January 28, 2013, 09:29:19 am » |
I haven't managed to get it working yet, I was a little short in time last week. I'll might try soon, of course If I don't burn my soundcard. :-)
I hope you'll get your alogyrthm to work. The hardest part is to harvest data to decode as I see...
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #12 on: January 31, 2013, 07:27:40 am » |
To have any real chance of helping you need to capture a transmission from your sensor and post it up here. It seems every make of sensor has a different way of transmitting the data. I have knocked together a bit of test code that I just posted here http://arduino.cc/forum/index.php/topic,110662.msg1094167.html#msg1094167 As I don't have such a device to test it with it's really a shot in the dark. I have also contacted the original thread poster joshhawk and he said the attached code below worked for his sensor. Hopefully yours is the same.
|
|
|
|
|
Logged
|
|
|
|
|
Poland
Offline
Newbie
Karma: 0
Posts: 38
|
 |
« Reply #13 on: January 31, 2013, 07:35:23 am » |
I might try to build voltage divider tonight. I'm not sure how to do it by soldering into the sensor board, so I don't get signals from other sensors. And I don't want to break the mic-in port in laptop (I don't have line-in in there). Any suggestions? I'll get knowledge from here: http://davehouston.net/learn.htm which shows the 39K and 10K resistors.
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #14 on: January 31, 2013, 08:02:29 am » |
Ideally you would tap into the data feeding the transmitter but I have no idea what point that would be on the pictures you posted without testing with a scope. There is a data line (yellow wire) that goes to another PCB. What does this PCB look like?
If you don't have a line level input on your laptop then the voltage divider you linked to is not much use.
|
|
|
|
|
Logged
|
|
|
|
|
|