Arduino - How to read a string from the Serial Port - bluetooth

I just recently started working with Arduino. I just have a quick question, I tried searching for an answer but have failed for days. Basically what I wanna ask is if there is a way to read a whole line from the Serial Port. Like the line highlighted in the picture below.
What I'm trying to do is using a Bluesmirf Silver Rn-42 to search the area for a bluetooth device and trigger a signal if a matching address is found. I just cant figure out how to read messages that are already on the Serial port.

Use .readString()
Example code:
String myString;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
myString = Serial.readString();
//do stuff with the string
}
}

If you want to read something that's already in the serial port from the Arduino end, then you need to rethink your code. Anything you produce within your code to print to the serial monitor will already be in your program ready to access if you make it available in the right way. The exemplar string you provided, is simply an array of characters that you can store in an element within an array, making it accessible whenever you need it.
Hints:
Never read back from the serial monitor, it's really slow -.-
Make all the resources you require accessible and available in memory at the time you need it to save hassel & processing power.
Never make the same mistake twice.
However, if you want to read from the COM port that the Arduino is connected to in Windows, then you'll need to work with Libusb libraries found here: http://www.libusb.org/ for C. Any other language will be library or import dependent.

Related

Clean way to real, always working, auto-reconnect

I hope to open a question that is useful for the users of this shield.
Incipit: WiFi.setAutoReconnect(true); seems that not prevent 100% of disconnections.
I've tested a lot of shields (ESP12F, ESP01) and in some case i noted that the auto-reconnect does not work properly.
Fact:
I was unable to reproduce when the shield is connect to pc/debugger.
When did it happen, i try to execute a task depending on a botton press, eg:
loop(){
if (digitalRead... == HIGH) do_something()
}
And the shield... do something! So, the shield was not frozen.
I try to reset (BOTH via HW and SW) and the shield immediately reconnect.
I read some other source and this behavior is often described (es. https://randomnerdtutorials.com/solved-reconnect-esp32-to-wifi/ ). Brief: try WiFi.reconnect(), if it does not work try ESP.restart().
Then, the question:
Why does this happen? Is there a problem with the arduino libraries, or with the native expressif interface? Or is a well-known hardware problem unsolvable via SW?
If indeed so, what techniques do you use to prevent disconnection? I have set a ticker every 30 minutes, which if it sees the card disconnected for more than a certain time, it restarts it. Eg.
void checkWifi() {
if (lastPing + DELTA < millis()) ESP.restart();
}
ticker.attach(checkWifi, ...)
void loop() {
if WiFi.isConnect() {
lastPing = millis();
...
}
}
If there is nothing to do, what do you think of the restart technique? Is it risky to restart frequently, can it reduce the life of the device?
Thanks to anyone who wants to contribute or exchange impressions!

Arduino read get values after ? in url with ethernet shield

Sorry if it has been asked before but I cannot find anything on the internet. I just want to host a page in arduino ethernet shield and when I visit it from a browser with Get parameters (e.g http://xxx.xxx.xx.xx/led.html?red=255,green=0,blue=255) to change to change the led's color. I cannot find how to send data from browser to arduino.
The answer involves the use of The Ethernet Library (Ethernet.h) - which provides an interface to Client and Server, a degree of conceptualizing low-level I/O and buffering the input, and context.
In the following code snippet ( of the sample code at http://arduino.cc/en/Tutorial/WebServer )
...
char c = client.read();
Serial.write(c);
...
the line char c = client.read() is taking a byte from the request stream and assigning it to a char type, it is then serialing that charbyte to a string where it perform conditional logic on it.
The conditional logic of that sample only cares about reading whether a return character \n on a blank line, but the bytes that are being read (on each iteration) byte to byte, actually compose the RAW Request.
At minimal, a RAW HTTP GET Request looks like this:
GET /?first=John&Last=Doe HTTP/1.1
Host: localhost
So, for you to read the query string, you'll need to buffer the bytes being read from the stream.
Then, you'll likely serialize the whole buffer into a string, and perform string operations on them, as well as your conditional logic....

How can I connect a EV3 mindstorms via bluotooth to a unity game using unityscript?

I am making a race game in Unity with Unityscript and I made a steer with the lego mindstorms EV3 robot. I let te robot send information by bluetooth to the game, but I can't find how I can do that.
I already have the code for the bluetooth running and working a C#, but know I need to know how to translate it to unityscript.
I already tried to find it on google, but I only seem to get some software to hack the robot, but not to make code in unityscript for connecting the steer.
Under here stands the C# code:
// EV3: The EV3Messenger is used to communicate with the Lego EV3.
private EV3Messenger ev3Messenger;
// EV3: Create an EV3Messenger object which you can use to talk to the EV3.
ev3Messenger = new EV3Messenger();
// EV3: Connect to the EV3 serial port over Bluetooth.
// If the program 'hangs' on a call to ev3Messenger.Connect,
// then your EV3 is not paired with your PC yet/anymore.
// To pair: Remove the EV3 from the Windows Bluetooth device list and add it again.
ev3Messenger.Connect("COM3"); // Hardcoded serial port: put the serial port
// of the Bluetooth connection to your EV3 here!
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// Unload any non ContentManager content here
// EV3: Disconnect
if (ev3Messenger.IsConnected)
{
ev3Messenger.Disconnect();
}
}
// EV3: send Brake message to mailbox with name "MakeNoise"
if (ev3Messenger.IsConnected)
{
ev3Messenger.SendMessage("MakeNoise", "Brake");
}
// Game can be controlled by both the arrow keys and the Steer, gas and brake paddle of the connected EV3
UpdatePaddlePositionUsingKeys();
UpdatePaddlePositionUsingEV3();
base.Update(gameTime);
}
///Steer update
private void UpdatePaddlePositionUsingEV3()
{
if (ev3Messenger.IsConnected)
{
// EV3: Receive a new command from mailbox "COMMAND" of the EV3
// and use it to change the direction of the paddle or to exit the game.
EV3Message message = ev3Messenger.ReadMessage();
if (message != null
&& message.MailboxTitle == "Command")
{
if (message.ValueAsText == "")
{
}
{
ev3Messenger.Disconnect();
Exit();
}
}
}
}
I hope that you know where I can find how I can do this or even help me further.
If you want the original code for a small pong game where I got my inspiration from, just comment it.
I hope you can help me.
Here are some useful links with documentation on the EV3 firmware :
Firmware Documentation
LMS2012 Documentation
HDK/SDK
In particular, you need to learn how to send direct commands and then use that to read and write bluetooth mailboxes.
For communicating with the COM port itself using javascript, just do a little searching. For example, I found this SO question which has quite a few different ideas.
As part of c4ev3, We open-sourced our EV3 uploader, which can also be used to send connection-agnostic commands to the device.
Here is how you would move the motors in Perl (Complete version):
use IPC::Open2;
print open2(\*EV3OUT, \*EV3IN, "ev3 tunnel") or die "couldn't find: %!";
print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 09 50 A6 00 09\n";
This would probe for an EV3 accessible over USB, Bluetooth or WiFi and connect to it, then send the direct messages associated with turning the motors. For more information on the direct commands protocol check out LEGO's Communication Developer Manual and David Lechner's answer.
Alternatively, you can write a C program for the EV3 with c4ev3 and communicate with that. That way you got a nicer looking C-API you can use.

App Inventor 2: Not finding a specific block

In the App Inventor 2, where is the block mentioned in last item (figure) of the url: http://beta.appinventor.mit.edu/learn/reference/blocks/definition.html
I am trying to use bluetooth to connect my custom device with Android App.
The internet says:
This block provides a "dummy socket" for fitting a block that has a
plug on its left into a place where there is no socket, such as one of
the sequence of blocks in the do part of a procedure or an if block.
The block you fit in will be run, but its returned result will be
ignored. This can be useful if you define a procedure that returns a
result, but want to call it in a context that does not accept a
result.
You are looking for the evaluate but ignore result block
However in your bluetooth connecting example let me recommend you to use an if statement instead
if BluetoothClient1.connect ...
then do something
else display a message, e.g. "connection was not successful"
see also this bluetooth example
and see the documentation of the Bluetooth components
boolean Connect(text address)
Connect to the Bluetooth device with the specified address and the Serial Port Profile (SPP).
Returns true if the connection was successful.

Execute AT commands in J2ME

I want to know how to execute AT commands inside a J2ME application. The approach that I am taking in brief is as below:
First get all the ports that are present in the phone by
String ports = System.getProperty("microedition.commports");
Now just try to write "AT" and wait for the response from each port (YES I said EACH!!!)
try{
commConnection = (CommConnection) Connector.open("comm:" + portsArr[i] + ";baudrate=19200");
} catch (IOException e) {
print("IOException:Port:" + portsArr[i] + "~Mess: " + e.getMessage());
}
Once I get an "OK" from some port I can execute my intended commands in the same way.
I am trying to execute this on two diffrent phones
Nokia SuperNova 7210
ports=USB1
When I try to write to the port nothing happens.
Nokia Xpress music
ports= USB2,COM1,IR1,USB1,BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,BT12,BT13,BT14,BT15,BT16,BT17,BT18,BT19,BT20,BT21,BT22,BT23,BT24,BT25,BT26,BT27,BT28,BT29,BT30,BT31,BT32,BT33,BT34,BT35,BT36,BT37,BT38,BT39,BT40,BT41,BT42,BT43,BT44,BT45,BT46,BT47,BT48,BT49,BT50,BT51,BT52,BT53,BT54,BT55,BT56,BT57,BT58,BT59,BT60,BT61,BT62,BT63,BT64
When I try to write to USB2,COM1,BT1 port
IOException:Port:COM1~Mess: SymbianOS error = -1 : General:
System error
IOException:Port:USB1~Mess: SymbianOS error = -21 : General:
System error
IOException:Port:BT1~Mess: SymbianOS error = -44 : General:
System error
Is this a correct approach?
Smslib uses AT commands but I'm not understanding how do they execute AT commands or how they get the port on which to write the AT commands?
If not possible with J2ME I don't mind not writing the execution of AT commands in some other language as long as both are able to communicate and the solution will support a most of the vendors.
Related - https://stackoverflow.com/questions/3803508/can-i-use-at-commands-insider-j2me-app
What you are trying to achieve is absolutely not possible.
Your approach would only work if Java ME provided access to the GSM modem via COMM ports, which it does not!
(I suppose there could possibly be a device somewhere which offers this, anything's possible in Java ME land, but I have never seen or heard of this).
The library you are referring to runs on a PC which has a device connected to it via the COMM port, it does not work in a Java ME context.
I suspect that what you're really trying to do is access the handset's native SMS inbox via a MIDlet. I promise you, there is absolutely no way to do this!
If what you are trying to do is just send an SMS, maybe you can get to it using an APDU. It seems technically possible:
First you will need JSR 177 SATSA-APDU. Check Nokia devices that have support for it at http://www.developer.nokia.com/Community/Wiki/Java_ME_API_support_on_Nokia_devices
Then you will have to create an SMS APDU just like an STK Applet would do. Please check "Sending a message in the PDU mode" at http://www.dreamfabric.com/sms/
I did not try this, but this is the path I would go if I had to. If it does work with you, please share.

Resources