[2017.6 Digital Twin] Protocal/Timer On Arduino
Protocal
Before I continue design Arduino, I should first confirm the Protocal between the devices. Arduino and Raspberry, Raspberry and server, server and the app. I should make a common form. So that all the communication datas can all follow it.
The Data of sensor will be saved by server in SQL database. Arduino and Raspberry do the only one job: transmit. the data must add some information about the devices. So it needs to add some datas or flags in their data packet.
The flow of data transmission:
Sensor1 : int r=50; string id="temperature"
Sensor2 : bool r=true; string id="switch"
Sensor3 : floot r=38.6; string id="humidity"
Sensor4 : double r=1225.24string id="light intensty"
Controller1: bool s=true; string id="switch"
=> data paket:
[ string :id ][ unsigned char: type of variable ][ value ]
type of variable to describe which type is, for example,
1 represent int
2 represent bool
3 represent floot
4 represent double
5 represent string
6 represent char
EG, sensor1's packet
["temperature"][1][50]
When the packet arrives at raspberry, it need to add a lable to it
EG, sensor1's packet
["room"]["temperature"][1][50]
---------------------------------------------------------------------
In order to test my protocal. I design a test program
in this test program, I use a led to represent controller, use a switch to represent a sensor.
for the arduino program. I want to use defferent file to write different part of devices. But they all have a common Setup() and Loop();
In most of the sensor, the major job is send a message periodly. So I design a
Timer, this Timer don't use delay. So it will not interrupt other program.
unsigned long Timer_Count[10];
void (*point[10])();
bool Timer_Start[10];
void TimeSetup(){
int i=0;
for(i=0;i<10;i++){
Timer_Count[i]=0;
Timer_Start[i]=false;
}
}
void StartNewTimer(unsigned long t,int n,void (*te)()){
Timer_Count[n]=millis()+t;
Timer_Start[n]=true;
point[n]=te;
}
void TimerLoop(){
int i=0;
for(i=0;i<10;i++){
if(Timer_Start[i]){
if(millis()>Timer_Count[i]){
Timer_Start[i]=false;
point[i]();
}
}
}
}
1 represent int
2 represent bool
3 represent floot
4 represent double
5 represent string
6 represent char
EG, sensor1's packet
["temperature"][1][50]
When the packet arrives at raspberry, it need to add a lable to it
EG, sensor1's packet
["room"]["temperature"][1][50]
---------------------------------------------------------------------
In order to test my protocal. I design a test program
in this test program, I use a led to represent controller, use a switch to represent a sensor.
for the arduino program. I want to use defferent file to write different part of devices. But they all have a common Setup() and Loop();
In most of the sensor, the major job is send a message periodly. So I design a
Timer, this Timer don't use delay. So it will not interrupt other program.
unsigned long Timer_Count[10];
void (*point[10])();
bool Timer_Start[10];
void TimeSetup(){
int i=0;
for(i=0;i<10;i++){
Timer_Count[i]=0;
Timer_Start[i]=false;
}
}
void StartNewTimer(unsigned long t,int n,void (*te)()){
Timer_Count[n]=millis()+t;
Timer_Start[n]=true;
point[n]=te;
}
void TimerLoop(){
int i=0;
for(i=0;i<10;i++){
if(Timer_Start[i]){
if(millis()>Timer_Count[i]){
Timer_Start[i]=false;
point[i]();
}
}
}
}
------------------
as test:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
TimeSetup();
pinMode(2,INPUT_PULLUP);
pinMode(12,OUTPUT);
StartNewTimer(1000,1,Time_Event1);
}
void loop() {
TimerLoop();
}
void Time_Event1(){
StartNewTimer(1000,1,Time_Event1);
Serial.println("Time out");
}
this program will send message pro 1 sec.
I have aproject about arduino and resberry resberry pi, some is help for me, and some I can't understand ☺️☺️☺️☺️,
回复删除