/*
*NRF24l01针脚连接线
* MISO -> 12
* MOSI -> 11
* SCK -> 13
* Configurable:
* CE -> 8
* CSN -> 7
*/
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <EEPROM.h>
#include <IRremote.h>
int sid=20;//模块类型
int nid=1;//模块编号
//声名变量
//无线串口通信处理(zigbee/bluetooth等)
unsigned long serial1nowlast;
char serial1buff[129]={0};
char serial1Data;
int serial1i=0;
//NRF24l01
unsigned long nrf24l01nowlast;
char nrf24l01buff[33]={0};
char nrf24l01Data;
int nrf24l01i=0;
int pinIn0=A0;
int val0;
int pinOut0=A1;
IRsend irsend;
int isdump=0;//
int RECV_PIN = A5;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(115200);
char client[10]={0};//client
sprintf(client,"clie%d",sid);
//初始化Mirf,用于NRF24l01收发
Mirf_Init(0,client,sid);
pinMode(pinIn0,INPUT);
pinMode(pinOut0,OUTPUT);
pinMode(pinIn0,INPUT_PULLUP); //将管脚设置为输入并且内部上拉模式
digitalWrite(pinIn0, HIGH);
irrecv.enableIRIn(); // Start the receiver
Serial.println("zwifi_hongweixian");
}
void loop()
{
//检测无线串口数据处理 (zigbee/bluetooth等)
{
unsigned long serial1now = millis();//获取现在的时间
if(serial1now - serial1nowlast >= 5000)//如果数据间隔超过5秒而清空字符(为了防止数据错乱)
{
serial1nowlast = millis();
memset(serial1buff, 0, 129);
serial1i=0;
}
while( Serial.available() )//如果无线串口有数据
{
if(serial1i==0)
{
Serial.println("serial->");//打印出来方便调试
}
serial1Data=(char)Serial.read();//读取串口数据
//Serial.print(serial1Data);////这里不打印,否则检测到{ckxxxx}就认为是命令
serial1buff[serial1i]=serial1Data;////保存到数组
serial1i++;////数组长度+1
if(serial1Data=='}' || serial1i>=129)//如果发现}而说明命令结束(并少于129个字符,太长会出错)
{
serial1nowlast = millis(); //更新当前时间,不然5秒就超时了
char body[129]={0};
get_znck_body(serial1buff,body);//获取只是{ckxxxxxx}的字符,因为这是我们的命令格式
//serial.println(body);
//如果命令格式真确则发送到无线串口
if(strstr(body,"{ck") && strstr(body,"}") )
{
//Serial.println(body);
if(strlen(body)>10)
{
send_infrared(body);
}
}
serial1i=0;//字符长度为0
Serial.println("-------------------");
delay(100);
}
}
}
unsigned long nrf24l01now = millis();//获取现在的时间
if(nrf24l01now - nrf24l01nowlast >= 5000)//如果数据间隔超过5秒而清空字符(为了防止数据错乱)
{
nrf24l01nowlast = millis();
memset(nrf24l01buff, 0, 33);
nrf24l01i=0;
}
byte data[Mirf.payload];
if(Mirf.dataReady()){//!Mirf.isSending() &&
Mirf.getData(data);
Mirf.rxFifoEmpty(); //清理24L01援存
//Serial.println((char)*data);
for (int i = 0; i < Mirf.payload; i++) //把收到的信息拼起来
{
if(nrf24l01i==0)
{
Serial.println("nrf24l01->");//打印出来方便调试
}
nrf24l01Data=(char)data;
if( nrf24l01Data=='{') nrf24l01i=0;
nrf24l01buff[nrf24l01i]=nrf24l01Data;//保存到数组
nrf24l01i++;////数组长度+1
if(nrf24l01Data=='}' || nrf24l01i>=33)//如果发现}而说明命令结束(并少于33个字符,太长会出错)
{
nrf24l01nowlast = millis(); //更新当前时间,不然5秒就超时了
char body[33]={0};
get_znck_body(nrf24l01buff,body);//获取只是{ckxxxxxx}的字符,因为这是我们的命令格式
//Serial.println(body);
//如果命令格式真确则发送到无线串口
if(strstr(body,"{ck") && strstr(body,"}") )
{
//Serial.println(body);
if(strlen(body)>10)
{
send_infrared(body);
}
}
memset(nrf24l01buff, 0, 33);
nrf24l01i=0;//字符长度为0
Serial.println("-------------------");
delay(100);
}
}
}
val0=digitalRead(pinIn0);//读取数字
if(val0==LOW || isdump==1)//检测按键是否按下
{
digitalWrite(pinOut0,HIGH);
//delay(2000);
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
dump(&results);
irrecv.resume(); // Receive the next value
}
//Serial.println("dumpcheck...");
//delay(100);
}
digitalWrite(pinOut0,LOW);
delay(10);
}
//初始化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("");
}
//获取只是{ckxxxxxx}的字符,因为这是我们的命令格式
void get_znck_body(char *p,char *s){
char rechar[33]={0};
int bufi=0;
bool isend=false;
int charnum=0;
for(bufi=0;bufi<strlen(p);bufi++){
//Serial.print(p[bufi]);
if(p[bufi]=='{'){
isend=true;
}
if(p[bufi]=='}' && isend==true){
isend=false;
rechar[charnum]=p[bufi];
break;
}
if(isend){
if(charnum<33)
{
rechar[charnum]=p[bufi];//Serial.print(rechar[charnum]);
charnum++;
}
}
}
//Serial.println("");
//memcpy(s,rechar,33);
sprintf(s,"%s",rechar);
}
//
void send_infrared(char *body){
int s=get_sid(body);
int n=get_nid(body);
char d[32]={0};
get_data(body,d);
if( s==sid && n==nid )
{
if( strlen(d)==1 && d[0]=='0' )
{
isdump=1;
}
if( strlen(d)==1 && d[0]=='1' )
{
char sdata[20]={0};
sendEEPROM(sdata);
}
if( strlen(d)>5 )
{
sendEEPROM(d);
}
}
if( s==sid ){
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",s,n);
sprintf(updateData,"%supdate}",front);
Serial.println(updateData);
Mirf_Send(1,server,updateData);
char client[10]={0};//client
sprintf(client,"clie%d",sid);
Mirf_Init(1,client,sid);
delay(2000);
}
}
int get_sid(char *buff){
if( strstr(buff,"{ck") && strstr(buff,"}") && strlen(buff)>10)
{
char charSid[4]={0};
memcpy(charSid,buff+3,3);
Serial.println(charSid);
int intSid=atoi(charSid);
Serial.println(intSid);
return intSid;
}
else
{
return 0;
}
}
int get_nid(char *buff){
if( strstr(buff,"{ck") && strstr(buff,"}") && strlen(buff)>10)
{
char charNid[4]={0};
memcpy(charNid,buff+6,3);
Serial.println(charNid);
int intNid=atoi(charNid);
Serial.println(intNid);
return intNid;
}
else
{
return 0;
}
}
int get_data(char *buff,char *sdata){
if( strstr(buff,"{ck") && strstr(buff,"}") && strlen(buff)>10)
{
for (int i = 0; i < strlen(buff); i++) {
if( buff[9+i]=='}' ) break;
sdata=buff[9+i];
}
//char charData[20]={0};
//memcpy(charData,buff+9,1);
//Serial.println(charData);
//int intData=atoi(charData);
//Serial.println(intData);
return 1;//intData;
}
else
{
return 0;
}
}
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
// decode_results *results = (decode_results *)v
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
return;
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: ");
Serial.print(results->panasonicAddress,HEX);
Serial.print(" Value: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
else {
return;
}
Serial.print(results->value, DEC);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf*USECPERTICK, DEC);
}
/*
Serial.print(",");
char tbuf[10]={0};
sprintf(tbuf,"%02X",results->rawbuf*USECPERTICK);
//Serial.print(tbuf);
strcat(rawbuf,tbuf);
Serial.print(" ");
*/
}
Serial.println("");
char rawbuf[255]={0};
char decode_type[10]={0};
sprintf(decode_type,"%d,",results->decode_type);
strcat(rawbuf,decode_type);
char decode_value[16]={0};
sprintf(decode_value,"%ld,",results->value);
strcat(rawbuf,decode_value);
char decode_bits[10]={0};
sprintf(decode_bits,"%d,",results->bits);
strcat(rawbuf,decode_bits);
for (int i = 0; i < 256; i++) {
EEPROM.write(i, 0);
}
for (int i = 0; i < strlen(rawbuf); i++) {
EEPROM.write(i, rawbuf);
}
Serial.println(rawbuf);
Serial.print("EEPROM.write count=");
Serial.print(strlen(rawbuf));
Serial.println("");
isdump=0;
}
void sendEEPROM(char* data){
Serial.print("sendEEPROM=");
Serial.println(data);
char eepchar[32]={0};
if( strlen(data)>5 ){
memcpy(eepchar,data,strlen(data));
}else{
for (int i = 0; i < 256; i++) {
long eep = EEPROM.read(i);
if(eep==0){
break;
}
eepchar=eep;
Serial.print(eep,DEC);
Serial.print(" ");
}
}
Serial.println("");
Serial.println(eepchar);
int ir_type=get_ir_type(eepchar);
long ir_value=get_ir_value(eepchar);
int ir_bits=get_ir_bits(eepchar);
Serial.println(ir_type); Serial.println(ir_value); Serial.println(ir_bits);
if( ir_type>0 && ir_value>0 && ir_bits>0 ){
Serial.println("irsend.sendRaw");
irsenddata(ir_type,ir_value,ir_bits);
}
//irsend.sendRaw(rawbuf, strlen(rawbuf), 38 /* kHz */);
//irsend.sendNEC(0x82e0040a, 32);
}
void irsenddata(int type, unsigned long value, int bits) {
if (type == NEC) {
irsend.sendNEC(value, bits);
Serial.print("sendNEC=");
Serial.print(value,DEC);
Serial.print(" bits=");
Serial.print(bits,DEC);
Serial.println("");
}
else if (type == SONY) {
irsend.sendSony(value, bits);
Serial.print("sendSony=");
Serial.print(value,DEC);
Serial.print(" bits=");
Serial.print(bits,DEC);
Serial.println("");
}
else if (type == RC5) {
irsend.sendRC5(value, bits);
Serial.print("sendRC5=");
Serial.print(value,DEC);
Serial.print(" bits=");
Serial.print(bits,DEC);
Serial.println("");
}
else if (type == RC6) {
irsend.sendRC6(value, bits);
Serial.print("sendRC6=");
Serial.print(value,DEC);
Serial.print(" bits=");
Serial.print(bits,DEC);
Serial.println("");
}
else {
Serial.print(type);
Serial.println("Bad type!");
}
delay(200);
}
int get_ir_type(char *buff){
if( strstr(buff,",") && strlen(buff)>5)
{
int n=0;
int m=0;
char chartype[4]={0};
for(int i=0;i< strlen(buff);i++){
if((char)buff==','){
n++;
if(n==1) break;
}
chartype[m]=buff;
m++;
}
//memcpy(chartype,buff+6,3);
Serial.print("get_ir_type");
Serial.print(" char=");
Serial.print(chartype);
int inttype=atoi(chartype);
Serial.print(" int=");
Serial.print(inttype);
Serial.println("");
return inttype;
}
else
{
return 0;
}
}
long get_ir_value(char *buff){
if( strstr(buff,",") && strlen(buff)>5)
{
int n=0;
int m=0;
char charvalue[16]={0};
for(int i=0;i< strlen(buff);i++){
if(buff==','){
n++;
if(n==1) { m=0; memset(charvalue,0,16); continue;};
if(n==2) break;
}
charvalue[m]=buff;
m++;
}
//memcpy(charvalue,buff+6,3);
Serial.print("get_ir_value");
Serial.print(" char=");
Serial.print(charvalue);
long longvalue=atol(charvalue);
Serial.print(" long=");
Serial.print(longvalue);
Serial.println("");
return longvalue;
}
else
{
return 0;
}
}
int get_ir_bits(char *buff){
if( strstr(buff,",") && strlen(buff)>5)
{
int n=0;
int m=0;
char charbits[16]={0};
for(int i=0;i< strlen(buff);i++){
if((char)buff==','){
n++;
if(n==1) { m=0; memset(charbits,0,16); continue;};
if(n==2) { m=0; memset(charbits,0,16); continue;};
if(n==3) break;
}
charbits[m]=buff;
m++;
}
//memcpy(charvalue,buff+6,3);
Serial.print("get_ir_bits");
Serial.print(" char=");
Serial.print(charbits);
int intbits=atoi(charbits);
Serial.print(" int=");
Serial.print(intbits);
Serial.println("");
return intbits;
}
else
{
return 0;
}
}
/*
* IRremote: IRrecvDump - dump details of IR codes with IRrecv
* An IR detector/demodulator must be connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
*/
#include
int RECV_PIN = 11;//定义红外接收器的引脚为11
//unsigned int rawCodes_ac_close[264]={3288,1591,432,1186,432,372,444,383,433,375,443,1184,434,382,434,383,433,384,433,383,433,1187,432,382,433,1186,432,1185,432,382,434,1186,431,1187,432,1186,432,1186,432,1186,432,383,433,1185,432,383,434,383,433,1186,432,383,433,383,433,383,433,384,433,1186,432,1186,432,1186,432,1186,432,383,433,383,433,383,433,384,433,384,433,384,433,383,433,383,433,383,433,383,433,383,433,384,433,384,432,384,432,383,433,383,432,384,432,385,432,385,432,384,432,384,432,385,432,384,432,384,432,384,432,1187,431,384,433,384,432,1187,431,1186,431,1186,431,384,433,27841,3287,1590,432,1186,431,384,434,383,433,383,433,1185,434,375,441,383,433,383,434,383,432,1186,433,382,433,1187,431,1186,432,383,433,1187,431,1186,432,1186,431,1186,432,1186,432,382,432,1187,431,383,433,384,433,1187,432,383,434,383,433,384,433,384,433,384,433,384,433,384,433,384,433,1186,432,1186,431,1187,431,1186,432,383,433,384,433,382,433,1186,430,1188,431,384,433,383,433,384,432,384,432,385,432,384,433,384,432,384,432,384,432,384,432,384,432,385,431,384,432,384,432,385,432,384,432,1188,431,383,433,384,432,1187,431,384,432,384,433,384,432,27841};
//unsigned int rawCodes_ac_close[264]={3448,1668,453,1244,453,390,466,402,454,393,464,1242,455,400,455,401,454,402,454,402,454,1244,453,401,454,1244,453,1243,453,401,455,1243,452,1244,453,1243,453,1243,453,1243,453,402,454,1243,453,402,455,401,454,1244,453,401,454,402,454,401,454,402,454,1244,453,1244,453,1244,453,1243,453,401,454,402,454,402,454,403,454,402,454,402,454,402,454,402,454,402,454,402,454,402,454,402,454,403,453,402,453,402,454,402,453,403,453,403,452,403,453,403,453,402,453,403,453,403,453,403,453,403,453,1245,452,402,454,402,452,1245,452,1244,452,1244,452,402,454,29191,3446,1668,453,1244,452,402,455,402,454,402,454,1242,455,394,462,401,454,402,455,402,453,1244,454,401,454,1244,452,1244,453,401,454,1244,452,1243,453,1243,452,1244,453,1243,453,401,453,1245,452,402,454,402,454,1244,452,401,455,402,454,402,454,402,454,402,454,402,454,402,454,403,454,1244,453,1244,452,1245,452,1244,453,402,454,402,454,401,454,1244,451,1245,452,402,454,402,454,403,453,403,453,403,453,403,454,403,453,403,453,402,453,403,453,403,453,403,452,403,453,403,453,403,453,403,453,1245,452,402,454,402,453,1245,452,403,453,402,454,402,453,29191};
unsigned int rawCodes_ac_close[264]={3500,1700,450,1250,500,400,450,400,450,450,450,1250,450,400,500,350,500,400,450,400,500,1250,450,400,500,1250,450,1250,450,400,500,1250,450,1300,450,1250,450,1300,450,1250,450,400,500,1250,450,400,450,400,500,1250,450,400,500,400,450,400,450,450,450,1250,450,1300,450,1250,450,1250,500,400,450,400,450,400,500,400,450,400,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,400,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,1300,450,400,450,400,500,1250,450,1250,450,1300,450,400,450,29650,3500,1700,450,1300,450,400,450,400,500,400,450,1250,500,400,450,400,450,400,500,400,450,1250,500,400,450,1250,450,1300,450,400,450,1250,500,1250,450,1250,500,1250,450,1250,500,400,450,1250,450,400,500,400,450,1250,500,400,450,400,450,450,450,400,450,400,500,400,450,400,450,400,500,1250,450,1250,500,1250,450,1250,500,400,450,400,450,400,500,1250,450,1250,500,400,450,400,450,450,450,400,450,400,500,350,500,400,450,450,450,400,450,400,450,450,450,400,450,400,500,400,450,400,450,450,450,1250,450,400,500,400,450,1250,450,400,500,400,450,400,500,13306};
//上面改成你接受到的数据,请注意把接受到的第一个数据放到最后
IRrecv irrecv(RECV_PIN);
decode_results results;
IRsend irsend;
//test
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // 初始化红外接收器
}
void dump(decode_results *results) {
int count = results->rawlen;
if (results->decode_type == UNKNOWN) {
Serial.print("Unknown encoding: ");
}
else if (results->decode_type == NEC) {
Serial.print("Decoded NEC: ");
}
else if (results->decode_type == SONY) {
Serial.print("Decoded SONY: ");
}
else if (results->decode_type == RC5) {
Serial.print("Decoded RC5: ");
}
else if (results->decode_type == RC6) {
Serial.print("Decoded RC6: ");
}
else if (results->decode_type == PANASONIC) {
Serial.print("Decoded PANASONIC - Address: ");
Serial.print(results->panasonicAddress,HEX);
Serial.print(" Value: ");
}
else if (results->decode_type == JVC) {
Serial.print("Decoded JVC: ");
}
Serial.print(results->value, HEX);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count, DEC);
Serial.print("): ");
for (int i = 0; i < count; i++) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
}
void loop() {
irsend.sendRaw(rawCodes_ac_close,264, 40);
delay(5);
// irsend.sendRaw(rawCodes_ac_close,264, 40);
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
dump(&results);
// irsend.mark(560);
// irsend.space(560);
// irsend.mark(2000);
// irsend.space(560);
//rsend.sendRaw(rawCodes_ac_close,6, 32);
irrecv.resume(); // Receive the next value
}
delay(5000);
}
unsigned int rawCodes_ac_close[200] ={4193,4096,595,1451,589,432,588,1453,589,1454,587,432,587,434,563,1475,591,433,564,458,586,1453,587,434,587,433,563,1476,590,1454,587,434,565,1475,566,456,563,1476,590,1453,587,1452,564,1477,566,457,585,1453,567,1477,587,1452,564,460,584,434,564,457,562,460,562,1475,587,434,588,433,562,1476,589,1455,563,1476,588,433,586,436,562,459,586,433,563,458,609,410,563,458,586,433,587,1454,587,1453,563,1477,564,1477,565,1476,581,4858,4237,4074,593,1453,562,460,563,1477,563,1477,565,458,587,434,584,1454,590,433,562,458,587,1453,588,436,584,436,562,1476,588,1453,590,434,562,1480,586,435,586,1453,565,1476,589,1453,589,1451,565,461,583,1453,589,1453,588,1454,564,457,587,434,587,433,587,435,561,1479,586,435,562,458,588,1452,590,1452,565,1476,564,458,587,436,584,433,562,458,588,432,562,461,585,434,584,435,589,1451,588,1453,588,1453,590,1453,590,1451,590};
坏的刚刚好 发表于 2015-3-9 15:10
代码有问题
znck007 发表于 2015-3-9 17:09
qq610854837截图,日志等。
yleer3407 发表于 2015-8-17 17:20
FILE->EXAMPLES->Arduino_IRremote_ 后面那个master找不到怎么办
欢迎光临 智能创客,中国最大的极客空间,智能平台,免费教学,视频教程,手把手教你创造儿时梦想! (http://luntan.znck007.com/) | Powered by Discuz! X3.2 |