# ThingSpeak

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

This example shows how to read temperature and humidity data from ThingSpeak channel 12397 - Weather Station, which collects weather related data from an Arduino® device. You write the temperature and humidity data into your Dew Point Measurement channel, along with the calculated dew point data. Your channel then allows you to visualize the results.

### Prerequisite Steps

This example requires that you have already performed these steps:

### Write Data to Your Channel

This procedure reads humidity and temperature from the public WeatherStation channel Fields 3 and 4, and writes that data to Fields 2 and 1, respectively, of your Dew Point Measurement channel. Dew point is calculated and written to Field 3.

Use a MATLAB® Analysis app to read, calculate, and write your data.

1. Go to the Apps tab and click MATLAB Analysis. Then click New. Select the Custom template, and click Create.

2. In the Name field, enter ```Dew Point Calculation```.

3. In the MATLAB Code field, enter the following lines of code.

1. Save the public WeatherStation channel ID and your Dew Point Measurement channel ID to variables.

```readChId = 12397; writeChId = 677;```
2. Save your Write API Key to a variable.

`writeKey = 'F6CSCVKX42WFZN9Y';`

To find your Channel ID and Write API Key, refer to Channel Info on the My Channels tab.

3. Read the latest 100 points of temperature data with timestamps and humidity data from the public WeatherStation channel into variables.

```[temp,time] = thingSpeakRead(readChId,'Fields',4,'NumPoints',100); humidity = thingSpeakRead(readChId,'Fields',3,'NumPoints',100);```

### Calculate the Dew Point

Add the following MATLAB code to calculate the dew point using temperature and humidity readings:

1. Convert the temperature from Fahrenheit to Celsius.

`tempC = (5/9)*(temp-32); `

2. Specify the constants for water vapor (`b`) and barometric pressure (`c`).

```b = 17.62; c = 243.5;```

3. Calculate the dew point in Celsius.

```gamma = log(humidity/100) + b*tempC./(c+tempC); dewPoint = c*gamma./(b-gamma)```

4. Convert the result back to Fahrenheit.

`dewPointF = (dewPoint*1.8) + 32;`

5. Write data to your Dew Point Measurement channel. This code performs a batch update and includes the timestamp to correctly write data.

```thingSpeakWrite(writeChId,[temp,humidity,dewPointF],'Fields',[1,2,3],... 'TimeStamps',time,'Writekey',writeKey);```

The full block of code now appears like this:

6. Click Save and Run to validate your code.

Any errors in the code will be indicated in the Output field.

7. To see if your code ran successfully, click on your Dew Point Measurement channel link in the Channel Info panel.

The Dew Point Measurement channel now shows charts with channel data from each Field.

### Schedule Code

Use the TimeControl app to schedule the dew point calculation in your MATLAB Analysis code. Schedule it to read data from the weather station every 5 minutes and calculate the dew point.

1. On your MATLAB Analysis Dew Point Calculation page, scroll to the bottom, and click TimeControl to open the app with MATLAB Analysis preselected in the Actions field and the Dew Point Calculation as the Code to execute.

2. Name your new TimeControl Dew Point TC

3. Choose Recurring in the Frequency field.

4. Choose Minute in the Recurrence field.

5. Select 5 in the Every — minutes field.

6. Keep the Start Time at the default value.

7. Verify that the Action is MATLAB Analysis, and the Code to execute is your Dew Point Calculation.

8. Click Save TimeControl

### Visualize Dew Point Measurement

Use the MATLAB Visualizations app to visualize the measured dew point data, temperature, and humidity from your Dew Point Measurement channel. This example uses the `thingSpeakPlot` function to show all three data points in a single visualization.

Go to Apps > MATLAB Visualizations, and click New to create a visualization.

Alternately, you can click on MATLAB Visualization in your Dew Point Measurement channel view.

1. Name the visualization "Dew Point."

```readChId = 677 readKey = '36LPYCQ19U37ANLE'```

3. Read data from your channel fields, and get the last 100 points of data for:

• Temperature: from Field 1

• Humidity: from Field 2

• Dew point: from Field 3

```[dewPointData,timeStamps] = thingSpeakRead(readChId,'fields',[1,2,3],... 'NumPoints',100,'ReadKey',readKey);```
4. Plot the data with x and y labels, a title, and a legend.

```thingSpeakPlot(timeStamps,dewPointData,'xlabel','TimeStamps',... 'ylabel','Measured Values','title','Dew Point Measurement',... 'Legend',{'Temperature','Humidity','Dew Point'},'grid','on');```

Your code should look like this:

5. Click Save and Run. If your MATLAB code has no errors, the plot output should look like this:

### Next Steps

In the next example, Act on Your Data, you can track your calculated dew point to trigger an automatic tweet when a specified level is exceeded.