Percobaan cloud4pi dengan PHP

<!--?php
// A sample PHP Script to POST data using cURL
// Data in JSON format

$json= '&#91;{"name":"Temperature","type":"numeric"},{"name":"LEDOn","type":"bool"}&#93;';
$arr=json_decode($json, false);

echo '

<pre-->';
print_r($arr);
echo '


';

$employee_object = new stdClass;
$employee_object-&gt;name = "Temperature";
$employee_object-&gt;type = "numeric";
$employee_object1 = new stdClass;
$employee_object1-&gt;name = "LEDOn";
$employee_object1-&gt;type = "bool"; 

$data = array
  (
  $employee_object,
  $employee_object1
);

$payload = json_encode($data);
echo "==&gt;".$payload;

$token = '2HSmdfzsEAXB3puVUTi6ZoirV';

// Prepare new cURL resource
$ch = curl_init('https://cloud4rpi.io/api/devices/'.$token.'/config');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload))
);

// Submit the POST request
$result = curl_exec($ch);

echo $result;
// Close cURL session handle
curl_close($ch);

$json = '{"ts":"date","payload":{"Temperature":24,"LEDOn":true}}';
$arr=json_decode($json, true);

echo '









<pre>';
print_r($arr);
echo '</pre>
';

$dt = date(DateTime::ISO8601);

$carss = array
(
"ts"=&gt;$dt,
"payload"=&gt;array("Temperature"=&gt;22,"LEDOn"=&gt;0)
);

$payload = json_encode($carss);
echo "==&gt;".$payload;

$token = '2HSmdfzsEAXB3puVUTi6ZoirV';

// Prepare new cURL resource
$ch = curl_init('https://cloud4rpi.io/api/devices/'.$token.'/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

// Set HTTP Header for POST request
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($payload))
);

// Submit the POST request
$result = curl_exec($ch);

echo $result;
// Close cURL session handle
curl_close($ch);

?&gt;

Setting printer USB di Raspberry Pi

sudo chown www-data:www-data etc
sudo chmod 775 etc
sudo chown www-data:www-data /dev/usb/lp0

source:
https://www.howtogeek.com/169679/how-to-add-a-printer-to-your-raspberry-pi-or-other-linux-computer/
https://kernelmastery.com/enable-regular-users-to-add-printers-to-cups/
https://maker.pro/raspberry-pi/projects/how-to-turn-a-usb-printer-into-a-wireless-printer-with-raspberry-pi-zero-w

Setting printer serial di raspberry pi

sudo raspi-config

“Interfacing Options,” “Serial.” Turn OFF: login shell over serial, ENABLE: hardware serial port
“Interfacing Options,” enable SSH

sudo apt-get update
sudo apt-get install git cups wiringpi build-essential libcups2-dev libcupsimage2-dev python-serial python-pil python-unidecode

cd ~
git clone https://github.com/adafruit/zj-58
cd zj-58
make
sudo ./install

sudo usermod -a -G lp pi
sudo usermod -a -G lp www-data
reboot

sudo lpadmin -p ZJ-58 -E -v serial:/dev/serial0?baud=9600 -m zjiang/ZJ-58.ppd
sudo lpoptions -d ZJ-58

git clone https://github.com/adafruit/Python-Thermal-Printer
cd Python-Thermal-Printer
python printertest.py

sudo usermod -a -G dialout www-data
sudo usermod -a -G dialout pi
reboot

Membuat Daemon di Raspberry Pi

Buka terminal lalu ketik:

hciconfig
hcitool scan

Nama perangkat biasanya bernama: hci0
Misalkan hasilnya adalah:

66:12:13:EE:D1:76 Bluetooth Printer

Test printer dengan perintah:

sudo rfcomm bind /dev/rfcomm0 66:12:13:EE:D1:76 1
echo “Test” > /dev/rfcomm0

Jika printer berhasil mencetak maka koneksi sudah berhasil

Selanjutnya tambahkan file untuk daemon service

sudo leafpad /etc/systemd/system/rfcomm.service

Ketik kode berikut dan simpan:

