Micropython and XBEE

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Micropython and XBEE

Post by schvic » Wed Jul 09, 2014 3:11 pm

Greetings happy micropython board owners,

I am currently trying to make my Arduino and my pyboard communicate with two XBEE series 2 modules.

The context of my project is to control 2 relays and monitorise some data coming from different sensors. There will be an Arduino that will concatenate all this data and send it via an XBEE in AT mode to the pyboard with another XBEE module.

My frame is composed of: "D" + datax + "," + datay + "," + ....
I separated my data by comas because I used a second arduino to receive the data and I used the serial.parseInt() method to parse the data. I putted a "D" character at the begining in order to use serial.find("D") method to detect a new message.

I have achieved to make the pyboard receive part of the messages from the Arduino but since there is only de uart.recv method, there is no parsing or serial.readline method. So for example if I send a message with the Serial.println method in my Arduino, the pyboard will receive the frame but will add "/n" to the message and wil not recognise that there is a new line that is equivalent to a new message.

I will try to make a function that reads the frame until "/n" appears but I wanted to submit the problem in order to see if there are better performing ideas.

You can find attached my arduino code (the importatn part is in bold characters". Just to explain, the XBEE is connected to the Serial1 in the arduno Mega.
The pyboard code side there is just an uart.init() and uart .recv() method to print what it receives (using the REPL tool).

Thank you for your support.

Code: Select all

//Relay configuration

int relay1=3;
int relay2=4;
int armRelays=11;
int relayRadioInput1=12;
int relayRadioInput2=13;
int armRelaySig=7;
int relay1Sig = 8;
int relay2Sig = 9;
int relay1SignalPinM1;
int relay2SignalPinM1;
int relayRadioSignal1;
int relayRadioSignal2;
int armRelaysSignal;

//Temparature Sensor DHT
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11 
int t;
int h;

DHT dht(DHTPIN, DHTTYPE);

//Data transmission

String c = ",";
String d= "D";
int r1Out;
int r2Out;
String data2;


void setup(){
  Serial.begin(9600);
 
//Relay configuration

  pinMode(relay1,OUTPUT);
  pinMode(relay2,OUTPUT);
  pinMode(armRelays,OUTPUT);
  pinMode(armRelaySig,INPUT);
  pinMode(relay1Sig,INPUT);
  pinMode(relay2Sig,INPUT);
  pinMode(relayRadioInput1,INPUT);
  pinMode(relayRadioInput2,INPUT);
  digitalWrite(armRelays,LOW);  
  digitalWrite(relay1,LOW);
  digitalWrite(relay2,LOW);

//Temperature sensor
   dht.begin();


//Data transmission
Serial1.begin(9600);
}

void loop(){
//Relay configuration
  armRelaysSignal =digitalRead(armRelaySig);
  relay1SignalPinM1 = digitalRead(relay1Sig);
  relay2SignalPinM1 = digitalRead(relay2Sig);
  relayRadioSignal1= digitalRead(relayRadioInput1);
  relayRadioSignal2= digitalRead(relayRadioInput2);
  
  if (armRelaysSignal==HIGH){
      digitalWrite(armRelays,HIGH);

      
      if((relay1SignalPinM1==HIGH&& armRelaysSignal==HIGH) || (relayRadioSignal1==HIGH&& armRelaysSignal==HIGH)){
        digitalWrite(relay1,HIGH);  
      } else {
        digitalWrite(relay1,LOW);
      }
      if((relay2SignalPinM1==HIGH&& armRelaysSignal==HIGH)  || (relayRadioSignal2==HIGH&& armRelaysSignal==HIGH) ){
        digitalWrite(relay2,HIGH);
      }else{
        digitalWrite(relay2,LOW);
      }
  } else {
    digitalWrite(armRelays,LOW);  
    digitalWrite(relay1,LOW);
    digitalWrite(relay2,LOW);  
  }
  
//Temperature sensor

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to A0 seconds 'old' (its a very slow sensor)
    h = dht.readHumidity();
    t = dht.readTemperature();

    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h))
    {
        Serial.println("Failed to read from DHT");
    }
    else
    {

    }
  
//Sensor serial output  

  r1Out = digitalRead(relay1);
  r2Out = digitalRead(relay2);
  [b]data2 = d + armRelaysSignal + c + relay1SignalPinM1 + c + relay2SignalPinM1 + c +  relayRadioSignal1 + c + relayRadioSignal2 + c +r1Out + c + r2Out + c + t + c + h;
  Serial1.println(data2); //OK to be replaced by data2[/b]
 
  delay(50);

  Serial.print("Arm Sig=");
  Serial.print(digitalRead(armRelaySig));
  Serial.print(" Relay1Sig=");
  Serial.print(digitalRead(relay1Sig));
  Serial.print(" Relay2Sig=");
  Serial.print(digitalRead(relay2Sig));
  Serial.print(" Relay1RadioSig=");
  Serial.print(digitalRead(relayRadioInput1));
  Serial.print(" Relay2RadioSig=");
  Serial.print(digitalRead(relayRadioInput2));
  Serial.print(" Relay1State=");
  Serial.print(digitalRead(relay1));
  Serial.print(" Relay2State=");
  Serial.print(digitalRead(relay2));
  Serial.print(" Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print(" Temperature: ");
  Serial.print(t);
  Serial.println(" *C");

}


