HomeGenie Forum
Automation Program Plugins and Wizard Scripting => Raspberry Pi GPIO/SPI/I2C => Topic started by: tomasz2101 on November 29, 2015, 10:01:15 AM
-
Hi all
I'm new user and I want to connect Raspberry Pi (master) with Arduino (slave) over UART or I2C. I don't know which method is better. With UART communication i need to connect them as in the picture 1.
With I2C it doesn't need level shifter if arduino works as slave but i don't know if there will be others I2C devices working on 5V (because rPi works on 3,3 V).
Raspberry should send and receive packages from arduino. In HG i want to set up few on/off switches, the rPi will sent info to arduino and it will turn it on or off and send info back to rPi.
Few days ago i wrote program for two arduinos master/slave in I2C and i'm sending info over array. Can anyone say if it will be helpful ?
Can anyone help me ?? Please
-
I tested using an Arduino with RPi around a year ago. I was able to get some versions of the Arduino to connect to the RPi and even had HG communicating and able to upload code. In my case, I was using the serial connection (UART). I don't remember all the details of how to get things working at this point, but know it did work at least to some degree.
In the end, I opted to use an Arduino Uno with Ethernet shield to work independently and then communicate the collected date to HG via MQTT. I used Mosquitto on the RPi as I was never able to get the HG MQTT broker to work.
If you would like details, please review the threads here as I posted all details from my work. I'll try to find the specific thread and post a link.
EDIT: This is the thread
http://www.homegenie.it/forum/index.php?topic=835 (http://www.homegenie.it/forum/index.php?topic=835)
-
I'm trying to connect RPI to Arduino over I2C with very simply program in python (i would prefer C# but I don't know how to write it):
import smbus
import time
bus = smbus.SMBus(1)
address = 0x08
while True:
data = ""
for i in range(0, 5):
data += chr(bus.read_byte(address));
print data
time.sleep(1);
And I' getting error in import smbus. I've installed sudo apt-get install python-smbus. Can anyone help me with it ?
-
try adding these lines before everything else:
import sys
sys.path.append("/usr/lib/python2.7/")
sys.path.append("/usr/lib/python2.7/dist-packages")
#sys.path.append("/usr/lib/python2.7/dist-packages/smbus")
not sure about the last one. Also read this:
http://www.homegenie.it/forum/index.php?topic=1062.msg6411#msg6411 (http://www.homegenie.it/forum/index.php?topic=1062.msg6411#msg6411)
Cheers,
g.
-
Btw, there's EdenV2 firmware source code for Arduino and the HG app (C#). This is an example of exchanging messages over SPI between RPi and Arduino.
https://sourceforge.net/projects/homegenie/files/testing/
g.
-
I found that you posted this program for SPI communication:
var adcClock = ConnectorPin.P1Pin23.ToProcessor();
var adcMiso = ConnectorPin.P1Pin21.ToProcessor();
var adcMosi = ConnectorPin.P1Pin19.ToProcessor();
var adcCs = ConnectorPin.P1Pin22.ToProcessor();
var spiConnection = new Raspberry.IO.SerialPeripheralInterface.SpiConnection(adcClock, adcCs, adcMiso, adcMosi, 0 /*Endianness.LittleEndian*/);
using(spiConnection.SelectSlave())
{
// Start bit
spiConnection.Write(true);
// Channel is single-ended
spiConnection.Write(true);
// Channel Id
spiConnection.Write((byte)2, 3);
// Let one clock to sample
spiConnection.Synchronize();
while (true)
{
// Read 8 bits
for (int i = 0; i < 8; i++)
{
var data = spiConnection.Read(8);
Console.Write( data.ToString("X2") + " ");
}
Console.WriteLine("");
}
}
But it gives me those errors:
Line 9, Column 21 (Code):
The best overloaded method match for `Raspberry.IO.SerialPeripheralInterface.SpiConnection.SpiConnection(Raspberry.IO.IOutputBinaryPin, Raspberry.IO.IOutputBinaryPin, Raspberry.IO.IInputBinaryPin, Raspberry.IO.IOutputBinaryPin, Raspberry.IO.SerialPeripheralInterface.Endianness)' has some invalid arguments
/usr/local/bin/homegenie/Raspberry.IO.SerialPeripheralInterface.dll (Location of the symbol related to previous error)
Line 9, Column 78 (Code):
Argument `#1' cannot convert `Raspberry.IO.GeneralPurpose.ProcessorPin' expression to type `Raspberry.IO.IOutputBinaryPin'
-
With time the SDK changed a little bit.
This is the code that should work:
var spiClock = ConnectorPin.P1Pin23.ToProcessor();
var spiMiso = ConnectorPin.P1Pin21.ToProcessor();
var spiMosi = ConnectorPin.P1Pin19.ToProcessor();
var spiCs = ConnectorPin.P1Pin24.ToProcessor();
var driver = GpioConnectionSettings.DefaultDriver;
driver.Allocate(spiClock, PinDirection.Output);
driver.Allocate(spiCs, PinDirection.Output);
driver.Allocate(spiMosi, PinDirection.Output);
driver.Allocate(spiMiso, PinDirection.Input);
var spiConnection = new SpiConnection(
driver.Out(spiClock),
driver.Out(spiCs),
driver.In(spiMiso),
driver.Out(spiMosi),
Endianness.LittleEndian
);
// Release allocated resource when program is stopping
When.ProgramStopping(()=>{
try
{
((IDisposable)spiConnection).Dispose();
} catch (Exception e) { Program.Notify("SPI Error", e.Message); }
((IDisposable)driver).Dispose();
return true;
});
For your reference you can look at a bunch of example programs in the Configure->Programs->RaspberryPi section.
Also see the RaspberrySharp-IO examples and source code that is located here:
https://github.com/raspberry-sharp/raspberry-sharp-io/tree/master/Tests
Cheers,
g.
-
Ok it doesn't give me errors but i think i need to add to your code the last part of the previous code yes ? it will look like this :
var spiClock = ConnectorPin.P1Pin23.ToProcessor();
var spiMiso = ConnectorPin.P1Pin21.ToProcessor();
var spiMosi = ConnectorPin.P1Pin19.ToProcessor();
var spiCs = ConnectorPin.P1Pin24.ToProcessor();
var driver = GpioConnectionSettings.DefaultDriver;
driver.Allocate(spiClock, PinDirection.Output);
driver.Allocate(spiCs, PinDirection.Output);
driver.Allocate(spiMosi, PinDirection.Output);
driver.Allocate(spiMiso, PinDirection.Input);
var spiConnection = new SpiConnection(
driver.Out(spiClock),
driver.Out(spiCs),
driver.In(spiMiso),
driver.Out(spiMosi),
Endianness.LittleEndian
);
// Release allocated resource when program is stopping
When.ProgramStopping(()=>{
try
{
((IDisposable)spiConnection).Dispose();
} catch (Exception e) { Program.Notify("SPI Error", e.Message); }
((IDisposable)driver).Dispose();
return true;
});
using(spiConnection.SelectSlave())
{
// Start bit
spiConnection.Write(true);
// Channel is single-ended
spiConnection.Write(true);
// Channel Id
spiConnection.Write((byte)2, 3);
// Let one clock to sample
spiConnection.Synchronize();
while (true)
{
// Read 8 bits
for (int i = 0; i < 8; i++)
{
var data = spiConnection.Read(8);
Console.Write( data.ToString("X2") + " ");
}
Console.WriteLine("");
}
}
With function spiConnection.Read program want to receive data and print it with Console.Write yes ??
Sry for stupid questions but everything is need for me here.