Skip to content

khjharris/IOT_Server

Repository files navigation

IOT_Server

Written in Node, and compatible with any device that can send HTTP messages, and JSON formatted Data. The main and only server file is serverhandler.js, and requires node to run.

Overview

This is a general purpose IOT server, that takes JSON input data. The server takes two types of methods POST webhooks, and Get requests.

Each of these methods has different event types that are declared within the JSON format. Below is a table listing all event types avaliable:

eventType Method Function
connect POST Generates and returns deviceID
update POST Updates the server with sensor information
test POST For sending test signals confirming connection
pull POST For retrieving sensor data files
reset POST For reseting specific files or sensors

JSON Structure

ALL devices must send JSON in the following format, but not all postions need to be populated:

    {
      deviceID:            //Only needed after intially connecting, is the reference to the server
      time:                 //The time that the message is sent
      event: {
        eventType:          //This is one of the eventTypes in the above table
        parameters: []      //Must be a list
              }
      deviceType:           //Unique device type, that partly determines the deviceID
      sensorTypes: []       //Must be a list of the different sensors connected to the device
      sensorData: []        //Must be a list comprised of the data values from the sensorTypes list
      group:                //Optional group number
      location:             //Optional location
      file:                 //Optional filename
    }

Client Information

1. Connecting Devices

In order to be able to sufficiently communicate with the server, a device must have a deviceID that is generated by the server. In order to generate this ID a device must initially send a POST webhook, with a connect eventType. An example is below:

 {
   deviceID: null,
   time: 123456,
   event: {
     eventType: connect,
     parameters: [],
           },
   deviceType: sensorBoard,
   sensorTypes: [lux,CO2,accelerometer],
   sensorData: [],
   group: null,
   location: null,
   file: null,
  }

After the deviceID is generated, a few things will unfold. The first of these is that the server will respond with JSON data of its own (a slightly different format), with the inclusion of the new deviceID. This value will be under the JSON value "response". Simultaneously the server will log the NEW device ID, along with the connection information.

After a deviceID is generated, it is up to the device to store, and maintain the ID, as it is needed for all further communication with the server. If a new ID is required another connect eventType may be sent.

2. Updating Data and Sensor Information

After obtaining a deviceID, a device may update the server, send test messages, pull information, or reset. The most common of these is a update, which is detailed in this section.

In order to update the server, an eventType of update is required. Other required information is the time, deviceType(which should remain constant), and of course the sensorTypes, and sensorData. In order to correctly update the information the postion "n" in the sensorType array should match the position n for the data of that sensor in sensorData.

    [lux, CO2, accelerometer-x,accelerometer-y,accelerometer-z]
    [24, 35, 15, 43, 12] //Will update the files with 24 as the value for lux 35 for CO2 and so on...

3. Testing Connection

In order to test the connection, simply send the formatted JSON data with an eventType of "test", a JSON formatted HTTP response will be sent back in the following format:

     mes = {
        'deviceID': 'ServerOG',
        'time':123456,
        'event':{
          'eventType': 'updateResponse',
          'parameters': []
        },
        'deviceType':'Server',
        'response': null,
        'status': 'Successful'
      }

4. Querying Data

In order to obtain localized data from the server you can use the pull eventType. This event is only used primarily to obtain a limited amount of data, and returns a concatenated string of the FULL file. The data returned will be delineated by commas in a csv format style, and multiple files will be delineated with "\r\n". When submitting a pull eventType, the parameters must be used. Incorrect use of parameters will result in a null response from the server. The parameters that a device may send can only be sensortypes that are attributed with that deviceType and deviceID. An example is below:

    {
    deviceID: test123
    time: 123456
    event: {
      eventType: pull            //eventType
      parameters: [lux,CO2]     //parameters of associated sensors
            }
    deviceType: sensorBoard
    sensorTypes: [lux,CO2,accelerometer]
    sensorData: []
    group:
    location:
    file:
   }

5. Local Resets

A client device with a deviceID can initiate a local reset to the server. This local reset ONLY resets the sensorFiles listed in its sensorTypes. During a reset the server logs backup files, into a directory with the time of the reset. To intiate this reset the client device must send an appropriate deviceID, with an eventType reset, and parameter reset, as follows:

    {
    deviceID: test123
    time: 123456
    event: {
      eventType: reset      //reset eventType
      parameters: [reset]   //regular reset parameter
            }
    deviceType: sensorBoard
    sensorTypes: [lux,accelerometer]    //will reset the sensors listed here, with
    sensorData: []
    group:
    location:
    file:
   }

Server Information

1. Resets

Only the server has access to master Resets.

About

IOT_Server project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published