Malladi Naga Subhash
Published

AWPS (Advanced Workers Protection System)

AWPS is a system that integrates different sensors which should be used by the workers in the construction site to safeguard themselves

IntermediateWork in progress10 hours274
AWPS (Advanced Workers Protection System)

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Arduino UNO
Arduino UNO
×1
LM35
Temperature Sensor
×1
GY-521
3-axis gyroscope, a 3-axis accelerometer, a digital motion processor (DMP), and a temperature sensor.
×1
RSL10-SENSE-GEVK
onsemi RSL10-SENSE-GEVK
×1

Software apps and online services

Adafruit cloud
Maker service
IFTTT Maker service
MQTT Dashboard app
Download the app from playstore
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit diagram

Code

Arduino Code

C/C++
#include "Wire.h"
#include<SoftwareSerial.h>// This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
  sprintf(tmp_str, "%6d", i);
  return tmp_str;
}

SoftwareSerial Arduino(5,6);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
  Wire.write(0x6B); // PWR_MGMT_1 register
  Wire.write(0); // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
}
void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
  Arduino.begin(4800); //Starting the communication between arduino and nodemcu
  pinMode(A3,INPUT);
  
  // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
 
  
  // print out data
  Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
  Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
  Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));

  Serial.println();
  if(accelerometer_x == 0 && accelerometer_y == 0)
  {
    int data = 404;
    Arduino.println(data);
  }
  else
  {
    int data = 200;
    Arduino.println(data);
  }
  int temp = analogRead(A3);
  int tempout = temp/340.00+36.53;
  Serial.println(tempout);
  Arduino.println(tempout);
  delay(1000);
}

NodeMCU code

C/C++
#include<SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

SoftwareSerial nodemcu(D3,D1);




/************************* WiFi Access Point *********************************/

#define WLAN_SSID       ""
#define WLAN_PASS       ""   // Add your wifi credentials

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "mns2610"
#define AIO_KEY         "aio_Mwpl96TNa24LSxDDeR3AbT8927JH"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;


// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/AWPStemp");
Adafruit_MQTT_Publish personstatus= Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/AWPSposition");

/*************************** Sketch Code ************************************/


void MQTT_connect();

void setup() {
  Serial.begin(115200);
  nodemcu.begin(4800);
  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());

  // Setup MQTT subscription for onoff feed.
  //mqtt.subscribe(&onoffbutton);
}

uint32_t x=0;

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here

  while(nodemcu.available()>0)
  {
    int temperature = nodemcu.parseInt();
    delay(3000);
    int positionstatus = nodemcu.parseInt();

    if(nodemcu.read()== '\n')
    {
    
      //Temperature Sensing
      Serial.println("temperature = ");
      Serial.println(temperature);
      delay(2000);

      //Status sensing
      Serial.println("Status ");
      Serial.println(positionstatus);
      
      delay(1000);

     
     
  // Now we can publish stuff!
      Serial.print(F("\nSending val "));
      Serial.print(x);
      Serial.print("...");
      if (! temp.publish(temperature)) {
        Serial.println(F("Failed"));
      } else {
        Serial.println(F("OK!"));
      }
      delay(30);

      if (!personstatus.publish(positionstatus)) {
        Serial.println(F("Failed"));
      } else {
        Serial.println(F("OK!"));
      }
      
     
      
  delay(2000);

  }
}
  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds

  if(! mqtt.ping()) {
    mqtt.disconnect();
  }

}


// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

Credits

Malladi Naga Subhash

Malladi Naga Subhash

1 project • 2 followers
Student | Tech Geek |Electronics Enthusiastic | Web developer | Ros developer

Comments