Homer
Published © GPL3+

PHPoC-Monitor Temperature & Humidity with Samsung IoT Cloud

Monitor humidity and temperature in your home: Send measured data from PHPoC to ARTIK (Samsung IoT) Cloud.

BeginnerFull instructions provided1,481

Things used in this project

Hardware components

PHPoC Blue
PHPoC Blue
×1
Grove - Temperature Sensor
Seeed Studio Grove - Temperature Sensor
In this project, I used Grove Temperature and Humidity Sensor (HDC1000), but you can choose other sensors.
×1
PHPoC Grove Expansion Board
PHPoC Grove Expansion Board
This is optional. You can directly wire the sensor to PHPoC via I2C pins.
×1

Software apps and online services

ARTIK Cloud for IoT
Samsung ARTIK Cloud for IoT

Story

Read more

Code

hdc1000.php

PHP
Library for Temperature and Humidity Sensor HDC1000
<?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, $data, 2);
	$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_ID, HDC1000_ADDR, "sm");
}


?>

task0.php

PHP
Main program: Read temperature and humidity data from sensor, and then send to ARTIK cloud
<?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);
}

?>

Credits

Homer

Homer

17 projects • 45 followers

Comments