Sending buffer via 433Mhz transmitter issue

Hi everybody I’m currently facing an issue and it seems it doesn’t want to get fixed by it self :frowning:

Basically I want to send a buffer that has 3 variables (actually I’m currently using simple numbers since I can’t even get the numbers to work it doesn’t make sense to try with variables)

When I open the debugging window I get :
photoRes 33 //Should be 400
photoRes2 211 //Should be 500
pression 0 //Should be 10

Here is the code for the transmitter

//int res2 = A1;
//int res = A0;


#include <VirtualWire.h>
  
void setup()
{
    Serial.begin(9600);	  
   // pinMode(res, INPUT_PULLUP);
    //pinMode(res2, INPUT_PULLUP);

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); 
    vw_setup(2000);
    vw_set_tx_pin(3);	 
}

void loop()
{
    
    //photoRes = analogRead(res);
    //photoRes2 = analogRead(res2);

  /*PPL */
    
     int photoRes =400;
   int photoRes2 = 500;
    int pression = 10;
    
	uint8_t buffer[20];
	
	buffer[0] = 1 ;   									
	buffer[1] = 15 ;   									
	buffer[2] = 3 ;   									
	buffer[3] = 0;    									
	buffer[4] = 2 ;    									
	buffer[5] = (uint8_t)((photoRes & 0xFF00)>>8); 	// MSB
	buffer[6] = (uint8_t)(photoRes & 0x00FF); 		// LSB	
	buffer[7] = 1;    									
	buffer[8] = 2 ;   									
	buffer[9] = (uint8_t)((photoRes2 & 0xFF00)>>8); 		
	buffer[10] = (uint8_t)(photoRes2 & 0x00FF); 			
	buffer[11] = 2;    									
	buffer[12] = 2  ;  									
	buffer[13] = (uint8_t)((pression  & 0xFF00)>>8); 		
	buffer[14] = (uint8_t)(pression & 0x00FF); 			
	
	
	vw_send(buffer, 15);
    
	


    delay(200);
}

Here is for the receiver

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);	// Debugging only
    

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(500);	 // Bits per sec
    vw_set_rx_pin(52);
    vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    int photoRes, photoRes2, pression;
	
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
		int i;
		int a=0;
       
    
	
		
		int counter = 0;
		uint8_t msgID = buf[counter];     
		int lenTotalMsg = buf[counter++];    
		
		uint8_t nombreParam = buf[counter++]; 
		for (int i ; i<nombreParam; i++)
		{
		    uint8_t paramID = buf[counter++];    
			uint8_t lenParam = buf[counter++];   
			uint8_t valeurParam = buf[counter++]; 
			
		
			if (lenParam==2)
			{  
			   valeurParam= ((int)(buf[counter++]))<<8 | (buf[counter++]);
			}
			else
			{   
			    valeurParam= (int)buf[counter++];
			}
			
			
			switch (paramID)
			{
			    case 0: 
				    photoRes = valeurParam;
				break;
			    case 1: 
				    photoRes2 = valeurParam;
				break;
			    case 2: 
				    pression = valeurParam;
				break;
				
			}
			
		
		}
		Serial.print("photoRes ");
		Serial.println(photoRes, DEC);		
		Serial.print("photoRes2 ");
		Serial.println(photoRes2);		
		Serial.print("pression ");
		Serial.println(pression);		

		
		//a=((int)(buf[1])<<8) |(buf[0]) ;
		

        //Serial.print(buf, buflen);
       
    }
}

Thanks in advance :slight_smile:

Transmitter is set to 2000 bps, and the receiver is set to 500 bps, something doesn’t look right.

        uint8_t msgID = buf[counter];     
        int lenTotalMsg = buf[counter++];

Which element of the buf array do you think you are storing in lenTotalMsg? The correct answer is NOT 1.

Why is lenTotalMsg an int, when the value being stored in it is a byte?

        uint8_t nombreParam = buf[counter++];

Which element of the buf array do you think you are storing in nombreParam? The correct answer is NOT 2.

