Commit 01cefbee authored by Patrick's avatar Patrick

EOD: cleaning reading/sending process

parent 50b39826
......@@ -223,11 +223,13 @@ long red_peg::level(t_SensorData data_record, long max_level)
}
}
long red_peg::distance(t_SensorData data_record)
long red_peg::distance(t_SensorData data_record, usound_e sensor_type)
{
long max_distance = 765L; // default MX7060 is 0-765cm
if (sensor_type == MB7366) { max_distance = 10240L; } // MB7366 is 0–10,240mm
if (data_record.the_command == ANA) {
long distance = map(data_record.the_reading, 0, MAX_20MA, 0, 765L);
return distance; // in cm
long distance = map(data_record.the_reading, 0, MAX_20MA, 0, max_distance);
return distance; // in mm or cm
} else {
return -1L;
}
......
......@@ -36,6 +36,9 @@ enum sensor_e {EMPTY, AVAILABLE, OK, RTC, ADC1, ADC2, ADC3, SDI_12, SET_RTC, SDI
#define ANA ADC2
#define TMP ADC3
// possible ultrasound sensors
enum usound_e {MB7060, MB7366};
// min and max values for 4-20mA readings
// ADC max is 2.048V
#ifndef MIN_4MA
......@@ -116,7 +119,7 @@ public:
float level(t_SensorData data_record, double max_level);
float level(t_SensorData data_record, float max_level);
// TODO:
long distance(t_SensorData data_record); // Ultrasonic distance reading
long distance(t_SensorData data_record, usound_e sensor_type = MB7060); // Ultrasonic distance reading (and sensor type)
};
#endif // red_peg_h
......@@ -2,7 +2,7 @@
#include "Arduino.h"
#include "teal_mallet_gsm.h"
CountingStream countIt;
JSONprint json;
GPRS Gprs;
GSM GsmAccess;
GSMClient Client;
......@@ -39,6 +39,7 @@ uint8_t teal_mallet_gsm::connectGPRS(char* _apn, char* _login, char* _passwd, ch
delay(1000);
}
}
return 0;
}
void teal_mallet_gsm::disconnectGPRS()
......
......@@ -5,24 +5,24 @@
#define AMON
#include "utility/GSM4.h"
#include "utility/CountingStream.h"
#include "utility/JSONprint.h"
#define BAUD 115200
// set some default sim values
// we use O2 data sims in the adv-micros course
// PIN Number
#define PIN_NUMBER "" // with no sim card pin
#define PIN_NUMBER ((char*)"") // with no sim card pin
// APN data
#define GPRS_APN "mobile.o2.co.uk" // for O2
#define GPRS_LOGIN "o2web" // for O2
#define GPRS_PASSWORD "password" // for O2
#define GPRS_APN ((char*)"mobile.o2.co.uk") // for O2
#define GPRS_LOGIN ((char*)"o2web") // for O2
#define GPRS_PASSWORD ((char*)"password") // for O2
// use the AMON JSON format for sending
#define AMON
#define DEFAULT_PORT 80
#define DEFAULT_ENTITY_ID "a3b40af1-16b5-4727-bcdc-60b0816a1e7b"
#define DEFAULT_ENTITY_ID ((char*)"a3b40af1-16b5-4727-bcdc-60b0816a1e7b")
#define REPORTING_PERIOD 1000UL*60UL // one minute
#define RECORDING_PERIOD 1000UL*10UL // 10 seconds
......@@ -33,6 +33,14 @@ typedef struct {
char period[8]; // reading:period
} t_SensorType;
// create a struct to hold each reading
typedef struct{
uint8_t reading; // reference which reading this if for (which index in the progmem arrays above)
uint16_t y;
uint8_t m, d, hh, mm, ss; //the timestamp in DateTime-ready format
float level; // then the actual reading as a float, ready to send to teal-mallet
} t_measurement;
class teal_mallet_gsm
{
private:
......
#include <Print.h>
class CountingStream : public Print
class DevNull : public Print
{
virtual size_t write(uint8_t) { return 1; };
virtual size_t write(const uint8_t *buffer, size_t size) { return size; };
......
/*
This file is part of the GSM4 communications library for Arduino
-- Multi-transport communications platform
-- Fully asynchronous
-- Includes code for the Arduino-Telefonica GSM/GPRS Shield V1
-- Voice calls
-- SMS
-- TCP/IP connections
-- HTTP basic clients
This library has been developed by Telefnica Digital - PDI -
- Physical Internet Lab, as part as its collaboration with
Arduino and the Open Hardware Community.
September-December 2012
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The latest version of this library can always be found at
https://github.com/BlueVia/Official-Arduino
*/
#ifndef __GSM4_SHIELDV1__
#define __GSM4_SHIELDV1__
#include <GSM4MobileNetworkProvider.h>
#include <GSM4ShieldV1ModemCore.h>
#include <GSM4ShieldV1BaseProvider.h>
#include <Arduino.h>
class GSM4ShieldV1 : public GSM4MobileNetworkProvider, public GSM4ShieldV1BaseProvider
{
// General code, for modem management
private:
/** Delay inside an interrupt (2 seconds)
*/
void delayInsideInterrupt2seg();
// Code for SMS Service
private:
long commandMillis;
bool commandSent;
const char* pinConfig; //PIN.
char* accessPoint; //APN.
char* userName; //User.
char* passw; //Password.
const char* remoteID; //Server.
char* dataSocket; //Data socket.
int local_Port; //Local Port.
char* local_IP; //Local IP.
int local_IP_Length; //Local IP length.
int socketDataSize; //Size of socket data to be read.
int socketDataSizeWritten; //Number of socket data written in buffer not to overflow the buffer
int socketsAccepted; //Status for remote clients accepted of closed.
public:
/** Constructor **/
GSM4ShieldV1(bool debug=false);
/** Manages modem response
@param from Initial byte of buffer
@param to Final byte of buffer
*/
void manageResponse(byte from, byte to);
/** Get last command status
@return returns 0 if last command is still executing, 1 success, >1 error
*/
int ready(){return GSM4ShieldV1BaseProvider::ready();};
/** Parse modem response
@param rsp Returns true if expected response exists
@param string1 Substring expected in response
@param string2 Second substring expected in response
@return true if parsed successful
*/
bool genericParse_rsp2(bool& rsp, char* string1, char* string2);
/** Recognize URC
@param oldTail
@return true if successful
*/
bool recognizeUnsolicitedEvent(byte oldTail);
/** Receive answer
@return true if successful
*/
bool answerReceived();
/** Receive socket
@param id_socket Socket ID
@return true if successful
*/
bool socketReceived(int id_socket);
/** Update active ID sockets
@param active Active sockets
@param ID Id for update
*/
void update_activeIDsockets (bool active, int ID);
/** Assign ID to socket
@param ID Id to assign to socket
@return true if successful
*/
bool assignIDsocket (int& ID);
/** Close data socket
@return true if successful
*/
bool closedDataSocket(); //Flag closed current data socket.
//bool writeIncomingCalls(char* bufferForCallerId) If isn't zero, doesn't wait calls
};
#endif
\ No newline at end of file
......@@ -8,9 +8,9 @@ This file is part of the GSM4 communications library for Arduino
-- TCP/IP connections
-- HTTP basic clients
This library has been developed by Telefnica Digital - PDI -
This library has been developed by Telefnica Digital - PDI -
- Physical Internet Lab, as part as its collaboration with
Arduino and the Open Hardware Community.
Arduino and the Open Hardware Community.
September-December 2012
......@@ -48,7 +48,7 @@ https://github.com/BlueVia/Official-Arduino
typedef enum GSM4GSMBand {UNDEFINED, EGSM_MODE, DCS_MODE, PCS_MODE, EGSM_DCS_MODE, GSM850_PCS_MODE, GSM850_EGSM_DCS_PCS_MODE};
//
//
// These are the bands and scopes:
//
// E-GSM(900)
......@@ -61,36 +61,36 @@ typedef enum GSM4GSMBand {UNDEFINED, EGSM_MODE, DCS_MODE, PCS_MODE, EGSM_DCS_MOD
class GSM4ShieldV1BandManagement
{
private:
GSM4ShieldV1DirectModemProvider modem; // Direct access to modem
char* quectelStrings[NUMBEROFBANDS];// = {"\"EGSM_MODE\"", "\"DCS_MODE\"", "\"PCS_MODE\"",
//"\"EGSM_DCS_MODE\"", "\"GSM850_PCS_MODE\"",
//"\"EGSM_DCS_MODE\"", "\"GSM850_PCS_MODE\"",
//"\"GSM850_EGSM_DCS_PCS_MODE\""};
public:
/** Constructor
@param trace If true, dumps all AT dialogue to Serial
*/
GSM4ShieldV1BandManagement(bool trace=false);
/** Forces modem hardware restart, so we begin from scratch
@return always returns IDLE status
*/
GSM4_NetworkStatus_t begin();
/** Get current modem work band
/** Get current modem work band
@return current modem work band
*/
*/
String getBand();
/** Changes the modem operating band
/** Changes the modem operating band
@param band Desired new band
@return true if success, false otherwise
*/
*/
bool setBand(String band);
};
#endif
/*
JSONprint.h — A library for streaming out JSON
By building dynamically while sending, the RAM usage is
minimised, at the expense of additional processing time
if the JSON data is used repeatedly
Created by Def-Proc Engineering 2017
MIT licensed
*/
#include <Arduino.h>
#ifndef JSONprint_h
#define JSONprint_h
#define MAX_JSON_DEPTH 8
// define the bitwise flags for keeping track of progress
#define OBJECT_OPEN B00000001
#define NOT_FIRST B00000010
#define KEY_SET B00000100
#define VALUE_OPEN B00001000
#define VALUE_CLOSED B00010000
#define ARRAY_OPEN B00100000
#define ARRAY_CLOSED B01000000
#define OBJECT_CLOSED B10000000
class JSONprint
{
private:
uint8_t _state[MAX_JSON_DEPTH];
uint8_t _depth = 0;
// TODO: set pretty mode to add linebreaks
uint8_t _pretty_mode = false;
uint16_t _char_count = 0;
// set a default print stream
Print* _thePrint;
uint8_t isObjectOpen() { return (_state[_depth] & OBJECT_OPEN); };
uint8_t isNotFirst() { return (_state[_depth] & NOT_FIRST); };
uint8_t isKeySet() { return (_state[_depth] & KEY_SET); };
uint8_t isValueOpen() { return (_state[_depth] & VALUE_OPEN); };
uint8_t isValueClosed() { return (_state[_depth] & VALUE_CLOSED); };
uint8_t isArrayOpen() { return (_state[_depth] & ARRAY_OPEN); };
uint8_t isArrayClosed() { return (_state[_depth] & ARRAY_CLOSED); };
uint8_t isObjectClosed() { return (_state[_depth] & OBJECT_CLOSED); };
// return err val for position set
uint8_t canSetKey();
uint8_t canSetValue();
public:
JSONprint();
// set a new stream output
void begin(Print &aPrint = Serial);
// begin with no output (counting only)
void beginNull();
// set pretty mode
void prettyMode(uint8_t pretty_mode = true);
void prettyPrint(uint8_t pretty_mode = true) { return prettyMode(pretty_mode); }
// how many characters in the json string
size_t length();
// open a new object
uint8_t openObject();
// write a double quoted string key with trailing colon
uint8_t writeKey(const __FlashStringHelper *);
uint8_t writeKey(char key[]);
uint8_t writeKey(char* key, size_t len);
uint8_t writeKey(char key);
// write a complete value.
uint8_t writeValue(const __FlashStringHelper *);
uint8_t writeValue(char value[]);
uint8_t writeValue(char* value, size_t len);
uint8_t writeValue(char value);
uint8_t writeValue(int value);
uint8_t writeValue(unsigned int value);
uint8_t writeValue(long value);
uint8_t writeValue(unsigned long value);
uint8_t writeValue(double value, int digits = 2);
// open a Value for a more multipart write
uint8_t openValue(uint8_t isString = true);
// append new values to an open value
uint8_t appendValue(const __FlashStringHelper *);
uint8_t appendValue(char value[]);
uint8_t appendValue(char* value, size_t len);
uint8_t appendValue(char value);
uint8_t appendValue(int value);
uint8_t appendValue(unsigned int value);
uint8_t appendValue(long value);
uint8_t appendValue(unsigned long value);
uint8_t appendValue(double value, int digits = 2);
// close a written value
uint8_t closeValue(uint8_t isString = true);
// open and close an array
uint8_t openArray();
uint8_t closeArray();
// close an object
uint8_t closeObject(uint8_t moveUp = false);
// helper function to just close everything
// will try and close value, array and objects in order
uint8_t closeAll();
// reset the JSON object
void reset();
};
#endif
......@@ -12,3 +12,4 @@
platform = atmelavr
board = uno
framework = arduino
upload-port = /dev/ttyACM1
......@@ -9,14 +9,38 @@ teal_mallet_gsm TM;
// set > 0 to get debug serial output
#define DEBUG_MODE 1
/*
#define PIN ""
// APN data (EE sim)
#define GPRS_APN "everywhere" // replace your GPRS APN
#define GPRS_LOGIN "eesecure" // replace with your GPRS login
#define GPRS_PASSWORD "secure" // replace with your GPRS password
// APN data (O2 sim)
#define GPRS_APN "mobile.o2.co.uk" // replace your GPRS APN
#define GPRS_LOGIN "o2web" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password
*/
const t_SensorType theSensor = {"Temp:3 min:TMP", "C", "INSTANT"};
/*const t_SensorType theSensor = {"Temp:3 min:TMP", "C", "INSTANT"};*/
// store each char array for datastream info as progmem strings
const char usnd_param[] PROGMEM = "Level";
const char usnd_quali[] PROGMEM = "2 min";
const char usnd_dtype[] PROGMEM = "STAGE";
const char usnd_units[] PROGMEM = "m";
const char usnd_perid[] PROGMEM = "INSTANT";
// store each char array for datastream info as progmem strings
const char temp_param[] PROGMEM = "Temperature";
const char temp_quali[] PROGMEM = "2 min";
const char temp_dtype[] PROGMEM = "AIR";
const char temp_units[] PROGMEM = "C";
const char temp_perid[] PROGMEM = "INSTANT";
// then add the strings to an array (for multiple datastreams), also in progmem
PGM_P const param[] PROGMEM = {usnd_param, temp_param};
PGM_P const quali[] PROGMEM = {usnd_quali, temp_quali};
PGM_P const dtype[] PROGMEM = {usnd_dtype, temp_dtype};
PGM_P const units[] PROGMEM = {usnd_units, temp_units};
PGM_P const perid[] PROGMEM = {usnd_perid, temp_perid};
// ultasound is element 0, temperature is element 1
// operating states
enum state_machine_e {
SLEEP_IDLE,
TAKE_READING,
......@@ -24,31 +48,48 @@ enum state_machine_e {
};
state_machine_e g_current_state = SLEEP_IDLE;
// readings are taken on ordinals (0/15/30/45)
t_SensorData last_reading_time;
#define ORDINAL_INTERVAL 3
// array to hold the variables
#define READINGS_ARRAY_LENGTH 50
/*// array to hold the variables
#define READINGS_ARRAY_LENGTH 5
t_SensorData readings[READINGS_ARRAY_LENGTH];
uint8_t readings_head = READINGS_ARRAY_LENGTH-1;
uint8_t readings_tail = READINGS_ARRAY_LENGTH-1;
*/
#define NUM_MMNT 10 // measurement buffer length
// then create a pre-sized array of these structs to hold a set of timestamped measurements
t_measurement measurements[NUM_MMNT] = {};
uint8_t readings_head = NUM_MMNT-1;
uint8_t readings_tail = NUM_MMNT-1;
// sends happen after a fixed number of ordinals
t_SensorData last_sending_time;
// how many readings should be collected for sending (<NUM_MMNT)
#define NUM_TO_SEND 5
#if NUM_TO_SEND >= NUM_MMNT
#error "Can't hold all the readings, reduce NUM_TO_SEND below NUM_MMNT"
#endif
void setup() {
RP.begin(true);
TM.begin(((char*)"training.teal-mallet.net"), ((char*)"a6c33855-49a0-5858-8ffe-3da7c2b0412d"));
delay(100);
TM.begin("training.teal-mallet.net", "355e6cff-165b-5355-80a7-38be25275cfd");
#if DEBUG_MODE > 0
Serial.begin(BAUD);
Serial.println(F("start EaMU purple-mallard"));
#endif
last_reading_time = get_reading(RTC);
get_reading(RTC);
last_sending_time = get_reading(RTC);
if (last_reading_time.y == 2165 or last_reading_time.y == 2000) {
// the RTC has not been set or is missing, abort now
Serial.println(F("RTC time is unset or missing"));
pinMode(LED_BUILTIN, OUTPUT);
while(true) {
// die
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
......@@ -96,14 +137,13 @@ void sleep_idle() {
delay(100);
#endif
if ((current_time.mm % ORDINAL_INTERVAL == 0)
&& (current_time.mm - last_reading_time.mm) % 60 > 1) {
&& (current_time.mm != last_reading_time.mm)) {
// if it's an ordinal time 0|15|30|45 min take a reading
g_current_state = TAKE_READING;
} else if (readings_head = READINGS_ARRAY_LENGTH-1
&& (current_time.hh - last_sending_time.hh % 24 > 1)) {
//} else if ((readings_head == NUM_MMNT-1)
//&& (current_time.hh - last_sending_time.hh % 24 > 1)) {
// the readings buffer is full, send them out
g_current_state = SEND_READING;
//g_current_state = SEND_READING;
}
}
......@@ -112,16 +152,16 @@ void take_reading() {
Serial.print(F("take_reading() at: "));
delay(100);
#endif
t_SensorData current_reading = get_reading(ANA);
t_SensorData current_reading = get_reading(TMP);
#if DEBUG_MODE > 0
RP.print_data(current_reading);
Serial.print(F("distance: "));
Serial.println(RP.distance(current_reading));
Serial.println(RP.distance(current_reading, MB7366));
delay(100);
#endif
// update the current time
if (current_reading.sensor == ANA) {
if (current_reading.sensor == TMP) {
last_reading_time.y = current_reading.y;
last_reading_time.m = current_reading.m;
last_reading_time.d = current_reading.d;
......@@ -149,7 +189,7 @@ void send_reading() {
Serial.print(F("send_reading() at: "));
delay(100);
#endif
t_SensorData send_time = get_reading(ANA);
t_SensorData send_time = get_reading(TMP);
#if DEBUG_MODE > 0
RP.print_data(send_time);
#endif
......@@ -161,6 +201,7 @@ ISR( WDT_vect ) {
/* --- Do Not Use --- */
/*
#define RECORDING_PERIOD 1000UL*60UL // 60 seconds
uint32_t last_record = -RECORDING_PERIOD; // send data immediately
......@@ -218,8 +259,9 @@ void tmp() {
WDTCSR |= (1 << WDIE); // enable interrupt mode
sleep_enable(); // enable the sleep mode ready for use
sleep_mode(); // trigger the sleep
/* ...time passes ... */
// ...time passes ...
sleep_disable(); // prevent further sleeps
Serial.println("awake!");
}
}
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment