0% found this document useful (0 votes)
2 views

Lecture 5 - ioT Programming Languages

The document discusses IoT architecture, focusing on cloud, fog, and edge computing, and their respective data processing capabilities. It also covers various programming languages used in IoT, including embedded languages for low-level coding, carrier languages for message formatting, and server-side languages like PHP for data handling. Additionally, it explains methods for sending data to servers, including GET and POST requests, along with their advantages and disadvantages.

Uploaded by

Bloody Assault
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 5 - ioT Programming Languages

The document discusses IoT architecture, focusing on cloud, fog, and edge computing, and their respective data processing capabilities. It also covers various programming languages used in IoT, including embedded languages for low-level coding, carrier languages for message formatting, and server-side languages like PHP for data handling. Additionally, it explains methods for sending data to servers, including GET and POST requests, along with their advantages and disadvantages.

Uploaded by

Bloody Assault
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

IoT and Programming

Languages

Troy Merritt 2025


Unit Co-ordinator Week 5
1
Recap: IoT Architecture

2
IoT Architectures

3
Cloud, Fog, Edge

Looking at where the data is being processed in your IoT


system.
Data aggregated in data centres (Cloud).
Services running on middleware devices (Fog).
Processing happening on end node devices (Edge).

4
Cloud Computing

Data is aggregated from end node devices onto


dedicated infrastructure in data centres.
Powerful processing capabilities, ability to scale
applications, processing and storage easily.
Public services make it easy to only ‘pay for what you
need’.
Potential for high latency.
Massive power consumption / maintenance
requirements.

5
Fog Computing

Data is aggregated across a distributed network of


middleware devices ie: gateways and routers.
More power than the end node, less power (and
potentially data) than the cloud.
Less pay-as-you-go options. (More likely that the
deployer will need to purchase the hardware.)
Less latency than cloud (more than on-device
processing).
Suited to services like authentication.

6
Edge

Stand-alone or Peer to peer or client in the Client


server Model.
Data and Programs are stored locally on devices.
Latency does not effect this.
Events can be recorded and processed very quickly.
Only small-scale processing is possible.
Low power consumption.
Maintenance requirements can be difficult.
Rolling out updates is risky.

7
Application Logic (for IoT devices)

The logic of what you want to achieve with the IoT


device and devices in group.
A program that tells the IoT device what to do OR
accepts inputs from device(s) to decide what to do.
e.g. authentication­
Send username, password or any ID details
e.g. send records or control outputs
send temperature etc …
e.g. receive records or setting
retrieve any settings
e.g. turn on the fan and at what speed

8
Webservers from and IoT Perspective

9
Client Pull

A client initiates a connection, requests information.


The server provides the information and closes the
connection.
Connections aren’t persistent - lots of opening and
closing connections.
Not as fast (more latency).
Data could have changed on the server multiple times
and the client won’t know until it initiates a request.

10
Server Push

The connection is established by the server and left


open.
Whenever data needs to be updated, the server pushes it
to the client.
Much less latency, which can allow real-time updates.
More resource intensive - connections need to be
maintained.
Can be harder to scale to lots of devices.

11
IoT
Programming
Languages

12
IoT Languages

13
Embedded Languages

Embedded Languages
support low-level coding to exploit the
underlying hardware,
support in-line assembly code,
flag dynamic memory-allocation and recursion,
provide exclusive access to I/O registers
Common Languages include C, C++, (micro)Python,
Rust

14
Order of Importance

Dedicated objective - every IoT device has a purpose.


The steps taken to achieve that purpose have a
different level of importance.
Every action you can take is based on how
important it is for the objective
In the loop() phase: each condition and execution
is done according to the order

15
Carrier Language / Protocols

Message parsing or carrier languages are


protocols that define how messages are
formatted for exchange between IoT devices.
Common languages include:
XML
JSON
CSV

REST architecture uses carrier languages to send and


receive data using some of the above formats.

16
Data Storage

IoT relies on data to be stored for later


processing.
Data can be stored in a file or a database e.g.
CSV, XML, MySQL, MongoDB, NoSQL
Relational languages e.g. SQL can be used to
retrieve information.
Languages like PHP can be used to retrieve
stored data for further analysis.

17
Data Analysis

Once data is retrieved, it can be analysed using


server-side scripting languages (PHP, Python,
Ruby, Java...)
AI algorithms can be run on the data set once
retrieved using R, NodeJS and Python.
Finally, raw and processed data is displayed to
the user using web scripting languages (HTML,
PHP, Javascript) or native mobile applications.

18
Creating a Balanced IoT Application

Where do you want the computing to happen in


your IoT application?
Edge (on device)
Middleware (Fog)
Server (Cloud)
Client Side?

Typicially, it’s some combination of all of the above.

19
Server Side
Programming

