Mongoose networking library on PIC32 chip
This example shows you how to build web server (or a TCP server, CoAP server, MQTT broker etc.) using a PIC32 chip and Mongoose Embedded Web Server in 5 minutes.
PIC32 is a big family of microprocessors with different features. Some of the chips have integrated ethernet. We won’t focus on those. Mongoose uses Harmony TCP/IP stack, thus, it’s possible to use any hardware supported by this library.
We are going to use a PIC32MX795F512L chip, Explorer 16/32 development board and Fast Ethernet PICTail Plus.
For this project you’ll need:
- And of course, any PIC32 chip with any ethernet controller supported by Harmony TCP/IP Stack
Also, you need Mongoose. Download sources from here.
Building the example
The simplest way to go, is to use example shipped with Mongoose. It is located in mongoose/examples/PIC32/http_server folder on GitHub.
- Open it with MPLAB X IDE.
- Out-of-box this project uses a PIC32MX795F512L chip. You can change it in the usual way (Right click on project->Properties->Device).
- Also, Fast Ethernet PICTail Plus and Explorer 16/32 are used by default. It can be changed in the Harmony Configurator. (Tools->Embedded->MPLAB Harmony Configurator). Configure the ethernet controller (and its dependencies, like SPI) and BSP.
- Depending on your development board configure the console system service (it is a UART console by default).
Running the example
Now, we are ready to run the example.
First of all, connect the terminal according to the console system service configuration. In my case (UART console + Explorer 16/32) I use an integrated UART-USB convertor, which appears in my system (Ubuntu Linux 16.04) as /dev/ttyACM0. I connect to it with picocom:
$ picocom /dev/ttyACM0 -b 115200
Press “Program Device” in MPLAB IDE, and if everything was set up correctly you’ll see something like this in your serial monitor
The output may vary depending on your hardware. But, at this moment we need to know board IP address, it is 192.168.10.24 in my network.
Now, navigate you browser to http://192.168.10.24/hello and you’ll see something like the picture below:
This is simple greeting page is created by the Mongoose event handler.
Congratulations, the simplest web server on PIC32 works!
Mongoose keeps things extremely simple. You can change the web server to CoAP server or something else easily. Check out the Mongoose documentation for more.
A couple of words how it works behind the scenes.
The example shipped with Mongoose is based on another example - shipped with Harmony. You can choose any suitable example from harmony’s tcpipfolder. Client or server - it doesn’t matter (and, of course, you can create a project from scratch. But, in this case you need to write Harmony TCP/IP initialization code, and the real heroes are lazy, right?).
So, we take the Harmony example (I used tcpip_tcp_client) and change it in order to use it with Mongoose:
- Add mongoose.c and mongoose.h to your project
- Add -DCS_PLATFORM=11 to xc32-gcc “Additional options” (11 is a PIC32 Harmony in Mongoose)
- Go to app.h file and replace APP_STATES enumeration with this one:
As you can see, we need only APP_STATE_INIT and APP_TCPIP_WAIT_FOR_IP from the original stuff.
- Add mg_mgr_init(&mgr, NULL); to APP_Initialize function (and declaremgr variable)
In app.c modify APP_Tasks function:
- Remove all unnecessary states from the switch statement
- Add case APP_START_LISTENING and write code which creates a listening connection and changes the application state to APP_POLL
- Optionally, add case APP_DONE which prints the information that the server was stopped
Finally write the Mongoose event handler. This example uses a very simple event handler.
That’s it. Your web server is ready.