Skip to content

Instantly share code, notes, and snippets.

@schollz
Created May 1, 2016 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schollz/d388b0c0ed1bb3b604eba6b7154a49d1 to your computer and use it in GitHub Desktop.
Save schollz/d388b0c0ed1bb3b604eba6b7154a49d1 to your computer and use it in GitHub Desktop.
// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"
// SWITCH
unsigned int SLEEP = 0;
unsigned int GREENLIGHT = 0;
unsigned int REDLIGHT = 0;
unsigned int nextTime = 0;
String group = "YOURGROUP";
String user = "YOURUSER";
String server = "ml.internalpositioning.com";
String password = "YOURPASSWORD"; // set with curl -X PUT "https://ml.internalpositioning.com/mqtt?group=YOURGROUP"
MQTT client("ml.internalpositioning.com", 1883, NULL );
void button_handler(system_event_t event, int duration, void* )
{
if (!duration) { // just pressed
RGB.control(true);
if (SLEEP == 0) {
RGB.color(0,255,0);
GREENLIGHT = 1;
SLEEP = 1; // sleep mode on
} else {
RGB.color(255,0,0);
REDLIGHT = 1;
SLEEP = 0; // sleep mode off
}
}
else { // just released
RGB.control(false);
GREENLIGHT = 0;
REDLIGHT = 0;
}
}
void setup() {
RGB.control(false);
// connect to the server
client.connect(server,group,password);
System.on(button_status, button_handler);
}
void loop() {
// SWITCH
if (REDLIGHT == 1) {
RGB.color(255,0,0);
}
if (GREENLIGHT == 1) {
RGB.color(0,255,0);
}
if (nextTime > millis()) {
return;
}
if (client.isConnected()) {
String body;
body = "";
WiFiAccessPoint aps[20];
int found = WiFi.scan(aps, 20);
for (int i=0; i<found; i++) {
WiFiAccessPoint& ap = aps[i];
char mac[17];
sprintf(mac,"%02x%02x%02x%02x%02x%02x",
ap.bssid[0] & 0xff, ap.bssid[1] & 0xff, ap.bssid[2] & 0xff,
ap.bssid[3] & 0xff, ap.bssid[4] & 0xff, ap.bssid[5] & 0xff);
body = body + mac;
float f = -1*ap.rssi;
if (f > 100) {
f = 99;
}
String sf(f, 0);
if (f < 10) {
body = body + " " + sf;
} else {
body = body + sf;
}
}
client.publish(group + "/track/" + user,body); // Use this for tracking
// client.publish(group + "/learn/" + user + "/desk",body); // Use this for learning
nextTime = millis() + 2000; // sends response every 5 seconds (2 sec delay + ~3 sec for gathering signals)
// // SWITCH
if (SLEEP == 1) {
System.sleep(5);
}
client.loop();
} else {
client.connect(server,group,password);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment