Instead of doing the what it should be why not try the what it is?
I have a wind vane that uses a resistor to generate a value that is sent to a A:D converter and I use 3.3V.
I pointed my wind vane north, using a compensated compass, noted the analog reading; got north.
I pointed my wind vane east, using a compensated compass, noted the analog reading; got east.
I pointed my wind vane south, using a compensated compass, noted the analog reading; got south.
I pointed my wind vane west, using a compensated compass, noted the analog reading; got west.
Then I worked out the range of analog values vs what I call north, south, east, and west.
Here are values I got for the directions I measured using the Sparkfunny thing.
void fWindDirection( void *pvParameters )
// read the wind direction sensor, return heading in degrees
{
float adcValue = 0.0f;
uint64_t TimePastKalman = esp_timer_get_time();
SimpleKalmanFilter KF_ADC( 1.0f, 1.0f, .01f );
float high = 0.0f;
float low = 2000.0f;
float ADscale = 3.3f / 4096.0f;
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 100; //delay for mS
int count = 0;
String windDirection;
windDirection.reserve(20);
String MQTTinfo = "";
MQTTinfo.reserve( 150 );
while ( !MQTTclient.connected() )
{
vTaskDelay( 250 );
}
for (;;)
{
windDirection = "";
adcValue = float( adc1_get_raw(ADC1_CHANNEL_6) ); //take a raw ADC reading
KF_ADC.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
adcValue = KF_ADC.updateEstimate( adcValue ); // apply simple Kalman filter
TimePastKalman = esp_timer_get_time(); // time of update complete
adcValue = adcValue * ADscale;
if ( (adcValue >= 0.0f) & (adcValue <= .25f ) )
{
// log_i( " n" );
windDirection.concat( "N" );
}
if ( (adcValue > .25f) & (adcValue <= .6f ) )
{
// log_i( " e" );
windDirection.concat( "E" );
}
if ( (adcValue > 2.0f) & ( adcValue < 3.3f) )
{
// log_i( " s" );
windDirection.concat( "S");
}
if ( (adcValue >= 1.7f) & (adcValue < 2.0f ) )
{
// log_i( " w" );
windDirection.concat( "W" );
}
if ( count >= 30 )
{
MQTTinfo.concat( String(kph, 2) );
MQTTinfo.concat( ",");
MQTTinfo.concat( windDirection );
MQTTinfo.concat( ",");
MQTTinfo.concat( String(rain, 2) );
xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
MQTTclient.publish( topicWSWDRF, MQTTinfo.c_str() );
xSemaphoreGive( sema_MQTT_KeepAlive );
count = 0;
}
count++;
MQTTinfo = "";
xLastWakeTime = xTaskGetTickCount();
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
vTaskDelete ( NULL );
}