Getting Raspberry pi events from android/iOS app - android

I just got started with Raspberry pi and I wanted to make a program on my Raspberry which gets input from an app on my Android/iOS device over bluetooth. I wanted to first check if something like this is possible and second if you have any clues on how to do something like this.
Thanks
PS: Since I just got started I'm only looking for clues and I don't want anyone to write and app for me so don't down vote

You would likely need to establish a network communication between the Raspberry Pi and the device.
For the server:
import socket
HOST = '' # This should receive from all available interfaces.
PORT = 1111 # Random port number.
data = "Test" # Data to send to the client.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((HOST, PORT))
while True:
s.sendto(data, (HOST, PORT))
print data
On the client, very similar code but add:
data, addr = s.recvfrom(1024)
print addr
print "Message received: ", data
Under the while True
Reference the following for setting up RPI wireless hotspot:
http://elinux.org/RPI-Wireless-Hotspot
The HOST for each is going to be the device IPv4 address, usually in format:
192.168.x.x.
I have not personally found a way without using serials for data communication. The most common way to communicate information between devices is over network. Look into peer-to-peer network solutions.
The code may not work as is, you will need to make client/server-side code specific for your needs.
Reference the following for setting up sockets and a low-level network interface: https://docs.python.org/2/howto/sockets.html
Hopefully this helps.
EDIT:
There is a Bluetooth method for RPI.
Here's a good branch in Github that contains example of the Bluetooth library used in Python:
https://github.com/karulis/pybluez/blob/master/examples/simple
Good references:
Bluetooth programming with Python.
http://people.csail.mit.edu/albert/bluez-intro/c212.html
How to create a Bluetooth tag with RPI.
https://www.raspberrypi.org/magpi/create-a-raspberry-pi-3-bluetooth-tag/

Related

How to send OFFLINE String or Integer data to Arduino using ESP8226 wifi Module?

please someone one help me, is that possible to send Offline [string,integer] commands from android to Arduino using ESP8226 module?
like chat with WIFI direct without using any modem.
if it is possible can you give me a link about how to do it?
I'm new to Arduino and i dont know what should i look for.
EDIT:
this is my module picture
module picture
ESP8266 can run as an Access Point, that way you can connect to from Android(example).
Then you can send commands from ESP to the Arduino over serial the same, as you would from a PC.
EDIT:
If you controlling ESP by AT commands, there are some tutorials:
configure access point
AP with WifiBee (* WifiBee is just an PCB with ESP8266 on it)
Sumary:
Set SSID, password, channel and encryption.
AT+CWSAP="ESP8266","123",3,0
Set the IP address of the Access Point.
AT+CIPAP="192.168.0.101"
Enable DHCP for AP mode.
AT+CWDHCP=0,0
After that:
Configure TCP Server.
AT+CIPSERVER=1,1234
Set connection mode.
AT+CIPMUX=1
... ...
Basic description of ESP + AT commands on wiki
Complete documentation of AT commands is here
I found the how to program module with Arduino and problem resolved.
VCC=3.3V
Arduino ESP8266
Rx ----> Rx
Tx ----> Tx
VCC <---- VCC
GND <---- GND
////////////////////////////////////
Arduino
RESET -----> GND
////////////////////////////////////
ESP8266
EN ----> VCC+(270ohm Resistor) // just in case of high voltage
IO0 ----> GND
in the IDE's Board Manager select Generic ESP8266 Module then Upload your Sketch.
Notice: Remove EN AND IO0 pins after programming,if you dont do that when you connect power of board all data will be removed and you should program it again.
Thanks for #Martin for His Help and the link of library

data from android studio to matlab via bluetooth

I am new to android studios and I have the task to develop an app which transfers data from an app (Acceleration sensor data - i have created this app already which shows the data) to matlab (on the pc).
I don't really know how I should do this. I've experimented a bit with bluetooth apps, but I don't have a clue how to connect to Matlab.
I would be greatful for your help.
Thanks in advance,
Annika
Unfortunately I can not speak to the android side of things, but MatLab can connect to generic devices with the UART interface, which is fairly low level.
The process with some microprocessors that I am using is to connect the device to the PC, and then note the Outgoing com port.
(In windows 10, these can be found in Bluetooth settings -> More Bluetooth options)
Then you can use
s = serial('COM<what you found in settings>');
s.Baudrate=115200;
s.InputBufferSize = 100;
fopen(s{i});
serials = instrfindall;
to open an connection. The critical command is serial, the other parameters depend on your device/ configuration. Sometimes there can be issues, in which case one options is to build a loop that tries again until it works.
You then collect the data sent via UART via
flushinput(serials);
temp = fscanf(serials,'%s');
and then split the string. If data is sent continuously, you wrap this into a while loop.
After you are done, you can clean up via
fclose(s{i});
delete(instrfind)
instrreset
It should be noted, that establishing a connection takes longer, the more enabled COM ports there are. So it might be worth disabling all those you don't need.
For more specific things matlab can do, check out What Is the MATLAB Serial Port Interface

Python Bluetooth how to send a file to a phone

