Wimpertier

Send data from NodeMCU to WordPress – part 1/2

This experiment has differents parts. One part is the NodeMCU with the temperature sensor. This part takes a measurement each hour and sends i to the WordPress Rest-Api. The another part is how the data goes through the WordPress Rest-Api. In this tutorial we configure an application that allows us an Oauth1.0 authenfication.

Goal: a table with data from NodeMCU

The tutorials goal: a table with the actual soil temperature send from NodeMCU to WordPress Rest-Api. NodeMCU takes measurements hourly.

You see an example on the bottom from this blog post.

Enhance your WordPress Rest-api with plugins

I chose the two plugins “Disable WP Rest-Api” and “WP REST API OAUTH Server 1.0a”. I know these both plugins are a bit outdated, but good enough for my project.

By default the WordPress API is open and reachable for everyone. I closed it with the “Disable WP Rest-api”. But then I opened the Rest-Api for an application through the ” WP REST API OAUTH Server 1.0a ” Plugin.

Create a plugin for your application

I created a plugin to add custom fields to the WordPress Rest-api. Then I created a shortcode to get the fields in a post.

Add custom fields to the WordPress Rest-api

Add the following lines to your plugin. This code add the necessary code for the custom fields in the post. You need two fields, one for the temperature and one for the timestamp.

add_action( 'rest_api_init', 'wt_register_posts_meta_field' );

function wt_register_posts_meta_field() { 
$object_type = 'post';
$args1 = array( 
'type' => 'string',
'single' => true,
'show_in_rest' => true,
);
register_meta( $object_type, 'wt_soil_temperature', $args1 );

$args2 = array( 
'type' => 'string',
'single' => true,
'show_in_rest' => true,
);
register_meta( $object_type, 'wt_timestamp', $args2 );
}

Add a shortcode to output the custom fields

add_shortcode( 'soil_temperature', 'wt_soil_temperature');

function wt_soil_temperature() {

$wt_soil_temperature = get_post_meta( get_the_ID(), 'wt_soil_temperature', true );

$wt_timestamp = get_post_meta( get_the_ID(), 'wt_timestamp', true );
$wt_timestamp = date( 'Y-m-d H:i', $wt_timestamp );
	
ob_start();
?>
<table>
<tr>
<td colspan="2">Facts about the soil termometer</td>
</tr><tr>
<td>Jordtemperatur: </td>
<td><?php echo $wt_soil_temperature; ?></td>
</tr><tr>
<td>Last measurement: </td>
<td><?php echo $wt_timestamp; ?></td>
</tr>
</table>
<?php
return ob_get_clean();
}

Add an application to the WordPress Rest-Api

Make following steps to register an application with Oauth1.0 authentication.

The “application” tab has three fields. One for the application name, one for the description and another one for the callback. In my case I filled in the fields the following words.

Application name:SoilTemperature
Description:Measures the soil temperature hourly.
Callback:https://www.example.com/soiltemperature

Authorize the application and get the keys.

For the next steps I downloaded and used a program called “Postman”. You can get it for free from https://www.getpostman.com/. Open the program and do following:

If it goes right, the server sent you an oauth_token and an oauth_token_secret. Note this two values. You need them for the following steps.

Now you get a new oauth token and oauth token secret. Note them too. These are the definite values.

More information about this step find you in this blog post https://www.datafeedr.com/using-oauth-1-0-wordpress-api-custom-endpoints/.

In the next part we are setting up the NodeMCU with the temperature sensor. It includes also the code for the NodeMCU.

No time to wait? You see the whole code on Github Wimpertier.

Another Posts

Send data from NodeMCU to WordPress – part 1/2 2019-05-22