Skip to content

Instantly share code, notes, and snippets.

@alexander-daniel
Created March 15, 2014 00:35
Show Gist options
  • Save alexander-daniel/9559980 to your computer and use it in GitHub Desktop.
Save alexander-daniel/9559980 to your computer and use it in GitHub Desktop.
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include <plotly_streaming_cc3000.h>
#define WLAN_SSID "evilfriends"
#define WLAN_PASS "nomodernjesus"
#define WLAN_SECURITY WLAN_SEC_WPA2
// arguments: username, api key, streaming token, filename
// e.g. logger("my_username", "abcdefghij", "ABCDEFGHIJ", "My plotly filename");
// Sign up to plotly here: https://plot.ly
// View your API key and streamtokens here: https://plot.ly/settings
plotly logger("demos", "tj6mr52zgp", "0xjvjp3pp9", "cc3000");
void wifi_connect(){
/* Initialise the module */
Serial.println(F("\n... Initializing..."));
if (!logger.cc3000.begin())
{
Serial.println(F("... Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
if (!logger.cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("... Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("... Request DHCP"));
while (!logger.cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
}
#define FLOWSENSORPIN 2
// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}
if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);
wifi_connect();
logger.begin(10); // show 50 points in the plot
}
unsigned long x;
void loop() {
float liters = pulses;
liters /= 8.1;
liters -= 6;
liters /= 60.0;
/*
// if a brass sensor use the following calculation
float liters = pulses;
liters /= 8.1;
liters -= 6;
liters /= 60.0;
*/
Serial.print(liters); Serial.println(" Liters");
x = millis();
logger.plot(x, liters);
delay(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment