Moteino RH_RF95 to Yun data logger

Started by ghowe, May 16, 2016, 12:43:19 AM

ghowe

So I saw the post someone asked a while back about connecting the Moteino to the Yun and the recommendation to use SPI, but I am not following the recommendation.

Has anyone else out there used the Yun to interface with the Moteino? 

I am looking to pass the data packet from the Gateway Moteino to the sqlite database on the Yun, to then batch upload to a web server.  I have several Yun's already and would like to make this work instead of buying another board such as the Raspi.

Any help on how to interact the two would be greatly appreciated.

Thanks,

ghowe

I was able to find a solution that works for me.  In case anyone else is working on a similar project, below is my code solutions for both the Moteino Gateway with FTDI USB board and Arduino Yun.

Moteino/Arduino script:

#include <SPI.h>
#include <RH_RF95.h>
#include <LowPower.h>

#define FREQUENCY  	  915
#define LED           9 		// Moteinos have LEDs on D9
#define FLASH_SS      8 		// and FLASH SS on D8
#define BAUD_RATE     115200

// Singleton instance of the radio driver
RH_RF95 rf95;

//#define SERIAL_EN             // Uncomment when debugging
#ifdef  SERIAL_EN
#define DEBUG(input)   {Serial.print(input); delay(5);}
#define DEBUGln(input) {Serial.println(input); delay(5);}
#else
#define DEBUG(input);
#define DEBUGln(input);
#endif

#define DEVICE    "Gateway"

void setup() 
{
  pinMode(LED, OUTPUT);
  Serial.begin(BAUD_RATE);
  if (!rf95.init())
  {
    DEBUGln("init failed");  
  }else 
  { 
    DEBUG(DEVICE); DEBUG("_initialized- "); DEBUG(FREQUENCY); DEBUGln("mhz"); 
  }
  	rf95.setFrequency(FREQUENCY);
}

void loop()
{
  if (rf95.available())
  {
    // Should be a message for us now   
    uint8_t buf[50];
    uint8_t len = sizeof(buf);
    memset(buf, '\0', sizeof(buf));

    if (rf95.recv(buf, &len))
    {
      digitalWrite(LED, HIGH);
      //RH_RF95::printBuffer("request: ", buf, len);
      DEBUG("got request: ");
      Serial.println((char*)buf);
      DEBUG("RSSI: ");
      DEBUGln((rf95.lastRssi(), DEC));
      
      digitalWrite(LED, LOW);
    }
  }
}



Python script for Yun (note the packages/drivers that also need to be installed):

#!/usr/bin/python
#--This code simply records the values from the sensor.

#--Serial FTDI driver for Yun
#--opkg update
#--opkg install kmod-usb-serial-ftdi
#--opkg install pyserial

import serial
import sqlite3 as sqlite
#import sys

#serial = serial.Serial("/dev/ttyUSB0", baudrate=115200, )
serial = serial.Serial('/dev/ttyUSB0', 115200, timeout=3) # Open the serial port at 115200

code = ''

while True:
	data = serial.read()
	
	if data == '\r':
		print(data)
		(strdevice,strcount) = code.split(',',1)
		device = strdevice
		count = int(strcount)
		con = sqlite.connect('/mnt/sda1/sensor_flow_moteino.db')
		cur = con.cursor()
		cur.execute('''INSERT INTO data_log (device,count) VALUES(?,?)''',(device,count))
		con.commit()
		con.close()
		print(code)
		code = ''
        
	else:
		code = code + data