Browse Source

pi2server

master
test2 5 years ago
parent
commit
12dfdde9f9
  1. 170
      IoT/SensorNode2Server.adoc

170
IoT/SensorNode2Server.adoc

@ -79,7 +79,7 @@ image:./ssh-file-to-sd-card.jpg[alt="Create ssh file"]
- Linux Users
.sketch Create ssh file
.Create ssh file
[source,bash]
----
sudo fdisk -l
@ -178,10 +178,178 @@ It has the following options available:
----
=== Arduino Uno Raspberry Pi Serial Communication
==== Serial config on Raspi
.config 1 (recommended)
[source,bash]
----
whoami
sudo usermod -a -G dialout pi
reboot
----
This gives read/write permission for all users to the Raspberry Pi (potentially unsafe):
.config 2
[source,bash]
----
sudo chmod 777 /dev/ttyACM0
----
This provides some configuration for the Arduino serial connection:
.configuration for the Arduino serial connection
[source,bash]
----
sudo stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
----
==== Reading in the arduino
.handle the reading in the arduino
[source,bash]
----
void loop() {
meas = analogRead(a);
if (Serial.available())
{
if (Serial.read() == '1')
{
Serial.println(meas);
}
}
}
----
==== Python
.Python code in Raspberry Pi
[source,python]
----
import serial
from datetime import datetime
from time import sleep
now = datetime.now()
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write("1".encode())
sleep(0.05);
s = ser.readline()
file = open("dataset", "a")
file.write(now.strftime("%Y-%m-%d %H:%M") + " Sensor Value:" + str(s)+ "\n")
file.close()
----
==== PHP
[NOTE]
.PHP Class
====
https://gist.github.com/gravataLonga/6c89821b845d15e939a0/archive/0d0063684d388a8ff53df8e73e55f4cb1187d7cd.zip[Download Class]
====
.PHP code in Raspberry Pi - read
[source,php]
----
<?php
include "php_serial.class.php";
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$read = $serial->readPort();
$serial->deviceClose();
echo $read
----
Sends a string to the Arduino.
.PHP code in Raspberry Pi - send
[source,php]
----
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
include "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("/dev/ttyAMA0");
$serial->confBaudRate(115200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
$serial->sendMessage("Hello from my PHP script, say hi back!");
$serial->deviceClose();
echo "I've sended a message! \n\r";
----
==== NodeJS
[NOTE]
====
Read the writing carefully on your Raspberry Pi circuit board to confirm it indicates something like “Raspberry Pi 4 Model B” or “Raspberry Pi 2 Model B”. If in doubt, run the following command in the terminal:
$ uname -m
If the result returned starts with **“armv6”**, you are running a Raspberry Pi based on the older ARMv6 chipset and the next Node.js installation step **will not work**; otherwise, you are ready for the next step.
====
.Install NodeJS
[source,bash]
----
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt install -y nodejs
npm install raspi-serial
----
.Install NodeJS - armv6
[source,bash]
----
cd ~
wget http://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-armv6l.tar.gz
tar -xzf node-v6.2.1-linux-armv6l.tar.gz
cd node-v6.2.1-linux-armv6l/
sudo cp -R * /usr/local/
export PATH=$PATH:/usr/local/bin
npm install raspi-serial
----
.NodeJS code in Raspberry Pi - read
[source,c]
----
mport { init } from 'raspi';
import { Serial } from 'raspi-serial';
init(() => {
var serial = new Serial();
serial.open(() => {
serial.on('data', (data) => {
process.stdout.write(data);
});
serial.write('Hello from raspi-serial');
});
});
----
[appendix]

Loading…
Cancel
Save