Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created April 21, 2016 21:16
Show Gist options
  • Save dwblair/5c381584b1ab50ae1c339ccac22290a1 to your computer and use it in GitHub Desktop.
Save dwblair/5c381584b1ab50ae1c339ccac22290a1 to your computer and use it in GitHub Desktop.
#include <Adafruit_FONA.h>
#include <JeeLib.h>
#include <TimerOne.h>
// for the turbidity measurement
const int led = 3; // the pin with a LED
int ledState = LOW;
char sensorIDChar [] = "2"; //the sensor id
#define sleepMinutes 1
char sensorTypeChar []="A";
//http://159.203.128.53/streams/DawQO646KGUJ0k41pbA0fxpQ8w6
char publicKey[]="DawQO646KGUJ0k41pbA0fxpQ8w6";
char privateKey[]= "zv4NLm9mz5sglxzZmYqlHegqwAQ";
char tokenChar[]="ffc15f4fa95d4893d1ba";
char phoneNum[]="85870";
//thermistor stuff -----------------------
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 5
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor
#define SERIESRESISTOR 10000
//fona stuff -----------------------------------
#define DEBUG 0
#define FONA_RX 2
#define FONA_TX 9
#define FONA_RST 4
#define FONA_KEY 8
#define FONA_POWER_STATUS A1
#define failCountMax 5
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
char sendBuffer[220];
#ifdef __AVR__
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
#else
HardwareSerial *fonaSerial = &Serial1;
#endif
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
float rando;
//conductivity stuff
long pulseCount = 0; //a pulse counter variable
unsigned long pulseTime,lastTime, duration, totalDuration;
int samplingPeriod=3; // the number of seconds to measure 555 oscillations
int interruptPin = 1; //corresponds to D2
void setup() {
//blink
pinMode(13, OUTPUT);
pinMode(FONA_POWER_STATUS,INPUT);
pinMode(FONA_KEY, OUTPUT);
// make sure the FONA power cycling key is initially pulled 'high' -- i.e. not triggered
digitalWrite(FONA_KEY, HIGH); //go back to non-trigger
if (DEBUG) {
while (!Serial);
Serial.begin(115200);
}
}
void loop() {
// fona ---------------------------------------
power_up_fona();
int fonaStatus=initialize_fona();
if (!fonaStatus) {
power_down_fona();
if (!DEBUG) go_to_sleep_minutes(sleepMinutes);
if(DEBUG) delay(20000);
}
else{
int networkStatus=fona_find_network();
if ((networkStatus!=1)&&(networkStatus!=5)) {
}
if (((networkStatus==1)||(networkStatus==5))&&fonaStatus) { //then we're good to send a message!
fona.sendCheckReply(F("AT"), F("OK"));
delay(100);
//fona.sendCheckReply(F("AT+SAPBR=3,1,\"APN\",\"internet.comcel.com.co\""), F("OK"));
fona.sendCheckReply(F("AT+SAPBR=3,1,\"APN\",\"wholesale\""), F("OK"));
delay(100);
fona.sendCheckReply(F("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\""), F("OK"));
delay(100);
fona.sendCheckReply(F("AT+SAPBR=1,1"),F("OK"));
delay(100);
fona.sendCheckReply(F("AT+CMGF=1"), F("OK"));
delay(5000);
fona.sendCheckReply(F("AT+CGATT=1"), F("OK"));
delay(5000);
uint16_t returncode;
char gpsbuffer[120];
fona.getGSMLoc(&returncode,gpsbuffer,120);
char *lonChar = strtok(gpsbuffer, ",");
char *latChar = strtok(NULL,",");
// lat = atof(latp);
// lon = atof(lonp);
char *dateChar = strtok(NULL,",");
char *timeChar = strtok(NULL,",");
// get battery level
uint16_t battLevel;
fona.getBattVoltage(&battLevel);
// char conversions
char tempChar[10];
float steinhart=10.0;
float freqHertz=10.0;
dtostrf(steinhart, 3, 2, tempChar);
char conductChar[10];
dtostrf(freqHertz,3,2,conductChar);
char battChar[10];
dtostrf(battLevel,3,2,battChar);
char phChar[]= "0";
char doChar[]= "0";
//fona.sendCheckReply(F("AT+SAPBR=3,1,\"APN\",\"internet.comcel.com.co\""), F("OK"));
fona.sendCheckReply(F("AT+SAPBR=3,1,\"APN\",\"wholesale\""), F("OK"));
delay(100);
fona.sendCheckReply(F("AT+SAPBR=1,1"),F("OK"));
delay(2000);
fona.sendCheckReply(F("AT+CMGF=0"), F("OK"));
delay(2000);
fona.sendCheckReply(F("AT+HTTPTERM"), F("OK"));
delay(2000);
fona.sendCheckReply(F("AT+HTTPINIT"), F("OK"));
delay(2000);
fona.sendCheckReply(F("AT+HTTPPARA=\"CID\",1"), F("OK"));
delay(2000);
// BEGIN post -- PHANT
//fona.sendCheckReply(F("AT+HTTPINIT"), F("OK"));
//delay(2000);
fona.sendCheckReply(F("AT+HTTPPARA=\"CID\",1"), F("OK"));
delay(2000);
//sprintf(sendBuffer,"AT+HTTPPARA=\"URL\",\"http://159.203.128.53/input/%s?private_key=%s&hour=%s&lat=%s&lon=%s&date=%s&temp=%s&conduct=%s&ph=%s&do=%s&batt=%s&sensortype=%s&sensorid=%s\"",publicKey,privateKey,timeChar,latChar,lonChar,dateChar,tempChar,conductChar,phChar,doChar,battChar,sensorTypeChar,sensorIDChar);
sprintf(sendBuffer,"AT+HTTPPARA=\"URL\",\"http://159.203.128.53/input/%s?private_key=%s&lat=%s&lon=%s&temp=%s&turbid=%s&batt=%s&sensortype=%s&sensorid=%s\"",publicKey,privateKey,latChar,lonChar,tempChar,conductChar,battChar,sensorTypeChar,sensorIDChar);
fona.sendCheckReply(sendBuffer,"OK");
//fona.sendCheckReply(F("AT+HTTPDATA=1000,5000"), F("OK"));
fona.sendCheckReply(F("AT+HTTPDATA=0,1000"), F("OK"));
delay(15000);
fona.sendCheckReply(F("AT+HTTPACTION=1"), F("OK"));
delay(20000);
fona.sendCheckReply(F("AT+HTTPTERM"), F("OK"));
delay(5000);
fona.sendCheckReply(F("AT+SAPBR=0,1"), F("OK"));
delay(10000);
/// send SMS
}
else{
if (!DEBUG) go_to_sleep_minutes(sleepMinutes);
if(DEBUG) delay(60000);
}
power_down_fona();
if (!DEBUG) go_to_sleep_minutes(sleepMinutes);
// go_to_sleep_seconds(60);
if(DEBUG) delay(60000);
}
}
void power_up_fona() {
int fonaPower=digitalRead(FONA_POWER_STATUS);
if (!fonaPower) { // off, so power up
digitalWrite(FONA_KEY, HIGH); //go back to non-trigger
digitalWrite(FONA_KEY, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's on
digitalWrite(FONA_KEY, HIGH); //go back to non-trigger
}
else {
// error! was already on for some reason -- do nothing
}
}
int initialize_fona() {
int fonaStatus;
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
// things are bad -- couldn't find FONA
fonaStatus=0;
}
else {
//things are good -- FONA OK
fonaStatus=1;
}
return fonaStatus;
}
int fona_find_network() {
uint8_t n = fona.getNetworkStatus();
int avail = fona.available();
int registerCountDelaySeconds=10;
int registerWaitTotalSeconds=0;
int failCount=0;
while ((n!=1)&&(n!=5)&&(failCount<failCountMax)) {
n = fona.getNetworkStatus();
//avail = fona.available();
registerWaitTotalSeconds+=registerCountDelaySeconds;
delay(registerCountDelaySeconds*1000);
failCount=failCount+1;
}
return n;
}
void power_down_fona() {
int fonaPower=digitalRead(FONA_POWER_STATUS);
if (fonaPower) { // on, so power down
digitalWrite(FONA_KEY, HIGH); //go back to non-trigger
digitalWrite(FONA_KEY, LOW); //turn on the SMS subcircuit
delay(2000); //so now it's on
digitalWrite(FONA_KEY, HIGH); //go back to non-trigger
}
else {
// error! was already off for some reason -- do nothing
}
}
void go_to_sleep_minutes(int minutes) {
int LOG_INTERVAL_BASE = 60000; // 1 sec
for (int k=0;k<minutes;k++) {
Sleepy::loseSomeTime(LOG_INTERVAL_BASE); //-- will interfere with serial, so don't use when debugging
}
}
void onPulse()
{
pulseCount++;
//Serial.print("pulsecount=");
//Serial.println(pulseCount);
lastTime = pulseTime;
pulseTime = micros();
duration=pulseTime-lastTime;
totalDuration+=duration;
//Serial.println(totalDuration);
}
void flushSerial() {
while (Serial.available())
Serial.read();
}
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
uint16_t buffidx = 0;
boolean timeoutvalid = true;
if (timeout == 0) timeoutvalid = false;
while (true) {
if (buffidx > maxbuff) {
//Serial.println(F("SPACE"));
break;
}
while (Serial.available()) {
char c = Serial.read();
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
if (c == '\r') continue;
if (c == 0xA) {
if (buffidx == 0) // the first 0x0A is ignored
continue;
timeout = 0; // the second 0x0A is the end of the line
timeoutvalid = true;
break;
}
buff[buffidx] = c;
buffidx++;
}
if (timeoutvalid && timeout == 0) {
//Serial.println(F("TIMEOUT"));
break;
}
delay(1);
}
buff[buffidx] = 0; // null term
return buffidx;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment