Click image for larger version

Name:	Architecture.png
Views:	1
Size:	111.5 KB
ID:	418


As you can see in other blogs, PHPoC devices can provide real-time monitoring via web or email. I just want to introduce another feature of PHPoC: monitoring via IoT cloud.

Samsung ARTIK Cloud is an open data exchange platform for the Internet of Things, which allows us to collect, store, visualize and analyze data from any device. For more information, you can visit https://artik.cloud.

This is a simple project that shows how to read temperature and humidity data from sensor, then send data to ARTIK cloud, by using PHPoC Blue. It's one more step to set up your own IoT system: monitoring temperature and humidity in your home via cloud server.

Demo



Hardware components:
PHPoC Blue × 1
Grove Temperature and Humidity Sensor (HDC1000) × 1
PHPoC Grove Expansion Board × 1

Wiring
- Stack Grove expansion board to PHPoC Blue
- Connect Grove Temperature and Humidity sensor (HDC1000) via I2C connector.

Click image for larger version

Name:	wiring_SrTXpy0Uez.jpg
Views:	1
Size:	40.7 KB
ID:	426


Setup ARTIK Cloud


First step is to create a new device type.
- Sign in to Developer Dashboard https://developer.artik.cloud/
- Select the tab Devices Types https://developer.artik.cloud/dashboard/devicetypes
- Click the "+ New Device Type"
- Set the device display name "HDC1000" and give it a unique name, for example, "com.phpoc.hdc1000"
- Click "Create Device Type" to create the device type

Create a new Manifest for the new device type
- Click "HDC1000" ==> "Set Up" ==> "Manifest" and then "New Manifest"
- Add two new data field, "temp" and "humid", with data type is Double, as below.

Click image for larger version

Name:	Manifest.png
Views:	1
Size:	51.0 KB
ID:	419

- Then "Next: Device Actions", and leave Actions as default
- Click "Activate Manifest" to finish creating new device type.

Click image for larger version

Name:	Active Manifest.png
Views:	2
Size:	65.1 KB
ID:	421

Go to MY ARTIK Cloud to create a new "HDC1000" device.

- Sign in to MY ARTIK Cloud https://www.artik.cloud/

Click image for larger version

Name:	CreateDevice.png
Views:	1
Size:	49.6 KB
ID:	422

- Choose Devices ==> select the "HDC1000" device type to add a new "HDC1000" device.

Click image for larger version

Name:	AddDevice.png
Views:	1
Size:	106.2 KB
ID:	423

- Click "Add device", then set the name for this device (e.g. Temp and Humid Sensor)
- Select the newly created device. In the pop-up, click "GENERATE DEVICE TOKEN…".

Click image for larger version

Name:	GenerateToken.png
Views:	1
Size:	16.0 KB
ID:	424

After that, copy the Device ID and Device token. Set those values in your code (task0.php). Now the ARTIK setting up part is done.

$cur_device_id = "YOUR_DEVICE_ID";
$cur_device_token = "YOUR_DEVICE_TOKEN";

Sending data and monitoring on ARTIK Cloud

After the connection with ARTIK cloud has been set up, you can start sending data and visualize it on ARTIK.

- PHPoC will continuously read data from Temperature and Humidity Sensors, then it will send the message to ARTIK cloud using Web REST protocol.
- On MY ARTIK Cloud page, select Charts https://my.artik.cloud/data. From this window, you can monitor the visualized data.

Click image for larger version

Name:	Visualize.png
Views:	1
Size:	39.9 KB
ID:	425

Source code


hdc1000.php: Library for Temperature and Humidity Sensor HDC1000
PHP Code:
<?php

if(_SERVER("REQUEST_METHOD"))
   exit; 
// avoid php execution via http request

include_once "/lib/sd_340.php";

define ("I2C_ID",   0);
define ("HDC1000_ADDR",      0x40);

