Arduino Forum

Topics => Device Hacking => Topic started by: EmilyJane on Sep 04, 2011, 10:14 pm

Title: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 04, 2011, 10:14 pm
I am resuming the discussion started here:
http://arduino.cc/forum/index.php/topic,71206.msg530634.html#msg530634 (http://arduino.cc/forum/index.php/topic,71206.msg530634.html#msg530634)

since this is probably a more appropriate place to continue it.

Basically, I'd like to replace the code in the AAG RS485 Weather Station because the suppliers haven't done a very good job exploiting its potential and they aren't very responsive to communication so they're not likely to improve it on their own. In their defense, they buy it preassembled from a Chinese supplier and they may not have a lot of control over it. Regardless, it's a nice piece of hardware and by definition "I own it" because I can take it apart and reprogram it. :)

I've spent a little while figuring out the communication protocol and having done so, I have decided it can be improved. As it comes from the supplier, it has a half duplex RS485 transceiver chip for communication but uses a proprietary protocol that doesn't use the standard RS485 slave addressing method or as it is referred to in the Atmega literature, Multi-processor Communication mode (MPCM). I intend to change that as well as some other things that I consider are shortcomings.

It is a descendent of the 1-wire Dallas weather station, which AAG still make. One of the advantages of 1-wire is the ease by which networks of sensors can be wired, typically with cat5 cable. Sometimes when these networks get lengthy or "branchy" they aren't as reliable to communicate with. RS485 solves some of those problems but adds some of its own. For example, it doesn't easily provide a way to utilize existing 1-wire sensors.

It would be nice to be able to extend the 1-wire bus that currently exists inside the weather station (WS). There is a DS18S20 temperature chip inside that is virtually useless for outside temperature because the sealed enclosure heats up to over 140 degrees F on a sunny day. There is a feed-thru right by the chip that I will use to extend the 1-wire buss outside the enclosure. That way it can be used for ambient temperature and what ever other 1-wire sensors one might already have an want to use close to the WS.

I disassembled my WS this morning to try to get an idea just how hard it's going to be to "fix". The PCB is easy to remove with two connectors for communication and power. It's a 4 wire system, all though you wouldn't guess from the RJ45 that AAG connects with. They have +5VDC, +12VDC, and RS485 A and B wired to the connector but only +12VDC and RS485 are used. The logic part of the circuit draws very little current and regulates the +12 to +5 on board with a TO-92 style three terminal regulator. The +12 is used to power the 35 LEDS that one may or may not want to keep. I'm going to forget about the LEDS and I initially intend to reduce the Vcc to 3.3VDC.

The RS485 transceiver is accessible enough to put in a Hi-Z state so that I can initially program the Atmega8L with the Arduino boot loader. Currently, I think that will be the easiest way to start.

I broke the needle tip on my soldering iron so it will be a couple of days before I can start soldering on leads for the USB-serial interface. It has a 5-pin programming header already on the PCB.

As mentioned earlier, the processor is an Atmega8L and the clock is crystal controlled at 3.686MHz. I'll need some guidance about how to handle the initial reprogramming based on this clock speed. I'd like to end up with an 8MHz clock. The crystal is a through-hole.

Coding Badly and Graynomad have given me some good advice about how to proceed and I hope they and others keep doing that!  :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 05, 2011, 01:58 am
Quote
crystal controlled at 3.686MHz. I'll need some guidance about how to handle the initial reprogramming based on this clock speed.

I'd replace the Xtal with a 16Mhz version as I doubt there's a bootloader set for 3.686. Maybe you can recompile one of the existing loaders though.

Quote
5-pin programming header already on the PCB

Curious, the standard ISP header is 6 pins.

______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 05, 2011, 03:21 am

Quote
crystal controlled at 3.686MHz. I'll need some guidance about how to handle the initial reprogramming based on this clock speed.

I'd replace the Xtal with a 16Mhz version as I doubt there's a bootloader set for 3.686. Maybe you can recompile one of the existing loaders though.


Curious, the standard ISP header is 6 pins.

______
Rob


I had first thought I might want to leave the original crystal in and run the thing off 3V3 but after thinking it through, I won't be adding any 3V3 peripherals inside the case after all so I might as well make it 16MHz as you suggest. Since it's a through-hole crystal, it won't be a problem to replace.

