Go Down

Topic: TX/RX are not working on MKRFOX1200 (Read 2835 times) previous topic - next topic

pixelgamma

I'm trying to communicate between my Adafruit Ultimate GPS and my Arduino MKRFOX1200 in order to get data through the serial monitor, but it's not working. I tried 9600/115200 bauds and I even switched RX/TX pins with no success. My GPS is linked to the 3.3v output of the board.

I also tried my GPS on an Arduino UNO and it's working fine.


Here is my sketch:



MarkDerbyshire

Post your code so we can see how you are accessing the Serial port

Are you using Serial() or Serial1() to access the GPS?

Serial() is the USB
Serial1() are pins 13 & 14

You also have the Tx and Rx the wrong way round in the above drawing


pixelgamma

Here is my code:
Code: [Select]
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}


Normally, I don't have to use Serial() or Serial1() because it's directly linked to RX/TX. I should get the data directly in the Serial Monitor and that's what I get with an Arduino Uno, at least.

About the drawing, I even switched TX/RX to Arduino pin, nothing more happens.

MarkDerbyshire

If that is your code no wonder nothing is happening.  It's empty!!

The Uno and the MKR boards are completely different.  You cannot compare the two

pixelgamma

Could you please tell me the differences between Uno and MKR boards regarding Serial please? Could you eventually give me a code example in order to use TX/RX? Thanks for your help.

MarkDerbyshire

The Uno has only 1 hardware serial port shared by the USB and the TX/Rx pins.  The MKR has two pre configured hardware serial ports

Serial() is the USB
Serial1() are pins 13 & 14

You can also program more hardware serial ports if required

Below is the code I use for my Solar charger data.  It used 3 serial ports.  Serial() which is the USB, Serial1() for the Charger interface and a programmed one on pin D0 & D1 - Serial2() for the Xbee

The code is split into two files (two tabs on the IDE) as I find it easier to debug

Code: [Select]
#include <Arduino.h>   // required before wiring_private.h
#include "wiring_private.h" // pinPeripheral() function
#include "DHT.h"

// DHT Sensor
#define DHTPIN 5
#define DHTTYPE  DHT21

DHT dht(DHTPIN, DHTTYPE);


Uart Serial2 (&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0);  //Rx Pin D1, Tx Pin D0

void SERCOM2_Handler()
{
  Serial2.IrqHandler();
}

uint8_t start[] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xEB, 0x90, 0xEB, 0x90, 0xEB, 0x90};
uint8_t id = 0x16;
uint8_t cmd[] = { 0xA0, 0x00, 0xB1, 0xA7, 0x7F };
uint8_t buff[128];

float LiPo_voltage;

float Ambient_t;
float Ambient_h;

int hiveID = 1200; //Hive identification value for multiple hives

unsigned long previousMillis = 0;        // will store last time readings were updated

// constants won't change :
const long interval = 15000;           // interval at which to read (milliseconds)

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600); // Tracer
  Serial2.begin(9600); //Xbee

  pinPeripheral(0, PIO_SERCOM); //Tx
  pinPeripheral(1, PIO_SERCOM); //Rx

  dht.begin();

  Serial.println("Tracer 1215 Solar Power Monitor 2");
  Serial.print("Serial 2 online \r\n");
  delay(5000); // Delay to let everything boot up
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // Read Humidity and Temperature Values
    Ambient_h = dht.readHumidity();
    Ambient_t = dht.readTemperature();
    Serial.print("Ambient Temperature: ");
    Serial.println(Ambient_t);
    Serial.print("Ambient Humidity: ");
    Serial.println(Ambient_h);

    // Check if any reads failed and exit early (to try again).
    if (isnan(Ambient_h) || isnan(Ambient_t) ) {
      Serial.println("Failed to read from DHT sensor!");
      Ambient_h = 0.00;
      Ambient_t = 0.00;
      //return;
    }

    solar_read();
    LiPo_Read(); //Zero Only
  }
}

 // Zero Only

void LiPo_Read() {
  // read the input on analog pin 0:
  int LiPo_Input = analogRead(ADC_BATTERY);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 4.3V):
  LiPo_voltage = LiPo_Input * (4.3 / 1023.0);
  // print out the value you read:
  Serial.print("LiPo Voltage: ");
  Serial.print(LiPo_voltage);
  Serial.println("V");
}


