[C] 纯文本查看 复制代码
/*
*NRF24l01针脚连接线
* MISO -> 12
* MOSI -> 11
* SCK -> 13
* Configurable:
* CE -> 8
* CSN -> 7
*/
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
int sid=1;//模块类型
int nid=6;//模块编号
const unsigned long interval = 1000;
unsigned long last_sent;
int shuidival=0;
int shuidi_PIN=A5;
//NRF24l01
unsigned long nrf24l01nowlast;
char nrf24l01buff[33]={0};
char nrf24l01Data;
int nrf24l01i=0;
void setup()
{
Serial.begin(115200);
char client[10]={0};//client
sprintf(client,"clie%d",sid);
//初始化Mirf,用于NRF24l01收发
Mirf_Init(0,client,sid);
Serial.println("zwifi_shuidi");
}
void loop()
{
unsigned long now = millis();
if ( now - last_sent >= interval )
{
last_sent = now;
shuidival=0;
run_shuidi();
if(shuidival==LOW)
{
char data[10]={0};
sprintf(data,"%d",shuidival);
send_data(data);
delay(10000);
}
}
}
//初始化Mirf 0初始化1为接收2为发送
void Mirf_Init(int txrx,char *server,int channel){
//初始化Mirf,用于NRF24l01收发
if(txrx==0) {
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)server);//设置接收地址
}
if(txrx==1) {
Mirf.setRADDR((byte *)server);//设置接收地址
}
if(txrx==2) {
Mirf.setTADDR((byte *)server);//设置发送地址
}
Mirf.payload = sizeof(char);//收发字节
Mirf.channel = channel;
Mirf.config();
}
//NRF24l01发送函数
void Mirf_Send(int channel,char *server,char *str){
Mirf_Init(2,server,channel);
int bufi=0;
for(bufi=0;bufi<strlen(str);bufi++){//循环发送
char words=str[bufi];//发送的字符
Mirf.send((byte *)&words);//发送命令
while(Mirf.isSending()){//等待发送完闭
}
delay(50);//延时,否则可能出现发送丢失现象
//Serial.print(words);
}
//Serial.println("");
}
void run_shuidi()
{
shuidival=digitalRead(shuidi_PIN);
Serial.println(shuidival,DEC);
delay(100);
}
//
void send_data(char *data){
char server[10]={0};//server
sprintf(server,"serv%d",1);
//Serial.println(server);
char updateData[33]={0};
char front[10]={0};
//memcpy(front,body,9);
sprintf(front," {ck%03d%03d",sid,nid);
sprintf(updateData,"%s%s}",front,data);
Serial.println(updateData);
Serial.println();
Mirf_Send(1,server,updateData);
char client[10]={0};//client
sprintf(client,"clie%d",sid);
Mirf_Init(1,client,sid);
}