The header is a single row with the normal pins minus +5V. Nothing these people do is standard. :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 05, 2011, 09:39 pm
Looking at the data sheet for the Atmega8 under serial programming, it states that "If the device is clocked by the Internal Oscillator, it is no need to connect a clock source to the XTAL1 pin". When using an Arduino to program a target processor, is the target somehow placed in internal oscillator mode for programming?
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 06, 2011, 08:12 pm
Here is a schematic of the weather station.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 07, 2011, 02:36 am
I can't believe they have a patent on that simple circuit, that has to be a joke, doesn't it? Or am I missing something?

No wait, I see the clever bit, they have the current limiting resisters in the middle of the LEDs :)

Anyway it looks easy enough to hack, for that matter you could use an Arduino (mini or some such) or make your own board.

______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: buzzdavidson on Sep 07, 2011, 03:49 am
Apparently it's the "atmage8" that provides the patent-able magic.  (heh.  see what I did there?)

Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 07, 2011, 03:52 am
Oh, I wasn't aware that you could patent a typo, in that case I'd better get busy with my applications because I can hardly type more than three words without erras :)

And just to prove it, here's an unedited version of the above

Quote
Oh, I wasn;t aware that you could patent a typo, in that case I'd better bet busy with my applicaions because I can hadrly type more than three words without weerors.


______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 07, 2011, 04:30 am
Yeah, the whole patent thing is pretty funny. It's a Chinese patent so it may in fact be for the typo.

Anyway, I've got it open and I've soldered some leads to the TX/RX and RS485 enable pins so if I'm successful getting an Arduino bootloader into it, I can talk to it.

I've also hooked it up to an Arduino like it describes in the Arduinoisp sketch with the three LEDs and I've pulled the reset line up with a 150 Ohm resistor.

This is what I get when I run AVRDUDE.

Code: [Select]
pro:~ ejp$ avrdude -P /dev/cu.usbmodem621  -c arduino -p m8 -v -v

avrdude: Version 5.8cvs, compiled on Jan 15 2010 at 17:27:01
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/usr/local/CrossPack-AVR-20100115/etc/avrdude.conf"
         User configuration file is "/Users/ejp/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/cu.usbmodem621
         Using Programmer              : arduino
         AVR Part                      : ATMEGA8
         Chip Erase delay              : 10000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom         4    20   128    0 no        512    4      0  9000  9000 0xff 0xff
           flash         33    10    64    0 yes      8192   64    128  4500  4500 0xff 0x00
           lfuse          0     0     0    0 no          1    0      0  2000  2000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  2000  2000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  2000  2000 0x00 0x00
           calibration    0     0     0    0 no          4    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 3.3
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: Expected signature for ATMEGA8 is 1E 93 07
         Double check chip, or use -F to override this check.

avrdude done.  Thank you.

pro:~ ejp$
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: robtillaart on Sep 07, 2011, 10:05 pm
Quote
I can't believe they have a patent on that simple circuit, that has to be a joke, doesn't it? Or am I missing something?

In fact you can patent very much, the real question is if it will hold in court (that differs per country). One of the fuzziest criteria is (in my words) "that there must be something innovative not obvious for the skilled person". So a resistor in the middle may be innovative, but any electrical engineer can think of this as a solution, so it will probably won't hold in court. And of course if you find prior art, a (public) schematic that does the same with a verifyable older date, the patent is gone ...
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 02:50 am
As you can see from the schematic, there are 8 Hall effect devices going to inputs to the Atmega. The data sheet identifies them as open drain switches so even though they are attached to what we might normally assume are analog inputs they must be being used as digital inputs with internal pull-ups enabled.

On the circuit board the Hall effects are arranged on about a 25mm circle with 45 degrees between them. Above them rotates the little circuit board, whose picture I've attached, that is mounted to the end of the shaft that the vane rotates. Those little gray things will just fit between two of the Hall devices or will line up over two of the devices as the shaft rotates. I presume that when a magnet is between two of the Hall devices, both are switched on.

Now, the question is, how does one compute the direction of the vane pointer? The weather station reports 1 of 16 unique positions.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 08, 2011, 03:01 am
First question, why have 8 if they are spaced at 45 degrees, surely that means 4 of them are redundant?

Apart from that just read the value as a byte and have a switch statement to get the direction.

switch (reading)
   0x12:
  // north
   0x23:
  // north west
   
A bit ugly but will work.

Or have a loop that shifts and tests the reading's LSB.
_____
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: buzzdavidson on Sep 08, 2011, 03:02 am

Now, the question is, how does one compute the direction of the vane pointer? The weather station reports 1 of 16 unique positions.


You'll need to define one of the 'positions' as North - obviously this is installation dependent.  The rest is basic trigonometry.  Determining current position and direction of travel is most likely similar to handling rotary encoders.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 03:20 am

First question, why have 8 if they are spaced at 45 degrees, surely that means 4 of them are redundant?
_____
Rob


I don't understand why you say 4 are redundant. How would you get 16 unique directions with only four switches? I see that 24 = 16 but I don't see that that helps.




switch (reading)
   0x12:
  // north
   0x23:
  // north west



Where does "reading" come from?
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 08, 2011, 03:42 am
I was thinking that with the HE sensors at 45 degrees they are effectively operating in pairs as you would always have both opposites on at the same time.

Would that not be the case?

I can see that if the magnet actuates adjacent pairs when it's in the centre that would double the resolution, but I still can't see the point in having sensors directly opposite each other.

"reading" is the value read from the IO port that these sensors are on. In this case PORTC, so

byte reading;

reading = PINC;
switch (reading)
   0x12:
  // north
   0x23:
  // north west

maybe "direction" is a better name.

_______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 04:07 am
So, if I number the HEs 0-7 like in the attached drawing and they are wired to PC0-PC7 for purposes of this discussion and the top of the drawing is North. Let's also assume that an activated HE has a value of "1", regardless of how we would accomplish that.

Then if the rotor is pointing North, what would be the value of "reading"? If the rotor was pointing South, what would it be?
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: billcramer07 on Sep 08, 2011, 04:09 am
The original Dallas 1-wire instrument used a DS2450 4 input A/D converter with 8 reed switches and rotating magnets to determine direction. Someone wrote a sketch to read the speed and direction with an Arduino. I am using it as part of my weather station. Find it here...

http://www.audon.co.uk/1-wire_weather.html

Maybe the new AAG instrument is using a similar method.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 04:21 am

The original Dallas 1-wire instrument used a DS2450 4 input A/D converter with 8 reed switches and rotating magnets to determine direction. Someone wrote a sketch to read the speed and direction with an Arduino. I am using it as part of my weather station. Find it here...

http://www.audon.co.uk/1-wire_weather.html

Maybe the new AAG instrument is using a similar method.



Your link didn't work for me. I believe the original 1-wire instrument switched resistors with its reed switches so as to get unique A/D readings for each direction. This one doesn't appear to be switching any resistors.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: billcramer07 on Sep 08, 2011, 04:23 am
Yea the link is dead. I have the sketch and a schematic of the old wind instrument. I will post here.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 08, 2011, 04:24 am
Quote
Then if the rotor is pointing North, what would be the value of "reading"? If the rotor was pointing South, what would it be?


North = 0 and 4 on = 00010001
South = 4 and 0 on = 00010001

That's my point.

East of north = 0,1 and 4,5 = 00110011
West of south = 4,5 and 0,1 = 00110011

So you have better resolution but again, I don't see how you can tell the difference.

Unless I'm missing something here (it wouldn't be the first time :)) you have to remove one of the magnets.

_____
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 08, 2011, 04:25 am
Quote
I believe the original 1-wire instrument switched resistors with its reed switches so as to get unique A/D readings for each direction.

That would make a difference. Still I think removing a magnet would be easier. Maybe add a similar weight to keep the balance.

______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 04:34 am

Quote
I believe the original 1-wire instrument switched resistors with its reed switches so as to get unique A/D readings for each direction.

That would make a difference. Still I think removing a magnet would be easier. Maybe add a similar weight to keep the balance.

______
Rob


Very good. That's exactly what those inscrutable Chinese did. That gray thing that looks exactly like the other one is a dummy. Only one is a magnet.

I'm proud to say I figured it out before I actually verified it. Like Sherlock Holmes said, whenever you've exhausted all the possibilities, whatever is left, no matter how improbable, is the answer.

So maybe that's what the patent is for.  :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 08, 2011, 04:39 am
He he, do I get a prize :)

