Skip to content

Commit

Permalink
0.2.6
Browse files Browse the repository at this point in the history
+ Remove String from bayeos
+ simplify SPIFlash code
+ cleanup SIM800 code
+ some bug fixes
  • Loading branch information
holzheu committed Oct 19, 2023
1 parent 0c3be2f commit b1e50a9
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 135 deletions.
78 changes: 29 additions & 49 deletions libraries/BayEOS/BayEOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ void BayEOS::startDataFrame(uint8_t subtype, uint8_t checksum) {
addToPayload(subtype);
}

void BayEOS::startOriginFrame(const String &o, uint8_t routed) {
void BayEOS::startOriginFrame(const char* o, uint8_t routed) {
if(routed) startFrame((uint8_t) BayEOS_RoutedOriginFrame);
else startFrame((uint8_t) BayEOS_OriginFrame);
addToPayload((uint8_t) o.length());
addToPayload((uint8_t) strlen(o));
addToPayload(o);
}

void BayEOS::startDataFrameWithOrigin(uint8_t subtype, const String &o,
void BayEOS::startDataFrameWithOrigin(uint8_t subtype, const char* o,
uint8_t checksum, uint8_t routed) {
_next = 0;
if (checksum)
addToPayload((uint8_t) BayEOS_ChecksumFrame);
if(routed) addToPayload((uint8_t) BayEOS_RoutedOriginFrame);
else addToPayload((uint8_t) BayEOS_OriginFrame);
addToPayload((uint8_t) o.length());
addToPayload((uint8_t) strlen(o));
addToPayload(o);
addToPayload((uint8_t) BayEOS_DataFrame);
addToPayload(subtype);
Expand Down Expand Up @@ -258,9 +258,10 @@ uint8_t BayEOS::addToPayload(const uint8_t* c) {
}
return _success;
}
uint8_t BayEOS::addToPayload(const String &s) {
for (uint8_t i = 0; i < s.length(); i++) {
_success = addToPayload((uint8_t) s[i]);
uint8_t BayEOS::addToPayload(const char* s) {
while(*s){
_success = addToPayload((uint8_t) *s);
s++;
}
return _success;
}
Expand All @@ -285,19 +286,19 @@ uint8_t BayEOS::addToPayload(long l) {
return addToPayload(&l, 4);
}

uint8_t BayEOS::sendError(const String &s) {
uint8_t BayEOS::sendError(const char* s) {
startFrame((uint8_t) BayEOS_ErrorMessage);
addToPayload(s);
return sendPayload();
}

uint8_t BayEOS::sendMessage(const String &s) {
uint8_t BayEOS::sendMessage(const char* s) {
startFrame((uint8_t) BayEOS_Message);
addToPayload(s);
return sendPayload();
}

uint8_t BayEOS::createMessage(const String &s,uint8_t checksum, uint8_t frametype) {
uint8_t BayEOS::createMessage(const char* s,uint8_t checksum, uint8_t frametype) {
_next = 0;
uint8_t ret;
if (checksum)
Expand Down Expand Up @@ -342,48 +343,27 @@ uint8_t BayEOS::readBinaryFromBuffer(unsigned long pos, unsigned long stop,
}

uint8_t BayEOS::readFromBuffer(void) {
while (_buffer->available()) {
_buffer->initNextPacket();
if(_buffer->packetLength()>_buffer->available()){
//invalid packet!!
_buffer->skip();
return 0;
}
if (!_buffer->packetLength() || _buffer->packetLength()>BayEOS_MAX_PAYLOAD) {
_buffer->next(); //skip empty or too long packet
continue;
}
if (_buffer->rtc()) {
switch(_buffer->_timeType){
case RTC_RELATIVE_MILLIS:
startDelayedFrame(
(_buffer->getTime() - _buffer->packetMillis()) * 1000);
break;
case RTC_RELATIVE_SECONDS:
startDelayedSecondFrame((_buffer->getTime() - _buffer->packetMillis()));
break;
case RTC_ABSOLUTE_SECONDS:
startTimestampFrame(_buffer->packetMillis());
break;
if(! _buffer->available()) return 0;
_buffer->initNextPacket();
if (_buffer->rtc()) {
switch(_buffer->_timeType){
case RTC_RELATIVE_MILLIS:
startDelayedFrame((_buffer->getTime() - _buffer->packetMillis()) * 1000);
break;
case RTC_RELATIVE_SECONDS:
startDelayedSecondFrame((_buffer->getTime() - _buffer->packetMillis()));
break;
case RTC_ABSOLUTE_SECONDS:
startTimestampFrame(_buffer->packetMillis());
break;

}
}

} else
startDelayedFrame(millis() - _buffer->packetMillis());
} else startDelayedFrame(millis() - _buffer->packetMillis());

if (getPayloadBytesLeft() < _buffer->packetLength()) {
_buffer->next(); //skip packet - now way to send this...
continue;
}
_buffer->readPacket(&_payload[_next]);
if(_payload[_next]==0){
_buffer->next(); //skip invalid frame
continue;
}
_next += _buffer->packetLength();
return _buffer->packetLength();
}
return 0;
_buffer->readPacket(&_payload[_next]);
_next += _buffer->packetLength();
return _buffer->packetLength();
}

uint8_t BayEOS::sendOrBuffer(void) {
Expand Down
12 changes: 6 additions & 6 deletions libraries/BayEOS/BayEOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ class BayEOS {
/**
* Send Error Message
*/
uint8_t sendError(const String &s);
uint8_t sendError(const char* s);

/**
* Send Message
*/
uint8_t sendMessage(const String &s);
uint8_t sendMessage(const char* s);

/**
* Create a Message in payload
*/
uint8_t createMessage(const String &s, uint8_t checksum=0, uint8_t frametype=BayEOS_Message);
uint8_t createMessage(const char* s, uint8_t checksum=0, uint8_t frametype=BayEOS_Message);

/**
* Create a Action respons in payload
Expand All @@ -239,7 +239,7 @@ class BayEOS {
/**
* Set the start of the payload buffer to be a valid origin or routedOrigin frame
*/
void startOriginFrame(const String &o, uint8_t routed=0);
void startOriginFrame(const char* o, uint8_t routed=0);

/**
* Set first two bytes of payload buffer and set _next to 2
Expand All @@ -251,7 +251,7 @@ class BayEOS {
*
* just call addChannelValue() to add values
*/
void startDataFrameWithOrigin(uint8_t subtype,const String &o,uint8_t checksum=0,uint8_t routed=0);
void startDataFrameWithOrigin(uint8_t subtype,const char* o,uint8_t checksum=0,uint8_t routed=0);

/**
* Adds a channel value to the payload
Expand Down Expand Up @@ -355,7 +355,7 @@ class BayEOS {
/**
* Add String to payload buffer
*/
uint8_t addToPayload(const String &s);
uint8_t addToPayload(const char* s);

/**
* Add float (4 byte) to payload buffer
Expand Down
2 changes: 2 additions & 0 deletions libraries/BayEOS/BayEOSCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
// Returns [0x3][0x31][command][uint8_t]
#define ROUTER_SET_NAME 0x4
// Returns [0x3][0x31][command][uint8_t]
#define ROUTER_SET_CONFIG 0x5
// Returns [0x3][0x31][command][uint8_t]



Expand Down
13 changes: 9 additions & 4 deletions libraries/BayEOSBoards/LowCurrentBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ ISR(TIMER2_OVF_vect) {
current_micros=micros();
adjust_osccal_flag++;
if(adjust_osccal_flag>2){
if((current_micros-last_micros)>(1005000L/TICKS_PER_SECOND)){
if((current_micros-last_micros)>(1006000L/TICKS_PER_SECOND)){
if(OSCCAL) OSCCAL--;
else adjust_osccal_flag=0; //reached limit!
}
else if((current_micros-last_micros)<(995000L/TICKS_PER_SECOND)){
if(OSCCAL<255) OSCCAL++;
else if((current_micros-last_micros)<(994000L/TICKS_PER_SECOND)){
if(OSCCAL<252) OSCCAL++;
else adjust_osccal_flag=0; //reached limit!
}
else {
Expand Down Expand Up @@ -145,9 +145,14 @@ ISR(TIMER2_OVF_vect) {

else
action0_pending_count=0;
if(action0_pending_count>RESET_COUNT)
if(action0_pending_count>RESET_COUNT){
#ifdef RESET_FUN
RESET_FUN
#endif
asm volatile (" jmp 0"); //restart programm

}

}
#endif
action|=(1<<tick_mod);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#define SAMPLING_INT 32
#define BOARD_NAME "HX711ESP_Single"
#define SEND_COUNT 60 /*collect 60 measurements before send... */
#define MIN_VOLTAGE 3.8

#include <HX711Array.h>
#include <NTC.h>

uint8_t dout[] = {6};
uint8_t sck = 3;

HX711Array scale;
Scale4PointCal cal;
NTC_HX711 ntc(scale, 2*470000, 3.0); //adjust resistor values

#include <BayEOSBufferSPIFlash.h>

SPIFlash flash(8);
BayEOSBufferSPIFlash myBuffer;

unsigned long last_sent;
#include <BaySerial.h>
BaySerialESP client(Serial, 7);


float temp;

#include <LowCurrentBoard.h>


uint16_t measurements = SEND_COUNT;

void setup()
{

myBuffer.init(flash); //This will restore old pointers
//myBuffer.reset(); //This will set all pointers to zero
myBuffer.skip(); //This will move read pointer to write pointer
myBuffer.setRTC(myRTC, RTC_RELATIVE_SECONDS); //Nutze RTC relativ!
client.setBuffer(myBuffer, 20); // use skip!
initLCB(); //init time2
scale.begin(dout, 1, sck); //start HX711Array with 1 ADCs
scale.set_gain(128);
scale.power_down();
cal.readConf();

startLCB();
client.begin(38400);
client.powerUp();
while (client.isReady()) {
blinkLED(2);
delay(2000);
}
uint8_t res;
while (res = client.setName(BOARD_NAME)) {
if (res == 10 + strlen(BOARD_NAME)) break;
blinkLED(res);
delay(res * 500 + 2000);
}

blinkLED(client.sendMessage("Board started") + 1);
client.powerDown();
delay(2000);
}


void loop() {
if (ISSET_ACTION(0)) {
UNSET_ACTION(0);
//eg measurement
ntc.readResistance();
temp = ntc.getTemp(0);

scale.power_up();
scale.read_average(1);
long adc = scale.read_average();
pinMode(7, OUTPUT);
digitalWrite(POWER_PIN, HIGH);
delay(2);
analogReference(DEFAULT);
float bat_voltage = 3.3 * 200 / 100 / 1023 * analogRead(A7);

client.startDataFrame();
client.addChannelValue(millis());
client.addChannelValue(bat_voltage);
client.addChannelValue(temp);
client.addChannelValue(adc);
client.addChannelValue(cal.getWeight(adc, temp));

client.writeToBuffer();
measurements++;
if (measurements >= SEND_COUNT & bat_voltage > MIN_VOLTAGE) {
client.powerUp();
uint8_t tx_res = client.sendMultiFromBuffer(1000);
blinkLED(tx_res + 1);
while (! tx_res && myBuffer.available() && ! ISSET_ACTION(0)) {
tx_res = client.sendMultiFromBuffer(1000);
blinkLED(tx_res + 1);
}
if (! myBuffer.available()) measurements = 0;
}
client.powerDown();
digitalWrite(POWER_PIN, LOW);

}

sleepLCB();

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ void loop() {

}

if (ISSET_ACTION(7)) {
UNSET_ACTION(7);
client.sendFromBuffer();
}
sleepLCB();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define MCPPOWER_PIN 6
char channel_map[] = "time;bat;T1;T2;T3;T4;T5;T6;T7;T8;T9;T10;T11;T12;T13,T14;T15;T16";
char unit_map[] = "ms;V;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C;C";
#define BAT_DIVIDER 3.3 * (100+100) / 100
#define BAT_REFERENCE DEFAULT

#include <BayEOSBufferSPIFlash.h>
#include <BaySerial.h>
Expand Down Expand Up @@ -76,9 +78,9 @@ void measure() {


digitalWrite(POWER_PIN, HIGH);
analogReference(INTERNAL);
analogReference(BAT_REFERENCE);
if (digitalRead(CONNECTED_PIN))
myLogger._bat = (1.1 * 320 / 100 / 1023 * analogRead(A0)) * 1000;
myLogger._bat = (BAT_DIVIDER * analogRead(A7) / 1023 ) * 1000;
values[0] += ((float)myLogger._bat) / 1000;
digitalWrite(POWER_PIN, LOW);
digitalWrite(MCPPOWER_PIN, HIGH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const uint8_t channel = 0x70;
const uint8_t adr[] = {0x12, 0xae, 0x31, 0xc4, 0x45};

#define BAT_WARNING 3800

#define BAT_DIVIDER 3.3 * (100+100) / 100
#define BAT_REFERENCE DEFAULT

//channel map and unit map must not exceed 98 characters!
char channel_map[] = "time;bat;T1;T2;T3;T4;T5;T6;T7;T8;T9;T10;T11;T12;T13,T14;T15;T16";
Expand Down Expand Up @@ -87,8 +88,8 @@ void measure() {


digitalWrite(POWER_PIN, HIGH);
analogReference(INTERNAL);
myLogger._bat = (1.1 * 200 / 100 / 1023 * analogRead(A0)) * 1000;
analogReference(BAT_REFERENCE);
myLogger._bat = (BAT_DIVIDER * analogRead(A7) / 1023 ) * 1000;
values[0] += ((float)myLogger._bat) / 1000;
digitalWrite(POWER_PIN, LOW);
digitalWrite(MCPPOWER_PIN, HIGH);
Expand All @@ -101,6 +102,7 @@ void measure() {
delayLogger(10);
mcp342x.runADC(0);
delayLogger(mcp342x.getADCTime());
span = mcp342x.getData();
strom = span / PRE_RESISTOR; //current in A

mcp342x.runADC(1);
Expand Down
Loading

0 comments on commit b1e50a9

Please sign in to comment.