- Sat Jun 04, 2016 7:40 am
#48558
OK, I did my home work and port to ESP8266 the arduino code!
The original wroted 1200microsec is not perfect, there was some shadow bit. I am increase 1220 and all going very well. I am not sure what is the correct amount but with 1220 on my board is working. If the two checksum is not equal, try to modify that delay.
Here is the working solution to testing and debugging the TX-20 wind sensor.
You do not need any extra level shifting just simple connect the D1 to TX-20, because both working on 3.3V.
1 Brown TxD (Digital pin)
2 Red Vcc (3.3V)
3 Green DTR (Ground)
4 Yellow GND (Ground)
For understand the data, here is the details:
https://www.john.geek.nz/2011/07/la-crosse-tx20-anemometer-communication-protocol/The decoding algorithm based on this Arduino code:
http://fabrizio.zellini.org/decoding-la-crosse-tx20-anemometer-with-arduinoAnd at last , there is the Wemos and I think a common ESP8266 code.
Change the DATAPIN constant to your data pin.
Code: Select allconst byte DATAPIN=D2;
volatile boolean TX20IncomingData = false;
void readTX20() {
if (!TX20IncomingData) {
TX20IncomingData = true;
}
}
/*
* SETUP
*/
void setup() {
pinMode(DATAPIN, INPUT);
attachInterrupt(digitalPinToInterrupt(DATAPIN), readTX20, RISING);
Serial.begin(115200);
}
/*
* LOOP
*/
void loop() {
if (TX20IncomingData) {
char a[90];
unsigned char chk;
int bitcount=0;
unsigned char sa,sb,sd,se;
unsigned int sc,sf, pin;
String message = "";
sa=sb=sd=se=0;
sc=0;sf=0;
for (bitcount=41; bitcount>0; bitcount--) {
pin = (digitalRead(DATAPIN));
if (!pin) {
message += "1";
} else {
message += "0";
}
if ((bitcount==41-4) || (bitcount==41-8) || (bitcount==41-20) || (bitcount==41-24) || (bitcount==41-28)) {
message += " ";
}
if (bitcount > 41-5){
// start, inverted
sa = (sa<<1)|(pin^1);
} else
if (bitcount > 41-5-4){
// wind dir, inverted
sb = sb>>1 | ((pin^1)<<3);
} else
if (bitcount > 41-5-4-12){
// windspeed, inverted
sc = sc>>1 | ((pin^1)<<11);
} else
if (bitcount > 41-5-4-12-4){
// checksum, inverted
sd = sd>>1 | ((pin^1)<<3);
} else
if (bitcount > 41-5-4-12-4-4){
// wind dir
se = se>>1 | (pin<<3);
} else {
// windspeed
sf = sf>>1 | (pin<<11);
}
delayMicroseconds(1220);
}
Serial.println(message);
chk= ( sb + (sc&0xf) + ((sc>>4)&0xf) + ((sc>>8)&0xf) );chk&=0xf;
sprintf(a, "ID: %d\t%d\n", sa, B00100);
Serial.write (a);
sprintf(a, "Wind direction: %d\t%d\n", sb, se);
Serial.write (a);
sprintf(a, "Wind speed: %d\t%d\n", sc, sf);
Serial.write (a);
sprintf(a, "Checksum: %d\t%d\n", sd, chk);
Serial.write (a);
if (sa==4 && sb==se && sc==sf && sd==chk){
Serial.println(" :) OK :) OK :) OK :) OK");
} else {
Serial.println(" !!! ERROR !!! ERROR !!!");
}
Serial.println("");
delayMicroseconds(2000); // just in case
TX20IncomingData = false;
}
}