none

PIC Serial Bridge (via Bluetooth and WiFi)




Introduction

There are some projects were I could use a simple serial text user interface to an embedded PIC processor.  It occurred to me that Bluetooth may provide me this link so that I could use my iOS phone or tablet to quickly access a microprocessor without looking for cables.

With some searching I identified Bluetooth Low Energy (BLE) as a good possibility.  There are some inexpensive and small modules that put a wrapper on some chipsets and allows you to talk via the ubiquitous serial asynchronous protocol built into many processors.  Among these, the industry standard appears to be the HC-10 series of interface boards.

Logic-level serial interface

I could tell that these modules all use logic level voltages for the serial interface, rather than the +/- 12V of normal RS232 communications.  I decided to modify one of my USB serial boards for this task.  For this I selected a GMUS-03 USB to serial adapter as it had a small circuit board that was accessible.  I traced the wiring on the board and bypassed the 12V voltage translator circuits on the transmit and receive data so that the interface was direct logic level IO.


I bypassed the +/-12V translator circuit on this serial
interface to talk direct logic (0-5V) levels.

I kept the same 9 pin D interface for the physical connector.  Per the standard, pin 2 flows data TO the computer.  The voltage level on pin 3 was 5V (idle), and pin 2 had a threshold of 2V.  So that means that 3.3V logic is able to send data to this board.

A simple loop back connector (pin 2 tied to pin 3) showed this adapter was operational and I was up and running.

AT-09

The first BLE board I tried was purchased from Banggood for $3.  This is the AT-09 and it stated HM-10 compatibility on the web page.  However, once I received the item (3 week delivery, not too bad), it was clear there were several differences with the HC-10.  One big one is that this is 5V compatible, both power and logic.  That makes it very convenient to interface to my serial adapter and a PIC.  Another difference is the pinout of the module.
I built a small circuit board to interface and power the module as shown below.  This allowed me to use a serial interface program such as Tera Term on my PC to exchange live data with the module first without using a PIC as a middle man.


AT-09 test board.  This has a Flash 16F877 PIC that is programmed via the
RJ11 connector at the top.  The AT-09 module itself is in the top left.
The three colored (red-black-white) cable on the bottom left goes
to the modified serial interface at the top of this page and
forms the link to the PC running Tera Term.

The module should flash its red LED upon power up.  You can then send it commands via 9600, 8, none, 1.  It is vital to follow every command with CR and LF or you will not get a response.

After some trial and error I finally got the desired "OK" response from the AT-09 and I could query it for information.  Some links to pages I found helpful:
When not connected on the Bluetooth side, the serial interface will respond to "AT" commands.  Be sure to terminate each with carriage return and line feed.  This is a list using the module's 'help' command:

********************************************************************
* Command             Description                                  *
* ---------------------------------------------------------------- *
* AT                  Check if the command terminal work normally  *
* AT+RESET            Software reboot                              *
* AT+VERSION          Get firmware, bluetooth, HCI and LMP version *
* AT+HELP             List all the commands                        *
* AT+NAME             Get/Set local device name                    *
* AT+PIN              Get/Set pin code for pairing                 *
* AT+PASS             Get/Set pin code for pairing                 *
* AT+BAUD             Get/Set baud rate                            *
* AT+LADDR            Get local bluetooth address                  *
* AT+ADDR             Get local bluetooth address                  *
* AT+DEFAULT          Restore factory default                      *
* AT+RENEW            Restore factory default                      *
* AT+STATE            Get current state                            *
* AT+PWRM             Get/Set power on mode(low power)             *
* AT+POWE             Get/Set RF transmit power                    *
* AT+SLEEP            Sleep mode                                   *
* AT+ROLE             Get/Set current role.                        *
* AT+PARI             Get/Set UART parity bit.                     *
* AT+STOP             Get/Set UART stop bit.                       *
* AT+START            System start working.                        *
* AT+IMME             System wait for command when power on.       *
* AT+IBEA             Switch iBeacon mode.                         *
* AT+IBE0             Set iBeacon UUID 0.                          *
* AT+IBE1             Set iBeacon UUID 1.                          *
* AT+IBE2             Set iBeacon UUID 2.                          *
* AT+IBE3             Set iBeacon UUID 3.                          *
* AT+MARJ             Set iBeacon MARJ .                           *
* AT+MINO             Set iBeacon MINO .                           *
* AT+MEA              Set iBeacon MEA .                            *
* AT+NOTI             Notify connection event .                    *
* AT+UUID             Get/Set system SERVER_UUID .                 *
* AT+CHAR             Get/Set system CHAR_UUID .                   *
* -----------------------------------------------------------------*
* Note: (M) = The command support slave mode only.                 *
********************************************************************

