Need help: Android with MIT app, to Raspberry Pi, Python, Waveshare servo HAT to 16 servo

My project: Use MIT App Inventor on my Android phone (Currently Samsung Galaxy S9+ with Android version 10 but need it to run on any android); Send text to a Raspberry Pi (currently using a Pi4 but will be switching to Pi Zero W because I want to reduce the size); on top of the Pi 40 pin GPIO is a Waveshare servo driver hat (Which controls 16 servos, (I'm currently using the Waveshare sample Python code to get the 16 servos rotating, with minimal understanding of Python code.))

So the MIT app needs to send text such as S1F for rotating servo number one to the full +180 degrees (clockwise), S1N to turn it to the mid position; and S1B to rotate it to the opposite -180 degrees.

So 48 different text, to Python on the Pi, and the Waveshare hat turns the servos.

Now there are several video tutorials showing similar workings with an Arduino. (Credit to Binary Updates for there video tutorial turning an LED on and off, this is where I got started)

But...
I have not seen this working with a Raspberry Pi.

Seems everyone is getting error code: 507 unable to connect. Is the device turned on?

I have spent countless hours building an MIT app, with tons of bells and whistles, but I can't get it to communicate with the Pi.


Can someone help me get this project working?
Sample code,
Tutorials,
(willing to pay for either as long as I can get this working)

  1. It's not an appinventor bug, but a programming bug, so bad department.
  2. The error number indicates that you want to connect via bluetooth, which you didn't mention.
  3. Which BT module are you using.
  4. Show the blocks used to connect to BT.

Hello Eric

Your best move is to make just a tiny project with the sole aim of getting your App and Pi comms working. There are probably several reasons that the Arduino is more popular than Pi, one of them undoubtedly is the size.

Now, you say you need to "send text" (commands) - but you don't say how. The most popular way is via Bluetooth. Does your Pi (Pi4 and Pi zero) have bluetooth built-in? Yes they do, they have BLE. So your App needs to use the latest MIT BLE extension. Is that what you have been trying to use? When you say you can't get your App to communicate, where in the process does it actually fail? Code 507 = Unable to connect:

  1. Ensure the Pi is switched on and it's BLE is activated.
  2. On your phone, ensure Bluetooth is on and Google Fine Locate is on too.

You will find MIT BLE Extension 20200828 on my website, along with a lot of other useful info.
https://www.professorcad.co.uk/appinventortips#TipsBluetooth
https://www.professorcad.co.uk/appinventortips#TipsRaspberryPi

I went with the MIT app because the Binary Update video seemed simple enough. Simple in that it was NOT technical, and to be honest, I don't want to spend weeks figuring this out.

Hi Eric

You are using "Classic" Bluetooth when your Pi is a BLE device. Hence your issue.

Attached is a basic send text project. For it to be able to send it's test text, you will need to replace the UUIDs with those of your Pi.

We probably need to see your Python code (file) that receives the text from the App, unless you are 100% sure it is already correct.

BLE_BASIC_SEND_TEXT.aia (195.6 KB)
In this project, buttons become available as each stage of the connection process is completed.

A few years ago you could connect an app with App Inventor with RBP using classic Bluetooth.
Setting up Bluetooth on Raspberry can be tricky.
http://kio4.com/raspberry/31_bluetooth.htm (it is in Spanish)

Here other example:

All this is becoming too technical

This is the Waveshare Python code

#!/usr/bin/python

import time
import math
import smbus

============================================================================

Raspi PCA9685 16-Channel PWM Servo Driver

============================================================================

class PCA9685:

Registers/etc.

__SUBADR1 = 0x02
__SUBADR2 = 0x03
__SUBADR3 = 0x04
__MODE1 = 0x00
__PRESCALE = 0xFE
__LED0_ON_L = 0x06
__LED0_ON_H = 0x07
__LED0_OFF_L = 0x08
__LED0_OFF_H = 0x09
__ALLLED_ON_L = 0xFA
__ALLLED_ON_H = 0xFB
__ALLLED_OFF_L = 0xFC
__ALLLED_OFF_H = 0xFD

def init(self, address=0x40, debug=False):
self.bus = smbus.SMBus(1)
self.address = address
self.debug = debug
if (self.debug):
print("Reseting PCA9685")
self.write(self.__MODE1, 0x00)

def write(self, reg, value):
"Writes an 8-bit value to the specified register/address"
self.bus.write_byte_data(self.address, reg, value)
if (self.debug):
print("I2C: Write 0x%02X to register 0x%02X" % (value, reg))

def read(self, reg):
"Read an unsigned byte from the I2C device"
result = self.bus.read_byte_data(self.address, reg)
if (self.debug):
print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg))
return result

