ESP8266 WiFi 是Arduino常用的WIFI模組,該模組有兩種運行模式(AP模式和STA模式)
AP模式(相當於區域網路連結,手機本地控制):
把ESP8266 WiFi 熱點,也就是預先設定ESP8266 AP熱點名稱與密碼,這樣手機搜尋WIFI找到設定的名稱與密碼就可以連線使用。
執行手機APK程式就可以用WIFI方式與Arduino ESP8266溝通。
STA模式(相當於Internet網路連結,用於外部網路讓手機、網頁控制):
網路資源
另一塊WIFI模組 :nRF24L01 (電壓不可超過3.6V)
nFR24L01 2.4G無線模組只能單向雙工,一個模組只能單純發送或接收,不能改變,所以我們使用兩塊Arduino開發板來分別收發
網路資源 網路資源02 網路資源03 網路資源04
使用器材
arduino UNR R3*2
NRF2401 *2
使用庫
RF24
RF24Network
//////////////////////////////////////主機-發送信號/////////////////////////////////////
#include
#include
#include
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 1;
// Address of the other node
const uint16_t other_node = 0;
// How often to send 'hello world to the other unit
const unsigned long interval = 150; //ms
// When did we last send?
unsigned long last_sent;
// How many have we sent already
//unsigned long packets_sent;
// Structure of our payload
struct payload_t
{
uint32_t ms;
uint32_t sensorDataA;
uint32_t sensorDataB;
};
boolean power_SW;
int led_VOL;
void setup(void)
{
Serial.begin(115200);
Serial.println("RF24Network/examples/helloworld_tx/");
SPI.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS ) ;
network.begin(/*channel*/ 50, /*node address*/ this_node);
randomSeed(analogRead(0));
pinMode(7,INPUT_PULLUP);
}
void loop(void)
{
power_SW=!digitalRead(7);
led_VOL=analogRead(A0)/4;
// Pump the network regularly
network.update();
// If it's time to send a message, send it!
unsigned long now = millis();
if ( now - last_sent >= interval )
{
last_sent = now;
Serial.print("power_SW:");
Serial.println(power_SW);
Serial.print("led_VOL:");
Serial.println(led_VOL);
Serial.print("Sending...");
payload_t payload = {
millis(),power_SW,led_VOL };
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,&payload,sizeof(payload));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
}
}
//////////////////////////////////////從機-接收信號/////////////////////////////////////
#include
#include
#include
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 0;
// Address of the other node
const uint16_t other_node = 1;
// Structure of our payload
struct payload_t
{
uint32_t ms;
uint32_t sensorDataA;
uint32_t sensorDataB;
};
#define power_PIN 5
#define led_PIN 6
boolean power_SW;
int led_VOL;
void setup(void)
{
Serial.begin(115200);
SPI.begin();
radio.begin();
radio.setDataRate( RF24_250KBPS ) ;
network.begin(/*channel*/ 50, /*node address*/ this_node);
pinMode(power_PIN,OUTPUT);
pinMode(led_PIN,OUTPUT);
}
void loop(void)
{
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, grab it and print it out
RF24NetworkHeader header;
payload_t payload;
network.read(header,&payload,sizeof(payload));
power_SW=payload.sensorDataA;
led_VOL=payload.sensorDataB;
Serial.print("power_SW:");
Serial.println(power_SW);
Serial.print("led_VOL:");
Serial.println(led_VOL);
}
digitalWrite(power_PIN,power_SW);
analogWrite(led_PIN,led_VOL);
}
沒有留言:
張貼留言