It is interesting to note that the baud rate setting persists after a power cycle.  This makes me wonder how to reset it if I forget or get out of sync.

With the setup completed, it is time to send data to and from your iOS device.  I loaded the BLE Terminal app onto my phone and when it does a scan of devices, it found "MLT-BT05", which matches the name of the device as found on the serial interface via the AT+NAME command.  Tapping on that device opens a terminal window as shown below.  Typing characters into Tera Term caused them to appear immediately on the phone and vice versa.  I was able to get this to work from my basement while the PC was on my second floor.  BLE Terminal has a handy signal strength indicator for the link margin (not shown below).

Once you are "connected" via BLE Terminal, you need to perform a Disconnect in order for the AT command set to be interpreted (instead of the characters being relayed over Bluetooth).


  Note.  Fig 6.6 on SS Page 51 7E (FMC_B_P1)

BLE Terminal app on my phone shows two-way
stream between module and phone.
Sorry for the typo.

JDY-09

The next one I tried out was purchased from Ebay seller 'super-genius!' and arrived quickly due to US sender's address.  Cost was slightly higher at about $4, but not surprising as it shipped from a US address.  The appearance of this board is almost the same except some resistors are different in size, and this does not have a pushbutton.  I expected it to be a drop-in replacement as the one above, but to my surprise could not get any acknowledgement to the AT command nor any other command.  Simply connecting my iPhone to it (shows up as "JDY-09-V4.3") caused the red LED to go solid.  I could then exchange data from the PC connected to the serial wires to the iPhone at 9600 baud.  At this point, I will only use this variant for slower speed projects.  I had purchased two of these units and both behaved the same way.

Bluefruit

The above solutions worked as advertised but my main issue was the software development options on the iOS side.  I could only use "BLE terminal" for the user interface, which is only a text-based serial console.  I then found a line of BLE devices from adafruit.com, and it looked like it included support for GUI development on the iOS side.  I decided to try the Bluefruit UART Friend.  Tutorials here.

After examining the documentation, I decided to mod my GMU serial interface to connect the USB 5V to pin 1 of the 9-pin connector.  This is usually a Carrier Detect input.  I then built an adapter cable from the 9-pin to a header.  The idea came from this page.


Friend module wired to the 9-pin connector on my serial interface.

In the above adapter, I looped back the CTS and RTS on the module, and powered it from pin 1 of the 9-pin.  the transmit and receive data are then wired accordingly.  Plugging this into my laptop caused a red light to flash on the Friend.

Putting the tiny switch on the Friend module to CMD, I was able to run Tera Term (9600 8,N,1) and got an "OK" once I typed "AT".  That confirms the module is alive.  (Other AT commands here).  I then flipped the switch to UART mode in preparation for the part below.

On my iPad, I loaded "BluefruitConnect" from the Apple Store, and I could see the Friend on that app.  Using the UART feature of the BluefruitConnect, I was able to send characters from my laptop to the iPad.  A successful first try.

First transfer of characters between laptop and iPad.

Unlike my initial impression, code development on the iOS platform is not supported by Adafruit.  Bluefruit Connect offers more options than BLE Terminal, but at this point cannot be changed by the user.  It does offer a GUI that allows you to exchange UART strings, and a standard button pushpad along with plotting and exchanging accelerometer and GPS info with the Friend.

First use of Bluetooth Interface: X-10 Signal Monitor

The first project that I installed one of these modules into was the X-10 Monitor.  It is the perfect application of this bluetooth link as I wanted to keep the monitor's portability and the simple remote control was what I needed.


AT-09 module added to the monitor (upright on the left).

With this interface, I can connect to the monitor anywhere in the house and check the received signal strength at the main breaker panel (or where ever the monitor is located).


BLE Terminal on my phone.

The above image shows a typical interaction.  The bottom left button is defined as the "Sample" button, and hitting that sends an 's' to the PIC on the monitor.  It responds with 8 samples of the power line noise level that spans one half wave of the 60 Hz line and is triggered by the zero crossing of the power line.  Thus each sample shows the noise level over one millisecond of time.  The most important time period is the first millisecond, and that is shown as 26 units.  The lower this number, the lower the background noise is on the power line.  Then a D-4 OFF is sent by a controller some where in the house, and the Monitor shows:

