Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| test-and-measurement:analog-discovery-pro-3x50:linux-thingspeak-demo [2021/10/13 21:06] – external edit 127.0.0.1 | test-and-measurement:analog-discovery-pro-3x50:linux-thingspeak-demo [2022/10/04 11:38] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Sending Data to ThingSpeak.com with Analog Discovery Pro (ADP3450/ | ||
| + | {{ reference: | ||
| + | ~~TechArticle~~ | ||
| + | ===== Overview ===== | ||
| + | ThingSpeak, created by MathWorks, is an online platform for collecting, viewing, and analyzing data in the cloud. This guide runs through the steps required to send data captured by the Analog Discovery Pro (ADP3450/ | ||
| + | |||
| + | To view examples for uploading data to ThingSpeak with other Test & Measurement devices, or with Analog Discovery Pro in Standard mode, check this guide: [[software: | ||
| + | |||
| + | To take measurements and to upload data to ThingSpeak without the help of a PC, one can use the Analog Discovery Pro (ADP3450/ | ||
| + | |||
| + | To successfully run this example, you will need the latest Linux version for your device. To update the ADP3450/ | ||
| + | ---- | ||
| + | |||
| + | ===== Inventory ===== | ||
| + | <WRAP group> | ||
| + | == Hardware == | ||
| + | * [[test-and-measurement: | ||
| + | * [[https:// | ||
| + | * A FAT/FAT32 USB drive | ||
| + | == Software == | ||
| + | <WRAP group>< | ||
| + | * On PC: | ||
| + | * [[software: | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | </ | ||
| + | * On Analog Discovery Pro: | ||
| + | * [[https:// | ||
| + | * WaveForms SDK - preinstalled | ||
| + | * [[https:// | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | **Note:** //WaveForms can be installed by following the [[software: | ||
| + | |||
| + | **Note:** //In this guide, the Python script will be created and edited on the PC, then copied to an USB drive to move it on the Analog Discovery Pro. If you choose to edit the script directly on your Test & Measurement device, the steps are similar.// | ||
| + | ---- | ||
| + | |||
| + | ===== Setting Up the Analog Discovery Pro (ADP3450/ | ||
| + | <WRAP group> | ||
| + | The first step is to connect your device to the internet. If you already have internet enabled for Linux mode, just connect the Ethernet cable and skip this step. | ||
| + | </ | ||
| + | |||
| + | --> Connect the Analog Discovery Pro (ADP3450/ | ||
| + | <WRAP group> | ||
| + | {{section> | ||
| + | </ | ||
| + | <-- | ||
| + | ---- | ||
| + | |||
| + | <WRAP group> | ||
| + | Now boot into Linux mode. If your device already is in Linux mode, just skip this step. | ||
| + | |||
| + | --> Booting into Linux Mode # | ||
| + | <WRAP group>< | ||
| + | Mode selection can be done through WaveForms using the Device Manager. With the Analog Discovery Pro connected to the host computer by Ethernet, open WaveForms. If the Device Manager hasn't opened yet, then click Settings -> Device Manager. | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Select the Analog Discovery Pro from the device menu, then click the Boot button. The Boot Mode window should open. By default, Standard Mode which allows the user to communicate with the instrumentation device over USB or Ethernet will be selected. | ||
| + | |||
| + | To change to Linux Mode, select Linux, then click "Apply & Reboot" | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | At this time, an audible click should be heard and the Analog Discovery Pro will disconnect from WaveForms. About 10-15 seconds should pass before it reconnects. Confirm that the second column next to the Analog Discovery Pro says " | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | <-- | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | ===== Configuring a ThingSpeak Channel ===== | ||
| + | <WRAP group>< | ||
| + | Go to [[http:// | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | In the // | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Configure the channel settings, name, and fields. The field will be the containers for your data. Configure a field for every data type that you want to store/ | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Under //API Keys//, note the **Write API Key** that will be needed to push data to the server from the custom application or script. The **Read API Key** can be used to access stored data. | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | When uploading data to a ThingSpeak channel, it will be displayed field-by-field on different plots. To modify these default plots, or add other data visualization methods to your channel, you can use the visualization or widget presets, you can modify the MATLAB code of the default plots, or you can write your own code to visualize the data. | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | To make your visualizations/ | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | ===== Script Overview ===== | ||
| + | <WRAP group> | ||
| + | The complete source code of this project can be downloaded from here: {{ : | ||
| + | |||
| + | Discussing in detail the usage of the WaveForms SDK is beyond the scope of this project. For more information about it, check [[test-and-measurement: | ||
| + | |||
| + | For a demo project with Pmod MIC3, check [[https:// | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | At the beginning of the script, the necessary modules are imported, then the address of the ThingSpeak channel is defined. To access your own channel, use your own API key in the address. | ||
| + | |||
| + | DIO lines used to communicate with the Pmod, as well as global variables controlling communication and data processing, are also defined here. Before calling any function from the WaveForms SDK, it is necessary to load the dynamic library. | ||
| + | </ | ||
| + | """ | ||
| + | import sys # This module provides access to some objects used or maintained by the interpreter and to functions that interact strongly with the interpreter | ||
| + | import time # This module provides various functions to manipulate time values | ||
| + | from ctypes import * # C data types in Python | ||
| + | import signal | ||
| + | import requests | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | """ | ||
| + | url = " | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | """ | ||
| + | # define the communication frequency in Hz | ||
| + | spi_frequency = 1e6 | ||
| + | # pin used for chip select (DIO 24 on Digital Discovery) | ||
| + | spi_CS = 0 | ||
| + | # pin used for master in - slave out (DIO 25 on Digital Discovery) | ||
| + | spi_MISO = 1 | ||
| + | # pin used for serial clock (DIO 26 on Digital Discovery) | ||
| + | spi_SCK = 2 | ||
| + | # samples to average | ||
| + | AVERAGE = 1000 | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | """ | ||
| + | if sys.platform.startswith(" | ||
| + | dwf = cdll.LoadLibrary(" | ||
| + | |||
| + | elif sys.platform.startswith(" | ||
| + | dwf = cdll.LoadLibrary("/ | ||
| + | |||
| + | else: | ||
| + | dwf = cdll.LoadLibrary(" | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | For repeating tasks like resetting and closing the used instruments, | ||
| + | </ | ||
| + | """ | ||
| + | |||
| + | |||
| + | def close_device(signum=0, | ||
| + | signum = frame # dummy | ||
| + | frame = signum | ||
| + | print(" | ||
| + | dwf.FDwfDigitalSpiReset(hdwf) | ||
| + | dwf.FDwfDigitalOutReset(hdwf) | ||
| + | dwf.FDwfDigitalInReset(hdwf) | ||
| + | dwf.FDwfAnalogIOEnableSet(hdwf, | ||
| + | dwf.FDwfDeviceClose(hdwf) | ||
| + | sys.exit() | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | |||
| + | """ | ||
| + | |||
| + | |||
| + | def display_error(err_msg=" | ||
| + | if err_msg == "No error": | ||
| + | err_msg = create_string_buffer(512) | ||
| + | dwf.FDwfGetLastErrorMsg(err_msg) | ||
| + | err_msg = str(err_msg.value)[2: | ||
| + | if err_msg == "": | ||
| + | err_msg = " | ||
| + | print(" | ||
| + | return err_msg | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | |||
| + | """ | ||
| + | |||
| + | |||
| + | def spi_read(): | ||
| + | lsb = c_int() | ||
| + | msb = c_int() | ||
| + | dwf.FDwfDigitalSpiSelect(hdwf, | ||
| + | time.sleep(.001) | ||
| + | spi_command = c_int(0) | ||
| + | dwf.FDwfDigitalSpiWriteRead(hdwf, | ||
| + | spi_command), | ||
| + | dwf.FDwfDigitalSpiWriteRead(hdwf, | ||
| + | spi_command), | ||
| + | time.sleep(.001) | ||
| + | dwf.FDwfDigitalSpiSelect(hdwf, | ||
| + | return lsb.value | (msb.value << 8) | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | |||
| + | """ | ||
| + | signal.signal(signal.SIGINT, | ||
| + | |||
| + | HIGH = c_int(1) | ||
| + | LOW = c_int(0) | ||
| + | DwfDigitalOutIdleZet = c_int(3) | ||
| + | |||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Before using a Test and Measurement Device, it needs to be connected to the host computer. To safely connect it, cases in which the device is used by other software, or is not available from some other reason, should be evited. | ||
| + | </ | ||
| + | """ | ||
| + | device_count = c_int() | ||
| + | dwf.FDwfEnum(c_int(0), | ||
| + | |||
| + | if device_count.value == 0: | ||
| + | # terminate the program if no devices are connected | ||
| + | display_error(" | ||
| + | sys.exit() | ||
| + | |||
| + | for device_index in range(device_count.value): | ||
| + | # get the name of the device | ||
| + | device_name = create_string_buffer(64) | ||
| + | dwf.FDwfEnumDeviceName(device_index, | ||
| + | |||
| + | # connecting the device | ||
| + | hdwf = c_int() | ||
| + | dwf.FDwfDeviceOpen(device_index, | ||
| + | |||
| + | # check for success | ||
| + | if hdwf.value != 0: | ||
| + | break | ||
| + | |||
| + | if hdwf.value == 0: | ||
| + | # terminate the program if the device can't be connected | ||
| + | display_error() | ||
| + | sys.exit() | ||
| + | |||
| + | # display message | ||
| + | device_name = str(device_name.value)[2: | ||
| + | print(device_name + " is connected\n" | ||
| + | |||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | To start communicating with the connected Pmod, it has to be powered. In devices which support this feature, the output voltage of the power supply should be set to 3.3V, as this is the recommended operating voltage of the Pmod. However, a supply voltage of 5V shouldn' | ||
| + | |||
| + | After the microphone is powered, the SPI interface should be initialized. After setting the communication parameters, a dummy read is performed to start driving the serial clock. | ||
| + | </ | ||
| + | """ | ||
| + | if device_name == " | ||
| + | dwf.FDwfAnalogIOChannelNodeSet(hdwf, | ||
| + | 0), c_double(3.3)) | ||
| + | elif device_name == " | ||
| + | dwf.FDwfAnalogIOChannelNodeSet(hdwf, | ||
| + | 0), c_double(True)) | ||
| + | dwf.FDwfAnalogIOChannelNodeSet(hdwf, | ||
| + | 1), c_double(3.3)) | ||
| + | elif device_name == " | ||
| + | dwf.FDwfAnalogIOChannelNodeSet(hdwf, | ||
| + | else: | ||
| + | display_error(" | ||
| + | close_device() | ||
| + | dwf.FDwfAnalogIOEnableSet(hdwf, | ||
| + | print(" | ||
| + | time.sleep(5) | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | """ | ||
| + | dwf.FDwfDigitalSpiFrequencySet(hdwf, | ||
| + | spi_frequency)) | ||
| + | dwf.FDwfDigitalSpiClockSet(hdwf, | ||
| + | dwf.FDwfDigitalSpiDataSet(hdwf, | ||
| + | dwf.FDwfDigitalSpiIdleSet(hdwf, | ||
| + | spi_mode = c_int(1) | ||
| + | dwf.FDwfDigitalSpiModeSet(hdwf, | ||
| + | dwf.FDwfDigitalSpiOrderSet(hdwf, | ||
| + | dwf.FDwfDigitalSpiSelect(hdwf, | ||
| + | |||
| + | # dummy read to start driving the channels, clock and data | ||
| + | dwf.FDwfDigitalSpiReadOne(hdwf, | ||
| + | |||
| + | print(" | ||
| + | time.sleep(1) | ||
| + | |||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Measurements are done continuously until Ctrl+C is pressed. The results of the measurements are averaged, then converted to dB (this conversion is just an approximation of the true noise level). | ||
| + | |||
| + | The result of the conversion is pushed to the ThingSpeak channel. If a communication error appears, or the process is interrupted, | ||
| + | </ | ||
| + | """ | ||
| + | print(" | ||
| + | |||
| + | while True: | ||
| + | try: | ||
| + | # receive initial data | ||
| + | level = 0 | ||
| + | |||
| + | # average data | ||
| + | for index in range(AVERAGE): | ||
| + | level += (spi_read() / 10) / AVERAGE | ||
| + | |||
| + | # calculate noise level | ||
| + | db = 729.0532027 - level * 0.4239354122 + \ | ||
| + | pow(level, 2) * 0.8875384813 * 1e-4 - \ | ||
| + | pow(level, 3) * 0.6195715088 * 1e-8 | ||
| + | |||
| + | # send data and check for errors | ||
| + | send_state = requests.get(url+"& | ||
| + | if send_state.status_code != 200: | ||
| + | display_error(" | ||
| + | close_device() | ||
| + | except KeyboardInterrupt: | ||
| + | break | ||
| + | |||
| + | # ---------------------------------------------------------------------------------------------------------- | ||
| + | |||
| + | """ | ||
| + | close_device() | ||
| + | |||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | ===== Hardware Setup ===== | ||
| + | <WRAP group>< | ||
| + | The Pmod MIC3 communicates on a SPI interface which will be connected to the Analog Discovery Pro's digital I/O lines as follows: SS to DIO line 0, MISO to DIO 1 and SCK to DIO 2. Power to the Pmod is also supplied by the Test and Measurement device used, so the VCC pin of the Pmod should be connected to the VIO pin on Analog Discovery Pro, and the grounds of the two devices should be connected together, as shown in the image to the right. | ||
| + | |||
| + | Prepare a FAT/FAT32 USB drive (while this is the default format for many USB drives, you may need to format it). Copy the Python script (PmodMIC3_to_Thingspeak.py) to the USB drive' | ||
| + | </ | ||
| + | {{ reference: | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | ===== Running the Script and Results ===== | ||
| + | <WRAP group> | ||
| + | To command the ADP3450/ | ||
| + | |||
| + | Tera Term will be used in this guide, but the steps are similar for each of the mentioned programs. | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Open WaveForms and open the //Device Manager// from the // | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Open the terminal emulator and establish a new Secure Shell (SSH) connection to the Analog Discovery Pro's IP address. You can leave every other setting as default. When required, enter the user name and the password: | ||
| + | |||
| + | |<50% 25% 25%>| | ||
| + | ^ user name: | digilent | | ||
| + | ^ password: | digilent | | ||
| + | |||
| + | You should have now a connected terminal. | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | <WRAP group>< | ||
| + | As the Python script uses the // | ||
| + | |||
| + | < | ||
| + | |||
| + | When required, enter the sudo password: | ||
| + | |||
| + | |<50% 25% 25%>| | ||
| + | ^ sudo password: | digilent | | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Check the drive letter and partition number of the USB drive by entering the following command: | ||
| + | |||
| + | < | ||
| + | |||
| + | It should look like /// | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | Make the drive contents accessible by entering the command: | ||
| + | |||
| + | < | ||
| + | |||
| + | And then: | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | You can start the Python script by entering the command below, where // | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | {{ : | ||
| + | </ | ||
| + | |||
| + | <WRAP group>< | ||
| + | The indicators can be added from the channel' | ||
| + | |||
| + | The measured noise levels can be plotted against time or displayed on a gauge widget. A histogram of the measured values can also be displayed, along with a virtual LED, to indicate dangerous noise levels. One important MATLAB function to use in data processing is [[https:// | ||
| + | </ | ||
| + | {{ : | ||
| + | {{ reference: | ||
| + | </ | ||
| + | ---- | ||
| + | |||
| + | ===== Next Steps ===== | ||
| + | Now that data can be transferred between the Analog Discovery Pro and ThingSpeak, the script can be modified as needed for your project. | ||
| + | |||
| + | For more information on WaveForms SDK, see its [[software: | ||
| + | |||
| + | For technical support, please visit the [[https:// | ||

