PHP receive script for KMTronic DS1820 LAN POST version module

 

Receive POST data using PHP

KMtronic LAN DS18B20 Temperature Monitor

 

 

LAN DS18B20 High-Precision 1-Wire Digital Temperature Monitor

 


 

PHP file:

<?php


$date = date('Ymd');
$time = date('H:i:s');
$hourtime = date('H:i');
$unixtime = time();

$file = "/var/www/html/data/".$date.".log";
$current = file_get_contents($file);

$current .= $date;
$current .= "-";
$current .= $time;
$current .= "\t";
$current .= $unixtime;
$current .= "\t";

$temp = $_POST['temperature'];

$temp2 = implode("",$_POST['temperature']);

$current .=  $temp2;
$current .= "\r\n";

// Write the contents back to the file
file_put_contents($file, $current);

// a:2:{s:9:"device_id";s:10:"B3D53A5348";s:11:"temperature";a:1:{s:18:"'2874EDD0040000F3'";s:5:"15.43";}}

?>

 


 

DATA file:
20161229-00:00:21    1482962421    22.75
20161229-00:00:56    1482962456    22.75
20161229-00:01:30    1482962490    22.75
20161229-00:02:04    1482962524    22.75
20161229-00:02:38    1482962558    22.75
20161229-00:03:12    1482962592    22.75
20161229-00:03:46    1482962626    22.75
20161229-00:04:20    1482962660    22.75
20161229-00:04:54    1482962694    22.75
20161229-00:05:28    1482962728    22.81
20161229-00:06:02    1482962762    22.81




 

 

 

Create a Crontab server side PHP script for uploading temperature to a MySQL database

 

KMtronic LAN DS18B20 WEB 1-Wire Digital Temperature Monitor
KMtronic LAN DS18B20 WEB 1-Wire Digital Temperature Monitor

 


 

Temperature Monitor STATUS XML data

http://XXX.XXX.XXX.XXX/status.xml
XXX.XXX.XXX.XXX is IP ADDRESS Temperature monitor

 


 

MySQL table

Create a Crontab server side PHP script for uploading temperature to a MySQL database
Create a Crontab server side PHP script for uploading temperature to a MySQL database
Create a Crontab server side PHP script for uploading temperature to a MySQL database
Create a Crontab server side PHP script for uploading temperature to a MySQL database

 


 

Cron is a system daemon used to execute desired tasks (in the background) at designated times. 

A crontab file is a simple text file containing a list of commands meant to be run at specified times. It is edited using the crontab command. The commands in the crontab file (and their run times) are checked by the cron daemon, which executes them in the system background.

Crontab PHP file:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "temperature_database";


// Loading the XML file
$xml = simplexml_load_file("http://XXX.XXX.XXX.XXX/status.xml");
// XXX.XXX.XXX.XXX is IP ADDRESS Temperature monitor

foreach($xml->children() as $sensor)
 {
  if ($sensor->id == "2874EDD0040000F3" )
  {
  $temp_value = $sensor->temp;
  }
 }
// "2874EDD0040000F3" is ID DS18B20 sensor

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO km_temperature (temp_id, temp_value, temp_time )
VALUES ('2874EDD0040000F3', $temp_value , NOW())";
// "km_temperature" is TABLE name


