Skip to main content
update arduino and node code
Source Link
lbrutti
  • 111
  • 4

UPDATE 2 JUN 2018 I managed to make things a little bettere with the following code: ARDUINO: #include <TinyGPS++.h> #include <Wire.h> #include "Adafruit_HTU21DF.h" /* Sketch per creare json contenente lat,lon,data,ora,temp, hum */ static const uint32_t GPSBaud = 9600;

    Adafruit_HTU21DF htu = Adafruit_HTU21DF();

    // The TinyGPS++ object
    TinyGPSPlus gps;

    void setup()
    {
      Serial1.begin(9600);
      //  Serial.begin(9600);
      if (!htu.begin()) {
        Serial1.println("Couldn't find Adafruit_HTU21DF!");
        while (1);
      }
    }

    void loop()
    {
      while (Serial1.available()) {
        gps.encode(Serial1.read());
      }
      float lat = gps.location.lat();
      float lon = gps.location.lng();
      int 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.print(";_");
      Serial1.println();
    }

NODE JS:

    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("_"),
      // parser: serialport.parsers.raw,
      baud: 9600,
      buffersize: 1024
    });
    wss.on('connection', function(ws) {

      sp.on('data', function(chunk) {
        var fromArduino = chunk.toString();
        // console.log(fromArduino);
        ws.send(fromArduino);
        sp.flush();
      });
    });

Now data comes out tidy on both console and ws for most of the times: I guess the problem was the serialport buffersize.

I also removed serial software from Arduino code.


UPDATE 2 JUN 2018 I managed to make things a little bettere with the following code: ARDUINO: #include <TinyGPS++.h> #include <Wire.h> #include "Adafruit_HTU21DF.h" /* Sketch per creare json contenente lat,lon,data,ora,temp, hum */ static const uint32_t GPSBaud = 9600;

    Adafruit_HTU21DF htu = Adafruit_HTU21DF();

    // The TinyGPS++ object
    TinyGPSPlus gps;

    void setup()
    {
      Serial1.begin(9600);
      //  Serial.begin(9600);
      if (!htu.begin()) {
        Serial1.println("Couldn't find Adafruit_HTU21DF!");
        while (1);
      }
    }

    void loop()
    {
      while (Serial1.available()) {
        gps.encode(Serial1.read());
      }
      float lat = gps.location.lat();
      float lon = gps.location.lng();
      int 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.print(";_");
      Serial1.println();
    }

NODE JS:

    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("_"),
      // parser: serialport.parsers.raw,
      baud: 9600,
      buffersize: 1024
    });
    wss.on('connection', function(ws) {

      sp.on('data', function(chunk) {
        var fromArduino = chunk.toString();
        // console.log(fromArduino);
        ws.send(fromArduino);
        sp.flush();
      });
    });

Now data comes out tidy on both console and ws for most of the times: I guess the problem was the serialport buffersize.

I also removed serial software from Arduino code.

updated code
Source Link
lbrutti
  • 111
  • 4
    #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.printlnprint(";"";\n");

      //////

      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.printlnprint(";"";\n");

      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);
    }
    #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);
    }
    #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.print(";\n");

      //////

      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.print(";\n")

      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);
    }
update image
Source Link
lbrutti
  • 111
  • 4

telnet 192 168 1 1drone console shows messed up strings, adruino ide monitor shows them fine

telnet 192 168 1 1

drone console shows messed up strings, adruino ide monitor shows them fine

Source Link
lbrutti
  • 111
  • 4
Loading