7 years, 10 months ago.

how to send 4 bytes of data using mbed nucleo F042K6

i only can send 1 byte using XCTU now. this my code.i am using a xbee connected to the mcu on breadboard and i sending to another xbee if the sensor detect movement. another person is receiving what i send from the xctu using putty.his xbee is connected to the beaglebone board. but when open the serial port on the xctu and type any word it just send only that and the another person can only recieve 1 word at a time.but im going to use 2 sensor. so if both detect people at the same time i nid to differentiate it. so for the 1st sensor i wan to make it send like A1. the other will be A2. so the 1 and 2 will be differentiating them. but i dont know how to do tat. i ask one guy he say nid to have this 4, start byte, cmd ,source id and status. how to do that?

include the mbed library with this snippet

#include "mbed.h"

DigitalOut led1(LED1);
DigitalIn alarm(PF_1, PullUp); 
Serial pc(SERIAL_TX, SERIAL_RX); 
 
int main() {  
 
   pc.printf("running\n");
    
   while(1) {
     if (alarm){
        led1=1;          
        pc.printf("motion detected");
     }
     else
       led1=0;
   }
}

Can you please give a bit more detail. It looks like you are sending 8 bytes on start up and then constantly sending blocks of 15 bytes as fast as you can on an alarm.

Are you saying that you only see the r from running?

Also please use <<code>> tags when posting code. e.g.

<<code>>
#include "mbed.h"
DigitalOut led1(LED1);
DigitalIn alarm(PF_1, PullUp);
Serial pc(SERIAL_TX, SERIAL_RX);
<</code>>

It makes the code a lot more readable.

__

Update - please don't completely change the original post, it makes answers and comments misleading and doesn't make it clear to people who have previously answered that anything has changed. If you need to add further information make it obvious rather than in the middle of the post.

posted by Andy A 18 May 2016

oops wrong edit

posted by vic nes 06 Jun 2016

1 Answer

7 years, 10 months ago.

So you are asking how to send 4 bytes of text over a serial port? Your program is already sending text over the serial port, just change the text it sends.

pc.puts("A fixed text string"); // to send a constant piece of text puts is fastest but printf can also be used.

pc.putc('A'); // use putc send a single character

int x = 3;
pc.printf("A variable text string with a number %d", x); // To send variable text use printf
                                                   // google for the correct syntax for different variable types


// to send non-text data use putc in a loop.
#define dataLen 4
char binaryData[dataLen] = { 0x00, 0xff, 0x55, 0xaa };
for (int i = 0; i<dataLen; i++)
  pc.putc(binaryData[i]);

For your specific problem how will the unit know if it is unit 1 or unit 2? An IO pin or different versions of the software?

It it's different versions of the software then it's trivial, just change the text in the software. It it's an IO pin then you want something like

DigitalIn unitID(p5); // change to the correct ID pin.

DigitalOut led1(LED1);
DigitalIn alarm(PF_1, PullUp);
Serial pc(SERIAL_TX, SERIAL_RX);

void sendUnitID() { // no reason why this needs to be a separate function, this could just as easily be in main.
  if (unitID)
    pc.puts("A2");
  else
   pc.puts("A1");
}

main() {

  while(1) {
     if (alarm){
        led1=1;          
        sendUnitID();
        pc.printf("motion detected");
        wait(0.5); // pause so that we don't flood the system, only send the alarm twice per second.
     }
     else
       led1=0;
   }
}

Accepted Answer

actually its this 1st byte (Start byte) 2nd byte (XBee Id) 3rd byte (Cmd) 4th byte (Data)

0xAB 1st byte

· 0x01 for 1st STM32 Nucleo board with XBee · 0x02 for second STM32 Nucleo board with XBee

· 0x88 for command to send PIR sensor value · Other values are reserved for future use

· 0x01 for motion detected in PIR; 0x02 for motion not detected in PIR

/media/uploads/vicnes105/image001.png

is this coorect, <<code title=include the mbed library with this snippet>>#include "mbed.h"

DigitalOut led1(LED1); DigitalIn alarm(PF_1, PullUp); Serial pc(SERIAL_TX, SERIAL_RX); Serial xbee1(PA_9, PA_10); int main() { pc.printf("running\n");

while(1) {

unsigned char binaryData[4] = { 0xAB, 0x00, 0x88, 0x01 }; for (int i = 0; i<4; i++) pc.putc(binaryData[i]); if (alarm){ led1=1; pc.printf("detected"); } else led1=0; } }<</code>>

posted by vic nes 20 May 2016

Not quite, that code would send the alarm active message constantly no matter what the alarm should be doing.

This will send the alarm message at 10Hz while the alarm is active. It will send the alarm not active message once when the alarm turns off and then stop transmitting.

There are more efficient ways of doing this but this keeps it simple.

#include "mbed.h"

DigitalOut led1(LED1);
DigitalIn alarm(PF_1, PullUp);
Serial pc(SERIAL_TX, SERIAL_RX);
Serial xbee1(PA_9, PA_10);

int main() {
 pc.printf("running\n");

 bool oldAlarm = false;
 
 while(1) {

   unsigned char alarmOn[4] = { 0xAB, 0x00, 0x88, 0x01 };
   unsigned char alarmOff[4] = { 0xAB, 0x00, 0x88, 0x00 };
  
   if (alarm){ 
        led1=1;
        pc.printf("detected");
        for (int i = 0; i<4; i++)
           xbee1.putc(alarmOn[i]);

        oldAlarm = true;
        wait(0.1); // limit alarm updates to 10Hz
      } else {
         if (oldAlarm) { // alarm was on.
            for (int i = 0; i<4; i++)
               xbee1.putc(alarmOff[i]);
            led1=0; 
            pc.printf("Alarm off");
            oldAlarm = false;
         } // end if oldAlarm
      } // end else
  } // end while
}

You need to limit the update rate when the alarm is active. If you send the message constantly while the alarm is active then you'll jam the radio channel and stop other alarms from being detected if they are also going off.

For a more robust alarm system you may want to send the alarm off signal at a fixed rate (probably not 10Hz but at 1Hz or every 5 seconds), that way someone can't avoid the alarm by unplugging it or jamming the radio channel.

posted by Andy A 20 May 2016

sorry for the late reply was away on weekends. but i want the Serial pc object for printing messages so that i know where the code is. where do i insert that?

posted by vic nes 23 May 2016

Where do you insert what? It's already printing something to the PC when the alarm is on and when it turns off. That's what the pc.printf() lines are for. If you need something else you need to be a little more specific as to what you want to print and when.

posted by Andy A 23 May 2016

i did it. it was actually suppose be like this, <<code>>

  1. include "mbed.h"

DigitalOut led1(LED1); DigitalIn alarm(PF_1, PullUp); Serial pc(SERIAL_TX, SERIAL_RX); Serial xbee1(PA_9, PA_10);

int main() { pc.printf("running\n");

int sent = false;

while(1) {

unsigned char alarmOn[4] = { 0xAB, 0x00, 0x88, 0x01 }; unsigned char alarmOff[4] = { 0xAB, 0x00, 0x88, 0x00 };

if (alarm){ led1=1; if(sent == false){ pc.printf("detected"); for (int i = 0; i<4; i++) xbee1.putc(alarmOn[i]);

sent = true; wait(0.1); } } else { led1=0; if (sent == true) { alarm was on. pc.printf("not detected"); for (int i = 0; i<4; i++) xbee1.putc(alarmOff[i]);

sent = false; wait(0.1); } } } }<</code>>

but the sensor does not work. when i replaced it with a switch it works. maybe might be the sensor prob ill try get a new one before i reply.

posted by vic nes 23 May 2016

nvm its working.

posted by vic nes 24 May 2016

i dont know why the sensor works but its like taking so long to respond. like i leave putty on and like wait 2 min or more then it change. then it works normally for about 5 sec. then when the led on again i have to wait like 2 min again. do u know wats causing this problem?

posted by vic nes 24 May 2016

There is nothing in he code that would cause that so check the sensor specifications and how you've connected it up.

posted by Andy A 24 May 2016

its working now. but how do i do debouncing

posted by vic nes 25 May 2016

You only need to debounce mechanical switches not electronic inputs from sensors. Search the site, there are dozens of examples.

posted by Andy A 25 May 2016

oh yes . some guy told me had to debounce. i was wondering. but seems does not nid. also regarding pin D6 from this web> https://developer.mbed.org/platforms/ST-Nucleo-F042K6/. is it a adc pin?it states thr is only 2 adc pin.

posted by vic nes 26 May 2016

D6 is an analog in. There are 10 ADC inputs, the CPU has two ADCs but each one has multiple inputs that it can switch between.

posted by Andy A 26 May 2016

can i ask if i wan to have both a ultrasonic sensor and pir sensor i need to combine the code. my ultrasonic sensor code is ,

include the mbed library with this snippet

#include "mbed.h"

AnalogIn sensor(A0);   /* Potentiometer middle pin connected to PA_11, other two ends connected to GND and 3.3V */
Serial pc(SERIAL_TX, SERIAL_RX);
int main()
{
    float ain;   /* Variable to store the analog input*/
    float dist;
    while(1) {
        ain = sensor.read(); /* Read analog value (output will be any value between 0 and 1 */
        dist = ain*1024*5/1000;
       pc.printf("ain = %.4f, dist = %.4f\t", ain, dist);
        wait(2);

    }
}

my pir code is

include the mbed library with this snippet

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalIn alarm(A0, PullUp);
Serial pc(SERIAL_TX, SERIAL_RX);
Serial xbee1(PA_9,PA_10);
 
