Arduino 使用以太卡发送Mirf值
我有一个项目,我通过nRF24L01获取数据,并使用Mirf实现。现在我在Hub工作,它需要向我的Web服务发送数据。对于以太网,我选择的是带有以太卡库的ENC28j60 问题:我如何等待来自Mirf的数据,然后用以太卡浏览器发送数据?我可以在没有Mirf的情况下发送数据,但有一些循环我不明白 我的代码:Arduino 使用以太卡发送Mirf值,arduino,Arduino,我有一个项目,我通过nRF24L01获取数据,并使用Mirf实现。现在我在Hub工作,它需要向我的Web服务发送数据。对于以太网,我选择的是带有以太卡库的ENC28j60 问题:我如何等待来自Mirf的数据,然后用以太卡浏览器发送数据?我可以在没有Mirf的情况下发送数据,但有一些循环我不明白 我的代码: #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHard
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
#include <EtherCard.h>
// Set network settings
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
// My webservice
const char website[] PROGMEM = "my.webservice.com";
// Mirf variables
int tmpVal1;
// Local components
const int Yellow = 6;
const int Blue = 5;
void setup() {
Serial.begin(57600);
// Setup leds
pinMode(Yellow, OUTPUT);
digitalWrite(Yellow, LOW);
pinMode(Blue, OUTPUT);
digitalWrite(Blue, LOW);
setupMirf();
setupEthernet();
}
void loop() {
// Waiting to get date from Mirf
while (!Mirf.dataReady()) {
//ether.packetLoop(ether.packetReceive());
}
Mirf.getData((byte *)&tmpVal1);
Serial.print(tmpVal1);
Serial.println(F(" C"));
// Receive responses
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 5000;
//Serial.println();
Serial.println("Sending data to webservice : ");
ether.browseUrl(PSTR("/sendingdata.asmx/sendingdata?"), "Device=100&DeviceValue=80", website, my_callback);
}
//ShowLedNotification();
}
// called when the client request is complete
static void my_callback (byte status, word off, word len) {
Serial.println(">>>");
Ethernet::buffer[off+300] = 0;
Serial.print((const char*) Ethernet::buffer + off);
Serial.println("...");
digitalWrite(Blue,HIGH);
delay(200);
digitalWrite(Blue,LOW);
}
void ShowLedNotification() {
if (tmpVal1 > 0 ) {
digitalWrite(Yellow, HIGH);
delay(1000);
digitalWrite(Yellow, LOW);
}
else
{
digitalWrite(Blue, HIGH);
delay(1000);
digitalWrite(Blue, LOW);
}
}
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
//Setting up network and getting DHCP IP
void setupEthernet() {
Serial.println(F("Setting up network and DHCP"));
Serial.print(F("MAC: "));
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller"));
Serial.println(F("Setting up DHCP"));
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
// Check network connection
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("SRV: ", ether.hisip);
}
void setupMirf() {
//Initialize nRF24
Serial.println(F("Initializing Mirf"));
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)"serv1");
Mirf.payload = sizeof(tmpVal1);
// we use channel 90 as it is outside of WLAN bands
// or channels used by wireless surveillance cameras
Mirf.channel = 90;
Mirf.config();
}
#包括
#包括
#包括
#包括
#包括
//设置网络设置
静态字节mymac[]={0x74、0x69、0x69、0x2D、0x30、0x31};
字节以太网::缓冲区[700];
静态uint32_t定时器;
//我的网络服务
const char网站[]PROGMEM=“my.webservice.com”;
//Mirf变量
int tmpVal1;
//局部分量
常数int黄色=6;
常数int蓝色=5;
无效设置(){
序列号。开始(57600);
//设置指示灯
pinMode(黄色,输出);
数字写入(黄色,低电平);
pinMode(蓝色,输出);
数字写入(蓝色,低电平);
setupMirf();
设置以太网();
}
void循环(){
//等待从Mirf获得日期
而(!Mirf.dataReady()){
//ether.packetLoop(ether.packetReceive());
}
getData((字节*)&tmpVal1);
串行打印(tmpVal1);
连续打印(F(“C”);
//收到回复
ether.packetLoop(ether.packetReceive());
如果(毫秒()>计时器){
定时器=毫秒()+5000;
//Serial.println();
Serial.println(“将数据发送到webservice:”);
ether.browseUrl(PSTR(“/sendingdata.asmx/sendingdata?”),“Device=100&DeviceValue=80”,网站,my_回调);
}
//ShowLedNotification();
}
//当客户端请求完成时调用
静态void my_回调(字节状态、字关闭、字len){
Serial.println(“>>>”);
以太网::缓冲区[off+300]=0;
串行打印((常量字符*)以太网::缓冲+关闭);
Serial.println(“…”);
数字写入(蓝色,高);
延迟(200);
数字写入(蓝色,低电平);
}
void ShowLedNotification(){
如果(tmpVal1>0){
数字写入(黄色,高);
延迟(1000);
数字写入(黄色,低电平);
}
其他的
{
数字写入(蓝色,高);
延迟(1000);
数字写入(蓝色,低电平);
}
}
long readVcc(){
长期结果;
//根据AVcc读取1.1V参考电压
ADMUX=_BV(参考文献0)|u BV(MUX3)|u BV(MUX2)|u BV(MUX1);
延迟(2);//等待Vref结算
ADCSRA |=_BV(ADSC);//转换
而(位_是_集(ADCSRA,ADSC));
结果=ADCL;
result |=ADCH确实完成了这项工作。现在使用if子句not while Mirf.dataReady()
void loop() {
if (Mirf.dataReady()) {
Mirf.getData((byte *)&tmpVal1);
Serial.print(tmpVal1);
Serial.println(F(" C"));
ShowLedNotification();
// Send data to webservice
if (millis() > timer) {
timer = millis() + 5000;
Serial.println("Sending data to webservice");
String myVarsStr = "Device=";
myVarsStr += myDeviceID;
myVarsStr += "&DeviceValue=";
myVarsStr += tmpVal1;
char myVarsCh[40];
myVarsStr.toCharArray(myVarsCh, 40);
ether.browseUrl(PSTR("/receivedata.asmx/ReceiveData?"), myVarsCh, website, my_callback);
}
}
else
{
word pos = ether.packetReceive();
word len = ether.packetLoop(pos);
delay(200);
}
}