MangoCube, HF-LPxx00, USR-WIFI232 How-to

Introduction

This topic documents the procedure I followed to get a MangoCube WiFi, which incorporates a HF-LPT200 or USR-WIFI232S WiFi module, working with Blynk. The procedure should also apply to other Arduino based systems using one of these modules for the Wifi link.

Background

The Shanghai High-Flying Electronics Technology Co. HF-LPxx00 series modules, also marketed as Jinan USR IOT Technology Ltd. USR-WIFI232 series modules, are small WiFi IP to serial converters similar to the ESP8266 modules.

The Megavizon MangoCube WiFi is essentially a miniaturised Arduino Leonardo with a HF-LPT200 (or USR-WIFI232S) based WiFi board attached to the Serial1 Tx and Rx pins. The MangoCube was developed through a Kickstarter campaign. The Wifi board was also available separately as a campaign reward. I’m not sure if any Megavizon products are currently available for sale.

Bhargav Mistry, the founder of Megavizon, stated in a Kickstarter comment that he intended to support Blynk on the MangoCube. I don’t think anything became of this so I decided to look into what it would take to do it.

Additional information on the modules can be found on the manufacturers’ web sites.

Implementation

The HF-LPxx00 modules are similar to the ESP8266 in that they are configured and controlled using AT commands. However, the commands themselves are quite different, so ESP8266 libraries will not work.

It turns out that the module can be configured in a such way that it will automatically connect to a Wifi Access Point (AP) and open a TCP link. It will then go directly into a mode that transparently passes data, unaltered, between TCP and the serial port (called throughput mode, which the ESP8266 calls unvarnished mode).

Using this throughput mode configuration, no new libraries or module specific code is necessary to work with Blynk. After pre-configuring the module, just loading a sketch that uses the standard hardware or software serial connection code is all that’s needed. The module is first manually configured using AT commands in command mode. (This configuration is saved and retained in the module over subsequent power cycles). A sketch is then loaded to run Blynk as if connected by a serial link.

One drawback to using this technique is that whenever the WiFi connection parameters (such as SSID or passphrase), TCP parameters (such as host address or port) or other parameters need to be changed, a sketch must be loaded to allow manually changing the module settings and then the Blynk sketch must then be reloaded. An advantage is that by not needing Wifi configuration code in the Blynk sketch more code space will probably be available for user functionality.

Procedure

Load a configuration sketch

If the module is in the default throughput mode it must be put into command mode so it can be configured. This is done by sending +++ then waiting for the letter a to be received then sending a letter a which is acknowledged by receiving +ok.

This could be done manually but to make it easier I wrote a sketch that will automatically try to enter command mode at either the 115200 (default) baud or 9600 baud. It will also try to determine if the module is already in command mode (it could possibly have been previously configured to come up in command mode).

If the sketch fails, the module may have been configured for a different baud rate. In this case, the sketch can be changed or extended to try different rates.

Once the module is in command mode, the sketch will echo data between the Arduino Serial1 port (the module) and the Serial port (the USB port on a Leonardo). The module can then be configured manually by entering commands from the Arduino IDE Serial Monitor (or other serial terminal program). The Serial Monitor should be configured for 9600 baud, regardless of the speed of the module, and should be set to send a carriage return as a line ending.

Here is the sketch (I know it’s not very elegant but it gets the job done):

// Put a HF-LPxx00 / USR-WIFI232 module on Serial1 into command mode
// and then echo data between Serial and Serial1

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  // wait for serial port to connect.
  while (!Serial);

  Serial1.begin(9600);
  Serial1.setTimeout(250);

  delay(7000); // wait 7 seconds to allow starting serial monitor
  
  if (enterCommandMode()) {
    Serial.println("Switched to command mode at 9600");
    return;
  }

  if (inCommandMode()) {
    Serial.println("Already in command mode at 9600");
    return;
  }

  flushRead();
  Serial1.flush();
  Serial1.end();
  Serial1.begin(115200);
  Serial1.setTimeout(100);

  if (enterCommandMode()) {
    Serial.println("Switched to command mode at 115200");
    return;
  }

  if (inCommandMode()) {
    Serial.println("Already in command mode at 115200");
    return;
  }

  Serial.println("Connection failed at 9600 and 115200");
}

void loop() {
  // read from port 1, send to port 0
  if (Serial1.available()) {
    Serial.write(Serial1.read());
  }
  // read from port 0, send to port 1
  if (Serial.available()) {
    Serial1.write(Serial.read());
  }
}

boolean enterCommandMode() {
  for (byte i = 0; i < 3; i++) {
    flushRead();
    Serial1.write("+++");
    if (Serial1.find((char *)"a")) {
      Serial1.write("a");
      if (Serial1.find((char *)"+ok")) {
        return true;
      }
    }
  }
  return false;
}

boolean inCommandMode() {
  for (byte i = 0; i < 3; i++) {
    flushRead();
    Serial1.write("at+\r");
    if (Serial1.find((char *)"+ok")) {
      return true;
    }
  }
  return false;
}

void flushRead() {
  while (Serial1.available()) {
    Serial1.read();
  }
}

Configure the module

With the module I used to develop this procedure, at+ver reported V1.0.05 and at+lver reported 21 (2014-11-08 13:29 8B)

With the configuration sketch loaded and the Arduino IDE running on a PC, plug the Arduino into a USB port to power it up. Open the IDE Serial Monitor. Wait about 10 seconds for a message indicating success.

The following at+ commands are the ones required to configure for Blynk use. Most of the commands can be used to display their current setting by just entering the command without the = character or anything following it. Commands are case insensitive but some parameters (such as a passphrase) may be case sensitive.

