Muhammad Afzal
Published © Apache-2.0

Factory/Warehouse/Control Shed/ Environment Monitoring

An industrial IoT prototype using Arduino, WIZ750SR, Raspberry Pi, and Hologram Nova to monitor environment.

IntermediateFull instructions provided6 hours5,612
Factory/Warehouse/Control Shed/ Environment Monitoring

Things used in this project

Story

Read more

Schematics

Arduino & Wiz750SR Connectivity

Code

wiz750sr.ino

Arduino
/*
  Project Name= Factory/Warehouse/Control Shed/ Environment Monitoring
  Project desc= An Industrial IoT Prototype using Arduino, Wiz750Sr, Nova Hologram to Monitor Environment.
  Written by @Muhammad Afzal
  

 */

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <SoftwareSerial.h>


#define GasModulePin      A0           //MQ-135 Gas Sensor Pin 
#define DHTPIN            13         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT22     // DHT 22 (AM2302)
int GasLevel=0;

DHT_Unified dht(DHTPIN, DHTTYPE);

SoftwareSerial mySerial(10, 11); // RX, TX
String data="";
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // set the data rate for the SoftwareSerial port
  mySerial.begin(115200);
}

void loop() { // run over and over
  if (mySerial.available()) {
        if(mySerial.read()=='d'){
          String SensorValues=GetSensorValues();
          for(int i=0;i<SensorValues.length();i++){
            mySerial.write(SensorValues[i]);  
          }
        }
      }
}

String GetSensorValues(){
    sensors_event_t event;  
    
    //Get Temperature
    dht.temperature().getEvent(&event);
    float Temperature=event.temperature;
    if (isnan(Temperature)) {
      Serial.println("Error reading temperature!");
    }
    else {
      Serial.print("Temperature: ");
      Serial.print(Temperature);
      Serial.println(" *C");
    }
    
    // Get humidity
    dht.humidity().getEvent(&event);
    if (isnan(event.relative_humidity)) {
      Serial.println("Error reading humidity!");
    }
    else {
      Serial.print("Humidity: ");
      Serial.print(event.relative_humidity);
      Serial.println("%");
    }

    //Read MQ-2 Gas Sensor Level
    GasLevel=analogRead(GasModulePin);
    if(GasLevel>0){
      Serial.println("Gas Level=");
      Serial.print(GasLevel);
    }else{
      Serial.println("Gas Level=");
      Serial.print(GasLevel);
      Serial.println("Unable to Take Gas Level Readings");
    }

    return "{temp:"+String(Temperature)+",hum:"+String(event.relative_humidity)+",gas:"+String(GasLevel)+"}";
}

wiz750sr-hologram.py

Python
#
# wiz750sr-hologram.py - Taking Data from Wiz750SR Server to send messages to the Hologram Cloud.
# 
#
# Author: M. Afzal (maker.pk)
#
#
# LICENSE: Distributed under the terms of the MIT License
#


#!/usr/bin/python

import socket,struct
import sys
import Adafruit_DHT
import time
import requests
import json

sys.path.append(".")
sys.path.append("..")
sys.path.append("../..")
from Hologram.HologramCloud import HologramCloud


def recv_timeout(the_socket,timeout=2):
    #make socket non blocking
    the_socket.setblocking(0)

    #total data partwise in an array
    total_data=[];
    data='';

    #beginning time
    begin=time.time()
    while 1:
        #if you got some data, then break after timeout
        if total_data and time.time()-begin > timeout:
            break

        #if you got no data at all, wait a little longer, twice the timeout
        elif time.time()-begin > timeout*2:
            break

        #recv something
        try:
            data = the_socket.recv(8192)
            if data:
                total_data.append(data)
                #change the beginning time for measurement
                begin=time.time()
            else:
                #sleep for sometime to indicate a gap
                time.sleep(0.1)
        except:
            pass

    #join all parts to make final string
    return ''.join(total_data)
	
while 1:
       clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       clientsocket.connect(('192.168.11.2',5000))
       clientsocket.send('d')
       data=recv_timeout(clientsocket,2)
       json_data = json.dumps(data)
       #send data to Hologram
       if __name__ == "__main__":
		print "Sending Wiz750Sr data Hologram Cloud"
		hologram = HologramCloud(dict(), network='cellular')
		print 'Cloud type: ' + str(hologram)
		recv = hologram.sendMessage(json_data,topics = ['wiz750sr-Warehouse-1'],timeout = 5)
		print 'RESPONSE MESSAGE: ' + hologram.getResultString(recv)
       print data
       clientsocket.close()
       time.sleep(5)

Credits

Muhammad Afzal

Muhammad Afzal

25 projects • 117 followers
I am Software Eng having 13+ Years of experience. Hackster.io & Cayenne Mydevices Ambassador in Pakistan.

Comments