Code: [Select]
/*
   An interface to the Tracer solar regulator.
   Communicating in a way similar to the MT-5 display
*/

float to_float(uint8_t* buffer, int offset) {
  unsigned short full = buffer[offset + 1] << 8 | buff[offset];

  return full / 100.0;
}

void solar_read() {
  Serial.println("Reading from Tracer");

  Serial1.write(start, sizeof(start));
  Serial1.write(id);
  Serial1.write(cmd, sizeof(cmd));

  int read = 0;

  for (int i = 0; i < 255; i++) {
    if (Serial1.available()) {
      buff[read] = Serial1.read();
      read++;
    }
  }

  Serial.print("Read ");
  Serial.print(read);
  Serial.println(" bytes \r\n");

  /*
    for (int i = 0; i < read; i++){
        Serial.print(buff[i], HEX);
        Serial.print(" ");
    }
  */

  //Serial.println();

  float battery = to_float(buff, 9);
  float pv = to_float(buff, 11);
  //13-14 reserved
  float load_current = to_float(buff, 15);
  float over_discharge = to_float(buff, 17);
  float battery_max = to_float(buff, 19);
  // 21 load on/off
  // 22 overload yes/no
  // 23 load short yes/no
  // 24 reserved
  // 25 battery overload
  // 26 over discharge yes/no
  uint8_t full = buff[27];
  uint8_t charging = buff[28];
  int8_t battery_temp = buff[29] - 30;
  float charge_current = to_float(buff, 30);

  //Runtime
  Serial2.print(",");
  Serial2.print(hiveID);
  Serial2.print(",");
  Serial2.print(buff[21] ? 1 : 0);
 // Serial2.print(buff[21] ? "On" : "Off");
  Serial2.print(",");
  Serial2.print(load_current);
  Serial2.print(",");
  Serial2.print(battery);
  Serial2.print(",");
  Serial2.print(battery_max);
  Serial2.print(",");
  Serial2.print(full ? 1 : 0 );
  //Serial2.print(full ? "yes " : "no" );
  Serial2.print(",");
  Serial2.print(battery_temp);
  Serial2.print(",");
  Serial2.print(pv);
  Serial2.print(",");
  Serial2.print(charging ? 1 : 0 );
 // Serial2.print(charging ? "yes" : "no" );
  Serial2.print(",");
  Serial2.print(charge_current);
  Serial2.print(",");
  Serial2.print(LiPo_voltage);
  Serial2.print(",");
  Serial2.print(Ambient_t);
  Serial2.print(",");
  Serial2.print(Ambient_h);
}





pixelgamma

Thank you very much for your answer.

I tried this sketch but it seems Serial1 isn't available at all. I've got "0" each 2 seconds...

Code: [Select]
void setup()
{
  Serial.begin(9600);   // open serial over USB
  Serial1.begin(9600); // TX + RX
}

void loop()
{
  Serial.println(Serial1.available());
  delay(2000);
}


Any idea how I could make it work?

pixelgamma

Ok, I inverted the TX/RX on Arduino and I get something using Serial1.avalaible().

But, now I've got a problem using Serial1.readString(), it doesn't output anything:

Code: [Select]
String a;

void setup()
{
  Serial.begin(9600);   // open serial over USB
  //while(!Serial.available())
  Serial1.begin(9600); // TX + RX
}

void loop()
{
  if (Serial1.available()) {
    a = Serial1.readString();
    Serial.println(a);
  }
}


But if I just use Serial1.read(), I've got something.

Any idea ?

pixelgamma

I finally write a working code:

Code: [Select]
char Char;

void setup()
{
  Serial.begin(9600);   // open serial over USB
  //while(!Serial.available())
  Serial1.begin(9600); // TX + RX
}

void loop()
{
  if (Serial1.available()) {
    Char = Serial1.read();
          Serial.print(Char);
  }
}

andreacapezza

Hi,
with a geo-neo6m GPS
i try with tx on rx and rx on tx nothing
i try with tx on tx and rx on rx nothing

i don't now what to do
could you help me ?

asez73

Hi @andreacapezza
Normally you do tx on rx and rx on tx.
Check your bauds rates on both sides too.
Gps should default to 9600 bauds when it boots!

Go Up