The rest of the code is similarly off by 1. I suspect that you want ++counter as the index (or to use counter++ as the index in the 1st statement).

Thank you both for answering, I forgot to change the bauds when I posted but even with 2000/2000 it wasn't working.

PaulS can you help me improving the program because I'm a bit confused ? :blush:

PaulS can you help me improving the program because I'm a bit confused ?

Sure. Until you KNWO shat counter++ does, do NOT use it as the array index. Separate the increment of the index from the use of the index.

        uint8_t msgID = buf[counter]; 
                counter++;    
        int lenTotalMsg = buf[counter];
                counter++;

Thanks mate will remembber that, is there anything else that makes the program fails ?

is there anything else that makes the program fails ?

I don't know. What serial output do you get? What results do you get?

The issue with counter++ as the index is that the ++ is an operator. Operators do something and return a value. The ++ can be used in pre-fix form (++counter) and post-fix form (counter++). In either case what the operator does is the same. The difference is the returned value. In the pre-fix form, the value returned is the value in the variable after increment happens. In the post_fix form, the value returned is the value in the variable before the increment happens.

So:

        uint8_t msgID = buf[counter];     
        int lenTotalMsg = buf[counter++];

causes the value in buf[0] to be stored in both variables, and counter is 2 (as expected) after the second statement ends.

        uint8_t msgID = buf[counter++];     
        int lenTotalMsg = buf[counter++];

would read from buf[0] and then increment counter to 1, and read from buf[1] and increment counter to 2.

int res2 = A1;
int res = A0;


#include <VirtualWire.h>
   int photoRes =0;
   int photoRes2 = 0;
void setup()
{
    Serial.begin(9600);	 
    pinMode(res, INPUT_PULLUP);
    pinMode(res2, INPUT_PULLUP);

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); 
    vw_setup(2000);
    vw_set_tx_pin(3);	 
}

void loop()
{
    
    photoRes = analogRead(res);
    photoRes2 = analogRead(res2);

  /*PPL */
    
    
    int pression = 10;
    int counter=0;  
	uint8_t buffer[20];
	

	buffer[counter] = 1 ;   									
counter++;	
        buffer[counter] = 15 ;
counter++;									
	buffer[counter] = 3 ;
counter++;									
	buffer[counter] = 0;
counter++;									
	buffer[counter] = 2 ;
counter++;									
	buffer[counter] = (uint8_t)((photoRes & 0xFF00)<<8); 	// MSB
counter++;	
        buffer[counter] = (uint8_t)(photoRes & 0x00FF); 		// LSB	
counter++;	
        buffer[counter] = 1;    									
counter++;	
        buffer[counter] = 2 ;
counter++;   									
	buffer[counter] = (uint8_t)((photoRes2 & 0xFF00)<<8); 		
counter++;	
        buffer[counter] = (uint8_t)(photoRes2 & 0x00FF); 			// LSB	
counter++;	
        buffer[counter] = 2;    									
counter++;	
        buffer[counter] = 2  ;  									
counter++;
	buffer[counter] = (uint8_t)((pression  & 0xFF00)<<8); 		
counter++;
	buffer[counter] = (uint8_t)(pression & 0x00FF); 			
counter++;	
	
	vw_send(buffer, counter);
    

	
	vw_wait_tx(); 

    delay(200);
}

Here it is for the transmitter with the information you gave me, and the following code is the receiver, the problem is I get the wrong values all the time, and the worst is I read an analog input but the value returned by the receiver is static it doesn’t change, here is the values I get

