Integrating Temperature, Light, and Infrared Sensors in PHP IoT Projects
This article explains how to connect and program temperature‑humidity, light, and infrared sensors in PHP‑based IoT projects, covering serial communication setup, Raspberry Pi GPIO handling, data parsing, and sample code for each sensor type.
With the rapid development of IoT technology, many devices need network connectivity for remote control and data collection; sensors are core components, and integrating them into PHP projects requires specific code to read and process their data.
1. Temperature‑Humidity Sensor – Connect the sensor to a serial port, configure the port with a terminal tool, then use the PHP dio extension to read data. Example code:
<code><?php
$fd = dio_open('/dev/ttyUSB0', O_RDWR | O_NOCTTY | O_NONBLOCK);
dio_fcntl($fd, F_SETFL, O_SYNC);
if ($fd) {
$data = dio_read($fd, 1024);
echo $data;
dio_close($fd);
}
?></code>The device path /dev/ttyUSB0 and flags O_RDWR | O_NOCTTY | O_NONBLOCK configure the serial connection. After reading, parse the hexadecimal payload (e.g., "0xaa 0x01 0x08 0x1e 0x45 0x00 0x46 0xaa" ) to extract temperature and humidity:
<code><?php
$data = "\xaa\x01\x08\x1e\x45\x00\x46\xaa";
$data = bin2hex($data);
$len = strlen($data);
if ($len >= 18 && $data[0] == 'aa' && $data[1] == '01' && $data[$len-1] == 'aa') {
$temperature = hexdec($data[3].$data[4]) / 10;
$humidity = hexdec($data[5].$data[6]) / 10;
echo "Temperature: $temperature C\n";
echo "Humidity: $humidity%\n";
}
?></code>2. Light Sensor – The sensor outputs an analog voltage; on a Raspberry Pi it can be read via the bcm2835 extension. Example code initializes the library, configures GPIO 18 as input, sets PWM parameters, and continuously reads the analog value:
<code><?php
include('bcm2835.php');
if (!bcm2835_init()) {
die('Unable to initialize BCM2835 library.\n');
}
bcm2835_gpio_fsel(18, BCM2835_GPIO_FSEL_INPT);
bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_512);
bcm2835_pwm_set_mode(0, 1, 1);
while (true) {
$value = bcm2835_analog_read(0);
bcm2835_pwm_set_data(0, $value / 4);
bcm2835_delay(500);
}
bcm2835_close();
?></code>The functions bcm2835_init , bcm2835_gpio_fsel , bcm2835_pwm_set_clock , bcm2835_pwm_set_mode , bcm2835_analog_read , bcm2835_pwm_set_data , and bcm2835_delay handle initialization, GPIO configuration, PWM control, analog reading, and timing.
3. Infrared Sensor – Used for detecting hot spots or remote‑control commands. After wiring the IR receiver to a microcontroller, PHP can parse the received binary string. Example:
<code><?php
$data = "01011110111010011101000010101010";
if (substr($data,0,8) == "01011110" && substr($data,8,8) == "11101001" && substr($data,16,8) == "11010000") {
$payload = substr($data,24);
$command = bindec(substr($payload,0,8));
$param = bindec(substr($payload,8,8));
switch ($command) {
case 0x01: // turn on light
break;
case 0x02: // turn off light
break;
case 0x03: // adjust brightness
break;
default:
break;
}
}
?></code>The substr function extracts portions of the binary string, and bindec converts them to decimal values for further processing.
Conclusion – By using appropriate PHP extensions and handling different communication protocols (serial, analog, IR), developers can integrate various sensors into IoT projects to achieve comprehensive environment monitoring and intelligent control.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.