if ($conn->query($sql) === TRUE) {
 echo "New record created successfully";
} else {
 echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>

 

 

 

 

 

 

UDP packet send from ESP8266 to KMtronic UDP LAN Relay board

 

KMtronic UDP LAN Ethernet IP 2 channels Relay Controller
KMtronic UDP LAN Ethernet IP 2 channels Relay Controller

 

NodeMcu
NodeMcu

 

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "KMTRONIC";
const char *password = "12345678";

WiFiUDP Udp;
unsigned int localUdpPort = 12345; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back


void setup()
{
 Serial.begin(115200);
 Serial.println();

Serial.printf("Connecting to %s ", ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED)
 {
 delay(500);
 Serial.print(".");
 }
 Serial.println(" connected");

Udp.begin(localUdpPort);
 Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
 
 // Turn relay 1 ON KMtronic UDP Relay board
 // connected to IP: 192.168.1.199 PORT:12345
 Udp.beginPacket("192.168.1.199", 12345);
 Serial.println("Send UDP...");
 Udp.write("FF0101");
 Serial.println(Udp.endPacket());
 // --------------------------------------
}


void loop()
{
 int packetSize = Udp.parsePacket();
 if (packetSize)
 {
 // receive incoming UDP packets
 Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
 int len = Udp.read(incomingPacket, 255);
 if (len > 0)
 {
 incomingPacket[len] = 0;
 }
 Serial.printf("UDP packet contents: %s\n", incomingPacket);

// send back a reply, to the IP address and port we got the packet from
 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
 Udp.write(replyPacket);
 Udp.endPacket();
 }
}

 

 

 

 

 

Randomnerdtutorials: ESP8266 Controlled with Android App (MIT App Inventor)

 

ESP8266 Controlled with Android App (MIT App Inventor)

 

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "KMTRONIC";
const char *password = "12345678";

ESP8266WebServer server(80);

const int led = D0;

void handleRoot() {
 digitalWrite(led, 1);
 server.send(200, "text/plain", "hello from esp8266!");
 digitalWrite(led, 0);
}

void handleNotFound(){
 digitalWrite(led, 1);
 String message = "File Not Found\n\n";
 message += "URI: ";
 message += server.uri();
 message += "\nMethod: ";
 message += (server.method() == HTTP_GET)?"GET":"POST";
 message += "\nArguments: ";
 message += server.args();
 message += "\n";
 for (uint8_t i=0; i<server.args(); i++){
 message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
 }
 server.send(404, "text/plain", message);
 digitalWrite(led, 0);
}

void setup(void){
 pinMode(led, OUTPUT);
 digitalWrite(led, 0);
 Serial.begin(115200);
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 Serial.println("");

// Wait for connection
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.print("Connected to ");
 Serial.println(ssid);
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) {
 Serial.println("MDNS responder started");
 }

server.on("/", handleRoot);

server.on("/ON1", [](){
 server.send(200, "text/plain", "this works as well");
 digitalWrite(led, 0);
 });

server.on("/OFF1", [](){
 server.send(200, "text/plain", "this works as well");
 digitalWrite(led, 1);
 });

server.onNotFound(handleNotFound);

server.begin();
 Serial.println("HTTP server started");
}

void loop(void){
 server.handleClient();
}

 

 

Randomnerdtutorials: ESP8266 Controlled with Android App (MIT App Inventor)
Randomnerdtutorials: ESP8266 Controlled with Android App (MIT App Inventor)

 

 

ESP8266 NodeMCU: KMtronic UART 8 Channel Relay Controller control

KMtronic UART 8 Channel Relay Controller control

 

KMtronic UART Relay Controller Eight Channel
KMtronic UART Relay Controller Eight Channel

 

ESP8266 NodeMCU: KMtronic UART 8 Channel Relay Controller control
ESP8266 NodeMCU: KMtronic UART 8 Channel Relay Controller control

 

 

void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(9600);

}


void loop() {

for(uint8_t i=1; i < 9; i++ ){
 KMtronic_Relay_ON(i);
 delay(50);
 }

for(uint8_t i=1; i < 9; i++ ){
 KMtronic_Relay_OFF(i);
 delay(50);
 }


 KMtronic_Relay_ON(1);
 delay(500);

KMtronic_Relay_OFF(1);
 delay(500);

KMtronic_Relay_ON(8);
 delay(500);

KMtronic_Relay_OFF(8);
 delay(500);
 
}

void KMtronic_Relay_ON(uint8_t number)
{
 Serial.write((uint8_t)0xFF);
 Serial.write((uint8_t)number);
 Serial.write((uint8_t)0x01);
}

void KMtronic_Relay_OFF(uint8_t number)
{
 Serial.write((uint8_t)0xFF);
 Serial.write((uint8_t)number);
 Serial.write((uint8_t)0x00);
}