_____
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 04:58 am

He he, do I get a prize :)

_____
Rob


Sorry, no. :) Good job, though!

The remaining mystery to me is why they went to such trouble to hide what they are doing. The dummy looks EXACTLY like the real magnet. It's like they had a big batch of defective ceramic magnets to use up. The rotor that operates the HE for wind speed does have two functional magnets and looks identical to the Wind direction rotor. Curiouser and curiouser.

I'm still left with the problem of reprograming the stock chip. Anyone with any suggestions about that please step in.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 08, 2011, 05:22 am
Quote
reprograming the stock chip

I'd buy a $20 programmer like this

http://www.pololu.com/catalog/product/1300 (http://www.pololu.com/catalog/product/1300)

from Pololu or someone (there are a few around) and make up a 6-way to 5-way cable.

______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 05:31 am

Quote
reprograming the stock chip

I'd buy a $20 programmer like this

http://www.pololu.com/catalog/product/1300 (http://www.pololu.com/catalog/product/1300)

from Pololu or someone (there are a few around) and make up a 6-way to 5-way cable.

______
Rob


I might end up doing that but there's no reason why I can't use an Arduino for this project unless the manufacturer has burned the fuses to disable reprogramming.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: buzzdavidson on Sep 08, 2011, 05:36 am

The remaining mystery to me is why they went to such trouble to hide what they are doing. The dummy looks EXACTLY like the real magnet. It's like they had a big batch of defective ceramic magnets to use up. The rotor that operates the HE for wind speed does have two functional magnets and looks identical to the Wind direction rotor. Curiouser and curiouser.


Unlikely that they're hiding anything.  Perhaps a counterweight? Provided for balance?


I'm still left with the problem of reprograming the stock chip. Anyone with any suggestions about that please step in.


Adafruit sells a little ISP programmer (usbtiny) that should do the job nicely.  It's supported directly by avrdude.

BTW, the Bus Pirate that I mentioned yesterday also fulfills this purpose.  Also supported by avrdude.  Just sayin'...

Oh, and my earlier suggestion regarding position is based upon experience with my own AAG weather station.  You really don't get a useful position until it moves.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 06:14 am


The remaining mystery to me is why they went to such trouble to hide what they are doing. The dummy looks EXACTLY like the real magnet. It's like they had a big batch of defective ceramic magnets to use up. The rotor that operates the HE for wind speed does have two functional magnets and looks identical to the Wind direction rotor. Curiouser and curiouser.


Unlikely that they're hiding anything.  Perhaps a counterweight? Provided for balance?


I'm still left with the problem of reprograming the stock chip. Anyone with any suggestions about that please step in.


Adafruit sells a little ISP programmer (usbtiny) that should do the job nicely.  It's supported directly by avrdude.

BTW, the Bus Pirate that I mentioned yesterday also fulfills this purpose.  Also supported by avrdude.  Just sayin'...

Oh, and my earlier suggestion regarding position is based upon experience with my own AAG weather station.  You really don't get a useful position until it moves.


Those are all good suggestions. Thank you!

Well, I'm going to call it a night.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 08, 2011, 11:55 pm


The remaining mystery to me is why they went to such trouble to hide what they are doing. The dummy looks EXACTLY like the real magnet. It's like they had a big batch of defective ceramic magnets to use up. The rotor that operates the HE for wind speed does have two functional magnets and looks identical to the Wind direction rotor. Curiouser and curiouser.


Unlikely that they're hiding anything.  Perhaps a counterweight? Provided for balance?


I'm still left with the problem of reprograming the stock chip. Anyone with any suggestions about that please step in.


Adafruit sells a little ISP programmer (usbtiny) that should do the job nicely.  It's supported directly by avrdude.

BTW, the Bus Pirate that I mentioned yesterday also fulfills this purpose.  Also supported by avrdude.  Just sayin'...

Oh, and my earlier suggestion regarding position is based upon experience with my own AAG weather station.  You really don't get a useful position until it moves.


I'm sure it's a counterweight as you suggest but it is made out of the same material that the magnet is made from. Maybe they are readily available from another process/product.

I finally got avrdude/ArduinoISP to talk to the WS but I'll probably order one of the Adafruit programmers to use in the future. The problem was I needed to over-ride the baud rate in avrdude. Coding Badly figured that one out.

The new AAG will be even easier to determine wind direction now that I realize that there is only one magnet. It will work when the vane is stopped as well.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 14, 2011, 04:03 pm
I decided to go ahead and bump the clock frequency up to 16 MHz so as soon as a crystal arrives, I'll get back on this project. I'll be using a USBtinyisp and a USB/TTL serial interface instead of an Arduino for programming and communication also, so everything should go smoothly.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: graynomad on Sep 14, 2011, 04:20 pm
Quote
so everything should go smoothly

He he.

______
Rob
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 14, 2011, 04:28 pm

Quote
so everything should go smoothly

He he.

______
Rob


Yeah, that's the thought that went through my head, too. :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: Coding Badly on Sep 15, 2011, 08:02 am

Bah.  You two are pessimists.  What could possibly go wrong?

An old boss helped design and build the control system for a very large tank farm.  While driving to the plant for the startup, he stopped at 7-Eleven and picked up a stack of job applications ... just in case things went poorly.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 15, 2011, 03:40 pm
:) Funny story.

If I didn't break anything and the schematic diagram is correct it really should go smoothly. With the FTDI bridge and a 16 MHz clock, it should look exactly like an Arduino NG with LEDs on every pin. :D
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 21, 2011, 01:19 am
Well, I've got an Arduino bootloader programmed into the AAG Atmega8 now and am just playing around with the on-board hardware. I'm programming it from the IDE through an FTDI USB/serial bridge with a 100nF capacitor from DTR to reset. The first thing I have noticed is that the schematic is wrong for P1, the photocell. On the schematic it should show R20 (3K0) and P1 reversed. The 3K0 resistor goes to Gnd and the photocell goes to +5 volts. If you're is playing along at home, you'll want to change your schematic. :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 04:44 pm
I had a chance to play with the weather station and wrote this code to set up the ports and get wind direction. I created an array of bytes to hold the 16 possible states of the Hall effects. Either one Hall effect is triggered or two are when the magnet is between two of them. The 16 states map into a string array that corresponds to the 16 directions.

Next I will get the 1-wire temperature working and wind speed and then it will be at the same point where I took it apart.

AAG wired the DS1850 directly into PD5 with no pull-up resistor so I'm going to re-wire it with one so I can have a proper 1-wire bus to extend outside the enclosure. I don't have too much flash so I'm going to have to work on the 1-wire code to make it as slim as possible.

Code: [Select]
/*
PC0-PC5 PD6-PD7 = Q1-Q8  Wind Direction
PD5 = 1-Wire DS18B20
PD4 = Q9 Wind Speed
PD2 = DD RS485 Data Direction
PD1 = TXD
PD0 - RXD
ADC7 = P1 Photocell Schematic wrong actually pulls up to 5V
ADC6 = Voltmeter
*/

#define ADC6  6
#define ADC7  7

const char* windDirection[16] = { // strings for the 16 wind directions
  "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW",\
  "SW", "WSW", "W", "WNW", "NW", "NNW"};

uint8_t WD[16] =  { // Hall effect outputs for 16 wind vane positions
  B01111111, B00111111, B10111111, B10011111, B11011111, B11001111,\
  B11101111, B11100111, B11110111, B11110011, B11111011, B11111001,\
  B11111101, B11111100, B11111110, B01111110};


uint8_t match_wind(uint8_t* mask_array, uint8_t mask){ // match wind direction mask with array of possibles
  for (uint8_t i=0; i<16; i++)
    if (*mask_array++ == mask)
      return i; // return match which corresponds to string array index too
  return 16; // signal no match.
}

uint8_t make_wind(void){ // build mask for wind direction
  uint8_t temp = PINC; // read Q1-Q6(PC0-PC5)
  temp &= B00111111; // clear bits 6 and 7
  temp |= (PIND & B11000000); // OR on Q7-Q8(PD6-PD7) bits
  return temp;
}

void setup() {
  Serial.begin(9600);
  DDRC &= B11000000; // PC0-PC5 = inputs (Wind Direction)
  PORTC |= B00111111; // switch on pull-up resistors
  DDRD &= B00001011; // PD4, PD5, PD6, PD7 = inputs (Wind Direction and Speed + 1-Wire) PD2 temporarily for RS485 disable
  PORTD |= B11110000; // switch on pull-up resistors
}