Temperature= 1024
pression= 3329
humidite= -7679

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);	
    

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); 
    vw_setup(2000);	 
    vw_set_rx_pin(52);
    vw_rx_start();       
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    int temperature, humidite, pression;
	
    if (vw_get_message(buf, &buflen)) 
    {
		int i;
		int a=0;
       
    
	
		// on decode le message
		int counter = 0;
		uint8_t msgID = buf[counter];
                counter++;                                                
		int lenTotalMsg = buf[counter];
                counter++;                                 
		
		uint8_t nombreParam = buf[counter];         
		counter++;
                for (int i ; i<nombreParam; i++)
		{
		    uint8_t paramID = buf[counter];    counter++;			
uint8_t lenParam = buf[counter];        
counter++;			
int valeurParam; 
			
		      
			if (lenParam==2)
			{  // on a deux octets
			   valeurParam= ((int)(buf[counter++]))<<8 | (buf[counter++]);
			}
			else
			{   // on a un octet
			    valeurParam= (int)buf[counter++];
			}
			
			 
			switch (paramID)
			{
			    case 0: // Temperature
				    temperature = valeurParam;
				break;
			    case 1: // pression
				    pression = valeurParam;
				break;
			    case 2: // humidite
				    humidite = valeurParam;
				break;
				
			}
			
		}
		
		Serial.print("Temperature= ");
		Serial.println(temperature);		
		Serial.print("pression= ");
		Serial.println(pression);		
		Serial.print("humidite= ");
		Serial.println(humidite);		

	
    }

It’s impossible since my sensors (photo resistors, 2 of them) can go from 0 to 1023.

Print out the contents of buffer on the sender side before you send it. Print out the contents of buffer on the receiver side before you interpret it. Are the contents the same? If so, it's a problem of interpretation. If not, it's a different problem. Which are we trying to solve?

When I print out the values on the transmitter I almost get the good values, but when I print out buflen on the receiver I only get 30, actually I don't even cleary understand that buffer story :blush:

I mean what those lines mean : uint8_t buf[VW_MAX_MESSAGE_LEN]; uint8_t buflen = VW_MAX_MESSAGE_LEN;

uint8_t buf[VW_MAX_MESSAGE_LEN];

This defines an array called buf, of type uint8_t (unsigned 8 bit integer type) with a size of VW_MAX_MESSAGE_LEN (which is defined in the VW library.

uint8_t buflen = VW_MAX_MESSAGE_LEN;

This defines a variable, buflen, of type uint8_t, with an initial value of VW_MAX_MESSAGE_LEN.

When I print out the values on the transmitter I almost get the good values, but when I print out buflen on the receiver I only get 30

Can't see your code OR your serial output.

Here you go

int res2 = A1;
int res = A0;


#include <VirtualWire.h>
   int photoRes =0;
   int photoRes2 = 0;
void setup()
{
    Serial.begin(9600);	  // Debugging only
    pinMode(res, INPUT_PULLUP);
    pinMode(res2, INPUT_PULLUP);

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);
    vw_set_tx_pin(3);	 // Bits per sec
}

void loop()
{
    
    photoRes = analogRead(res);
    photoRes2 = analogRead(res2);

  /*PPL */
    
    
    int pression = 10;
    int counter=0;  
	uint8_t buffer[20];
	
	// on remplit notre buffer
	buffer[counter] = 1 ;   									// MsgID : Identifiant de message 1: envoi donn?es, 2: blabla....
counter++;	
        buffer[counter] = 15 ;
counter++;									// Taille totale du message avec l'entete (MsgID + Taille Totale)
	buffer[counter] = 3 ;
counter++;									// Nombre de parametres (ici 3 car on veut envoyer temp?rature, pression et humidit? )
	buffer[counter] = 0;
counter++;									// ParametreID : 0 : temp?rature, 1: pression; 2: humidit?
	buffer[counter] = 2 ;
counter++;									// taille en octet de la variable (ici temperature)
	buffer[counter] = (uint8_t)((photoRes & 0xFF00)>>8); 	// MSB
counter++;	
        buffer[counter] = (uint8_t)(photoRes & 0x00FF); 		// LSB	
counter++;	
        buffer[counter] = 1;    									// ParametreID : 0 : temp?rature, 1: pression; 2: humidit?
counter++;	
        buffer[counter] = 2 ;
counter++;   									// taille en octet de la variable (ici temperature)
	buffer[counter] = (uint8_t)((photoRes2 & 0xFF00)>>8); 		// MSB (octet de gauche)
counter++;	
        buffer[counter] = (uint8_t)(photoRes2 & 0x00FF); 			// LSB	(octet de droite)
counter++;	
        buffer[counter] = 2;    									// ParametreID : 0 : temp?rature, 1: pression; 2: humidit?
counter++;	
        buffer[counter] = 2  ;  									// taille en octet de la variable (ici temperature)
counter++;
	buffer[counter] = (uint8_t)((pression  & 0xFF00)>>8); 		// MSB (octet de gauche)
counter++;
	buffer[counter] = (uint8_t)(pression & 0x00FF); 			// LSB	(octet de droite)
	
	// envoi du buffer
	vw_send(buffer, counter);
    
	
/*	
    vw_send((uint8_t *)&a, sizeof(a));
*/    
	
	vw_wait_tx(); // Wait until the whole message is gone

Serial.println(buffer[5, 6]);
Serial.println(buffer[9, 10]);
Serial.println(buffer[13, 14]);
    delay(200);
}