[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm bind hci0 66:12:13:EE:D1:76 1

[Install]
WantedBy=multi-user.target

Ganti nama hci0 dengan nama hasil hciconfig dan ganti 66:12:13:EE:D1:76 dengan hasil hcitool scan

Aktifkan daemon, ubah preferensi printer dan booting

sudo systemctl enable rfcomm
sudo systemctl start rfcomm
Sudo reboot

Menambahkan RTC pada Raspberry Pi 3

Setting antarmuka I2C

sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo raspi-config

Pilih Interfacing Option > I2C > Enable lanjutkan dengan booting. Selanjutnya test koneksi dengan perintah i2cdetect untuk antaruka I2C 1. (Cek juga antarmuka ke 0 jika perlu)

sudo i2cdetect -y 1 #isikan angka 1 bila mengunakan I2C1, isikan 0 bila menggunakan I2C 0

Setting RTC

sudo modprobe rtc-ds1307 #load rtc ke kernel
sudo bash #masuk ke super user
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device #tambahkan device ds1307 di alamat 0x68 ke I2C1
exit #keluar dari super user
sudo hwclock -r #baca waktu dari RTC
sudo hwclock -w #tulis RTC berdasarkan sistem

Set agar waktu yang digunakan menggunakan RTC

sudo nano /etc/modules #agar modul rtc-ds1307 diload
#tambahkan baris berikut:
i2c-bcm2708 
rtc-ds1307

sudo nano sudo nano /etc/rc.local #agar dijalankan saat booting
#tambahkan baris berikut sebelum exit 0:
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device #tambahkan device ds1307  di alamat 0x68 ke I2C1
sudo hwclock -s #set waktu yang digunkan adalah RTC
date #baca waktu saat ini dari RTC

Sumber:

Memprogram 24LC64 dengan Port Serial

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace _24LC64
{
    /// 
<summary>
    /// Pin 3 (TX)  --          -- CS
    /// pin 4 (DTR) -- SCL      -- SCK
    /// Pin 6 (DSR) -- SDA IN   -- 
    /// Pin 7 (RTS) -- SDA OUT  -- MOSI (DO)
    /// Pin 8 (CTS) --          -- MISO (DI)
    /// </summary>


    public partial class Form1 : Form
    {
        public const bool H = true, L = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.BaudRate = 9600;
            serialPort1.PortName = textBox1.Text;
            serialPort1.Open();

            //serialPort1.DtrEnable = true;
            //serialPort1.RtsEnable = true;

            //perintah Write
            StartBit();
            kirim(0xA0);
            bit(1); //ACK
            
            //Alamat memory (0x001)
            //0x00 
            kirim(0x00);
            bit(1); //ACK
            //0x01
            kirim(0x01);
            bit(1); //ACK

            //Data (0x33)
            //kirim(0x33);
            byte[] data = StringToByteArray(textBox2.Text);
            kirim(data[0]);
            bit(1); //ACK
            
            StopBit();


            serialPort1.Close();

        }
        void kirim(int byteData) {
            for (int i = 7; i >= 0; i--) 
            {
                int bitData = (byteData >> i & 0x01);
                bit(bitData);
            }
        }
        void StartBit()
        {
            SDA(H); SCL(H);
            Thread.Sleep(1);
            SDA(L); SCL(H);
            Thread.Sleep(1);
        }
        void StopBit()
        {
            SDA(L); SCL(H);
            Thread.Sleep(1);
            SDA(H); SCL(H);
            Thread.Sleep(1);
        }
        void bit(int val) 
        {
            SCL(L);
            if (val == 0) SDA(L);
            else SDA(H);
            SCL(H);
            Thread.Sleep(1);
            SCL(L);
            SDA(H);
            Thread.Sleep(1);
        }

        void SCL(bool status)
        {
            serialPort1.DtrEnable = status;
        }
        void SDA(bool status)
        {
            serialPort1.RtsEnable = status;
        }

        int bacaBit() {
            int bitData = 0;
            SCL(L);
            SCL(H);
            bool bit = serialPort1.DsrHolding;
            if (bit == true) bitData = 1;
            Thread.Sleep(1);
            SCL(L);
            SDA(H);
            Thread.Sleep(1);
            return bitData;
        }

        public static byte[] StringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.BaudRate = 9600;
            serialPort1.PortName = textBox1.Text;
            serialPort1.Open();


            //perintah Write
            StartBit();
            kirim(0xA0);
            bit(1); //ACK

            //Alamat memory (0x001)
            //0x00 
            kirim(0x00);
            bit(1); //ACK
            //0x01
            kirim(0x01);
            bit(1); //ACK

            //perintah Read
            kirim(0xA1);
            bit(1); //ACK


            int data = 0;
            for (int i = 7; i >= 0; i--)
            {
                data = data << 1;
                data = data | bacaBit();
            }
            bit(1); //ACK

            StopBit();


            serialPort1.Close();
            byte[] hasil = new byte[1]; hasil[0] = (byte)data;
            textBox2.Text = BitConverter.ToString(hasil);
        }
    }
}

Mengubah script python menjadi executable

Menggunakan Linux

Gunakan pyinstaller, pasang dengan:

pip install pyinstaller

Jika gagal, maka:

Buka: https://github.com/pyinstaller/pyinstaller/releases
Download: PyInstaller-3.3.1.tar.gz lalu extract dan masuk kedirektori tersebut.
Eksekusi dengan perintah:

sudo python setup.py build 
sudo python setup.py install

Selanjutnya ubah script python ke executable dengan

pyinstaller --onefile <script_phyton>.py

Jika dikatakan bootloader belum tersedia, misalnya untuk Linux ARM 32Bbit, maka harus dibuat dahulu. Pada hasil download PyInstaller ada direktori bootloader. Masuk ke direktori tersebut dan jalankan perintah:

sudo python ./waf all
cd ../PyInstaller/bootloader
cp -R Linux-32bit-arm/ /home/pi/.local/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-32bit-arm/
 

sumber: https://pyinstaller.readthedocs.io/en/stable/bootloader-building.html