void loop(){
  int photocell = analogRead(ADC7);
  Serial.print(photocell, DEC);
  delay(1000);
  int voltmeter = analogRead(ADC6);
  Serial.print('\t');
  Serial.println(voltmeter, DEC);
  delay(1000);

  uint8_t d = make_wind(); // ;-)
  d = match_wind(WD, d);
  if (d < 16)
    Serial.println(windDirection[d]);
  else Serial.println("Invalid Direction");
  delay(1000);
}
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: robtillaart on Sep 22, 2011, 07:19 pm
Is it possible to read the strength of the hall sensors? (now I see only 0/1)

If so these could be used to interpolate between the values. The 16 wind directions are steps of 360/16 = 22.5, if interpolation is possible you may could get to 5 degrees accuracy

Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: retrolefty on Sep 22, 2011, 07:26 pm

Is it possible to read the strength of the hall sensors? (now I see only 0/1)

If so these could be used to interpolate between the values. The 16 wind directions are steps of 360/16 = 22.5, if interpolation is possible you may could get to 5 degrees accuracy




Not likely. Hall effect sensors come in two varieties, those with a digital output and those with a analog output. Sounds like this weather station is using simple digital output hall sensors.

Lefty
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 07:51 pm
Quote
Is it possible to read the strength of the hall sensors? (now I see only 0/1)


They are Allegro 3144 which is now obsolete and replaced by the A1104. Unfortunately, like Lefty points out, they are digital devices, so no interpolation will be possible. Just from watching the wind vane while I had it up on a pole, the direction oscillated quite a bit. I don't know that wind direction is stable enough to warrant 5 degree resolution. Not Texas wind anyway. :) We have a saying here, if you don't like the weather just wait 5 minutes.

That's an interesting idea though. If I ever decide to replace the circuit board, I could try linear Hall sensors.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: Coding Badly on Sep 22, 2011, 07:55 pm
I had a chance to play with the weather station and wrote this code to set up the ports and get wind direction.


Nice!


A suggestion and an idea.  Suggestion...

Quote
const char* windDirection[17] = { // strings for the 16 wind directions
 "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW",\
 "SW", "WSW", "W", "WNW", "NW", "NNW", "---"};


...very slightly simplifies displaying the wind...

Quote
uint8_t d = make_wind(); // Sophomore humor.  Got to love it.  Hmm, I think I'll watch Shaun the Sheep tonight...
 d = match_wind(WD, d);
 Serial.println(windDirection[d]);



Idea... interpolating (guessing) the wind direction... By tracking the wind direction versus time, it is possible for the application to provide a guess at the exact wind direction.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 08:01 pm
I just finished modifying the board to include a 4K7 pull-up on the 1-wire pin. The DS1850 that I removed is obsolete too. The Chinese must have a stockpile of old parts. Or maybe they buy them up as they go obsolete.

I don't know if I'll put a temp sensor back inside the enclosure or not. It's useless for weather applications.

I removed 4 (of the 35!) LEDs just to give myself a little room to work. I may remove all of them. That will free up 6 I/O pins. Maybe I'll leave a couple as a warning to low flying birds or squirrels.  :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 08:15 pm
Quote
...very slightly simplifies displaying the wind...


So, if i don't get a match, I display "---"? That's a good idea. I think it's physically impossible to not have one of those patterns though. That would signal a problem with the circuit.

Quote
Idea... interpolating (guessing) the wind direction... By tracking the wind direction versus time, it is possible for the application to provide a guess at the exact wind direction.


That's an interesting idea. That would be helpful for displaying trends also. I'll work on that. I'll probably average wind speed over some period also.

Quote
Sophomore humor.  Got to love it.  Hmm, I think I'll watch Shaun the Sheep tonight...


Sometimes you get tired of meaningful function names and have to go with a little smile. :) Makes life a treat!
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: Coding Badly on Sep 22, 2011, 08:19 pm
Quote
We have a saying here, if you don't like the weather just wait 5 minutes.


Bah!  I grew up in the place that has the World's Record for the fastest temperature change.  Texas weather is slow and stable by comparison.   :D
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 08:26 pm

Quote
We have a saying here, if you don't like the weather just wait 5 minutes.


Bah!  I grew up in the place that has the World's Record for the fastest temperature change.  Texas weather is slow and stable by comparison.   :D



