I'm trying to build my own code for my 433MHz RF modules. The transmit side works fine, but the receiver side is more complicated. The signal I emit is as we can see on the image attached. The '0' is 80ms high and 40ms low. The '1' is coded by 40ms high and 80ms low. The code I send to test is 111000. The code below (tries to) measures bit times to find if it's a 0 or a 1.
I use a NodeMCU board, the module RF is connect: Vcc to Vin, GND to GND and DATA to D2 and below the code I wrote for the receiver side:
int receivePin=4;
String reception(int pinReceive){
bool transmissionTermine=0;
int ret=0;
int i=0;
unsigned long tDebutTransmission=0;
char data[50];
Serial.println("-------- Reception ----------");
if (digitalRead(pinReceive)==1){
Serial.println("-------- Debut de transmission ----------");
tDebutTransmission=micros();
while(transmissionTermine!=1){
ret = bitReceive(pinReceive);
//ret = recBit(pinReceive);
Serial.print("ret = ");
Serial.println(ret);
if (ret!=-1){
//strcat(data, *char(ret));
data[i]=char(ret);
i+=1;
}
else{
transmissionTermine=1;
Serial.print("data = ");
Serial.println(data);
}
}
return data;
}
return "A";
}
int bitReceive(int receivePin){
unsigned long tIndex1=0;
unsigned long tIndex2=0;
unsigned long tIndex3=0;
int sortir=0;
tIndex1=micros();
Serial.print("tIndex1= ");
Serial.println(tIndex1);
while (digitalRead(receivePin)==1){
}
tIndex2=micros();
Serial.print("tIndex2= ");
Serial.println(tIndex2);
while ( sortir!=1 ){
if ( ((micros() - tIndex2) > 85000) || (digitalRead(receivePin)==1) ){
sortir=1; //Get-out of the while loop
Serial.print("val= ");
Serial.println(digitalRead(receivePin));
Serial.print("(micros() - tIndex2) = ");
Serial.println((micros() - tIndex2));
}
}
}
tIndex3=micros();
Serial.print("tIndex3= ");
Serial.println(tIndex3);
if (((tIndex2 - tIndex1)>30000) && ((tIndex2 - tIndex1)<50000) && ((tIndex3 - tIndex2)>70000) && ((tIndex3 - tIndex2)<90000)){
return 1;
}
else{
if (((tIndex2 - tIndex1)>70000) && ((tIndex2 - tIndex1)<90000) && ((tIndex3 - tIndex2)>30000) && ((tIndex3 - tIndex2)<50000)){
return 0;
}
else{
Serial.print("tIndex2 - tIndex1 = ");
Serial.println(tIndex2 - tIndex1);
Serial.print("tIndex3 - tIndex2 = ");
Serial.println(tIndex3 - tIndex2);
return -1;
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(230400);
pinMode(receivePin, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
String donnees = reception(receivePin);
if (donnees!="A"){
Serial.print("Données reçues: ");
Serial.println(donnees);
}
}
And the result in the serial monitor is:
------ Receiving ----------
-------- Receiving ----------
-------- Transmission begins ----------
tIndex1= 256958229
tIndex2= 256979139
val= 1
(micros() - tIndex2) = 53045
tIndex3= 257039475
tIndex2 - tIndex1 = 20910
tIndex3 - tIndex2 = 60336
ret = -1
data =
Données reçues:
-------- Receiving ----------
-------- Transmission begins ----------
tIndex1= 257238426
tIndex2= 257259259
val= 0
(micros() - tIndex2) = 53122
tIndex3= 257319672
tIndex2 - tIndex1 = 20833
tIndex3 - tIndex2 = 60413
ret = -1
data =
Données reçues:
-------- Receiving ----------
-------- Receiving ----------
Is that due to interferences? I've tried to minimize it the input_pullup. Is that due to the serial monitor limit? I don't really know.
Thanks a lot for your help