My project is reading sensor data from a flow sensor, converting to flow rate and total volume, add time stamp from chronodot and publish to Carriots. I believe the problem is memory overload due to the size of the strings. Is there a better more efficient way to write the below code that may reduce SRAM? I don't have an ethernet shield. I do have a DUE but don't know how to convert the library for the CC3000.
Code: Select all
// Libraries
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include <string.h>
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
RTC_DS3231 RTC;
// Define CC3000 chip pins
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// Create CC3000 instances
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2); // you can change this clock speed
// WLAN parameters
#define WLAN_SSID ""
#define WLAN_PASS ""
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_UNSEC
// Carriots parameters
#define WEBSITE "api.carriots.com"
#define API_KEY ""
#define DEVICE ""
uint32_t ip;
#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(void)
{
// Initialize
RTC.begin();
Serial.begin(115200);
Wire.begin();
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);
Serial.println("Setup complete --->");
}
void loop(void)
{
// Connect to WiFi network
cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY);
Serial.println(F("Connecting -->"));
/* Wait for DHCP to complete */
Serial.println(F("Requesting DHCP -->"));
while (!cc3000.checkDHCP())
{
delay(100);
}
// Get the website IP & print it
ip = 0;
Serial.print(F("Connecting to site ")); Serial.print(WEBSITE); Serial.print(F(" -> "));
while (ip == 0) {
if (! cc3000.getHostByName(WEBSITE, &ip)) {
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
DateTime now = RTC.now();
float liters = pulses;
liters /= 4.8;
liters /= 60.0;
// Convert data to String
//**Total Liters to String
char tL[10];
String sL;
dtostrf(liters,1,3,tL);
sL = String(tL);
//**Flow Rate to String
char tR[10];
String sR;
dtostrf(flowrate,1,3,tR);
sR = String(tR);
//**Time Stamp to String
String sT = String(now.hour(),DEC)+ ":" +String(now.minute(),DEC)+ ":" +String(now.second(),DEC);
// Prepare JSON for Carriots & get length
int length = 0;
String data = "{\"protocol\":\"v2\",\"device\":\"" +String(DEVICE) + "\",\"at\":\"now\",\"data\":{\"Time\":"+ String(sT)+",\"Rate\":"+String(sR)+",\"Liters\":"+String(sL)+"}}";
length = data.length();
// Send request
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected()) {
Serial.println("");
Serial.println(F("Sending Data -->"));
client.println(F("POST /streams HTTP/1.1"));
client.println(F("Host: api.carriots.com"));
client.println(F("Accept: application/json"));
client.println(F("User-Agent: Arduino-Carriots"));
client.println(F("Content-Type: application/json"));
client.println("carriots.apikey: " +String(API_KEY));
client.println("Content-Length: " + String(length));
client.println(F("Connection: close"));
client.println();
client.println(data);
Serial.println(F("Data sent -->"));
data = "";
} else {
Serial.println(F("Send Failed!"));
return;
}
Serial.println(F("-------------------------------------"));
while (client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c);
}
}
client.close();
Serial.println(F("-------------------------------------"));
Serial.println(F("\n\nDisconnecting"));
cc3000.disconnect();
// Wait 10 seconds until next update
flowrate=0;
delay(1000);
}