Signed in as:
filler@godaddy.com
Signed in as:
filler@godaddy.com
<--Back to Panther Logger Tutorials
In this tutorial we will setup some code to turn on the Panther Logger's WiFi module, search for networks, and send some mock data to different online databases. This tutorial assumes one has familiarity with Arduino programming or has gone through the prior Panther Logger tutorials.
Sections in This Tutorial
To get started we need the WiFi101 library installed in Arduino IDE. Go to Sketch>Include Library>Manage Libraries. In the search box type wifi101. Click on WiFi101 and if not installed then install the latest version. If it is already installed check to be sure the latest version is installed which is 16.1. When finished close the dialog box and restart the Arduino IDE.
The Panther Logger board has a dip switch located on the right side of the board that allows one to keep the WiFi modem and LoRa modems off if not used. Since we will be using the WiFi modem in this tutorial, flip the right dip switch labeled "WiFi" up. This dip switch allows current from the 3.3V switched rail to flow to these modules. The 3.3V switched rail needs to be turned on in code for these modules to work (see below).
The dip switch is just below the LoRa and cell modem on the right side of the board
Let's look for available networks in your area. Go to our Github page here for the WiFiScanNets code. Copy and paste it into a new Arduino script. Save the script with a new file name of your choice.
In the setup function of this script the WiFi module needs to be turned on first. We set the dip switch on the Panther Logger board to use the WiFi module, but this only enables power to the module from the 3.3V switched rail. The 3.3V switched rail needs to be switched on in code in order to power the module. To do so, pin 4 on the MCP expander chip on the Panther Logger board needs to be set high. Near the beginning of the setup function you will see how this is programmed:
mcp.pinMode(4,OUTPUT); //Set mcp pin 4 as output
mcp.digitalWrite(4,HIGH); //Set mcp pin 4 high
In addition, we need to set the chip enable pin high on the WiFi module to enable it. We can do that with pin 15 on the MCP by setting it high with the following code:
mcp.pinMode(15, OUTPUT);
mcp.digitalWrite(15,HIGH);
However, the datasheet for the WINC1500 states that this enable pin should be low at power up (Table 2-1). As such, the sequence of events we program in the setup function to get the WiFi module up and running correctly should be the following:
mcp.digitalWrite(4,LOW);
mcp.digitalWrite(15,LOW);
mcp.digitalWrite(4,HIGH);
mcp.digitalWrite(4,HIGH);
So, with the above sequence of events we turn off the WiFi module, then set enable pin low and then power up the WiFi module with the enable pin low and then set enable pin high. This is all done in the WiFiScanNets setup function and in all other scripts using the WiFi module.
If the WiFi module were to be powered on at the same time as the processor was turned on, this sequence above might not be necessary, but the Panther Logger provides full control to turn on and off modules within code. Note that if you set the enable pin low after this power up sequence, this will put the module into Power-Down mode, which could be useful for saving battery power.
With the module powered up correctly, we then need to tell the WiFi101 library what pins are to be used for CS, IRQ, RST and Enable.
The CS pin is the chip select pin for SPI communications. This pin is used to turn on SPI communications between this module and the processor as opposed to other SPI devices like the SD card on the Panther Logger.
The IRQ pin is the Interrupt Request pin which allows the WiFi module to occasionally ask the processor to stop its activity momentarily so the WiFI module can finish communicating.
The RST is the reset pin, which if pulled low will reset the WiFi module.
The Chip Enable pin on the Panther Logger is pin 15 on the MCP we discussed above and we are managing this manually.
The Panther Logger does not use the default pin numbers that the WiFi101 library is expecting for these pins. So we run the function WiFi.setPins in setup like below to tell the library what pins we are using for these functions:
WiFi.setPins(9,7,3,-1);
The arguments for this function are (in order):
There is code to turn on the WiFi power LED, the green LED above the WiFi module labeled "WiFi PWR." That code looks like this:
m2m_periph_gpio_set_dir(M2M_PERIPH_GPIO6,1);
delay(100);
m2m_periph_gpio_set_val(M2M_PERIPH_GPIO6,0);
The first line above sets the GPIO pin #6 on the WiFi module to output, then we give a short delay and the final line sets the GPIO pin #6 to ground, which turns on the green LED (see image below).
The other LED labeled "WiFi Activ" is the WiFi activity indicator LED and will flash on its own when sending or receiving data.
The rest of the code in this script is not specific to the Panther Logger and contains the basic commands needed to scan for available networks that is in the example scripts provided with the WiFi101 library. Go ahead and upload the code and then open the serial monitor. You will see a printout of available WiFi networks in your area. It might take a little while for networks to be detected.
To continue, you will need the WiFi credentials to login to one of these networks. Note that the WINC1500 WiFi module on the Panther Logger should be able to connect to enterprise networks, but we have not had experience with this. Others have made modifications to get this working (see here). For now, use a non-enterprise WiFi network on a router such as used on a home network. You can also connect to most smart phone hotspots.
The WiFi green power LED can be turned on in code
We will show here how to send data from the Panther Logger to a few of the many IoT Databases that have free starter accounts or at least free trials. Most of them have an HTTP API service allowing us to send data using what is known as a REST (Representational State Transfer) protocol using GET or POST methods. These are hardware and language agnostic methods. We can send data to many different databases without relying on any platform specific libraries.
We will start out with using GET to send data to ThingSpeak and then show how to use POST methods to send data to Ubidots, Thinger, and ThingBoard (and possibly others as we update this tutorial.)
To get started, let's send data to ThingSpeak with GET. You will need to go to https://thingspeak.com/ and setup a free account. Once you have an account, login and click "New Channel" (see video to the right). Give the channel a simple name and description. The name only needs to be unique to the channels in your account. I'll call mine "loadstar" but you can choose whatever you want.
You have up to 8 fields in your channel that can receive sensor data from your device (if you need more then you can make an additional channel later to hold more fields). Let's say you have a weather station. Those 8 channels could be named:
Air Temperature
Humidity
Barometric Pressure
Windspeed
Wind Direction
Dew Point
Wind Chill
Battery Voltage
So for now go ahead and name the fields with those names to get started sending some mock weather data. The other options in the channel setup form are not necessary right now so just click save channel after naming the fields. Note that you do not need to remember the exact spelling of these fields for ThingSpeak, but you do need to remember the order.
After clicking save, click on the channel name and then click on the private view tab, which should be displayed by default. Here you can setup some graphs to display new data that is coming in. To do so click on add visualizations and select a field to be displayed in a graph. Do this for all fields. You can also setup a gauge for battery level if desired. These graphs and widgets will display the data once we start sending it.
Now click on the API_Keys tab. Copy the "write API key" or write it down as it will be needed later.
Go to our Github page for the "SendData_WiFi_ThingSpeak" code here. Copy and paste it into a new Arduino script, save as a new filename of your choice.
This script will connect to your local WiFi network, make up some weather data and send it to ThingSpeak.
You will need to enter your login credentials for your local WiFi network in the code starting with:
char ssid[] = "XXXXX";
char pass[] = "XXXXX";
The SSID is the name of your WiFi network and pass is the password. These should be in quotes and keep the semi colons at the end of each line.
Then where you see:
char ThingAPIKey[] = XXXXXXXXX
Replace the X's with your write api key you copied from ThingSpeak.
Save and then upload this code to the Panther Logger and open the serial monitor to watch it send data.
The script will send some mock data to ThingSpeak using the GET method and then read the server response. The response is a json formatted message containing the channel ID number, the time data was sent, the "entry_id" which corresponds to the number of data points sent and the data that was sent. Look at your ThingSpeak channel and you will see the channel ID number at the top of the page under the channel name. This number should match the channel ID number in the server response.
We will now show how to send data to Ubidots.com using the WiFi modem on the Panther Logger and a POST method. Go to Ubidots.com and sign up for a new account with free trial or if you are a student sign up for a free student STEM account at https://ubidots.com/stem. After setting up a new account go to "Devices" and click the plus button in the upper right to setup a new device (see video on the right). Choose to setup a "blank" device. In the next dialog box that appears add a name for your device. You will enter this name later in the Arduino script to send data. Click the check mark at the bottom of the page. Now, click on the device just created in your list of devices. The device dashboard page appears. On the left you will see that there is a "token" which is hidden. You will need to copy and paste this into code in Arduino. You can copy it with the copy button next to the token (see video on right). You can add a description of your device if you want, but otherwise nothing else is needed right now.
Go to our Github page for the "SendData_WiFi_Ubidots" code here. Copy and paste into a new Arduino script and save as a new file name. Find the section of the code that starts with:
char ssid[] = "XXXXXXXXXXX";
char pass[] = "XXXXXXXXXXX";
char server[] = "industrial.api.ubidots.com";
char UbiToken[] = "xxxxxxxxxxxxx";
char UbiDeviceName[] = "xxxxxxxxx";
Here, you need to replace the X's with the name of the WiFi network to connect to and the password. You also need to replace the x's with the token from ubidots associated with your new device and the device name (exactly as entered in Ubidots).
Save the sketch and upload to the Panther Logger.
After uploading code, open the serial monitor. You will see the WiFi modem connect to Ubidots (see video on right). A successful transmission of data will return "HTTP/1.1 200 OK." Other useful information is also returned like the server date and time and json formatted data with HTTP code 201 for each variable sent indicating that new data was created.
When data is sent for the first time to a Ubidots device something kinda cool happens. Without us even setting up new variables, Ubidots automatically creates them based on the data that was sent. Keep in mind that if you change the spelling or misspell one of the variables (temp vs temperature) a new variable will be created.
In contrast to how we sent data to ThingSpeak by GET, here data was sent with POST. Using the POST method requires header information. In the Arduino script this begins with the line:
client.print("POST /api/v1.6/devices/");
The syntax and type of information sent in the header is critical to a successful data transmission and varies somewhat between APIs. Using an API tester like postman can be very useful for troubleshooting problems with this header information.
We will now send data to two more IoT data hosting sites using a POST method. This will be very similar to Ubidots, but with all three cases the header information required is different. In addition, each service handles data and device setup a little differently.
Lets start with ThingsBoard. Go to the ThingsBoard login page (here) and click on "signup" at the bottom. Setup a new free trial account. Login to your account and in the left panel click on entities > devices. In the upper right side of the page click on the "+" button to add a new device. Give your device a name and label and then click on the "Add" button at the bottom. You will see your new device now in the list of devices. Click on this new device and a device detail window opens (see video on right). Click on the "Copy access token" button to copy the token. You will need to paste this token into the Arduino script to send data to ThingsBoard.
Go to our Github page for the SendData_WiFi_ThingsBoard script here. Copy and paste into a new Arduino script and save it with a new file name. Go to the section of code here:
char ssid[] = "XXXXXXXX";
char pass[] = "XXXXXXXX";
char server[] = "thingsboard.cloud";
char Token[] = "XXXXXXXX";
Replace the X's with the information required including the token you copied from ThingsBoard device details page.
Upload the script to the Panther Logger and open the serial monitor. Once data is successfully sent the monitor will report "HTTP/1.1 200." ThingsBoard doesn't return any information except the HTTP code and the time.
Go back to your ThingsBoard account and click on dashboard in the left panel then click on the "+" symbol off to the upper right. Give your dashboard a title and description and click "Add" at the bottom. Your new dashboard appears in the list of dashboards. Click on it and click on the "Add Widget" button. Choose a time series chart. For the data source choose your new device name. Under series delete the default "temperature" listed for the series set and click in the box to display the variables associated with your device. Since we have already sent temperature and battery voltage data those two variables should be listed. You can choose either one and just click add. A live time series chart now is displayed in the dashboard. Every time you see data sent successfully in the Arduino serial monitor you will see this chart update.
There many other chart widgets available to explore visualizing the data. For example, you could display battery voltage in a gauge.
Now let's send data to Thinger.io using the WiFi modem and POST method. Go to the Thinger.io login page here and click on the "Create an account" button. After your account is setup click on Devices on the left panel and then click "create a device" (see video to the right). For device type select HTTP device from the drop down list. Give the device a name in the device ID box then click Add Device at the bottom. A device dashboard will then appear. In the upper right click on the button with three horizontal lines next to the properties button. Select "callback" in the drop down list. Copy the Authorization key at the bottom of the page that is displayed.
Go to our Github page for the SendData_WiFi_Thinger code here. Copy it and paste it into a new Arduino script then save it as a new file. Find the part of the code here:
char ssid[] = "XXXXXXX";
char pass[] = "XXXXXXXX";
char ThingerToken[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Replace the X's with your WiFi network name and password as well as the authorization key (token) from Thinger.
Go back to Thinger and in the callback details page click on the overview tab. Look at the target URL. In the Arduino script, find the code:
char server[] = "backend.thinger.io";
Make sure the server given in the target url is the same as listed here. If not then change it to match.
Upload this code to the Panther Logger device and open the serial monitor. The Thinger server returns "HTTP/1.1 200 OK" indicating successful data transfer.
Let's go back to our Thinger account and setup some data handling and visualizations (See video to the right). On the left panel of the Thinger account page click on "Data Buckets" and then click on create a data bucket. Give the data bucket a name and ID like "PantherBucket" for both. Set the data source to be from device resource and then select the device we set up previously. For resource name just type anything for now. We will put Panther Logger. Select "Stream by Device" for refresh mode. Click on the "Add Bucket" button at the bottom to create the bucket.
Click on devices and then on the device name we set up above. Go to the call back details page and click the "Write Bucket" check box and then in the pull down list select the data bucket we just created. Click save button at the bottom of the page.
Go back to Data Buckets and click on the data bucket we just made. The bucket will now start filling with rows of the temperature and battery data we are sending from our device.
Click on Dashboards on the left panel and then click on create a dashboard. Add a dashboard ID, name and description and then click on the "Add Dashboard" button. You will see a page now that says "empty dashboard." Ok, lets make it not empty. Click on the write button in the upper right corner and then click on "Add Widget." Name the widget temperature and then check the "link to" box and select the dashboard name we just created. In the "type" drop down box select a time series chart. Select as the data source the data bucket we just created and temperature as the variable. Choose "configurable" as the time frame option. Then add widget with the save button. You will now see temperature time series plot and it will be updated with new data coming in from our device. You can also setup other types of widgets like a battery gauge.
The WiFi modem on the Panther Logger will draw a relatively large amount of current (~150 mAmps) constantly if it remains connected to the network. Connected to a wall outlet that's not so bad, but in a mobile battery operated application it would be best to disable the modem and possibly even sleep the board to reduce this power consumption.
To try this out go to our github repository and copy/paste the "SendData_WiFI_ThingSpeak_PowerSave1" script into a new Arduino file and save it with a name of your choice. This is a modification of our "SendData_WiFI_ThingSpeak" script. As previously, replace the SSID name and password for your network and the API key for ThingSpeak. Note that this is just a modifcation of the "SendData_WiFI_ThingSpeak" script above. It could be modified to send data to any online database.
The specific modifications in this script are to 1) disable the WiFi modem after data is sent, 2) turn off the 3.3VS power rail removing power to the modem, and 3) implement a function to sleep the SAMD21 microprocessor. To do these tasks, at the end of the loop we send the command, WiFi.end() to disconnect the modem from the WiFi network, then set pin 15 on the Panther Logger's mcp GPIO expander low to disable the modem and then the 3.3VS rail is turned off by dropping pin 4 on the mcp GPIO expander low. Then the Panther Logger's processor is put to sleep for 10 x 6 second sleep intervals or for 1 minute. The board will wake up after 1 minute and enter the start of the loop again. At the beginning of the loop we now need to turn on the 3.3VS rail by setting pin 4 on the mcp GPIO expander high. We also need to re- enable the WiFi modem by setting pin 15 on the mcp expander high and re-initiate a connection to the network with a WiFi.begin statement.
With the modem disabled and no sensors attached to the Panther Logger the board will pull about 45 mAmps. If the sleep function is also used then the board will pull about 10 to 15 mAmps. This assumes a cellular modem is not plugged into the board, no sensors are being powered and the LoRa modem is off. With the sleep functions enabled the Panther Logger's processor will likely stop reporting messages to the serial monitor. To know that it is working you should check for updated data at ThingSpeak.
Sending data to HTTPS addresses might require that the WiFi modem contains the correct SSL certificate, which enables an encrypted connection to the server's domain. To do upload a certificate we will use the older Arduino 1.x versions of the IDE. See here to get it. Connect the Panther Logger to your computer using a USB cable and open the Arduino IDE. Head over to our Github repo and download or copy/paste the WiFiFirmwareUpdate script into a new script and save it. Upload the script to the board and be sure the dip switch for the WiFi modem is on. Do not open the serial monitor. Instead click on the tools menu and then on Firmware updater. A new window opens up. Select the port connected to the Panther Logger. You can test the connection. In the bottom window click add a domain and type in the domain name of the HTTPS server that you want to connect to via WiFi. Click on "Upload certificates to the modem."
To send data securely via SSL connections, change the client class to WiFiSSLClient instead of WiFiClient and use the function connectSSL instead of connect. In the connect function instead of port 80 use port 443.
Copyright © 2023 ElectroRex - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.