Sounds like Gunnison. I used to have a house in Crested Butte and one day in Almont they measured -60! You had to keep your car battery indoors.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: robtillaart on Sep 22, 2011, 08:28 pm
Quote

Quote
Idea... interpolating (guessing) the wind direction... By tracking the wind direction versus time, it is possible for the application to provide a guess at the exact wind direction.

That's an interesting idea. That would be helpful for displaying trends also. I'll work on that. I'll probably average wind speed over some period also.


Point of attention is if it alternates around 360 / 0 degrees,
The way I wouold approach it is count the number of times every individual sensor is triggered. There are 8 sensor so 8 counters, e.g. B00111111 means count[7]++ and count[6]++ . After a number of measurements a nice frequency diagram will come up from which the average can be derived.



Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: retrolefty on Sep 22, 2011, 08:30 pm
I was stationed in Montana for the Air Force in the 60s where it would get down to -30F for a week at a time. We too had to keep our car batteries inside our barracks if we wanted to start our cars after work, All except one VW bug, it seemed to start no matter how cold.

Lefty

Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: retrolefty on Sep 22, 2011, 08:34 pm
Seems to me that the wind direction will be somewhat obscured by the mass of the wind follower, that is sure to overshoot and oscillate to some degree. So not sure other then some 'averaging' code one could count of wind direction being anything but just a relative measurement. Same might be the case for windspeed impulses.

Lefty

Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 08:43 pm
Gotta love the bugs. My '59 was indestructible.


Seems to me that the wind direction will be somewhat obscured by the mass of the wind follower, that is sure to overshoot and oscillate to some degree. So not sure other then some 'averaging' code one could count of wind direction being anything but just a relative measurement. Same might be the case for windspeed impulses.

Lefty


The wind vane does have significant momentum. That may account for the way it dances around in a gusty wind. I'm going to have an Arduino down on the ground managing it and the other sensors. I can keep a data base and play with the numbers a bit.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 22, 2011, 11:37 pm
Some project pics.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: Coding Badly on Sep 23, 2011, 12:14 am
Quote
All except one VW bug, it seemed to start no matter how cold.


Probably because the battery is underneath the back seat.  Which is all sorts of fun when the bug rolls over.
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 23, 2011, 12:46 am
There was a piece of plywood under mine to keep it from falling through the rusty floor pan. :)
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: floresta on Sep 27, 2011, 10:09 pm
I hadn't been following this thread so I am catching up.

Quote
I used to have a house in Crested Butte ...

Then you might recognize my user name.  Were you in C.B. when Larry T. was the doctor there?

Quote
I was stationed in Montana for the Air Force in the 60s where it would get down to -30F for a week at a time.

Were you at Glasgow AFB by any chance?

Don
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: retrolefty on Sep 27, 2011, 10:13 pm
Quote
Were you at Glasgow AFB by any chance?


No, Malstrom AFB in Great Falls, a SAC Minute Man missile base.

Lefty
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 27, 2011, 10:58 pm
Quote
Then you might recognize my user name.  Were you in C.B. when Larry T. was the doctor there?


That is the first thing that came to my mind when I saw your user name. A run off the East River chair or if you're coming off the North Face. I remember the lonnnnng lines at that chair near the end of the day. And the VW size moguls on Resurrection.

I remember a "happy" doctor I believe by that name. Is that the one?
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: floresta on Sep 27, 2011, 11:55 pm
I didn't get the name from the ski run but from the same town (aka Ruby and Ruby-Anthracite) that the run was undoubtedly named for.  Larry was the doctor for the ski area but I only visited him a few times, and that was in the summer, which as I recall lasted about two weeks.  All he had to deal with then was dog bites and fish hook injuries.  How many people do you know that have driven from Marble to Gothic via Schofield pass? 

Don
Title: Re: Reprogramming the AAG RS485 Weather Station
Post by: EmilyJane on Sep 28, 2011, 12:23 am
I know the area. I have hiked the West Elk Wilderness area not too far from there.
I think you're talking about the doctor in the clinic on the mountain. I saw him for an injury or two. Yeah, summer was that period between the end of mud season and winter.
Quote
How many people do you know that have driven from Marble to Gothic via Schofield pass? 


Not many. The "roads" are just jeep trails up there.