I can successfully communicate between my RPi and an Arduino Nano via i2c Serial to turn on an LED connected to the Nano from a Python script running on the RPi.
I have can successfully control a couple of servos connected to the RPi GPIO using WebIOPi but cannot control anyting connected to the Nano.
I now want to be able to do this from a web page using WebIOPi, this is where I am now stuck.
The problem appears to be that my working code uses the Python 2 'smbus' library which is not available in WebIOPi?
Here's my python code:
Code: Select all
# Prior to running this program you must start the pigpio daemon by
# entering 'sudo pigpiod' at the command prompt
# To start webiopi enter 'sudo webiopi -d -c /etc/webiopi/config' at the command prompt
#!/usr/bin/env python
import webiopi
import smbus
import time
import pigpio
pi = pigpio.pi()
bus = smbus.SMBus(1)
address = 0x04
def writeNumber(value):
bus.write_byte(address, value)
# bus.write_byte_data(address, 0, value)
return -1
def readNumber():
number = bus.read_byte(address)
# number = bus.read_byte_data(address, 1)
return number
# Start the webiopi server
# server = webiopi.Server(port=8000, login="Hammerstein", password="Hammerstein"
# Define functions
#@webiopi.macro
def cam_pan_left():
pi.set_servo_pulsewidth(23, 1900)
time.sleep(1)
pi.set_servo_pulsewidth(23, 0)
#@webiopi.macro
def cam_pan_right():
pi.set_servo_pulsewidth(23, 1100)
time.sleep(1)
pi.set_servo_pulsewidth(23, 0)
#@webiopi.macro
def cam_pan_centre():
pi.set_servo_pulsewidth(23, 1500)
time.sleep(1)
pi.set_servo_pulsewidth(23, 0)
#@webiopi.macro
def cam_tilt_up():
pi.set_servo_pulsewidth(24, 1100)
time.sleep(1)
pi.set_servo_pulsewidth(24, 0)
#@webiopi.macro
def cam_tilt_down():
pi.set_servo_pulsewidth(24, 1900)
time.sleep(1)
pi.set_servo_pulsewidth(24, 0)
#@webiopi.macro
def cam_tilt_centre():
pi.set_servo_pulsewidth(24, 1500)
time.sleep(1)
pi.set_servo_pulsewidth(24, 0)
#@webiopi.macro
def cam_stop():
pi.set_servo_pulsewidth(23, 0)
pi.set_servo_pulsewidth(24, 0)
#@webiopi.macro
def led_on():
number = 1
writeNumber(number)
time.sleep(1)
#@webiopi.macro
def led_off():
number = 0
writeNumber(number)
time.sleep(1)
# Start the webiopi server
server = webiopi.Server(port=8000, login="Hammerstein", password="Hammerstein")
# Register the macros
server.addMacro(cam_pan_left)
server.addMacro(cam_pan_right)
server.addMacro(cam_pan_centre)
server.addMacro(cam_tilt_up)
server.addMacro(cam_tilt_down)
server.addMacro(cam_tilt_centre)
server.addMacro(cam_stop)
server.addMacro(led_on)
server.addMacro(led_off)
# Run default loop
webiopi.runLoop()
while True:
var = input("Enter 1 - 9: ")
if not var:
continue
writeNumber(var)
print("RPI: Hi Arduino, I sent you ", var)
# sleep one second
time.sleep(1)
number = readNumber()
print("Arduino: Hey RPI, I received a digit ", number)
print()
# Cleanly stop server
server.stop()
Code: Select all
// Arduino Pin A5 to RPi SDA
// Arduino Pin A6 to RPi SCL
// Inspiration from Ocra Laing's Blog http://blog.oscarliang.net/raspberry-pi-arduino-connected-i2c
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // Start serial for output
Wire.begin(SLAVE_ADDRESS); // Initialise i2c as slave
// Define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
Serial.println("Ready!");
}
void loop() {
delay(100);
}
// Callback for received data
void receiveData(int byteCount) {
while(Wire.available()) {
number = Wire.read();
Serial.println("data received: ");
Serial.println(number);
if (number == 1){
if (state == 0){
digitalWrite(13, HIGH); // turn the LED on
state = 1;
}
else{
digitalWrite(13, LOW); // turn the LED off
state = 0;
}
}
}
}
// Callback for sending data
void sendData() {
Wire.write(number);The Nano code is only configured to switch on an LED connected to pin 13 of the Nano for now.
