Author Topic: need help with moteino + raspberry PI  (Read 1722 times)

seany

  • NewMember
  • *
  • Posts: 5
need help with moteino + raspberry PI
« on: December 09, 2018, 05:21:37 PM »
I am trying to dump some information from Raspberry Pi to a moteino USB.

My code on the raspberry side :


import serial
from time import sleep

Code: [Select]
ser = serial.Serial ("/dev/ttyS0", 115200)    #Open port with baud rate
while True:
    ser.write(b'\x43\x44\x45\x46')                #transmit data serially

this works, uart is enabled, checked with uart loopback.

Now, on moteino side : I have:

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial portTwo(8,9);

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

  portTwo.begin(115200);
}
void loop()
{

delay(2000);
  portTwo.listen();
  Serial.println("Data from port two:");
  while (portTwo.available() > 0) {
    char inByte = portTwo.read();
    Serial.write(inByte);
  }

  Serial.println();
}


but i have no response from the moteino
What is going on? please help

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: need help with moteino + raspberry PI
« Reply #1 on: December 09, 2018, 05:36:47 PM »
Just to clarify, you have the Raspberry Pi GPIO pins assigned to its UART connected to pins 8 & 9 of the Moteino USB and you're trying to send data from the Pi to the Moteino.  Is this correct?

If this is correct, then the main problem I see is trying to use 115200 baud on software serial driver.  It ain't gonna happen.  I believe the upper limit may be as low as 19200.

The second potential problem I see is that pins 8 & 9 on Moteino are connected to the SS of the Flash chip and LED respectively.  It's a potential problem because it depends on whether you've got Flash chip installed and whether pin 9 is used as RX or TX from the Moteino.  TX should work - it'll flash the LED, but maybe you want that.

What is the Serial port (USB) on the Moteino connected to?

« Last Edit: December 09, 2018, 05:40:50 PM by TomWS »

seany

  • NewMember
  • *
  • Posts: 5
Re: need help with moteino + raspberry PI
« Reply #2 on: December 09, 2018, 05:44:32 PM »
yes, this is correct - i tested from 9600 to 192000.
nothing happens

it is connected - to another pc over COM4
and raspberry has ttys0 as the uart pins (8 and 10 gpio)

so.
raspberry ----> moteino ----> PC

i also tested pin 0 and 1. didnt work.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: need help with moteino + raspberry PI
« Reply #3 on: December 09, 2018, 05:57:48 PM »
yes, this is correct - i tested from 9600 to 192000.
nothing happens

it is connected - to another pc over COM4
and raspberry has ttys0 as the uart pins (8 and 10 gpio)

so.
raspberry ----> moteino ----> PC

i also tested pin 0 and 1. didnt work.
Pins 0  and 1 are the HW serial  port on Moteino, so that won't work.  Pins 6 & 7 should be free, however.  A quick look around the web I see claims that SW Serial should work up to 38600, but, frankly, for reliable communications, I suggest 19200.

The other thing is that you're not metering the sends on the Pi side.  You have a continuous loop that is running full speed at filling up the transmit buffer.   Try a delay in the loop until you get the basic comms sorted out.

Finally, from the SoftwareSerial reference, the examples show the setup code using pinmode to configure the RX & TX pins as INPUT & OUTPUT respectively.  Apparently the library doesn't do this (odd behavior that).

Tom
« Last Edit: December 09, 2018, 06:24:02 PM by TomWS »

seany

  • NewMember
  • *
  • Posts: 5
Re: need help with moteino + raspberry PI
« Reply #4 on: December 10, 2018, 07:10:12 AM »
So final status :

Setup

I have an Raspberry pi 3 . The pin 8 UART tx is connected to a moteino pin 7 , and pin 10 UART rx is to moteino pin 6. Power is delivered by a PC usb. ttyAMA0 is removed from getty, and serial consol is turned off.

So i have :

Pi --- uart --> moteino --- usb --> pc. Moteino can be programmed with PC (arduino IDE) and a serial monitor can be opened.