> D 4 0:38 1:188

This means that the command unit code was D-4, and the 0 bits had amplitude of 38 units (basically the background noise), and the 1 bits had an amplitude of 188 units.  This large difference is an indicator of a healthy signal amplitude.  The difference of 150 units implies a signal amplitude of 210 mV.

WiFi Interface

Another option is to use WiFI for embedded interface.  In the past, this has meant a large hardware footprint, but with the introduction of the ESP8266 chip, it became possible to do this in a very small package.  This chip is commonly packaged into the ESP-0x series of modules, and can be very inexpensive.  I bought four units on Amazon for $8 shipped (so $2 each).

ESP-01S
WiFI interface on a tiny board for embedded applications.

The above board runs on 3.3V and cannot be fed 5V.  Also, the TX-RX serial interface is logic level (not RS-232).  This interface is commonly accessed through USB, and with some searching I found an inexensive all-in-one solution on Ebay that is based on the CH340G.

USB Serial Interface
USB to Serial interface module.  It is able to run at 3.3V.

These two modules only need four wires to interconnect, and by running Putty, I can talk to the WiFI module.  The built-in command set of the module is similar to the old Hayes Modem protocol and the full document is here

Wifi module test
The two modules wired together for a test.  Red is 3.3V, black is ground,
and white are the transmit and receive data lines.

I plugged the above assembly in and the USB serial device instantiated as COM3.  I then opened a session of Putty, but the result was an hour of unsuccessful attempts to communicate with the ESP-01S.  I tried changing the baud rate, but that lead to confusing cases of seeing garbled characters being echoed.  Finally, out of desperation, I started sending all the control characters from Ctrl-A onwards and as soon as I hit Ctrl-J, the ESP-01S replied with an "OK"!  It turns out that I need to send CR-LF to terminate every line.  Surprisingly, there is no way to tell Putty to do this.  One needs to manually send a Ctrl-J after every line.  As a result, I would later switch from Putty to Tera Term, a much more capable serial comm app.

Use as Access Point (AP Mode)

In this scenario, the ESP-01S acts as an Access Point, and a client/user can connect to it directly by selecting it as a Wi-Fi server.  The user can then use something like 'telnet' to exchange data/commands with the embedded processor. 

I started by entering these commands (leading dash not sent):
    - AT+CWMODE_DEF=2
    - AT+CWSAP_DEF="ESP","12345678",5,3
Which sets up an Access Point in the ESP-01S named "ESP".

Checking for access points with my phone, I could see this was successful, as I now saw "ESP" among the available ones.  The strength was surprisingly high considering such a tiny antenna, and I could see it with strong signal from two floors away in my home.

I then sent the following commands:
    - AT+CIPMUX=1
    - AT+CIPSERVER=1,333
Which sets up a server in the ESP-01S at port 333.  I then connected to the ESP access point with my computer, and ran a telnet client using "Tera Term" to connect to 192.168.4.1 (the IP address of the AP), and at port 333.  The connection was accepted and I see "0, CONNECT" on the USB interface.  This means that a client of ID 0 has connected.  When I type "HELLO PIC" on the telnet client, I see from the USB serial port:
    +IPD,0,11:HELLO PIC
The 0 is the link ID (multiple connections possible), and the 11 means 11 characters (including trailing CR-LF) received.

This demonstrates a successful transfer of data from the telnet client to the USB interface!

Next, I typed on the USB interface:
    - AT+CIPSEND=0,10
And received the "OK" reply.  This shows the ESP-01S is ready to accept 10 characters.  I then typed some random keys, prompting a short message to pop up and then "SEND OK" from the USB interface.  I then saw the characters appear on the telnet client Tera Term.  This confirms two way messages!

This experiment also works on my iOS phone using the app called  "Telnet Lite".

One thing to note is that after a few minutes of no activity, the ESP-01S closes the connection, and the client window closes.

Use as Station (Station Mode)

My second effort was to use the ESP-01S in Station mode.  This is for example where the embedded processor acts as a client by connecting to an Internet connected access point and then to a remote mail server to send email.  In this instance the embedded processor can be some dedicated device that sends out data regularly.

To start, I connected to my home WiFi access point:
    - AT+CWMODE=1
    - AT+CWJAP_DEF="ARUBA","PASS"
Where, "ARUBA" is the name of the Access Point with password "PASS".  This produced the reply "WIFI CONNECTED, WIFI GOT IP, OK", showing that the connection was successful.

