How to use Serial2 on Teensy 3.2

Status
Not open for further replies.

Daba

Member
I am trying to get Serial2 connected to an ESP8266 wifi module, and in turn talk to RemoteXY

I am using this simple pushbutton/led sketch to test the connection.

Code:
//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__ESP8266_HARDSERIAL_POINT

#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_SERIAL Serial2
#define REMOTEXY_SERIAL_SPEED 115200
#define REMOTEXY_WIFI_SSID "TeensyTest"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,1,0,1,0,19,0,8,13,0,
  1,0,17,19,12,12,2,31,88,0,
  65,4,48,20,9,9 };
  
// this structure defines all the variables of your control interface 
struct {

    // input variable
  uint8_t button_1; // =1 if button pressed, else =0 

    // output variable
  uint8_t led_1_r; // =0..255 LED Red brightness 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////
 



// **********************************************************************

void setup() {

  RemoteXY_Init (); 
 
} // end setup

// ***********************************************************************
// LOOP
// ***********************************************************************

void loop() {

  RemoteXY_Handler ();
 
  RemoteXY.led_1_r = RemoteXY.button_1 * 255;

  delay(25);
  
} // void(loop)

If I use Serial1 (pins 0,1) it works fine, however, compiled for Serial2 (as above), and using pins 9/10, it doesn't work, and the RemoteXY app reports "Error: (61) Connection refused"

Is there anything I have to do to enable Serial2 on those pins ??
 
omg, I've just read that Serial and Serial1 are separate ports, is this correct ?

Serial is used for USB, and Serial1, 2, and 3 are separate (I've come from Arduino nano where the usb uses the hardware serial port). This means I won't need to go to Serial2 for my ESP ....

Also, do I have to cut the USB power link on the board, or would it be better to break a cable for this ?
 
I don't fully understand your question.

Serial communicates with your PC and Serial1 communicates on pins 0 & 1, regardless of how Teensy gets power.

So I guess the answer is yes, all that stuff works the same as long as Teensy has proper power. It doesn't matter if the power arrives over the USB cable or is applied to the VIN pin. Maybe the schematic can help.

https://www.pjrc.com/teensy/schematic.html

I also don't see anything in the code (msg #1) about which port will really be used, Serial vs Serial1 vs Serial2. But I'm not familiar with that library. Maybe the port is configured somewhere else, not shown in your message?
 
It's all done in the library Paul...

The first line of the interface spec in RemoteXY block (post #1) : "#define REMOTEXY_SERIAL Serial2" (or whichever you want)

Then that is used in the library. For the esp8266 module there's this line in esp8266.h

Code:
#define RemoteXY_Init() remotexy = new CRemoteXY (RemoteXY_CONF_PROGMEM, &RemoteXY, REMOTEXY_ACCESS_PASSWORD, &REMOTEXY_SERIAL, REMOTEXY_SERIAL_SPEED, REMOTEXY_WIFI_SSID, REMOTEXY_WIFI_PASSWORD, REMOTEXY_SERVER_PORT)

apparently that "... creates a macro function named RemoteXY_Init(), which will pass REMOTEXY_SERIAL to the CRemoteXY constructor"

Code:
CRemoteXY (const void * _conf, void * _var, const char * _accessPassword, HardwareSerial * _serial, long _serialSpeed, const char * _wifiSsid, const char * _wifiPassword, uint16_t _port) {
    initSerial (_serial, _serialSpeed);

just looks like a jumble of letters to me, still on pretty flat ground with the language and syntax etc..
 
Status
Not open for further replies.
Back
Top