in my current project it is a requirement to send a file from a windows computer to an android device over bluetooth without anything on the phone other than it's standard state and of course a paired bluetooth connection. i've looked over pybluez and it seemed simple enough to send files between a client and server architecture (and in fact got it sending between my laptop and desktop rather quickly) but I cannot for the life of me find any way to get python to send a file from the computer to android once the connection is established; my attempts have been grabbing the bluetooth mac address like thing from the device like so
nearby_devices = bluetooth.discover_devices(
duration=8, lookup_names=True, flush_cache=True, lookup_class=False)
and then later trying to send the files like so
port = 1
for addr, name in nearby_devices:
bd_addr = addr
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
sock.send("download-app")
sock.close()
Of course with the example script given by the pybluez documentation I can seamlessly send files between a client and a server but I am still stuck without a way to send a file to the selected android device (even if I specify it's address and know it's within range)
You're most of the way there...
As you know, you need something to talk to at the other end of your Bluetooth connection. You just need to replace your custom server with a well-known service (generally one of these options).
In my case, my phone supports the "OBEX Object Push" service, so I just need to connect to that and use a suitable client to talk the right protocol. Fortunately, the combination of PyOBEX and PyBluez does the trick here!
The following code (quickly patched together from PyOBEX and PyBluez samples) runs on my Windows 10, Python 2.7 installation and creates a simple text file on the phone.
from bluetooth import *
from PyOBEX.client import Client
import sys
addr = sys.argv[1]
print("Searching for OBEX service on %s" % addr)
service_matches = find_service(name=b'OBEX Object Push\x00', address = addr )
if len(service_matches) == 0:
print("Couldn't find the service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
client = Client(host, port)
client.connect()
client.put("test.txt", "Hello world\n")
client.disconnect()
Looks like PyOBEX is a pretty minimal package, though, and isn't Python 3 compatible, so you may have a little porting to do if that's a requirement.
I haven't personally explored it but check out this blog -
http://recolog.blogspot.com/2013/07/transferring-files-via-bluetooth-using.html
The author uses the lightblue package as an API for the Obex protocol and send files over the connection. Now the lightblue package appears to be unmaintained. There are other packages like PyObex (which I could not import for whatever reason) which you could also explore as alternatives but lightblue seems to be the way to go.
I have made a Python 3 port of PyOBEX based on the PyOBEX code on bitbucket. I've tested so far only the client functionalities, but I expect the server to be working fine as well, since most of the compatibility issues with Python 3 were due to struct.pack/struct.unpack binary blobs appended to strings that should have all been tackled.

Send AT-command through bluetooth with python

I try to send AT commands from my computer (ubuntu 13.04) to my phone (Android 5.1) via bluetooth. I want to read the SMS.
I retrieve the MAC address of my phone with :
hcitool scan
I browse all available services on the device with :
sdptool browse XX:XX:XX:XX:XX:XX
I get the good RFCOMM channel for SMS/MMS service and now I'm trying to send the AT command.
I tried with pySerial with a bound and connected rfcomm to my phone but no response :
import serial
phone = serial.Serial('/dev/rfcomm0', 115200, timeout=2)
phone.write(b'AT\r')
data = phone.readall()
print data
I tried the same code on a USB serial port and I have a response :
import serial
phone = serial.Serial('/dev/ttyACM0', 115200, timeout=2)
phone.write(b'AT\r')
data = phone.readall()
print data
# *EMRDY: 1
# AT
# OK
I tried with pyBluez but same problem, no response of my AT command :
import bluetooth
client_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
client_sock.connect(('XX:XX:XX:XX:XX:XX', 4))
client_sock.send(b'AT\r')
data = client_sock.recv(1024)
print "received [%s]" % data
And I finally tried with native python sockets, but no response :
import socket
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect(('XX:XX:XX:XX:XX:XX',4))
s.send(b'AT\r')
data = s.recv(1024)
s.close()
print('Received', repr(data))
Note: The phone displays a prompt window to accept that my computer accesses my sms. Of course I accepted.
Can anyone tell me what is wrong and what I can try?
Well, for starter it is better to check first that you have a two-way communication between your Host computer and your Phone on bluetooth, like you said, it did work with USB, then there should be no reason it does not with bluetooth unless you didn't yet established a good communication, so I think it is better to try first that you have good communication by just sending and replying with the same string (kinda hand-shaking protocol) and make sure that you know what your python code is actually sending, may be unseen extra characters using bluetooth that you don't pay attention to, which makes your AT command unrecognizable by your phone.

In App Inventor Make TCP IP Client

Hi Guy's I'm new to programming Android device's I do have python, java, C#, C, C++, PHP, Bash and Visual Basic Experience but I'm new to this block programming, and I haven't done much work with forms. I'm trying to get make an application that posts data to an external IP. I have successsfully wrote a server and a windows based client, clicking buttons in my windows client posts data to the ip 192.168.1.9 port 9999. This is just in the testing phase to remote control a bunch of beaglebone gpio's. So far I've had great success with the windows side. In app inventor for android, However, I've created a series of buttons and tabs, different buttons post text or post and poll for response. The problem I have is that I can directly attach the web connector to 192.168.1.9 but when I add in the port 9999 it tells me the address is incorrect. The method I'm using is
when Screen1.initialize
do set Web1.Url to "http://192.168.1.9:9999"
when Button1.Click
do call Web1.PostText
text > 0
Again, if I type in just the IP of the beaglebone I see its ethernet port go crazy when I click button1. It does nothing when I add in the port. Of course my server is running on 9999 since port 80 is reserved for the internet. Any suggestions?
I would like to suggest you a two-step solution.
Step 1:
Problem>>Develop an android app which is capable to communicate via TCP-IP.
Solution>> I hope you are familiar with MIT-APP Inventor-2. Import an extension called ClientSocket extension V0.4.3 available here to app. Thanks to the developer of the extension.
Step 2:
Problem>>A server responding client request.
Solution>> I have written a Python code.
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 9000 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print (host)
s.listen(5)
while True:
c, addr = s.accept() # Establish connection with client.
data=(str(c.recv(1024)))
print data
conn.commit()
c.close()
cur.close()
Hope this will help.

Categories

Resources