Connecting a accelerometer to Particle Argon and sending on/off signals over RTW

24. januar 2019

In this tutorial I will show you how to connect a accelerometer to Particle Argon and using curl to send on/off commands over RTW. I will be using the MPU6050 accelerometer, which is a module that measures the force that is applied to the XYZ-axsises of the device it also has a gyro but we will not use this function in this example.

Hardware

1 x Particle Argon

1 x MPU6050

4 x Wire

Schematics

The MPU6050 has 8 pins which includes Vcc, Gnd, SCL and SDA.

GND

Gnd

Vcc

Vcc

SDA

SDA

SCL

SCL

Code

&amp;amp;lt;/pre&amp;amp;gt;
#include "MPU6050.h"
#include &amp;amp;lt;cmath&amp;amp;gt;
int ledPin = D7;
// MPU variables:
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
//Variables to save states
int lastZ = 0;
int lastS = false;
//Initiation of the RTWparticle libary(No parameters passed since we are not using the recive capabilitys of the libary)
RTWparticle rtwParticle("", "", "", "", "");
void setup() {
//Calling Wire.begin() to be able to use the sda and scl capabilitys of the particle
Wire.begin();
//Starting the accelgyrofunction.
accelgyro.initialize();
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&amp;amp;amp;ax, &amp;amp;amp;ay, &amp;amp;amp;az, &amp;amp;amp;gx, &amp;amp;amp;gy, &amp;amp;amp;gz);
//Setting the lastZ if it is not yet set
if(lastZ == 0){
lastZ = az;
}
//Setting the senstivity to 2000 so that there will be some force applied to the device to trigger.
int sensitivity = 2000;
//Calculating the treshold when the action should be triggered
int nowZMi = az - sensitivity;
//Defining string for rtwTxt
String rtwTxt;
//Checking if lastZ is lower than the minimum trigger value
if(lastZ &amp;amp;lt; nowZMi){
//Checking if last state is false if it is it sets rtwTxt to 1 and last state to true if not it does the oposite
if(!lastS){
lastS = true;
rtwTxt = "1";
}
else{
lastS = false;
rtwTxt = "0";
}
//Sending rtw message
rtwParticle.sendRTW("ServiceName", "ObjectID", "User", "password", "onOff=" + rtwTxt, "Domain");
//Delay to avoid double registration
delay(500);
}
}