Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
vincent wong
Created October 25, 2019 © GPL3+

Workspace Management System

Workspace Management System

IntermediateFull instructions provided76
Workspace Management System

Things used in this project

Hardware components

Proximity Sensor- Pyroelectric Infrared Sensor Module
KEMET Electronics Corporation Proximity Sensor- Pyroelectric Infrared Sensor Module
×1
Arduino MKR1000
Arduino MKR1000
×1
JST-SH 1.0mm 5 Pin 5P DIY Cable
×1
Crocodile Clips
×3

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Pyro Arduino Schematic

Code

kemet_pyro_sensor.ino

C/C++
#include <SPI.h>
#include <WiFi101.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char dweet_server[] = "dweet.io";    // name address for dweet.io (using DNS)
char thingsboard_server[] = "demo.thingsboard.io";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;



int Pyro = A1;
unsigned long PyroRead = 0;
unsigned long IR_threshold = 20000;   //198000;

// Note: SS-430 has two pulses of 200msec per detection.
// IR_threshold is in microsec (usec), therefore 198msec threshold

int LED = 7;
int Detected = LOW;
int IR_sensed = 0;


void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

    // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");

  pinMode (7, OUTPUT); //LED Connected to Pin 7
  pinMode (A1,INPUT); // IR Sensor connected to A1

}

void dweet(int onOff) {
  String command = "GET /dweet/for/my-pyro-sensor?pyro=";
  command += onOff ? "on" : "off";
  command += " HTTP/1.1";
  
  Serial.println("\nStarting connection to dweet.io...");
  // if you get a connection, report back via serial:
  if (client.connect(dweet_server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    // client.println("GET /dweet/for/my-pyro-sensor?pyro=on HTTP/1.1");
    client.println(command);
    client.println("Host: dweet.io");
    client.println("Connection: close");
    client.println();

    delay(2000);
   
    while (client.available()) { // Print on the console the answer of the server
      char c = client.read();
      Serial.write(c);
    }
    client.stop();  // Disconnect from the server
  }
}

void post_thingsboard(int onOff) {
  String command = String("POST /api/v1/") + THINGSBOARD_TOKEN + "/telemetry HTTP/1.1";
  String data = String("{ \"pyro\": \"");
  data += onOff ? "on" : "off";
  data += "\" }";
  
  Serial.println("\nStarting connection to demo.thingsboard.io...");
  // if you get a connection, report back via serial:
  if (client.connect(thingsboard_server, 80)) {
    Serial.println("connected to server");
    Serial.println(command);
    Serial.println(data);
    Serial.println(data.length());

    // Make a HTTP request:
    client.println(command);
    client.print("Host: "); client.println(thingsboard_server);
    client.print("Content-Length: "); client.println(data.length());
    client.println("Connection: close");
    client.println("Content-Type:application/json");
    client.println();
    client.println(data);
    client.println();

    delay(2000);
   
    while (client.available()) { // Print on the console the answer of the server
      char c = client.read();
      Serial.write(c);
    }
    // client.stop();  // Disconnect from the server
  }
  
}

void loop() {

  while ((IR_sensed < 2)){ 
    //Break after 2 good triggers 
    PyroRead = pulseIn(A1, HIGH); 

    Serial.print(PyroRead); Serial.println();
    
    //Measure trigger point 
    if(PyroRead > IR_threshold){ //Make sure trigger is over 198msec)
      IR_sensed++; //Mark as a good trigger
    }
  }
  
  if (Detected == HIGH){ // Turn LED OFF if it was previous ON
    Detected = LOW;
    digitalWrite(7, LOW);
  } else {
    Detected = HIGH; // Turn LED ON if it was previous OFF
    digitalWrite(7, HIGH); 
  }
  // dweet(Detected);
  post_thingsboard(Detected);
  
  PyroRead = 0; // Reset readings
  IR_sensed = 0;
  
  delay(1000); // Accept triggers after a second

}

arduino_secrets.h

C/C++
#define SECRET_SSID ""
#define SECRET_PASS ""
#define THINGSBOARD_TOKEN ""

Credits

vincent wong

vincent wong

81 projects • 205 followers

Comments