Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Gustavo Bertoli
Published © MIT

SaferWork 4.0: Industrial IoT for Safety

SaferWork 4.0 intends to provide real-time environmental data of industrial areas.

BeginnerFull instructions provided10 hours4,323
SaferWork 4.0: Industrial IoT for Safety

Things used in this project

Story

Read more

Schematics

Arduino Edge Device

Dragonboard Gateway

Code

SaferWork_Device.ino

Arduino
//SaferWork - Device Code
//
// References:
//     https://github.com/adafruit/DHT-sensor-library
//     https://quadmeup.com/hc-12-433mhz-wireless-serial-communication-module-configuration/
//     https://www.elecrow.com/download/HC-12.pdf
//     https://playground.arduino.cc/Main/MQGasSensors
//     https://github.com/bblanchon/ArduinoJson
//

#include <SoftwareSerial.h>
#include <ArduinoJson.h>
#include <DHT.h>

#define DHTPIN A1     // Analog PIN A1 for DHT11
#define DHTTYPE DHT11 // DHT 11

//Device Configuration
#define DEVID  "Device01"
#define MSG_PUSH_TIME 5000 //in miliseconds

DHT dht(DHTPIN, DHTTYPE);

//Pins for Serial connection to HC-12
SoftwareSerial HC12_Device(10, 11); // RX, TX
int HC12_SET = 7; // Pin to setup HC-12 Wireless module

// GAS Sensors Pinout no Arduino
int MQ2_gasPin = 5;    //sensitive for flamable and combustible gasses (Methane, Butane, LPG, smoke)
int MQ9_gasPin = 4;    //sensitive for Carbon Monoxide, flammable gasses
int MQ135_gasPin = 3;  //For Air Quality (sensitive for Benzene, Alcohol, smoke)

void setup(){
  pinMode(HC12_SET, OUTPUT);
  HC12_Device.begin(9600);
  Serial.begin(9600);

  digitalWrite(7, LOW);            // enter AT command mode
  HC12_Device.print("AT+DEFAULT"); // 9600, CH1, FU3, (F) to bypass flash memory
  delay(100);
  digitalWrite(7, HIGH);           // enter transparent mode
  
  dht.begin();
}

void loop(){
  // Reserve memory space
  StaticJsonBuffer<256> jsonBuffer;

  char buffer[256];
  JsonObject& root = jsonBuffer.createObject();


  delay(MSG_PUSH_TIME);
  
  //Sensors Readings
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  int MQ2 = analogRead(MQ2_gasPin);
  int MQ9 = analogRead(MQ9_gasPin); 
  int MQ135 = analogRead(MQ135_gasPin); 
  
  // JSON Construct
  // Example: {"DeviceID":"Device01","key":0x00,"data":[TEMPERATURE,HUMIDITY,MQ2_LEVEL,MQ9_LEVEL,MQ135_LEVEL]}
  
  root["DeviceID"] = DEVID;
  root["key"] = "0x00";
  
  JsonArray& data = root.createNestedArray("data");
  data.add(h);
  data.add(t);
  data.add(MQ2);
  data.add(MQ9);
  data.add(MQ135);
  
  root.printTo(buffer, sizeof(buffer));
  HC12_Device.println(buffer);

}

SaferWork_Gateway.py

Python
#
# Dragonboard 410c - SaferWork 4.0 Gateway
#
# Receives JSON packages through UART using a HC-12 chip
#
# Required python-serial and requests:
#   sudo apt-get install python-pip
#   sudo pip install pyserial
#   sudo pip install requests
#


import serial
import time
import requests
import json
from GPIOProcessor import GPIOProcessor

ser = serial.Serial('/dev/tty96B0',baudrate=9600)

# dweet.io configuration
thingname = "SaferWork_Prototype"
dweet_url = "https://dweet.io:443/dweet/for/"

def config():
    # HC-12 Configuration
    # Using UART port from Low Speed Connector (PINs 5 and 7)
    
    time.sleep(2)
    
    GP = GPIOProcessor()
                        
    SETPin = GP.getPin29()      # HC-12 SET Pin connected to DB410C LS pin 29
    
    SETPin.out()                # Defined as output
    print ">> SET PIN ---> " + SETPin.getDirection()
    print ">> SET PIN ---> LOW (AT Command)"
    SETPin.low()                # Enter to AT Command
    print ">> HC-12 Set to Default (FU3 / 9600bps / CH1 433.4MHz)"
    ser.write("AT+DEFAULT")     # SET HC-12 Default Configuration
    time.sleep(1)
    SETPin.high()               # Enter Transparent Mode
    print ">> SET PIN ---> HIGH (Transparent Mode)"
    time.sleep(2)
    print ">> HC-12 Setup OK!"

    # Usage
    ser.write("[rf_msg] Dragonboard (Gateway) to Device")
    GP.cleanup()
    time.sleep(2)

# procedure to send data to dweet.io
def send_data(thingname, data):
    rqsString = dweet_url+thingname+'?'+str(data)
    print rqsString
    try:
        rqs = requests.get(rqsString, timeout=10)
        print rqs.status_code
    except requests.exceptions.RequestException as e:
        print e
    except KeyboardInterrupt:
        raise

def main():
    config()
    # Infinite Loop for Data Receiver (Gateway)
    while True:
        jsonPackage = ser.readline()
        data = json.loads(jsonPackage)
        deviceId = data['DeviceID']
        temp = data['data'][1]
        humidity = data['data'][0]
        mq2 = data['data'][2]
        mq9 = data['data'][3]
        mq135 = data['data'][4]
        jsonPackage = "deviceId="+str(deviceId)+"&temp="+str(temp)+"&humidity="+str(humidity)+"&mq2="+str(mq2)+"&mq9="+str(mq9)+"&mq135="+str(mq135)
        send_data(thingname,jsonPackage)
    
    ser.close()

if __name__ == "__main__":
    main()

Github

https://github.com/gubertoli/SaferWork

Credits

Gustavo Bertoli
3 projects • 8 followers
Contact

Comments

Please log in or sign up to comment.