UbiMaker
Published © LGPL

How To Build A Motion Detection System With Arduino

Build a motion and presence production counter using a Feather HUZZAH programmed with Arduino and powered by Ubidots.

IntermediateProtip5 hours1,315
How To Build A Motion Detection System With Arduino

Things used in this project

Hardware components

Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1
MB7389-100 Ultrasonic Sensor
×1

Software apps and online services

Ubidots
Ubidots
Arduino IDE
Arduino IDE

Story

Read more

Code

Code snippet #1

Plain text
/********************************
   Libraries included
 *******************************/
#include <ESP8266WiFi.h>

/********************************
   Constants and objects
 *******************************/

/* Ubidots */
const char* SSID_NAME = "xxxxxxxx"; // Put here your SSID name
const char* SSID_PASS = "xxxxxxxx"; // Put here your password
const char* TOKEN = "Assig_your_ubidots_token"; // Put here your TOKEN
const char* DEVICE_LABEL = "motion-control"; // Your device label
const char* VARIABLE_LABEL = "distance"; // Your variable label
const char* USER_AGENT = "ESP8266";
const char* VERSION = "1.0";
const char* HTTPSERVER = "industrial.api.ubidots.com";// Ubidots Business users
//const char* HTTPSERVER = "things.ubidots.com"; // Ubidots Educational users

int HTTPPORT = 80;
/* Ultrasonic Sensor */
const int pwPin1 = 5; // The PWM pin where the sensor is connected

WiFiClient clientUbi;

/********************************
   Auxiliar Functions
 *******************************/
/**
   Gets the length of the body
   @arg variable the body of type char
   @return dataLen the length of the variable
*/
int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable[i] != '\0') {
      dataLen++;
    } else {
      break;
    }
  }
  return dataLen;
}

/********************************
   Main Functions
 *******************************/

void setup() {
  Serial.begin(115200);

  /* Connects to AP */
  WiFi.begin(SSID_NAME, SSID_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  pinMode(pwPin1, INPUT);
  WiFi.setAutoReconnect(true);
  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

}

void loop() {
  /* The sensor reading is returned in millimeters, if you desire convert
   * it to inches just create a variable pointing to -> distance / 25.4 */
  float distance = pulseIn(pwPin1, HIGH);
  sendToUbidots(DEVICE_LABEL, VARIABLE_LABEL, distance);
  delay(1000);
}

void sendToUbidots(const char* device_label, const char* variable_label, float sensor_value) {

  char* body = (char *) malloc(sizeof(char) * 150);
  char* data = (char *) malloc(sizeof(char) * 300);
  /* Space to store values to send */
  char str_val[10];

  /*---- Transforms the values of the sensors to char type -----*/
  /* 4 is mininum width, 2 is precision; float value is copied onto str_val*/
  dtostrf(sensor_value, 4, 2, str_val);

  /* Builds the body to be send into the request*/
  sprintf(body, "{\"%s\":%s}", variable_label, str_val);

  /* Builds the HTTP request to be POST */
  sprintf(data, "POST /api/v1.6/devices/%s", device_label);
  sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: things.ubidots.com\r\n", data);
  sprintf(data, "%sUser-Agent: %s/%s\r\n", data, USER_AGENT, VERSION);
  sprintf(data, "%sX-Auth-Token: %s\r\n", data, TOKEN);
  sprintf(data, "%sConnection: close\r\n", data);
  sprintf(data, "%sContent-Type: application/json\r\n", data);
  sprintf(data, "%sContent-Length: %d\r\n\r\n", data, dataLen(body));
  sprintf(data, "%s%s\r\n\r\n", data, body);

  /* Initial connection */
  clientUbi.connect(HTTPSERVER, HTTPPORT);

  /* Verify the client connection */
  if (clientUbi.connect(HTTPSERVER, HTTPPORT)) {
    Serial.println(F("Posting your variables: "));
    Serial.println(data);
    /* Send the HTTP Request */
    clientUbi.print(data);
  }

  /* While the client is available read the response of the server */
  while (clientUbi.available()) {
    char c = clientUbi.read();
    Serial.write(c);
  }
  /* Free memory */
  free(data);
  free(body);
  /* Stop the client */
  clientUbi.stop();
}

Code snippet #2

Plain text
/********************************
   Libraries included
 *******************************/
#include <ESP8266WiFi.h>

/********************************
   Constants and objects
 *******************************/

/* Ubidots */
const char* SSID_NAME = "xxxxxxxx"; // Put here your SSID name
const char* SSID_PASS = "xxxxxxxx"; // Put here your password
const char* TOKEN = "Assig_your_ubidots_token"; // Put here your TOKEN
const char* DEVICE_LABEL = "motion-control"; // Your device label
const char* VARIABLE_LABEL = "distance"; // Your variable label
const char* USER_AGENT = "ESP8266";
const char* VERSION = "1.0";
const char* HTTPSERVER = "industrial.api.ubidots.com";// Ubidots Business users
//const char* HTTPSERVER = "things.ubidots.com"; // Ubidots Educational users

int HTTPPORT = 80;
/* Ultrasonic Sensor */
const int pwPin1 = 5; // The PWM pin where the sensor is connected

WiFiClient clientUbi;

/********************************
   Auxiliar Functions
 *******************************/
/**
   Gets the length of the body
   @arg variable the body of type char
   @return dataLen the length of the variable
*/
int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable[i] != '\0') {
      dataLen++;
    } else {
      break;
    }
  }
  return dataLen;
}