I could then do things like query the obtained IP address for this Station:
    - AT_CIFSR
Reply was
    +CISFR:STAIP,"192.168.1.42"
    +CISFR:STAMAC,"d8:bf:c0:fd:9c:56"
    OK
And do a PING:
    - AT+PING="www.baidu.com"
Reply was:
    +73
    OK

The next thing to do is to open a TCP connection to the mail server and send an email.  Due to the complex exchange this section is arranged as a table and I used a Tera Term macro to send these exchanges.  The macro file contents is in the left column and the response is on the right.

Macro File Line (.ttl)
Response (ESP-01/Mail Server)
;Connect to server
send 'AT+CIPSTART="TCP","MAIL.SERVER.COM",25'
send 13
pause 2

CONNECT
OK
+IPD,25:220 server.com ESMTP
;HELO exchange
send 'AT+CIPSEND=6'
send 13
pause 1
send 'HELO'
send 13
send 10
pause 1

AT+CIPSEND=6
OK
>
Recv 6 bytes
busy s...
SEND OK
+IPD,19:250 server.com
;Sender
send 'AT+CIPSEND=30'
send 13
pause 1
send 'MAIL FROM:<EDS@EDCHEUNG.COM>'
send 13
send 10
pause 1

AT+CIPSEND=30
OK
>
busy s...
Recv 30 bytes
SEND OK
+IPD,8:250 ok
;Receiver
send 'AT+CIPSEND=27'
send 13
pause 1
send 'RCPT TO:<ED@EDCHEUNG.COM>'
send 13
send 10
pause 1

AT+CIPSEND=27
OK
>
busy s...
Recv 27 bytes
SEND OK
+IPD,8:250 ok
;Start payload
send 'AT+CIPSEND=6'
send 13
pause 1
send 'DATA'
send 13
send 10
pause 1

AT+CIPSEND=6
OK
>
busy s...
Recv 6 bytes
SEND OK
+IPD,14:354 go ahead
;Message body
send 'AT+CIPSEND=16'
send 13
pause 1
send 'From: "PIC" <>'
send 13 10 
pause 1

AT+CIPSEND=16
OK
>
Recv 16 bytes
SEND OK

send 'AT+CIPSEND=24'
send 13
pause 1
send 'Subject: PIC message'
send 13 10 13 10
pause 1
AT+CIPSEND=24
OK
>
Recv 24 bytes
SEND OK

send 'AT+CIPSEND=18'
send 13
pause 1
send 'Hello iPhone!'
send 13 10 
send '.'
send 13 10
pause 1
AT+CIPSEND=18
OK
>
Recv 18 bytes
SEND OK
+IPD,28:250 ok 1678571532 qp 44918


;Disconnect
send 'AT+CIPSEND=6'
send 13
pause 1
send 'QUIT'
send 13 10



AT+CIPSEND=6
OK
>
Recv 6 bytes
SEND OK
+IPD,19:221 server.com
CLOSED

The resulting email from the above exchange looks like this in Outlook:

The email sent via the ESP-01S

The reference that was very useful is here.  It was indespensible to learn the step by step exchange needed to talk to an SMTP server.

Finally to conclude the email request, disconnect from the Access Point:
    - AT+CWQAP
Reply was "WIFI DISCONNECT OK"

The second part of Station Mode is to pull data, such as from a web server.  For my exercise, I chose to pull from a non-SSL server such as shortcutmania.com.  This website was useful for me to figure out the message exchanges. The commands sent are as follows:

Macro file line (.ttl)
Response (ESP-01/Mail Server)
;Macro file to fetch from web server

;Connect to server
send 'AT+CIPSTART="TCP","SHORTCUTMANIA.COM",80'
send 13
pause 1

;GET exchange
send 'AT+CIPSEND=16'
send 13
pause 1
send 'GET / HTTP/1.1'
send 13
send 10
pause 1

;HOST exchange
send 'AT+CIPSEND=30'
send 13
pause 1
send 'Host: www.shortcutmania.com'
send 13
send 10
pause 1

;FILE exchange
send 'AT+CIPSEND=42'
send 13
pause 1
send 'http://www.shortcutmania.com/default.htm'
send 13
send 10
pause 1



AT+CIPSTART="TCP","SHORTCUTMANIA.COM",80
CONNECT
OK


AT+CIPSEND=16
OK
>
busy s...
Recv 16 bytes
SEND OK