User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Micropython and XBEE

Post by dhylands » Wed Jul 09, 2014 10:33 pm

I think that reading a single character at a time until you hit '\n' (note that \n is a newline '/n' is a two character sequence of a slash character followed by the letter n) is the way to go (or at least all that's available today).

CRImier
Posts: 11
Joined: Fri Aug 01, 2014 10:40 pm
Location: Latvia
Contact:

Re: Micropython and XBEE

Post by CRImier » Fri Aug 01, 2014 11:38 pm

It's not really a problem of MicroPython - it's not adding "\n" to the received data, it's the Arduino that sends "\n" when you use println() instead of just print () =) And IMO parsing with Python is much more easier - so it shouldn't be a problem. No, there's no better idea, idea of waiting for "/n" at the end of the buffer is already the best - and I'm pretty sure it will be easy to implement.

Wait, isn't it like there's no Serial.readline() on Arduino either and technically you'll do just the same you'd do on Arduino?

CrimsonZA
Posts: 4
Joined: Sun Jul 13, 2014 4:14 pm

Re: Micropython and XBEE

Post by CrimsonZA » Sat Aug 02, 2014 7:30 pm

I'd like to know how you connected the XBEE to the Micropython board.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Micropython and XBEE

Post by schvic » Fri Aug 08, 2014 1:55 pm

dhylands wrote:I think that reading a single character at a time until you hit '\n' (note that \n is a newline '/n' is a two character sequence of a slash character followed by the letter n) is the way to go (or at least all that's available today).
Cheers :)
CRImier wrote:Wait, isn't it like there's no Serial.readline() on Arduino either and technically you'll do just the same you'd do on Arduino?
I am just sending messages to the pyboard for now. I will soon try to send messages to my arduino but I am concidering that I will get another pyboard to replace my arduino since I need more processing power and there could be more interesting implementations if I use a pyboard.

And yes, parsing with python is much more easy than parsing in C.

CrimsonZA wrote:I'd like to know how you connected the XBEE to the Micropython board.
I connected the TX pin of the XBEE pin to X10 and the RX pin to X9. You have various UART ports so you can connectec your XBEE pins to anyone of them.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Micropython and XBEE

Post by dhylands » Fri Aug 08, 2014 3:47 pm

schvic wrote:
dhylands wrote:I connected the TX pin of the XBEE pin to X10 and the RX pin to X9. You have various UART ports so you can connectec your XBEE pins to anyone of them.
You also need to make sure that you connect ground from the XBEE to ground on the micropython board.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Micropython and XBEE

Post by schvic » Fri Aug 08, 2014 5:24 pm

dhylands wrote: You also need to make sure that you connect ground from the XBEE to ground on the micropython board.
Sure, the XBEE is supplied by the pyboard.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Micropython and XBEE

Post by dhylands » Fri Aug 08, 2014 7:29 pm

I've helped quite a number of people who are relatively new to microcontrollers who connect up all of the "important" signals and forget about ground, which is the only reason I mentioned it. For people who are famlliar with electronics, its fairly obvious, but it trip you up if you're new.

schvic
Posts: 15
Joined: Wed Jul 09, 2014 2:45 pm

Re: Micropython and XBEE

Post by schvic » Fri Aug 08, 2014 9:31 pm

You're right to mention it. I am new to sharing in forums and I am not aware of the amount of information to give in order to explain clearly everything I want to transmit.

Cheers for your support.

CrimsonZA
Posts: 4
Joined: Sun Jul 13, 2014 4:14 pm

Re: Micropython and XBEE

Post by CrimsonZA » Sat Aug 09, 2014 11:36 am

dhylands wrote:I've helped quite a number of people who are relatively new to microcontrollers who connect up all of the "important" signals and forget about ground, which is the only reason I mentioned it. For people who are famlliar with electronics, its fairly obvious, but it trip you up if you're new.
Your contributions to new users are highly appreciated!
schvic wrote:You're right to mention it. I am new to sharing in forums and I am not aware of the amount of information to give in order to explain clearly everything I want to transmit.

Cheers for your support.
Every little bit helps.
I was considering getting the XBEE Series 2 at first but I had a look at the BTBEE PRO and it was quite a bit cheaper so I went with that instead. I'm not sure it has TX and RX pins yet.

Post Reply