Node on Raspberry pi running npm wiring-pi how to send bit codes? - node.js

I'm currently trying to use node on a raspberry pi to host a HTTP server using socket.io to control it.
I currently have WASD keys binded to events ( forward, left, right, and back ) when I press the corresponding keys use light up LED's using code like this:
if( data.forward ) {
console.log('forward - ON');
wpi.digitalWrite(forwardPin, 1 );
} else {
console.log('forward - OFF');
wpi.digitalWrite(forwardPin, 0 );
}
This will turn off and on a LED. Now this is working, i want to now send hijack the controller of this tank i have bought.
Doing this my mimicking the signal the radio receiver sends out.
This guy has done this but in C : https://github.com/ianrenton/raspberrytank/blob/e311504642266d153ee434c85f91724a37403476/rt_ssh.c
You can see the codes in his code which are the same codes for my tank.
Here is one of them: int fwd_slow = 0xFE200F34;
I'm currently using this NPM module to control the GPIO pins (I'm totally open to other libs if you know better with better documentation).
Could someone show me a working example how to send "0xFE200F34" as a signal via a GPIO pin.?
Here is a link of his tutorial:
https://ianrenton.com/hardware/raspberry-tank/
I'm doing the same but in node only.

Related

noble.state is set to "poweredOff" immediately after running the program. (Node.js Noble)

as the title suggests, I made a simple program made with Node.js and Noble to scan for devices with Bluetooth LE. My end goal here is to connect to my daydream view controller and receive information from it.
My problem is that whenever I run the file the state is set to "poweredOff" even though I set it to "poweredOn". When the state is set to "poweredOff" it stops scanning so I am never able to find devices.
Here is my code:
const noble = require('noble')
noble.on('stateChange', function(state) {
console.log("[STATE] State changed to: ", state)
if (state === 'poweredOn') {
console.log("[STATE] Powered on, now scanning")
noble.startScanning();
} else {
console.log("[STATE] Powered off, stopped scanning")
noble.stopScanning();
}
})
noble.state = "poweredOn"
// we found something
noble.on("discover", function(peripheral){
console.log(peripheral)
})
Here is the output I get after running that:
[STATE] State changed to: poweredOff
[STATE] Powered off, stopped scanning
What have I tried?
I have checked that I have all the prerequisites for Noble.
I have tried to run the examples provided by Noble. (same thing happens)
And I have tried moving noble.state = "poweredOn" above and below the noble.on('stateChange') event
I also figured out the problem is not in the daydream controller because I downloaded the LightBlue app on my phone and it detects and connects to the daydream controller just fine. I get no errors or anything at all. This is very strange to me and I hope that anyone could help me.
Thanks in advance.

Want to make Node Red on Raspberry Pi detected Data from Arduino and React to it but unsure how to do so?

I recently made a sensor that can correctly display the distances on the Serial Monitor on the Arduino, it would continuously display the distance as I would move my hand up/down the sensor. Thing is though, I intended on connecting this to a Pi (mine being Pi 3B+), use Node Red and basically have it detect that whenever the distance was "20cm" for example, then it would go straight to a YouTube video and play it. I tried researching all over the net to see if such had been done before, but to no avail as I found content much different than what I tried to pull off with Node Red and my Arduino.
I did try on my end to set up a function on Node Red to my Arduino, using a conditional statement to detect something and print something else out.
Overview of what I tried to do on Node Red
As you can see, there was not much to add as I was initially trying to test the conditional statement I made, making it output something on the debug screen using the code below:
Contents of the function created
var newMsg = {payload:msg.payload.toString()}:
if (newMsg == 'Distance: 20cm') {
newMsg = 'Distance is 20, nice'
return newMsg;
}
return newMsg;
Even with the little test I created to return a message to the debug did not work, so I'm not sure what I'm missing here.
First, you should not be creating new message objects as a rule, this breaks flows that use things like http-in/http-response nodes that require the original message to pass from start to end.
Second, you can't compare an Object to a String and get anything meaningful. The function node probably should looks something like the following:
if (msg.payload === "Distance: 20") {
msg.payload = "Distance is 20cm, nice"
return msg;
}
Third, the input message appears to be terminated with a new line character so the test should probably be:
if (msg.payload === "Distance: 20\n") {

Johnny-Five, I2C, Controlling multiple temperature sensors using ESP8266

I'm trying figure out how to control multiple temperature sensors.
THE SETUP:
2 ESP8266 Micro Controllers
2 MCP9808 Temperature Sensors
1 Machine controlling both ESPs using Johnny-Five.
NOTE: Each ESP8266 micro controller handles one MCP9808 Temperature Sensor.
THE GOAL:
The central machine (MacOS running Johnny-Five) handles both microcontrollers under one Node JS script.
THE PROBLEM:
I can control one Micro Controller / Temperature pairing, but not both under the same script.
Apparently the key to handling both at once lies in knowing how to handle the IC2 addressing.
So far I haven't been able to find any pages, forums, instructions or combinations thereof that clearly explain the logic in terms that I can understand.
THE QUESTION:
How to handle I2C using Johnny-Five to control multiple devices
THE CODE:
It only works when handling one Sensor, not both
In other words with the 4th line commented out it works. Uncommented, it doesn't.
var five = require("johnny-five");
var {EtherPortClient}=require("etherport-client");
var Thermometers=[
//{Name:"Thermometer1", Ip:"192.168.1.101"}, //Uncommenting causes fail.
{Name:"Thermometer2", Ip:"192.168.1.102"}
];
TrackThermometers();
function TrackThermometers(){
Thermometers.forEach(function(ThisThermometer, ThermometerCount){
ThisThermometer.Board=new five.Board({
port: new EtherPortClient({
host: ThisThermometer.Ip,
port: 3030
}),
repl: false
});
ThisThermometer.Board.on("ready", function(){
ThisThermometer.Controller=new five.Thermometer({ //This cmd triggers the error
controller:"MCP9808"
});
ThisThermometer.Controller.on("change", function(){
console.log(this.id, this.fahrenheit);
});
})
});
}
SOLUTION
There is a board property (undocumented as of this post) under the J5's Thermometer API. Assigning the Board instance in question to that property associates the Thermometer instance with that board.
By way of example the above code would be edited as follows...
ThisThermometer.Controller=new five.Thermometer({
board: ThisThermometer.Board, //<-- the missing magic
controller:"MCP9808"
});
Thanks to Donovan Buck for figuring this out. May be documented soon.

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.

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