AT+CIPSEND=30
OK
>
Recv 30 bytes
SEND OK




+IPD,1460:HTTP/1.1 200 OK
(large amount of http data follows)

Use as Telnet Server connected to the Internet
In this scenario, the ESP-01S connects to an Internet connected access point and with an entry in the latter's forwarding table, is available to be connected via a remote Telnet session.

The list of commands to do this is as follows:
AT+CWQAP                      -- disconnect (in case connected)
AT+CWMODE_DEF=1
AT+CWJAP_DEF="AP5","PASS"     -- connect
AT+CIFSR                      -- checks IP address
AT+CIPMUX=1                   -- enable connections
AT+CIPSERVER=1,333            -- telnet server on port 333

-- typical data rx
+IPD,0,1:t                    -- from connection 0, number of chars
+IPD,0,1:e
+IPD,0,1:s
+IPD,0,1:t
+IPD,0,2:

-- typical send
AT+CIPSEND=0,7                -- send 7 chars to connection 0
hello<cr><lf>

AT+CWQAP                      -- disconnect FROM AP

Since this processor is Internet connected, it is of course advisable to implement a password scheme in the commands so that someone doing a port scan of the IP address from the Internet cannot simply connect and activate something.

In conclusion, I found the exercise of using the ESP-01S quite rewarding once I figured out the new-line and carriage return issues.  It was helpful to use Tera Term in telnet mode to emulate the serial terminal method of poking at the servers.

First use of WiFi Interface: Internet connected relay
I had a need to reset a PC remotely and I figured this was a good candidate.  The board is shown below with the ESP-01S circled.  The main processor is a FLASH microcontroller running at 20Mhz.  Serial traffic between the WiFI module and the micro is monitored by a serial connection to a local PC (black DB9 on bottom left).  The PC connection is able to see the characters that are sent back and forth and is able to send characters to the ESP-01S.  I found this to be extremely useful during debugging.  That serial merging network is shown on the DIP header, and was still in some state of flux when this image was shot. The relay that is controlled is on the right, near the grey pair of wires.  These wires are simply connected to the Power button of the PC.  By setting up a port forward in my home router, I can reach this from the Internet, and after entering a password, I can send a command to cycle the relay. The micro set the ESP-01S up as a telnet server after connecting to my home router.

I could have of course used a commercial Internet connected relay, but resetting and powering up a PC requires a particular series of timed ON/OFF cycles, and I felt it was more secure for me to build my own instead of worrying about back doors of a commercial unit.

Internet relay
Internet connected relay device.

Flipper Zero interface for the ESP-01S
Another way to connect to the ESP-01S is via a Flipper Zero multitool. The idea came from Speexvocon, and although two years old already and much of the features of this configuration have been taken over by other projects, I thought it was still worthwhile to build the interface so I could tinker with it.

The Gerbers for the interface board are available, and I used an online viewer to take this snapshot:

Board layout
I initially made a wrong guess about the "Prg EN" button
but viewing the layout helped me figure it out.

Assembled interface
ESP-01S interface board (white - hand wired) plugged into my Flipper Zero.
The two buttons are Reset (left) and Program Enable (right).

Once the interface board and ESP-01S are plugged into the Flipper Zero, you can make use of some custom firmware and flipper apps combos.  Make sure you follow the instructions to press the Program Enable and Reset buttons and also power down the Flipper as the 3.3V is not switched on the interface. 
There is a convenient web flasher that has three firmware versions equipped that allows you to flash the ESP-01S via the flipper plugged into a USB port on the host PC.  With Sequoaia San's Wifi Scanner loaded on the ESP-01S, you run the Flipper app "[WiFi] Scanner" (in the GPIO section of Unleashed).  It will scan for nearby access points and show their signal strength.  You can then walk around and locate them.  I had no luck with Spacehuhn's Deauther for ESP-01.  Running "[ESP8266] Deauther"on the Flipper caused it to hang in the Initialize section.  The third option in the web flasher is for the ESP-12
I think I can also use serial Flipper scripts to send commands to the standard firmware (AT commands).  More to follow on that method.

Project Log

  • July 2020 - Started this blog with the AT-09 module.
  • August 2021 - Bluefruit added.
  • March 2023 - Added WiFi interface module ESP-01S
  • December 2023 - Developed Telnet Server mode, and assembled Internet connected relay.
  • February 2024 - ESP-01S interface for Flipper Zero.

Back Home

(c) 2024 Edward Cheung, all rights reserved.