Skip to content

Instantly share code, notes, and snippets.

@dimme
Last active December 14, 2024 15:35
Show Gist options
  • Save dimme/0c2ed93c9d32bec7d96a to your computer and use it in GitHub Desktop.
Save dimme/0c2ed93c9d32bec7d96a to your computer and use it in GitHub Desktop.
MailBuzz
/****************************************************************/
/* MailBuzz implementation for Arduino by Dimme */
/****************************************************************/
#include <SPI.h>
#include <SFE_CC3000.h>
#include <SFE_CC3000_Client.h>
#include "LinkedList.h"
// Pins
#define CC3000_INT 2 // Needs to be an interrupt pin (D2/D3)
#define CC3000_EN 7 // Can be any digital pin
#define CC3000_CS 10 // Preferred is pin 10 on Uno
// Connection info data lengths
#define IP_ADDR_LEN 4 // Length of IP address in bytes
// Size for the list of differentials
#define LIST_SIZE 100
// Enable if you want serial port outputs
//#define DEBUG
// Constants
char ap_ssid[] = "ssid"; // SSID of network
char ap_password[] = "password"; // Password of network
unsigned int ap_security = WLAN_SEC_WPA2; // Security of network
unsigned int timeout = 30000; // Milliseconds
char server[] = "example.com"; // Remote host site
// Global Variables
SFE_CC3000 wifi = SFE_CC3000(CC3000_INT, CC3000_EN, CC3000_CS);
SFE_CC3000_Client client = SFE_CC3000_Client(wifi);
int pinX = A2; // select the input pin for the potentiometer
int pinY = A1; // select the input pin for the potentiometer
int pinZ = A0; // select the input pin for the potentiometer
int newAccelerometerValue = 0; // variable to store the value coming from the sensor
int oldAccelerometerValue = 0; // variable to store the value coming from the sensor
int diff = 0;
// Calculate the RMS value of a list of integers
int rms(LinkedList<int>& list) {
int sqsum = 0;
int i;
for (i = 0; i < list.size(); i++) {
sqsum += sq(list.get(i));
}
return sqrt(sqsum / list.size());
}
void setup() {
#ifdef DEBUG
// Initialize Serial port
Serial.begin(9600);
Serial.println("---------------------------");
Serial.println(" MailBuzz Detector ");
Serial.println("---------------------------");
#endif
oldAccelerometerValue = sqrt(sq(analogRead(pinX)) + sq(analogRead(pinY)) + sq(analogRead(pinZ)));
delay(2);
}
void loop() {
LinkedList<int> diffs = LinkedList<int>();
// Detect if the accelerometer has moved
while (diffs.size() < LIST_SIZE || diff / 6 <= rms(diffs)) {
newAccelerometerValue = sqrt(sq(analogRead(pinX)) + sq(analogRead(pinY)) + sq(analogRead(pinZ)));
diff = abs(newAccelerometerValue - oldAccelerometerValue);
// Keep history of diffs
diffs.unshift(diff);
if (diffs.size() > LIST_SIZE) diffs.pop();
#ifdef DEBUG
Serial.print(rms(diffs));
Serial.print(": ");
Serial.println(diff / 6);
#endif
// Store the old sensor value in order to take the differential
oldAccelerometerValue = sqrt(sq(analogRead(pinX)) + sq(analogRead(pinY)) + sq(analogRead(pinZ)));
delay(2);
}
// Now the accelerometer has moved
#ifdef DEBUG
Serial.print("Detected movement: ");
Serial.println(diff);
Serial.println("Initiating networking sorcery...");
#endif
// Connect the wifi
wifi.init();
if (wifi.connect(ap_ssid, ap_security, ap_password, timeout)) {
#ifdef DEBUG
Serial.print("Connected to SSID: ");
Serial.println(ap_ssid);
#endif
}
#ifdef DEBUG
ConnectionInfo connection_info;
if (wifi.getConnectionInfo(connection_info) ) {
Serial.print("Got IP Address: ");
int i;
for (i = 0; i < IP_ADDR_LEN; i++) {
Serial.print(connection_info.ip_address[i]);
if ( i < IP_ADDR_LEN - 1 ) {
Serial.print(".");
}
}
Serial.println();
}
#endif
// Make a GET request
#ifdef DEBUG
Serial.println("Sending report to server...");
#endif
client.connect(server, 80);
client.print("GET /mailbuzz/?strength=");
client.print(diff);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
client.read();
// Disconnect the wifi
client.close();
wifi.disconnect();
#ifdef DEBUG
Serial.println("Closed connection");
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment