Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RN4020 +atmega328 driver , STTS751 sensor
/*
SoftwareSerial, RN4020 BLE (Low Power version)
with Central= raspBerryPi , pubAddr version
stts751/STMicro sensor
v 0.9.03
*/
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <SoftwareSerial.h>
#include<Wire.h>
SoftwareSerial mySerial(5, 6); /* RX:D5, TX:D6 */
String mBuff="";
const int mVoutPin = 0;
//const int mA1_Pin =1;
const int mPinBLE =9;
const int mOK_CODE=1;
const int mNG_CODE=0;
uint32_t mTimer_runMax= 0;
//const int mNextSec = 600; //Sec
const int mNextSec = 1800; //Sec
const int mMax_runSec= 10; //Sec
const int mMode_RUN = 1;
const int mMode_WAIT = 2;
int mMode =0;
const int mMaxGap=25;
// STTS751 I2C address is 0x39(57)
#define Addr 0x39
// LOW power
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
volatile int wdt_cycle;
//
//
void init_stts(){
Wire.begin();
// Select configuration register
Wire.write(0x03);
// 12-bit Resolution, Continuous conversion mode
Wire.write(0x8C);
// Stop I2C Transmission
Wire.endTransmission();
// Select rate conversion register
Wire.write(0x04);
// 1 conversion / second
Wire.write(0x04);
// Stop I2C Transmission
Wire.endTransmission();
delay(300);
}
//
int get_sttsValue(){
int ret=0;
unsigned data[2];
// Starts I2C communication
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x00);
// Stop I2C transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
// Read 1 byte of data
// temp MSB
if(Wire.available() == 1)
{
data[0] = Wire.read();
}
// Starts I2C communication
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x02);
// Stop I2C transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
// Read 1 byte of data
// temp LSB
if(Wire.available() == 1)
{
data[1] = Wire.read();
}
// Convert the data to 12-bits
int temp = ((data[0] * 256) + (data[1] & 0xFC)) / 16;
if(temp > 2047)
{
temp -= 4096;
}
float cTemp = temp * 0.0625;
ret = (int)cTemp; ;
float fTemp = cTemp * 1.8 + 32;
delay(10 );
return ret;
}
//
String convert_hex(String src){
String sRet="";
// int iLen=src.length();
// int iLen=mMaxGap *2;
char sRead[mMaxGap+1 ];
sprintf(sRead, "%s", src.c_str() );
for(int i=0; i< mMaxGap ; i++){
char cBuff[2+1];
sprintf( cBuff, "%02x", sRead[i] );
String sBuff=String(cBuff );
// sRet += sBuff;
sRet.concat(sBuff );
// Serial.println(cBuff );
}
// Serial.println ( "sRet="+ sRet );
return sRet;
}
//
int Is_resWait(String value, uint32_t maxMsec ){
int ret= mNG_CODE;
uint32_t tm_maxWait=millis();
int iLen= value.length();
String sBuff="";
int iBool=1;
while(iBool ){
while( mySerial.available() ){
char c= mySerial.read();
Serial.print( c );
if( (c != 0x0a ) && (c != 0x0d ) ){
sBuff.concat(c );
}
} //end_while
if( (int )sBuff.length() >= iLen ){ iBool=0; }
delay(100);
uint32_t tmMax =millis() -tm_maxWait;
if(tmMax > (uint32_t)maxMsec ){
Serial.println("#Error-Is_resWait:maxSec!");
iBool=0;
}
}//end_while_1
Serial.println("");
if(sBuff.length() < 1){ return ret; }
int iLenBuff= sBuff.length();
int iSt=iLenBuff -(iLen);
Serial.println("iLenBuff="+ String(iLenBuff)+",iSt="+ String(iSt) ) ;
String sRecv = sBuff.substring(iSt , iLen );
Serial.println( "sBuff="+ sBuff+ " ,sRecv="+ sRecv );
if(sRecv == value ){
ret= mOK_CODE;
}
return ret;
}
//
void proc_getSSerial(){
while( mySerial.available() ){
char c= mySerial.read();
Serial.print( c );
} //end_while
}
//
void proc_sendCmd(){
int iWaitTm= 5000;
//int iLenBuff=(mMaxGap * 2) + 2+1;
int iLenBuff=(mMaxGap * 2) + 3 +1;
char cRead[25 +1];
char cBuff[iLenBuff ];
int iSenNum= 0;
for(int i=0; i<10 ; i++){
iSenNum= get_sttsValue();
}
//Serial.println("iSenNum=" + String(iSenNum) );
sprintf(cRead , "%05d%05d%05d%05d%05d", iSenNum ,0 ,0 , 0, 0 );
Serial.println("cRead="+ String(cRead ));
String sRead= convert_hex( String(cRead) );
Serial.println("Hex=" +sRead);
sprintf(cBuff , "N,%s\r", sRead.c_str() );
//N
Serial.println( "#Start N, cBuff="+ String(cBuff) );
mySerial.print(String(cBuff) );
if( Is_resWait("AOK", iWaitTm ) == mNG_CODE ){ return; }
else{Serial.println("#OK SN" ); };
proc_getSSerial();
//A
Serial.println( "#Start A" );
// mySerial.print("A\r");
mySerial.print("A,0064,07D0\r"); //100msec, total= 2000mSec
if( Is_resWait("AOK", iWaitTm ) == mNG_CODE ){ return; }
else{Serial.println("#OK A" ); };
proc_getSSerial();
wait_forSec(3 );
}
//
void wait_forSec(int wait){
for(int i=0; i<wait; i++){
delay(1000);
Serial.println("#wait_forMsec: "+ String(i) );
}
}
//
void setup() {
mMode = mMode_RUN;
pinMode(mPinBLE ,OUTPUT);
Serial.begin( 9600 );
mySerial.begin( 9600 );
Serial.println("#Start-setup-SS");
//stts
init_stts();
setup_watchdog(6 ); // WDT setting
//wait
delay(1000);
//wait_forSec(3);
proc_getSSerial(); //clear-buff
}
//
void loop() {
//const int iWait =2000;
// Serial.println( "mTimer="+ String(mTimer) + ",millis=" + String(millis()) );
if(mMode ==mMode_RUN){
if(mTimer_runMax <= 0 ){
mTimer_runMax = ((uint32_t)mMax_runSec * 1000) + millis();
digitalWrite(mPinBLE, HIGH);
delay(1000);
}
if(millis() < mTimer_runMax){
digitalWrite(mPinBLE, HIGH);
proc_sendCmd();
}else{
mTimer_runMax=0;
digitalWrite(mPinBLE,LOW );
mMode = mMode_WAIT;
}
}else{
mMode = mMode_RUN;
for(int i=0; i< mNextSec ; i++){
system_sleep(); // power down ,wake up WDT
//Serial.print("i=");
//delay(15);
//Serial.println(i);
//delay(15);
}
}
}
//
void system_sleep() {
cbi(ADCSRA,ADEN); // ADC power off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // power down mode
sleep_enable();
sleep_mode(); // sleep
sleep_disable(); // WDT time up
sbi(ADCSRA,ADEN); // ADC ON
}
// WDT setting, param : time
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) { //
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCSR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCSR = bb;
WDTCSR |= _BV(WDIE);
} //setup_watchdog
ISR(WDT_vect) { // WDT, time Up process
wdt_cycle++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.