Skip to content

Instantly share code, notes, and snippets.

@pkourany
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkourany/11237768 to your computer and use it in GitHub Desktop.
Save pkourany/11237768 to your computer and use it in GitHub Desktop.
Updated LSM303 code
#include "application.h"
#include "LSM303.h"
#define FEED_ID "xxxxxxxxxxxx" //note: fake id here..
#define XIVELY_API_KEY "Cxxxxxxxxxxxxxxxxxxxxxxx" //note: fake key here
TCPClient client;
LSM303 compass;
char report[100];
char doortemp[100];
char resultstr[120];
bool error;
int LEDpin = D7;
int doorstate = 0;
int laststate = 0;
int doorcount = 0;
int trigger = 0;
unsigned long LastUpTime = 0;
unsigned long LED_timer = 0;
bool LED_trigger = false;
enum xvar {COUNT, TEMP, MAGX, MAGY, MAGZ};
void xively(xvar type, int var);
void ledStatus(int x, int t);
void setup()
{
Serial.begin(9600);
Wire.begin();
error = compass.init(); // Returns "true" if device found
compass.enableDefault();
compass.writeReg(LSM303::CTRL5, 0xE4); //to enable temperature sensor
compass.writeReg(LSM303::CTRL2, 0x00); //to use 2g Acc scale
pinMode(LEDpin, OUTPUT);
Spark.variable("doorcount" , &doorcount, INT);
Spark.variable("doorstate" , &doorstate, INT);
Spark.variable("laststate" , &laststate, INT);
Spark.variable("report", &report, STRING);
Spark.variable("doortemp", &doortemp, STRING);
Spark.variable("result", &resultstr, STRING);
}
void loop()
{
compass.read();
byte tl = compass.readReg(LSM303::TEMP_OUT_L);
byte th = compass.readReg(LSM303::TEMP_OUT_H);
int temperature_raw = (int16_t)(th << 8 | tl);
int temperature = ((float)temperature_raw / 8) + 20;
int Mx = compass.m.x;
Serial.print("Mx: ");
Serial.print(Mx, 1);
int My = compass.m.y;
Serial.print("My: ");
Serial.print(My, 1);
int Mz = compass.m.z;
Serial.print("Mz: ");
Serial.print(Mz, 1);
const int xU = 3000; // Upper boundary - Set as required
const int xL = 2000; // Lower boundary
if ((Mx > xU) || (Mx <= xL)) {
LED_trigger = true;
}
if (Mx > xU) {
trigger = HIGH;
}
if (Mx <= xL) {
trigger = LOW;
}
// read the door input pin:
doorstate = trigger;
// compare the buttonState to its previous state
if (doorstate != laststate) {
// if the state has changed, increment the counter
if (doorstate == HIGH) {
// if the current state is HIGH then the button
// went from off to on:
doorcount++;
Serial.println("open");
Serial.print("number of door opens: ");
Serial.println(doorcount);
}
else {
// if the current state is LOW then the button
// went from on to off:
Serial.println("closed");
}
}
// save the current state as the last state,
//for next time through the loop
laststate = doorstate;
if ((millis()-LastUpTime) > 2000){
// digitalWrite(LEDpin, HIGH);
// Spark.publish("AccMag", String(report));
// Spark.publish("Mx", String(Mx));
xively(COUNT, doorcount);
xively(TEMP, temperature);
xively(MAGX, Mx);
xively(MAGY, My);
xively(MAGZ, Mz);
LastUpTime = millis();
}
if (LED_trigger) { // Check for LED trigger condition
LED_trigger = false; // Reset trigger for next event, set LED_timer to non-zero value
LED_timer = millis();
digitalWrite(LEDpin, HIGH); // Turn on the LED
}
// LED timer will only be checked if LED_timer is non-zero
if ((LED_timer != 0) && ((millis()-LED_timer) > 10000)){ // Wait for 10sec LED timer
digitalWrite(LEDpin, LOW); // Turn off the LED
LED_timer = 0; // Set LED_timer to zero to stop time check
}
}
void xively(xvar type, int var) {
int stat;
//Serial.println("Connecting to server...");
if (client.connect("api.xively.com", 8081))
{
// Connection succesful, update datastreams
client.print("{");
client.print(" \"method\" : \"put\",");
client.print(" \"resource\" : \"/feeds/");
client.print(FEED_ID);
client.print("\",");
client.print(" \"params\" : {},");
client.print(" \"headers\" : {\"X-ApiKey\":\"");
client.print(XIVELY_API_KEY);
client.print("\"},");
client.print(" \"body\" :");
client.print(" {");
client.print(" \"version\" : \"1.0.0\",");
client.print(" \"datastreams\" : [");
client.print(" {");
switch (type) {
case COUNT:
client.print(" \"id\" : \"countingsheep\",");
stat = 1;
break;
case TEMP:
client.print(" \"id\" : \"roomtemp2\",");
stat = 2;
break;
case MAGX:
client.print(" \"id\" : \"magx\",");
stat = 3;
break;
case MAGY:
client.print(" \"id\" : \"magy\",");
stat = 4;
case MAGZ:
client.print(" \"id\" : \"magz\",");
stat = 5;
break;
default:
break;
}
client.print(" \"current_value\" : \"");
client.print(var);
client.print("\"");
client.print(" }");
client.print(" ]");
client.print(" },");
client.print(" \"token\" : \"0x123abc\"");
client.print("}");
client.println();
ledStatus(stat, 50);
}
else
{
// Connection failed
Serial.println("connection failed");
// ledStatus(3, 2000);//
}
if (client.available())
{
// Read response
//char c = client.read();
//Serial.print(c);
}
if (!client.connected())
{
//Serial.println();
//Serial.println("disconnecting.");
client.stop();
}
client.flush();
client.stop();
}
void ledStatus(int x, int t)
{
for (int j = 0; j <= x-1; j++)
{
digitalWrite(LEDpin, HIGH);
delay(t);
digitalWrite(LEDpin, LOW);
delay(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment