티스토리 뷰

SWDesk/Firmware

ESP8266 POST Request Test

bizmaker 2019. 12. 6. 14:20

Test Completed

Filename : ESPNRF_Test21.ino

 

 

// 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 <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>


#define ENABLE_TESTVIASERIAL 0
#define POSTREQUEST 1
#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 1
#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 128
#define LOWERLIMIT_NRF24RX 10
#define SERVERACCESSPERIOD 11000


const int thisPORT = 80; // HTTP port
WiFiClient client;
unsigned long StartTime_WiFiWaiting = 0;
const char STX_BPM[] = ("[B!");
const char ETX_BPM[] = ("!]");


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;
bool IsWiFiRxCompleted = false;

long StartTime_Test1 = 0;
String RxString_WiFi = "";
unsigned long StartTime_ServerAccess = 0;

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();
  String Arg1 = String("A1=") + String(random(1000, 10000));
  if((millis()-StartTime_ServerAccess) > SERVERACCESSPERIOD){
    StartTime_ServerAccess = millis();
  if(h_SendDataViaWiFi(RequestWebpage, Arg1)) h_ReadDataViaWiFi();
  if(IsWiFiRxCompleted){
    IsWiFiRxCompleted = false;
    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");
  }
  }

  if(h_ReceiveDataVianRF24()){
    int tIndex0 = RxString_nRF24.indexOf("/");
    if(RxString_nRF24.substring(0, tIndex0).equals(String(THISDEVICEID))){
      int tIndex1 = RxString_nRF24.indexOf(":");
      if(RxString_nRF24.substring(tIndex0+1).equals(String("PM:ON"))){
        digitalWrite(PIN_RELAY, HIGH);
      }
      if(RxString_nRF24.substring(tIndex0+1).equals(String("PM:OFF"))){
        digitalWrite(PIN_RELAY, LOW);
      }
    }
  }
}


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://";
  //String HostPage = "";
  HostPage.concat(thisHOST);
  HostPage.concat(PagePath);
  http.begin(HostPage);
  Serial.println(HostPage);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  http.addHeader("Cache-Control", "no-cache");
//  http.addHeader("Content-Type", "text/plain");
  auto httpCode = http.POST(Options);
  Serial.print("httpCode >>> ");
  Serial.println(httpCode);
  String payload = http.getString();
  //Serial.print("Payload >>> ");
  //Serial.println(payload);
  http.end();
  int StartIndex = payload.indexOf("[B!") + 3;
  int EndIndex = payload.indexOf("!]");
  //Serial.print(line);
  if((StartIndex >= 3) && (EndIndex  > 3)){
      RxString_WiFi = payload.substring(StartIndex, EndIndex);
      Serial.print(">>>> ");
      Serial.println(RxString_WiFi);
      IsWiFiRxCompleted = true;
  }
  client.stop();
  return false;
#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;
}

void 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_WiFi = line.substring(StartIndex, EndIndex);
        Serial.print(">>>> ");
        Serial.println(RxString_WiFi);
        break;
    }
  }

  client.stop();
  IsWiFiRxCompleted = true;
}

반응형
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함