Hi, you can do as follow with a timer. But you should notice:
- rui_device_sleep(1) will power off bg96 and make all sensors sleep for save power maximized. It should cooperate with rui_device_sleep(0). And it costs times at least 1min or more. In IOT situation, node will not wake up often and sleep most of time. So delay several seconds has no practical significance. It won’t save power better than keep work if sleep a little time.
- You’d better run your task by the timer. There is no OS now. So timer is the best way.
- I got the sensors data and all ok.
#include “rui.h”
#include “lis3dh.h”
#include “opt3001.h”
#include “shtc3.h”
#include “lps22hb.h”
#define APN “pwg”
#define PORT
#define IP
RUI_I2C_ST st = {0};
uint8_t send_data[256]={0};
uint8_t at_rsp[1536] = {0};
void sendOk();
void sensor_on(void)
{
st.PIN_SDA = 14;
st.PIN_SCL = 13;
st.FREQUENCY = RUI_I2C_FREQ_400K;
rui_i2c_init(&st);
//lis3dh init
lis3dh_init();
//opt3001 init
opt3001_init();
//shtc3 init
SHTC3_Wakeup();
//lps22hb init 1 wake up
lps22hb_mode(1);
}
void sensor_off(void)
{
lis3dh_sleep_init();
sensorOpt3001Enable(0);
SHTC3_Sleep();
lps22hb_mode(0);
}
RUI_TIMER_ST rui_timer;
uint8_t timer_flag=0;
void timer_callback(void)
{
timer_flag = 1;
}
void timer_init()
{
rui_timer.timer_mode = RUI_TIMER_MODE_REPEATED;
rui_timer_init(&rui_timer, timer_callback);
rui_timer_setvalue(&rui_timer, 120000);
rui_timer_start(&rui_timer);
}
void main(void)
{
//system init
rui_sensor_register_callback(sensor_on,sensor_off);
rui_init();
timer_init();
while(1)
{
if (timer_flag == 1)
{
sendOk();
timer_flag = 0;
}
rui_running();
}
}
void sendOk()
{
uint8_t gsm_cmd[100] = {0};
uint8_t gsm_rsp[256] = {0};
float temp = 0; //Variables which will store sensor data
float humidity = 0;
float pressure = 0;
int x = 0;
int y = 0;
int z = 0;
float magnetic_x = 0;
float magnetic_y = 0;
float magnetic_z = 0;
float light = 0;
float voltage = 0;
RUI_GPS_DATA g_gps_data = {0};
uint8_t lat_data[20] = {0};
uint8_t lon_data[20] = {0};
uint8_t lora_config_data[10] = {0};
float _x = 0;
float _y = 0;
float _z = 0;
get_lps22hb_data(&pressure);
get_lis3dh_data(&x,&y,&z);
_x =x * 4000/65536;
_y =y * 4000/65536;
_z =z * 4000/65536;
get_opt3001_data(&light);
SHTC3_GetTempAndHumi(&temp,&humidity);
memset(lat_data,0,20);
rui_gps_get(&g_gps_data);
sprintf(lat_data,"%lf",g_gps_data.Latitude);
memset(lon_data,0,20);
sprintf(lon_data,"%lf",g_gps_data.Longitude);
rui_device_get_battery_level(&voltage);
memset(gsm_cmd,0,100);
memset(gsm_rsp,0,256);
memset(send_data,0,256);
//sprintf(send_data,"Acc:%.2f,%.2f,%.2f; ",_x,_y,_z); //Not Adding acceleration to reduce the size of send_data variable as it is to be stored in flash because of which it has to be of size less than 128 bytes
sprintf(send_data+strlen(send_data),"Tem:%.2f;Hum:%.2f; ",temp,humidity);
sprintf(send_data+strlen(send_data),"Pre:%.2f; ",pressure);
sprintf(send_data+strlen(send_data),"Lig:%.2f; ",light);
sprintf(send_data+strlen(send_data),"Lat(0-N,1-S):%d,%s,Lon(0-E,1-W):%d,%s; ",g_gps_data.LatitudeNS,lat_data,g_gps_data.LongitudaEW,lon_data);
if(voltage>0)
{
sprintf(send_data+strlen(send_data),"Battery:%.2f; ",voltage);
}
//MAKE SURE THE FIRST PARAMETER IS ALWAYS RUI_FLASH_USER and MAKE SURE THE SIZE OF VARIABLE WHOSE VALUE NEEDS TO BE PRESERVED SHOULD BE LESS THAN 128 BYTES
//rui_flash_write(RUI_FLASH_USER, send_data, strlen(send_data)); //Writing the value of send_data in the flash memory
/*
if(power_flag == 0)
{
rui_device_sleep(1); //Sleep
power_flag =1;
}
*/
//rui_delay_ms(10); //Delay between sleep and wakeup
//memcpy(at_rsp+strlen(at_rsp),"WAKING UP AFTER DELAY",strlen("WAKING UP AFTER DELAY"));
//rui_at_response(true, at_rsp, RAK_OK);
/*
rui_device_sleep(0); //WakeUp
power_flag = 0;
*/
//rui_flash_read(RUI_FLASH_USER,send_data, strlen(send_data)); //Reading value of send_data from the flash
rui_cellular_send("AT+CSQ"); //Checking signal strength
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
memset(gsm_cmd,0,100);
sprintf(gsm_cmd, "AT+QICSGP=1,1,\"%s\"", APN);
rui_cellular_send(gsm_cmd); //Connecting to a cellular network
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
rui_cellular_send("AT+QIACT=1"); //Activating the cellular connection
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
rui_cellular_send("AT+QIACT?"); //Checking activation status
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
memset(gsm_cmd,0,100);
sprintf(gsm_cmd, "AT+QIOPEN=1,0,\"UDP\",\"%s\",%s,1,1", IP, PORT);
rui_cellular_send(gsm_cmd); //Opening UDP socket with apporpriate port and ip address
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
rui_cellular_send("AT+QISTATE=0,1"); //Checking state of connection established
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
memset(gsm_cmd,0,100);
sprintf(gsm_cmd, "AT+QISEND=0,%d", strlen(send_data)); //Sending data
rui_cellular_send(gsm_cmd);
rui_delay_ms(2000);
rui_cellular_send(send_data);
memset(gsm_rsp,0,256);
rui_cellular_response(gsm_rsp, 256, 500 * 20);
rui_cellular_send("AT+CLOSE=0"); //Closing Socket
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
rui_cellular_send("AT+QIDEACT=0"); //Deactivating cellular connection
rui_cellular_response(gsm_rsp, 256, 500 * 60);
memset(gsm_rsp,0,256);
return;
}