Code on Raspberry
Code: [Select]
import serial
from time import sleep
ser = serial.Serial ("/dev/ttyS0", 19200)    #Open port with baud rate
while True:
  sleep(2)
  ser.flushOutput()
  ser.write(bytes([0x00]))
  sleep(0.1)
  ser.write(bytes([0x41]))
  sleep(0.1)
  ser.write(bytes([0x00]))
  sleep(0.1)
  ser.write(bytes([0x42]))
  sleep(0.1)
  ser.write(bytes([0x00]))
  sleep(0.1)
  ser.write(bytes([0x43]))
  sleep(0.1)
  ser.write(bytes([0x00]))
  sleep(0.1)
  ser.write(bytes([0x44]))
  sleep(0.1)
  ser.write(bytes([0x00]))


Note the
Code: [Select]
ser.write(bytes([0x00]))
part.

Code on Ardunio
Code: [Select]
#include <SoftwareSerial.h>
#define rxPin 7
#define txPin 6

// set up a new serial port
SoftwareSerial port =  SoftwareSerial(rxPin, txPin);

void setup()  {
// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// set the data rate for the SoftwareSerial port
port.begin(19200);
Serial.begin(9600);
}

void loop() {
  delay(500);
   if (port.available()>0){
    port.read();
    char inByte = port.read();
    Serial.write(inByte);
    Serial.write('_');
  }

 }

**Behavior**

The loopback test shows everything fine - no drop of packets. The Arduino is dropping every odd byte - producing :
Code: [Select]
A_
B_
C_
D_
⸮_
⸮_

If I remove the ser.write(bytes([0x00])) , then I only see :

Code: [Select]
B_
D_
⸮_

on the console monitor.

**Expected Behavior**

No drop of bytes. I would expect, that if i remove the 0 bytes - then not only B and D - but A and C as well should be visible.

please help.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: need help with moteino + raspberry PI
« Reply #5 on: December 10, 2018, 08:51:10 AM »
In your loop, the first port.read() is dumping a character, it's not saved, and therefore lost.

Also, I would make the moteino loop delay shorter than the delay you have between writes in the Pi code.  In general, you want your receiver to gobble faster than your transmitter.

I think your original code in the Pi was ok other than the fact that you were not delaying between writes, so:
Code: [Select]
import serial
from time import sleep
ser = serial.Serial ("/dev/ttyS0", 19200)    #Open port with baud rate
while True:
    ser.write(b'\x43\x44\x45\x46')                #transmit data serially
    sleep(2.5)                                              # time for 4 byte read plus 'extra'

Also, given that the Moteino loop checks to see if anything is available, the delay in that loop is unnecessary.  Reads (and debug outputs) will be synchronized to writes.




« Last Edit: December 10, 2018, 10:19:57 AM by TomWS »

seany

  • NewMember
  • *
  • Posts: 5
Re: need help with moteino + raspberry PI
« Reply #6 on: December 10, 2018, 10:11:24 AM »
Okey, thank you!

Code: [Select]



#include <SoftwareSerial.h>
#include <RFM69.h>
#include <SPI.h>
#include <SPIFlash.h>

#define NODEID      99
#define NETWORKID   100
#define GATEWAYID   1
#define FREQUENCY   RF69_433MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY         "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 115200
#define ACK_TIME    30  // # of ms to wait for an ack
#define IS_RFM69HW_HCW  //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!

int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms)
byte sendSize=0;
boolean requestACK = false;
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
RFM69 radio;

typedef struct {
  int           nodeId; //store this nodeId
  unsigned long uptime; //uptime in ms
  byte        px[2];   //temperature maybe?
} Payload;
Payload theData;



#define rxPin 7
#define txPin 6

// set up a new serial port
SoftwareSerial port =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  port.begin(9600);
  Serial.begin(9600);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW_HCW
  radio.setHighPower(); //must include this only for RFM69HW/HCW!