/********************************
   Main Functions
 *******************************/

void setup() {
  Serial.begin(115200);

  /* Connects to AP */
  WiFi.begin(SSID_NAME, SSID_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  pinMode(pwPin1, INPUT);
  WiFi.setAutoReconnect(true);
  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

}

void loop() {
  /* The sensor reading is returned in millimeters, if you desire convert
   * it to inches just create a variable pointing to -> distance / 25.4 */
  float distance = pulseIn(pwPin1, HIGH);
  sendToUbidots(DEVICE_LABEL, VARIABLE_LABEL, distance);
  delay(1000);
}

void sendToUbidots(const char* device_label, const char* variable_label, float sensor_value) {

  char* body = (char *) malloc(sizeof(char) * 150);
  char* data = (char *) malloc(sizeof(char) * 300);
  /* Space to store values to send */
  char str_val[10];

  /*---- Transforms the values of the sensors to char type -----*/
  /* 4 is mininum width, 2 is precision; float value is copied onto str_val*/
  dtostrf(sensor_value, 4, 2, str_val);

  /* Builds the body to be send into the request*/
  sprintf(body, "{\"%s\":%s}", variable_label, str_val);

  /* Builds the HTTP request to be POST */
  sprintf(data, "POST /api/v1.6/devices/%s", device_label);
  sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: things.ubidots.com\r\n", data);
  sprintf(data, "%sUser-Agent: %s/%s\r\n", data, USER_AGENT, VERSION);
  sprintf(data, "%sX-Auth-Token: %s\r\n", data, TOKEN);
  sprintf(data, "%sConnection: close\r\n", data);
  sprintf(data, "%sContent-Type: application/json\r\n", data);
  sprintf(data, "%sContent-Length: %d\r\n\r\n", data, dataLen(body));
  sprintf(data, "%s%s\r\n\r\n", data, body);

  /* Initial connection */
  clientUbi.connect(HTTPSERVER, HTTPPORT);

  /* Verify the client connection */
  if (clientUbi.connect(HTTPSERVER, HTTPPORT)) {
    Serial.println(F("Posting your variables: "));
    Serial.println(data);
    /* Send the HTTP Request */
    clientUbi.print(data);
  }

  /* While the client is available read the response of the server */
  while (clientUbi.available()) {
    char c = clientUbi.read();
    Serial.write(c);
  }
  /* Free memory */
  free(data);
  free(body);
  /* Stop the client */
  clientUbi.stop();
}

Credits

UbiMaker

UbiMaker

53 projects • 229 followers
Maker @ ubidots.com

Comments