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
#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");
}
/*
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);
}