ดึงข้อมูลจาก Payload CAT LoRa มาแสดงผล ด้วย PHP

หลังจากผมใช้ ESP32 LoRa ทำสถานีวัดอุณหภูมิ และความชื้นสัมพัทธ์ ด้วย DHT22 และส่งข้อมูลเข้า CAT LoRA โดยใช้ Cayenne LPP เป็นตัวแสดงผล ดังรูป

cat lora
Cayenne Mydevice

ซึ่งทาง cat จะมี Routing Profile ให้เราใส่ url webhook สำหรับ Cayenne ทาง CAT ได้ทำไว้ให้แล้ว https://loraiot.cattelecom.com/portal/home/routing

แต่ถ้าหากเราจะดึงข้อมูลจาก Payload มาเก็บไว้ที่ MySql โดยใช้ PHP ดึงข้อมูล และเอาข้อมูลที่เราเก็บไว้ใน DataBase มาใช้ต่อไปจะเขียนโค๊ดดึงโดยใช้ โปรโตคอล HTTP แล้วนำ url ไปใส่ไว้ใน Routing Profile

ตัวอย่างโค๊ด get.php

<?php 
$postdata = file_get_contents('php://input');
file_put_contents('data.txt', 
    $postdata . PHP_EOL, FILE_APPEND);
echo 'ok';
?>

จากโค๊ดด้านบน เราตั้งชื่อว่า get.php แล้วนำ url ไปใส่ไว้ที่ Routing Profile เช่น
http://myweb.com/get.php

อธิบายโค๊ด เป็นการรับข้อมูล Payload เก็บไว้ที่ตัวแปร แล้วส่งออกไปเขียนเป็น text file ชื่อ data.txt จะได้ข้อมูลจาก Payload ดังนี้

{"DevEUI_uplink":{"Time":"2020-05-30T21:21:08.822+07:00","DevEUI":"A00********************","DevAddr":"0102FF01","FPort":"1","FCntUp":"453","ADRbit":"1","MType":"2","FCntDn":"114","payload_hex":"01686e02670122","payload_parsed":{"frames":[{"channel":1,"type":104,"typeString":"Humidity Sensor","value":55.0},{"channel":2,"type":103,"typeString":"Temperature Sensor","value":29.0}]},"mic_hex":"c052f4b2","Lrcid":"00000231","LrrRSSI":"-116.000000","LrrSNR":"-9.750000","SpFact":"9","SubBand":"G1","Channel":"LC1","DevLrrCnt":"1","Lrrid":"10000952","Late":"0","LrrLAT":"14.006633","LrrLON":"100.572319","Lrrs":{"Lrr":[{"Lrrid":"10000952","Chain":"0","LrrRSSI":"-116.000000","LrrSNR":"-9.750000","LrrESP":"-126.187256"}]},"CustomerID":"1100009747","CustomerData":{"alr":{"pro":"LORA/Generic","ver":"1"}},"ModelCfg":"0","InstantPER":"0.333333","MeanPER":"0.236689"}}

สำหรับการดึง Payload มาเก็บไว้ที่ MySql จะเขียนในบทความต่อไป

Div24Hr.COM
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.