SqueejJoshua CinquemaniReece McCloskey
Published © GPL3+

MEGR 3171 Truck Analytics

We've bought a Truck and attached some sensor to analyze its surroundings.

BeginnerWork in progress3.5 hours105
MEGR 3171 Truck Analytics

Things used in this project

Hardware components

Argon
Particle Argon
×3
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Resistor 220 ohm
Resistor 220 ohm
×1
Breadboard (generic)
Breadboard (generic)
×3
Jumper wires (generic)
Jumper wires (generic)
×13
LED (generic)
LED (generic)
×1
RC Truck
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Temperature-Humidity Sensor Circuit

This was used to measure the temperature and humidity values of the surrounding air. We used a DHT22 and the sensor's pins were (left to right) INPUT - OUT - NULL (no connection) - GND. The resistor used is 10k ohm.

Ultrasonic Sensor Circuit

This was used to measure the proximity of the truck to a designated point/wall. The resistor used was 220 ohm.

LED Circuit

This was used to alert the user of when they came within a designated distance from a wall (using the ultrasonic sensor data). The resistor used is 220 ohm.

Code

Ultrasonic Sensor Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

// Set the trig and echo pins
const int trigPin = D2; 
const int echoPin = D6;

// Connect Vcc to 3.3V Pin
//Connect trig pin to D2
//Connect echo pin to 220 ohm resistor
//Connect Resistor to D6
//Connect GND to GND

//Define the variables
long duration;
int distance;
int safetyDistance;

TCPClient client; // Open hte TCP Client

unsigned long myChannelNumber = xxxxxx; //Change xxxx to your channel number
const char * myWriteAPIKey = "xxxxxxxxxxxxxxxx"; //Change xxxx to channels write API key

void setup() {
ThingSpeak.begin(client); //Begin ThinSpeak Client
pinMode(trigPin, OUTPUT); //Defines D2 as output
pinMode(echoPin, INPUT); //Defines D6 as input
Serial.begin(9600);
}
//Function begins collecting data from sensor
void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

duration = pulseIn(echoPin, HIGH); //Measures duration that the echo pin has a HIGH output
distance = duration*.034/2; //Converts the duration time to distance in centimeters
safetyDistance = distance;
delayMicroseconds(2000);

if (safetyDistance < 20) {
    Particle.publish("Running");
  //Publishes the function "Running" to the LED if the distance is less than 20cm
}
else 
 Particle.publish("off");
 //Publishes the function "off" to the LED if the distance is greater than 20cm
ThingSpeak.writeField(myChannelNumber, 1, String(safetyDistance), myWriteAPIKey);
//Sends the data collected to the Thing.Speak graph
Particle.publish("Distance in Cm", String(safetyDistance), PRIVATE);
//Publishes data to Particle Cloud
delayMicroseconds(20000);
//Delays data readings to every 20 seconds
}

Temperature-Humidity Sensor Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>


#define DHTPIN 2            // what pin we're connected to
#define DHTTYPE DHT22       // DHT 22 (AM2302)
#define INTERVAL 20000       // number of milliseconds between checks
                            // Reading temperature or humidity takes about 250 milliseconds! 
                            // Sensor readings may also be up to 2 seconds 'old' 
                            // (its a very slow sensor)

// Connect pin 1 (on the left) of the sensor to +3.3V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialise an object to represent the DHT22 sensor
DHT dht(DHTPIN, DHTTYPE);

// Declare globa variables for the readings and the time last checked
double temp;
double hum;
unsigned long lastCheck = 0;

TCPClient client;       // Open the TCP Client

unsigned long myChannelNumber = XXXXXXX;    // change this to your channel number
const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX"; // change this to your channels write API key
unsigned long myChannelNumber1 = XXXXXXX;     //Since Humidity and Temperature, need 2 Thingspeak Channels
const char * myWriteAPIKey1 = "XXXXXXXXXXXXXXXX";


// Function that can be called from the Particle console
void readSensor() {
    hum = dht.getHumidity();
    temp = dht.getTempCelcius();
}

// Callback function to allow the Particle console access to readSensor()
int checkHandler(String command) {
readSensor();
return 1;
}

void setup() {
    ThingSpeak.begin(client);     // Begin Thingspeak client
    dht.begin();

    // Declare variables that can be seen in the Particle console
    Particle.variable("temperature", temp);
    Particle.variable("humidity", hum);
    Particle.function("readSensor", checkHandler);
    Particle.subscribe("hook-response/temperature", myHandler, MY_DEVICES);
    Particle.subscribe("hook-response/humidity", myHandler, MY_DEVICES);
}

void loop() {
    if (lastCheck + INTERVAL < millis()) {
        lastCheck = millis();
        readSensor();

        // Check if any reads failed and exit early (to try again).
        if (isnan(hum) || isnan(temp)) {
            Serial.println("Failed to read from DHT sensor!");
            return;
            
             // Get some data
  String data = String(10);
  // Trigger the integration
  Particle.publish("temperature", data, PRIVATE);
  Particle.publish("humidity", data, PRIVATE);
    }
        Particle.publish("temperature", String(temp), PRIVATE);
        Particle.publish("humidity", String(hum), PRIVATE);
ThingSpeak.writeField(myChannelNumber, 1, String(temp), myWriteAPIKey); 
//Thingspeak for 1st Channel (Temp)
ThingSpeak.writeField(myChannelNumber1, 1, String(hum), myWriteAPIKey1);
}

}
void myHandler(const char *event, const char *data) {
  // Handle the integration response
}

Led Code

C/C++
const int led = D6; //States that the D6 Pin is connected to the Led.

void setup() {
    pinMode(led, OUTPUT); // Sets the Led Pin as output mode.
    digitalWrite(led, LOW); // This sets the led to initially be off.
    Particle.subscribe("Running", click, "xxxxxxxxxxxxxxxxxxxxxxxx"); 
  //This connects the Led Argon to the Ultrasonic Sensor Argon when the sensor is within 20 centimeters. 
    Particle.subscribe("off", off, "xxxxxxxxxxxxxxxxxxxxxxxx"); 
  //This connects the Led Argon to the Ultrasonic Sensor Argon when the sensor is outside of 20 centimeters. 
}

void click(const char *event, const char *data) {
  digitalWrite(led, HIGH); 
  //This is connected to the "Running" Particle.subscribe function.
}
void off(const char *event, const char *data) {
 digitalWrite(led, LOW);
 //This is connected to the "off" Particle.subscribe function.
}

Credits

Squeej
1 project • 3 followers
Contact
Joshua Cinquemani
1 project • 3 followers
Contact
Reece McCloskey
1 project • 4 followers
Contact

Comments

Please log in or sign up to comment.