20
IoT Server Side Languages
General Characteristics:
Web oriented programming language:
Any scripting language or CGI that supports
HTTP, HTTPS, TCP/IP or related protocols.
PHP, NodeJS, ASP, JSP etc.
Receive and process requests.­
Respond if necessary, or acknowledge.
There is no client-side scripting.
No JS involved for the IoT device,­but may be
used for client-side application

21
PHP Basics

Hypertext Preprocessor
Allows for the creation of dynamic websites.
PHP scripts run and generate HTML to be
rendered by a web browser.
Can respond to a request via a URL and return
information to an IoT device.
Messages for IoT devices can be simple without
any styling required for results to be rendered.

22
Why use PHP for IoT?

PHP is a mature and well understood language.


Designed for the building dynamic webpages, lots
of experience amongst web developers and
extensive support.
Lots of built in functions, lots of libraries.
Free.
Wide support for operating environments.
Allows for data to be ingested, processed,
returned to IoT devices, and to display web
content to end users.

23
Why use PHP for IoT?

Is PHP the best choice for building an IoT server


language?
Probably not, but it can do nearly everything
we need in a single language.
PHP is not the most efficient choice for
processing data.
But it is good at generating the small outputs
we need to return to IoT devices.

24
Re-introducing some PHP Basics

PHP code can be written inside a HTML


document.
Code starts with <?php and ends with ?>
echo is used to generate output / print.
PHP can be run from the command line like a
standard program.
Use terminal: php file.php

25
Re-introducing some PHP Basics

PHP has no data types


Variables start with $
Case sensitive $var != $Var
Control structures:
if, else, elseif
switch
while
for
foreach

26
Re-introducing some PHP Basics
Strings can be single quoted (literal)
echo 'this is a $var';
output: this is a $var
Double quoted will print variables
$var = ‘string’;
echo “this is a $var”;
output: this is a string
Strings can be concatenated with .
$s1 = “Hello”;
$s2 = “World”;
$echo $s1 . ” “ . $s2;
output: Hello World

27
Re-introducing some PHP Basics

Operators
Arithmetic: +, -, *, /, %
Setting variable: =
Comparison: ==, ===, !=, !==, <, > <=, >=
Bit: &, |, ^, ~, <<, >>
Logical Operators
$a and $b
$a or $b
$a xor $b
!$a;
$a && $b;
$a || $b;

28
End Node
Interaction

29
Sending data to the server

Data can be sent via a GET or a POST method


From an IoT perspective, we can use either method.
POST is more widely used in web browsing applications
Has advantages as the URL doesn’t contain sensitive
information
POST is usually used to send larger amounts of data.
IoT devices almost always aim to send as little data as
possible.

30
URL Encoding

Name/value pair joined with ‘=‘


Different name/value pair separated by & (ampersand)
Spaces are removed and replaced with ‘+’
Any other non alphanumeric characters are replaced with
hexadecimal values.
After it’s encoded, it’s sent to server.

temp=value1&humidity=value2&location=port+arthur

31
Get Method

GET sends the encoded information appended to a page request.


You need to include the server, and the file you want to receive
the data.
Use a ? to append the encoded URL to the page

http://123.com/test.php?temp=value1&humidity=value2

32
GET disadvantages

Produces long strings.


Can only send 1024 characters - this limits the amount of
data that can be sent in a single request. Particularly if you
need to send information other than the updated data (ie:
device ID, verification)
Can only send strings (no images or binary data)
Usually not a limitation for IoT
Information sent in the URL is not secure
URL requests are logged, and the logged request
contains the data requested.

33
GET example

34
Requests example

Requests is a library for python that makes it simple to send HTTP


requests from our simulated IoT device to a server.
As the information is encoded in the URL, it’s easy to make a GET
request with requests.
GET Example:

import requests

server = 'http://iotserver.com/test.php'
payload = {'num' : '6'}
r = requests.get(server, params = payload)
print(r.text)

35
POST Method

Rather than sending information appended to a URL,


POST uses the HTTP headers to transfer the information.
The data being sent is not visible just by looking at the
URL.
Information is still encoded in a similar way to the GET
method, but inserted into the header instead.
There is no limit on the size of the data that can be sent
this way. It can be used to send binary data too.
We can unpack a POST in PHP using $_POST.
POST is often used to update rather than fetch data.

36
POST disadvantages

POST requests aren’t cached (generally not an issue for


IoT devices).
Marginally slower than a GET request. GET can be faster
for pure retrieval of data.
Consumes more resources and bandwidth on low
powered end nodes, and can be harder to implement.

37
Requests example

Requests can also be used to send our data as a POST request.


POST Example:

import requests

server = 'http://iotserver.com/test.php'
payload = {'num': '6'}
r = requests.post(url, data = payload)
print(r.text)

38
Coming up...
Next week:
More on IoT server
programming, protocols and
languages.

Thanks for attending!


39

You might also like