I'm trying to get an arduino sending data to node but with no success.
I'm connecting to an ar-drone 2.0 as in droneduino · GitHub
Here is my Arduino code:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
/*
Sketch per creare json contenente
lat,lon,data,ora,temp, hum
*/
static const int GPS_RXPin = 16, GPS_TXPin = 10;
static const uint32_t GPSBaud = 9600;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(GPS_RXPin, GPS_TXPin);
//JSON
const size_t bufferSize = JSON_OBJECT_SIZE(6);
DynamicJsonBuffer jsonBuffer(bufferSize);
JsonObject& oData = jsonBuffer.createObject();
void setup()
{
Serial1.begin(9600);
Serial.begin(9600);
ss.begin(GPSBaud);
if (!htu.begin()) {
Serial1.println("Couldn't find Adafruit_HTU21DF!");
while (1);
}
}
void loop()
{
float lat = gps.location.lat();
float lon = gps.location.lng();
float date = gps.date.value();
float timestamp = gps.time.value();
float temp = htu.readTemperature();
float hum = htu.readHumidity();
Serial1.print(lat);
Serial1.print(";");
Serial1.print(lon);
Serial1.print(";");
Serial1.print(date);
Serial1.print(";");
Serial1.print(timestamp);
Serial1.print(";");
Serial1.print(temp);
Serial1.print(";");
Serial1.print(hum);
Serial1.println(";");
//////
Serial.print(lat);
Serial.print(";");
Serial.print(lon);
Serial.print(";");
Serial.print(date);
Serial.print(";");
Serial.print(timestamp);
Serial.print(";");
Serial.print(temp);
Serial.print(";");
Serial.print(hum);
Serial.println(";");
smartDelay(1000);
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial1.println(F("No GPS data received: check wiring"));
}
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
and this my NodeJS code:
var serialport = require('node-serialport');
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({host:'0.0.0.0',port: 9999});
var sp = new serialport.SerialPort("/dev/ttyO3", {
parser: serialport.parsers.readline("\n"),
baud: 9600,
buffersize: 65536
});
wss.on('connection', function(ws) {
ws.send('connected');
ws.on('message', function(message) {
console.log('received: %s', message);
});
sp.on('data', function(chunk) {
var fromArduino = chunk.toString();
ws.send(fromArduino);
console.log(fromArduino);
});
});
While the CSV comes out nice on Arduino IDE serial monitor, it is all messed up both on ws and node console.
I'm using this version of NodeJS and node-serial: https://github.com/felixge/node-cross-compiler/downloads
and set ttyO3 as in droneduino · GitHub
A voltage diveder is set between drone RX and Arduino TX1 to lower signal from 5v to 1.8v.
Thanks for any help!
Lorenzo