def setPWMFreq(self, freq):
"Sets the PWM frequency"
prescaleval = 25000000.0 # 25MHz
prescaleval /= 4096.0 # 12-bit
prescaleval /= float(freq)
prescaleval -= 1.0
if (self.debug):
print("Setting PWM frequency to %d Hz" % freq)
print("Estimated pre-scale: %d" % prescaleval)
prescale = math.floor(prescaleval + 0.5)
if (self.debug):
print("Final pre-scale: %d" % prescale)

oldmode = self.read(self.__MODE1);
newmode = (oldmode & 0x7F) | 0x10        # sleep
self.write(self.__MODE1, newmode)        # go to sleep
self.write(self.__PRESCALE, int(math.floor(prescale)))
self.write(self.__MODE1, oldmode)
time.sleep(0.005)
self.write(self.__MODE1, oldmode | 0x80)

def setPWM(self, channel, on, off):
"Sets a single PWM channel"
self.write(self.__LED0_ON_L+4channel, on & 0xFF)
self.write(self.__LED0_ON_H+4
channel, on >> 8)
self.write(self.__LED0_OFF_L+4channel, off & 0xFF)
self.write(self.__LED0_OFF_H+4
channel, off >> 8)
if (self.debug):
print("channel: %d LED_ON: %d LED_OFF: %d" % (channel,on,off))

def setServoPulse(self, channel, pulse):
"Sets the Servo Pulse,The PWM frequency must be 50HZ"
pulse = pulse*4096/20000 #PWM frequency is 50HZ,the period is 20000us
self.setPWM(channel, 0, int(pulse))

if name=='main':

pwm = PCA9685(0x40, debug=False)
pwm.setPWMFreq(50)
while True:

setServoPulse(2,2500)

pulse and degree

500 --- 0

500, 1000, 1500, 2000, 2500

2500 -- 180

pwm.setServoPulse(0,1500)
pwm.setServoPulse(1,1500)
time.sleep(5)

for i in range(450,2550,10):  
  pwm.setServoPulse(0,i)   
time.sleep(1)     
for i in range(2550,450,-10):  
  pwm.setServoPulse(1,i)   
time.sleep(1)
for i in range(2550,450,-10):
  pwm.setServoPulse(0,i) 
time.sleep(1)
for i in range(450,2550,10):
  pwm.setServoPulse(1,i) 
time.sleep(1)

I will need to modify it to control the servo with the text: S1F, S1N, and S1B. (changing the number for each servo being rotated)


and this is the start from that binary updates video...

char Incoming_value = 0;

void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
Incoming_value = Serial.read();
Serial.print(Incoming_value);
Serial.print("\n");
}
}


so I will need to combine the Python codes as well

Nobody said it would be easy. Raspberry is not arduino and requires a little more Linux skills. Do your rassbery have a built-in BT module? If so, as Chris wrote already, you can not connect to it using the built-in blocks of appinventor and you should use the extension to which the link pointed.

For someone that doesn't have ANY Linux skills, I guess I'm looking for someone to write a bit of code.

Anyone interested?

Bluetooth was my original plan because I thought I could do it.

But If I ask someone to write it for me, WiFi could also be an option.

Well, if you really don't want to code it yourself, try a Google search, something like "Android App development service" and you should find someone.

It's a cycle. make an attempt, get frustrated, make another attempt......
So I tried to get the extension https://github.com/thilankam/AppInventorRaspberryPiCompanion
but then what? how do I get this to MIT?

That isn't an extension Eric. It's add-on code (for MIT to add) for the Companion. I do not know if it has been added (it's 5 years old code), and it would only be there to accommodate TCP/IP and MQTT apparently, so an extension would also be required for those protocols. If the Companion does not support TCP, you can test your App as an APK.

There is a free TCP extension by Jean-Rodolphe available on my site.
https://www.professorcad.co.uk/appinventortips#TipsTCP

The mqtt and tcp extensions already exist for app inventor. Your problem starts elsewhere. You need to start by getting to know Rasberry. Can you turn on the built-in BT module in it and read the data received by this BT?