define ("HDC1000_TEMP",      0x00);
define ("HDC1000_HUMID",      0x01);
define ("HDC1000_CONFIG",      0x02);

define ("HDC1000_SERID_1",      0xFB);
define ("HDC1000_SERID_2",      0xFC);
define ("HDC1000_SERID_3",      0xFD);
define ("HDC1000_MFID",         0xFE);
define ("HDC1000_DEVID",      0xFF);

define ("HDC1000_RST",         0x80);
define ("HDC1000_HEAT_ON",   0x20);
define ("HDC1000_HEAT_OFF",   0x00);
define ("HDC1000_BOTH_TEMP_HUMID",   0x10);
define ("HDC1000_SINGLE_MEASUR",      0x00);
define ("HDC1000_TEMP_HUMID_14BIT",   0x00);
define ("HDC1000_TEMP_11BIT",      0x40);
define ("HDC1000_HUMID_11BIT",   0x01);
define ("HDC1000_HUMID_8BIT",      0x02);

function 
set_register($reg)
{
   
i2c_write(I2C_ID,($reg&0xff),1);
   
usleep(20000);
}

function 
read_16bit()
{
   
$data 0;
   
$LSB 0x00;
   
$MSB 0x00;
   
i2c_read(I2C_ID$data2);
   
$LSB = ($data>>8)&0xFF;
   
$MSB $data&0xFF;

   return ((
$MSB<<8)|$LSB);   
}

function 
read_config()
{
   
set_register(HDC1000_CONFIG);
   return 
read_16bit();
}


function 
get_rawtemp()
{
   
set_register(HDC1000_TEMP);
   return 
read_16bit();
}

function 
get_rawhumid()
{
   
set_register(HDC1000_HUMID);
   return 
read_16bit();
}

function 
get_temp()
{
   
$temp = (float)get_rawtemp();
   return (
$temp/65536.0*165.0-40.0);
}

function 
get_humid()
{
   
$humid = (float)get_rawhumid();
   return (
$humid/65536.0*100.0);
}

function 
hdc_1000_init()
{
   
i2c_setup(I2C_IDHDC1000_ADDR"sm");
}


?>

task0.php: Main program. Read temperature and humidity data from sensor, and then send to ARTIK cloud
PHP Code:
<?php

if(_SERVER("REQUEST_METHOD"))
    exit; 
// avoid php execution via http request

include_once "/lib/sd_340.php";
include_once 
"/lib/sn_http_b1.php";
include_once 
"hdc_1000.php";

$cur_device_id "YOUR_DEVICE_ID";
$cur_device_token "YOUR_DEVICE_TOKEN";


function 
artik_post_msg($post_body$device_token)
{
   
$post_len strlen($post_body);
   
http_req_header("Content-Type: application/json");
   
http_req_header("Authorization: Bearer $device_token");
   
http_req_header("Content-Length: $post_len");
   
$resp_head http_request("POST""https://api.artik.cloud/v1.1/messages"$post_body);

   if(!
$resp_head)
   {
      echo 
"artik connection failed\r\n";
      return 
0;
   }
   
$http_status = (int)http_find_header($resp_head"Status-Code");
   if(
$http_status != 200)
      echo 
"artik unexpected status $http_status\r\n";
   
$resp_body "";
   
http_read_sync($resp_body);
   
http_close();
   return 
$resp_body;
}


function 
gen_hdc_1000_msg($device_id$temperature$humidity)
{   
   
$msg ="{ "sdid": "$device_id", "type": "message",\n "data": {"temp":$temperature, "humid":$humidity}}";
   return 
$msg;
}


hdc_1000_init();
while(
1)
{
   
$temp get_temp();   
   
$humid get_humid();
   echo 
"Current temperature: $temp°C; humidity: $humid%\r\n";   

   
$rest_body gen_hdc_1000_msg($cur_device_id$temp$humid);   
   
artik_post_msg($rest_body$cur_device_token);   
   
usleep(200000);
}

?>
Attached Files