int main() {
 pc.printf("running\n");
 
 bool sent = false;
 
 while(1) {
 
   unsigned char alarmOn[4] = { 0xAB, 0x01, 0x88, 0x01 };
   unsigned char alarmOff[4] = { 0xAB, 0x01, 0x88, 0x00 };
  
   if (alarm){ 
        led1=1;
        if(sent == false){    
          pc.printf("detected");
          for (int i = 0; i<4; i++)
            xbee1.putc(alarmOn[i]);
          
          sent = true;
          wait(0.1);
        }
      } else {
         led1=0;
         if (sent == true) { // alarm was on.
            pc.printf("not detected");
            for (int i = 0; i<4; i++)
               xbee1.putc(alarmOff[i]);
            
            sent = false;
            wait(0.1);
         }
      } 
  } 
}

i want to place it in my door side and make it such that when a person walk within 40 cm of it the led will light up also. how do i combine the 2 codes?

posted by vic nes 03 Jun 2016

i actually managed i think. but i dont know how to combine them. i wanted do without the xbee 1st. how d o i combine both? the pir code is

include the mbed library with this snippet

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalIn alarm(PA_0, PullUp);
Serial pc(SERIAL_TX, SERIAL_RX);
 
int main() {
     while(1) {
         if(alarm == 1){
             led1=1;
            printf("motion detected");
            wait(2);
         }
         else if(alarm == 0){
             led1=0;
           printf("no motion");
           wait(2);
         }  
     }
}         

the ultrasonic sensor code is,

include the mbed library with this snippet

#include "mbed.h"

AnalogIn sensor(A0);
DigitalOut led1(LED1);  
Serial pc(SERIAL_TX, SERIAL_RX);
int main()
{
    float ain;   /* Variable to store the analog input*/
    float dist;
    while(1) {
        ain = sensor.read(); /* Read analog value (output will be any value between 0 and 1 */
        dist = ain*1024*5/1000;
        pc.printf("ain = %.4f, dist = %.4f\t", ain, dist);
        wait(2);
        if(dist>0.5){
            led1=1;
        }
       else led1=0;
    }
}
posted by vic nes 06 Jun 2016

At this point it makes sense to start splitting the functionality out into separate functions. This is taking that idea a little further than you normally would but the idea is to make it so that each function is easy to follow.

#include "mbed.h"
 
DigitalOut led1(LED1);
DigitalIn PIRSignal(A0, PullUp);
AnalogIn sensor(A0);   /* Potentiometer middle pin connected to PA_11, other two ends connected to GND and 3.3V */
Serial pc(SERIAL_TX, SERIAL_RX);
Serial xbee1(PA_9,PA_10);

// Signal that the ultrasound alarm should be activate.
bool getDistanceAlarm() {
  float dist = sensor.read()*1024*5/1000;
  if (dist <= 0.4)
    return true;
  else
    return false;
}

// returns true if the pir sensor is active.
bool getPIRAlarm() {
  return PIRSignal;
}

// returns true if we need to turn the alarm on, false if the alarm should be off. 
bool alarmOn() {
  if (getPIRAlarm() || getDistanceAlarm())
    return true;
  else
    return false;
}

// now the main loop is the same as for the PIR only system only
//  we call alarmOn() rather than just checking the PIR status.
int main() {
 pc.printf("running\n");
 
 bool sent = false;
 unsigned char alarmOn[4] = { 0xAB, 0x01, 0x88, 0x01 };
 unsigned char alarmOff[4] = { 0xAB, 0x01, 0x88, 0x00 };
 
 while(1) {
 
   if (alarmOn()){  // if the alarm should be on.
        led1=1;
        if(sent == false){    
          pc.printf("detected");
          for (int i = 0; i<4; i++)
            xbee1.putc(alarmOn[i]);
          
          sent = true;
          wait(0.1);
        }
      } else {
         led1=0;
         if (sent == true) { // alarm was on.
            pc.printf("not detected");
            for (int i = 0; i<4; i++)
               xbee1.putc(alarmOff[i]);
            
            sent = false;
            wait(0.1);
         }
      } 
  } 
}

One further refinement you could add if you wanted is to make it so the ultrasound alarm turns on at 40cm but then doesn't turn off again until the measurement is at least 50cm. You could do that by making the variable sent global and then using that to pick which threshold to use.

posted by Andy A 06 Jun 2016

currently my combined code is like this. both the pir sensor and the ultrasonic sensor on the same breadboard.

include the mbed library with this snippet

#include "mbed.h"
 
DigitalOut led1(PA_1);
DigitalIn alarm(PA_1, PullUp);
AnalogIn sensor(A0);
DigitalOut led2(LED1);  
Serial pc(SERIAL_TX, SERIAL_RX);
 
int main() {
     float ain;   /* Variable to store the analog input*/
    float dist;
     while(1) {
         if(alarm == 1){
             led1=1;
            printf("motion detected");
            wait(2);
         }
         else if(alarm == 0){
             led1=0;
           printf("no motion");
           wait(2);
         }  
         ain = sensor.read(); /* Read analog value (output will be any value between 0 and 1 */
        dist = ain*1024*5/1000;
        pc.printf("ain = %.4f, dist = %.4f\t", ain, dist);
        wait(2);
        if(dist>0.5){
            led2=1;
        }
       else led2=0;
     }
}         
posted by vic nes 07 Jun 2016

a nvm i done it.

posted by vic nes 08 Jun 2016