Angle brackets are not entered or received. They indicate a variable value.

If you get a +ERR= error code, their meanings are:
-1 Invalid command format
-2 Invalid command
-3 Invalid operation symbol
-4 Invalid parameter
-5 Operation not permitted

I decided to run the module serial connection at 9600 baud, which seems to work fine. Other baud rates would likely also work, if desired.


Set UART to 9600 baud, 8 bit, no parity, no flow control.
at+uart=9600,8,1,none,nfc

Disable UART packet framing mode.
at+uartf=disable

Set UART packet forwarding timing to normal.
at+uartte=normal

Set for “throughput” (passthrough) mode on power up.
at+tmode=throughput

Set WiFi “station” (client) mode.
at+wmode=sta

Set the SSID of the wireless router or access point.
Use the actual SSID in place of <ssid>.
at+wsssid=<ssid>

Set the Wifi key or passphrase.
<auth>: open, shared (for WEP), wpapsk, wpa2psk.
<encryption>:
    none (for open),
    wep-h (WEP key in hex), wep-a (WEP key in ASCII),
    tkip, aes.
at+wskey=<auth>,<encryption>,<key>
E.g. at+wskey=wpa2psk,aes,This is my WiFi passphrase

Set the IP address configuration.
IPv4 addresses are in the form: x.x.x.x E.g. 192.168.2.1
at+wann=dhcp
or
at+wann=static,<ip>,<mask>,<gateway>

If using a static IP address or if DHCP doesn’t provide a
Domain Name Server (DNS) address, set it.
<dns> format is: x.x.x.x
at+wsdns=<dns>

If you want to change the hostname (module id) used for DHCP.
(Use at+mid to query the name.)
at+wrmid=<hostname>

If you want to reduce the transmitter power.
<pwr>: 0 to 24 (0.5dBm steps. 0 = 16dBm maximum, 24 = 4dBm minimum)
at+txpwr=<pwr>

Disable switching to AP mode if the WiFi connection fails and
set the check interval from the default 10 minutes to 5 minutes.
at+mdch=5

Set the network parameters to connect to the Blynk server.
If using a local server or gateway, adjust the port and address to match.
The address can also be a x.x.x.x IPv4 address.
**at+netp=tcp,client,**8442,blynk-cloud.com

Reduce the TCP “no data” timeout from the default 5 minutes to 2 minutes.
Value is in seconds.
at+tcpto=120

Disable socket B. It’s not used.
at+sockb=none

Disable the Network Time Protocol (NTP) client.
at+ntpen=off


Verify the connection

Before loading the Blynk sketch, the connection can be verified using the following commands. The Serial Monitor should be closed, then the Arduino and module should be powered off and on and then re-connected to the Serial Monitor, to make sure automatic connection is working. The sketch should display Switched to command mode … not Already in command mode … indicating that the module came up in throughput mode.

If attempting to diagnose and fix connection problems, the command
at+wifi=down followed by at+wifi=up
can be used after changing values, instead of cycling power and restarting the Serial Monitor.


WiFi link status.
at+wslk
Reply:
<AP’s SSID>(<AP’s MAC address>) - if link establised
Disconnected - if no link

Wifi link quality (receive signal strength).
at+wslq
Reply:
<quality>, <signal strength> - if link established
Disconnected - if no link

TCP connection status.
at+tcplk
Reply:
on - if TCP connection established
off - if TCP not connected

Ping an IP location.
at+ping=<IP address or domain name>
Reply:
Success - if ping response received
Timeout - if no reply
Unknown host - if DNS lookup failed
E.g. at+ping=blynk-cloud.com
     at+ping=192.168.1.1

Get local MAC address for STA mode
at+wsmac
Reply:
<MAC address> - in hexadecimal without colons


Load a Blynk sketch

Once the module is automatically connecting in throughput mode, this is a basic skeleton sketch that can be loaded to work with Blynk.

/**************************************************************
 * Blynk basic hardware serial for
 * Megavizon MangoCube WiFi or other Arduino using:
 * High-Flying Electronics HF-LPxx00
 * Usr IOT USR-WIFI232
 **************************************************************/

//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <BlynkApiArduino.h>
#include <Adapters/BlynkSerial.h>

typedef BlynkTransportSerial<HardwareSerial> ArduinoHwSerial;

static ArduinoHwSerial _blynkTransport(Serial1);
BlynkSerial<ArduinoHwSerial> Blynk(_blynkTransport);

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, 9600);
}

void loop() {
  Blynk.run();
}

As usual, connecting the Serial Monitor will print some connection information. Uncommenting the //#define BLYNK_DEBUG statement will print lots of diagnostic information.

2 Likes

Wow, that’s a comprehensive post. Thanks for a great job. :clap:

We might need to contact Mango Cube creators and add their stuff to Blynk.

@vshymanskyy could you please lead here and contact them.

I pieced together the above sketch, to run Blynk on an Arduino hardware Serial1 port, using the library’s BoardsAndShields examples, without fully investigating or understanding what the functions do.

Would an expert please answer: Is my code doing it correctly or is there a more proper or better way to implement hardware serial? (It seems work OK.)

Hi, I have a troubleshooting when I try to upload the code to my Nano board. I need special libraries for upload the sketch? The error is on the line of “typedef BlynkTransportSerial ArduinoHwSerial;”. I hope you can help me, and sorry for my english, it is not my first language.

Libraries have changed since the last post in May 2016. Read the docs and look at Sketch Builder.