IOT

Industrial Internet Of Things

Contents

  • IOT Communication Protocol.
  • Thingspeak.
  • JSON.
  • Coding & Demo.
  • Project Ideas.
  • Queries.

Communication Protocol.

  • HTTP
  • MQTT
  • CoAP

HTTP

  • Hyper Text Transfer Protocol
  • Transmission Control Protocol (TCP)
  • client server (request-response) model
  • Secured and Large chunk of data

MQTT

  • Message Queuing Telemetry Transport
  • Transmission Control Protocol (TCP)
  • publish subscriber model
  • Asynchronous
  • low power consumption.

CoAP

  • Constrained Application Protocol
  • User Datagram protocol(UDP)
  • Asynchronous and Synchronous
  • client server (request-response) model

Thingspeak.

  • MathWorks.
  • Data analysis using MATLAB.
  • HTTP.
  • 15 sec interval between each data for free account.

JSON

JavaScript Object Notation

{
    channel: {
    id: 1234567,
    name: "IOT Platform",
    description: "Description of project",
    latitude: "0.0",
    longitude: "0.0",
    field1: "temperature",
    field2: "voltage",
    created_at: "2022-07-16T08:30:17Z",
    updated_at: "2022-09-01T05:03:31Z",
    last_entry_id: 137
    },
    feeds: [
        {
    created_at: "2022-12-07T10:11:34Z",
    entry_id: 136,
    field1: "2.45566"
        },
        {
    created_at: "2022-12-07T10:11:53Z",
    entry_id: 137,
    field1: "2.45244"
        }
    ]
}

Coding & Demo

  1. Configuring NodeMCU for MicroPython.
  2. Blinking a LED.
  3. Connecting mc to wifi.
  4. Write data to Thingspeak using write API.
  5. Sending RealTime data to Thingspeak.

Blink Built-in LED of NodeMCU ESP8266.


from machine import Pin
import utime

# Pin 2 is connected for Built-in LED
led = Pin(2, Pin.OUT)

while True:

    # Turn on LED for 1 sec
    led.value(0)
    utime.sleep_ms(1000)

    # Turn off LED for 1 sec
    led.value(1)
    utime.sleep_ms(1000)

Connecting NodeMCU to Wifi


ssid = "ssid"
password = "password"

def do_connect():
    import network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())

do_connect()

Sending data to Thingspeak account.

def do_connect(ssid, password):
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect(ssid, password)
        while not sta_if.isconnected():
            pass
    print('network config:', sta_if.ifconfig())


def thingspeak_write_data(api_key, value):
    import socket
    
    host = "api.thingspeak.com"
    path = f"update?api_key={api_key}&field1={value}"
    
    #_, _, host, path = url.split('/', 3)
    addr = socket.getaddrinfo(host, 80)[0][-1]
    s = socket.socket()
    s.connect(addr)
    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
    while True:
        data = s.recv(100)
        if data:
            print(str(data, 'utf8'), end='')
        else:
            break
    s.close()
    
api_key = "8FLKPPNP64E____" # Replace with write api key.
ssid = "ssid" # replace with ssid of wifi.
password = "password" # replace with password of wifi.
value = 10 # value to be sent to account.

do_connect(ssid, password)

thingspeak_write_data(api_key, value)

Sending realtime data to Thingspeak account.

def do_connect(ssid, password):
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect(ssid, password)
        while not sta_if.isconnected():
            pass
    print('network config:', sta_if.ifconfig())


def thingspeak_write_data(api_key, value):
    import socket
    
    host = "api.thingspeak.com"
    path = f"update?api_key={api_key}&field1={value}"
    
    #_, _, host, path = url.split('/', 3)
    addr = socket.getaddrinfo(host, 80)[0][-1]
    s = socket.socket()
    s.connect(addr)
    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
    while True:
        data = s.recv(100)
        if data:
            print(str(data, 'utf8'), end='')
        else:
            break
    s.close()
    
api_key = "8FLKPPNP64E____" # Replace with write api key.
ssid = "ssid" # replace with ssid of wifi.
password = "password" # replace with password of wifi.

do_connect(ssid, password)

from machine import ADC
import utime

pot = ADC(0)

while True:

    pot_value = pot.read()
    # print("POT Value = ", pot_value)
    
    # conversion of adc to voltage.
    voltage = (pot_value / 1024) * 3.3

    thingspeak_write_data(api_key, voltage)

    utime.sleep_ms(18000)

Project Ideas

Any Queries ?