#endif
  radio.encrypt(KEY);
  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
 
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

long lastPeriod = -1;
int cnt = 0;
void loop() {
     // delay(500);
    if (port.available()>0){
      Serial.println("hello");
     
    String s = "";
   
    while( port.available()) {
    delay(100);
    char inByte = port.read();
    Serial.println(inByte,HEX);
    s = s + inByte;
    theData.px[cnt] = inByte;
    cnt = cnt+1;
    if (cnt = 2){ Serial.print("breaking"); cnt = 0; break;}
    }
    //Serial.write(s);
    Serial.println('SOFAR');

      s = "";
      theData.nodeId = NODEID;
    theData.uptime = millis();
    Serial.print("Sending struct (");
    Serial.print(sizeof(theData));
    Serial.print(" bytes) ... ");
    radio.send(GATEWAYID, (const void*)(&theData), sizeof(theData));
      Serial.print(" ok!");
   
Serial.println();
   
    }
   
 }

This is, again sending nothing. please help.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: need help with moteino + raspberry PI
« Reply #7 on: December 10, 2018, 10:45:58 AM »
Your code, annotated with comments beginning with 'TWS'

Code: [Select]
#include <SoftwareSerial.h>
#include <RFM69.h>
#include <SPI.h>
#include <SPIFlash.h>

#define NODEID      99
#define NETWORKID   100
#define GATEWAYID   1
#define FREQUENCY   RF69_433MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ) // TWS: Is your radio really 433MHz????
#define KEY         "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define LED         9
#define SERIAL_BAUD 115200    // TWS: Note that this is not used anywhere
#define ACK_TIME    30  // # of ms to wait for an ack  // TWS Note this is not used anywhere
#define IS_RFM69HW_HCW  //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!

int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms)   // TWS: This is not used anywhere
byte sendSize=0;                                         // TWS: This is not used any where
boolean requestACK = false;                       // TWS: This is not used any where
SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip
RFM69 radio;

typedef struct {
  int           nodeId; //store this nodeId                             
  unsigned long uptime; //uptime in ms                         
  byte        px[2];   //temperature maybe?
} Payload;
Payload theData;



#define rxPin 7
#define txPin 6

// set up a new serial port
SoftwareSerial port =  SoftwareSerial(rxPin, txPin); 

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  port.begin(9600);                                                    // TWS: This baud rate is different than your previous examples, did you change the Pi code?
  Serial.begin(9600);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW_HCW
  radio.setHighPower(); //must include this only for RFM69HW/HCW!
#endif
  radio.encrypt(KEY);
  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
 
  if (flash.initialize())
    Serial.println("SPI Flash Init OK!");
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

long lastPeriod = -1;            // TWS: This is not used anywhere
int cnt = 0;
void loop() {
     // delay(500);
    if (port.available()>0){
      Serial.println("hello");
     
    String s = "";
   
    while( port.available()) {
    delay(100);
    char inByte = port.read();
    Serial.println(inByte,HEX);
    s = s + inByte;
    theData.px[cnt] = inByte;
    cnt = cnt+1;
    if (cnt = 2){ Serial.print("breaking"); cnt = 0; break;}
    }
    //Serial.write(s);
    Serial.println('SOFAR');

      s = "";
      theData.nodeId = NODEID;
    theData.uptime = millis();
    Serial.print("Sending struct (");
    Serial.print(sizeof(theData));
    Serial.print(" bytes) ... ");
    radio.send(GATEWAYID, (const void*)(&theData), sizeof(theData));  // TWS: I would recommend using radio.sendWithRetry()
      Serial.print(" ok!");
   
Serial.println();
   
    }
 }

seany

  • NewMember
  • *
  • Posts: 5
Re: need help with moteino + raspberry PI
« Reply #8 on: December 10, 2018, 11:30:18 AM »
Yes, baud in pi is also changed.

i should have mentioned that the serial monitor is capable of showing that the signal is being read.
but radio.send is sending nothing