Values I get are a little bit bugged but at least they vary !

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);	// Debugging only
    

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);	 // Bits per sec
    vw_set_rx_pin(52);
    vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    int temperature, humidite, pression;
	Serial.println(buflen);
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
		int i;
		int a=0;
       
    
	
		// on decode le message
		int counter = 0;
		uint8_t msgID = buf[counter];
                counter++;                                                // on recupere l'identifant du message
		int lenTotalMsg = buf[counter];
                counter++;                                 // on recupere la longueur du message
		
		uint8_t nombreParam = buf[counter];         // nombre de parametres
		counter++;
                for (int i ; i<nombreParam; i++)
		{
		    uint8_t paramID = buf[counter];    // on recupere le parametre ID
counter++;			
uint8_t lenParam = buf[counter];         // on recupere la longeur en octets du parametre ID  (soit 1, 2)
counter++;			
int valeurParam; 
			
		      // on formate notre valeur sur un int
			if (lenParam==2)
			{  // on a deux octets
			   valeurParam= ((int)(buf[counter++]))<<8 | (buf[counter++]);
			}
			else
			{   // on a un octet
			    valeurParam= (int)buf[counter++];
			}
			
			 
			switch (paramID)
			{
			    case 0: // Temperature
				    temperature = valeurParam;
				break;
			    case 1: // pression
				    pression = valeurParam;
				break;
			    case 2: // humidite
				    humidite = valeurParam;
				break;
				
			}
			
		}
		
		Serial.print("Temperature= ");
		Serial.println(temperature);		
		Serial.print("pression= ");
		Serial.println(pression);		
		Serial.print("humidite= ");
		Serial.println(humidite);		

		/*
		// Message with a good checksum received, dump it.
		Serial.print("Got: ");
		//Serial.print("size= ");
		//Serial.print(buflen);
		a=((int)(buf[1])<<8) |(buf[0]) ;
		//Serial.print(a);
/*
if (a> 260 )
{
Serial.print("H : superieur seuil");

}
else {
  Serial.print("L : inferieur seuil");
}
*/
/*
	for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i]);
	    Serial.print(" ");
	}
*/
        //Serial.print(buf, buflen);
       
    }
}

The values for the receiver stay the same all the time (see in previous post) and I can’t print buf or buflen I get an error (near match)

Serial.println(buffer[5, 6]);
Serial.println(buffer[9, 10]);
Serial.println(buffer[13, 14]);

What is this nonsense?

Was there some part of “print buffer before you send it” that was hard to understand? Use a for loop to print EVERY value in buffer with a prefix:

for(byte b=0; b<20; b++)
{
   Serial.print("buffer[");
   Serial,print(b);
   Serial.print("] = ");
   Serial.println(buffer[b]);
}

Do the same in the receiver. Do not bother printing anything else. If what you send is not what you get, then there is no sense trying ti use what you get. For anything!