티스토리 뷰
- Master(릴레이, ESP8266, nRF24L01)용 프로그램
- Arduino 파일명 : ESPNRF_TestM20.ino
- Webpage 파일명 : BPMTest_11.php
- Request with GET or POST
<Source code for Firmware>
// Testing ESP8266 + nRF24L01, 2019.12.01
// Developing for nRF24-Slave
// Sample Code1 : https://www.how2electronics.com/esp8266-nrf24l01-based-wireless-temperature-humidity-monitoring/
// Sample Code2 : https://iotdesignpro.com/projects/wireless-communication-between-arduino-and-nodemcu-using-nrf24l01-transceiver-module
// Reference URL1 : https://tttapa.github.io/ESP8266/Chap08%20-%20mDNS.html
#include
#include
#include
#include
#include
#define ENABLE_TESTVIASERIAL 0
#define POSTREQUEST 0
#define ENABLE_SLAVE_V1 0 // version 1.0 with buttons
#define ENABLE_MASTER_V1 1 // version 1.0 with relays
#define PIN_LED 16
#define PIN_RELAY 4
#define ISTHISMASTER 0
#define THISDEVICEID "BHM011" // Handy Module DeviceID
//#define THISDEVICEID "BPB010" // Power Box Device ID
#define MASTERADDRESS "BPM00M" // BPM-Master (listening) Channel
#define SLAVEADDRESS "BPM00S" // BPM-Slave (listening) Channel
#define PIN_CE 2 // GPIO2 or D7
#define PIN_CSN 5 // GPIO5 or D8
#define PIN_POWERCONTROL 4//GPIO4 or D5
#define TIMELIMIT_WIFIWAITING 3000
#define LENGTHLIMIT_NRF24BUFFER 36
#define LOWERLIMIT_NRF24RX 10
//******************************
//댓글 참조
//*******************************
const byte MasterAddress[] = MASTERADDRESS; // Master address
const byte SlaveAddress[] = SLAVEADDRESS; // Slave address
const char thisDeviceID[] = THISDEVICEID;
unsigned long StartTime_nRF24Waiting = 0;
RF24 radio(PIN_CE, PIN_CSN);
int PowerStatus = 1; // Power ON
int LEDStatus = 1;
String RxString_nRF24 = "";
bool IsnRF24RxStarted = false;
bool IsnRF24RxCompleted = false;
long StartTime_Test1 = 0;
String RxString_WiFi = "";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_LED, HIGH);
digitalWrite(PIN_RELAY, HIGH);
h_InitializeWiFi();
//h_InitializenRF24();
}
void loop() {
// put your main code here, to run repeatedly:
//if(h_SendDataViaWiFi("/BPM/tBPMMain.php", "DI=aa1")) h_ReadDataViaWiFi();
delay(10000);
if(h_SendDataViaWiFi("/BPM/BPMTest_11.php", "A=1")){
RxString_WiFi = h_ReadDataViaWiFi();
if(RxString_WiFi.indexOf("ON") > 0) digitalWrite(PIN_RELAY, HIGH);
if(RxString_WiFi.indexOf("OFF") > 0) digitalWrite(PIN_RELAY, LOW);
//h_SendDataViaWiFi("/BPM/BPMTest1_10.php", "A=1");
}
}
void h_InitializeWiFi(){
//? WiFi.mode(WiFi_STA); // Ref. : https://nodemcu.readthedocs.io/en/master/modules/wifi/
WiFi.begin(thisSSID, thisPASSWORD);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
#if(ENABLE_TESTVIASERIAL==1)
Serial.println("");
Serial.println("WiFi connected.");
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
#endif
}
bool h_SendDataViaWiFi(String PagePath, String Options){
// PagePath : "/BPM/tBPMMain.php"
// Options : "DI=aa1&DT=bb1&DV=cc1"
if(!client.connect(thisHOST, thisPORT)){
Serial.println("Connection failed");
return false;
}
Serial.println(">>> Server connected");
#if(ENABLE_TESTVIASERIAL==1)
Serial.print("Requesting URL : ");
Serial.print(PagePath);
Serial.print("?");
Serial.println(Options);
#endif
#if(POSTREQUEST == 1)
HTTPClient http;
String HostPage = "http://";
HostPage.concat(thisHOST);
HostPage.concat(PagePath);
http.begin(HostPage);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
auto httpCode = http.POST(Options);
Serial.println(httpCode);
String payload = http.getString();
Serial.println(payload);
http.end();
#else // GET Request
client.print(String("GET "));
client.print(PagePath);
client.print("?");
client.print(Options);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(thisHOST);
client.println(F("Connection: close"));
client.println();
StartTime_WiFiWaiting = millis();
#endif
while(client.available() == 0){
if((millis() - StartTime_WiFiWaiting) > TIMELIMIT_WIFIWAITING){
#if(ENABLE_TESTVIASERIAL==1)
Serial.println(" >>> Client Timeout ");
#endif
client.stop();
return false;
}
}
return true;
}
String h_ReadDataViaWiFi(){
String RxString="";
while(client.available()){
String line = client.readStringUntil('\r');
int StartIndex = line.indexOf("[B!") + 3;
int EndIndex = line.indexOf("!]");
Serial.print(line);
if((StartIndex >= 3) && (EndIndex > 3)){
RxString = line.substring(StartIndex, EndIndex);
Serial.print(">>>> ");
Serial.println(RxString);
break;
}
}
client.stop();
return RxString;
}
<HTML 소스>
서버 측 접속 파일
'SWDesk > Web' 카테고리의 다른 글
PHP를 이용한 XML 및 HTML 파싱 (0) | 2020.01.25 |
---|---|
Javafx용 예제(1) (0) | 2020.01.22 |
[MySQL] DB 백업 및 복원 (0) | 2020.01.20 |
Kotlin Excersing Example (0) | 2019.12.13 |
[Test Completed] BPM-Master (1) | 2019.12.03 |
- Total
- Today
- Yesterday
- 아두이노
- 오블완
- arduino
- 허들
- 혁신
- 절연형
- Innovations
- 심심풀이
- 심심풀이치매방지기
- 빌리언트
- 전압
- Innovation&Hurdles
- image
- Video
- ServantClock
- 둎
- bilient
- 혁신과허들
- 치매
- BSC
- Innovations&Hurdles
- 전류
- badp
- 치매방지
- Decorator
- DYOV
- 배프
- Hurdles
- 티스토리챌린지
- 전압전류모니터링
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |