Also check out or community

Modular codePostsVision

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

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

</pre>
#include "MPU6050.h"
#include <cmath>
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(&ax, &ay, &az, &gx, &gy, &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 < 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);
}

}
Related posts
NB-IoTPosts

Connecting NB-IoT to RTW with Telenor

PostsWifi

Code used to connect to WIFI with Esp32 (Referenced as WifiCode)

PostsWifi

Turning on the lights with a clap

PostsWifi

Automatic trash can opener based on distance

Legg igjen en kommentar

Din e-postadresse vil ikke bli publisert. Obligatoriske felt er merket med *