본문 바로가기
전자공학/LoRa 통신

아두이노 lora shield(로라 쉴드)로 thingspeak, thingview에 값을 띄워보자, lora shield 시작하기 - (5)

by ohj921189 2020. 8. 11.
반응형

저번 포스팅에서는 송신 측(노드)의 아두이노 코드 해석에 대해 설명드렸습니다. 이번 시간에는 게이트웨이 측의 코드를 설명드리도록 하겠습니다. 송신 측과는 다르게 게이트웨이 측은 설정해야 할 부분들이 많습니다.

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
#include "SNIPE.h"


#define TXpin D9
#define RXpin D8
#define ATSerial Serial


SoftwareSerial DebugSerial(RXpin,TXpin);
SNIPE SNIPE(ATSerial);
int dis,temp,humi;
char buffer[16];

//16byte hex key
String lora_app_key = "11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 00";

char ssid[] ="iptime***";   // wifi 이름 
char pass[] ="11******";   // wifi 비밀번호
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber =110****; // Thingspeak 채널 ID 번호
const char * myWriteAPIKey ="MIZM*******"; //Thingspeak Write API Key 

int number = 0;

void setup() {
  ATSerial.begin(115200);  // Initialize serial

 // put your setup code here, to run once:
  while(ATSerial.read()>= 0) {}
  while(!ATSerial);

  DebugSerial.begin(115200);
   /* SNIPE LoRa Initialization */
  if (!SNIPE.lora_init()) {
    DebugSerial.println("SNIPE LoRa Initialization Fail!");
    while (1);
  }

  /* SNIPE LoRa Set App Key */
  if (!SNIPE.lora_setAppKey(lora_app_key)) {
    DebugSerial.println("SNIPE LoRa app key value has not been changed");
  }
  
  /* SNIPE LoRa Set Frequency */
  if (!SNIPE.lora_setFreq(LORA_CH_1)) {
    DebugSerial.println("SNIPE LoRa Frequency value has not been changed");
  }

  /* SNIPE LoRa Set Spreading Factor */
  if (!SNIPE.lora_setSf(LORA_SF_7)) {
    DebugSerial.println("SNIPE LoRa Sf value has not been changed");
  }
  
  /* SNIPE LoRa Set Rx Timeout 
   * If you select LORA_SF_12, 
   * RX Timout use a value greater than 5000  
  */
  if (!SNIPE.lora_setRxtout(5000)) {
    DebugSerial.println("SNIPE LoRa Rx Timout value has not been changed");
  }          
  DebugSerial.println("SNIPE LoRa LED Recv");
  WiFi.mode(WIFI_STA); 
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {
  if(WiFi.status() != WL_CONNECTED){
    DebugSerial.println("Attempting to connect to SSID: ");
    WiFi.begin(ssid, pass);
    DebugSerial.println("\nConnected.");
  }

  
    String ver = SNIPE.lora_recv();
    DebugSerial.println(ver);
    memset(buffer, 0x0, sizeof(buffer));
    ver.toCharArray(buffer, sizeof(buffer));
    sscanf(buffer, "%d:%d:%d", &dis,&humi,&temp);
    DebugSerial.print("Humidity: "); 
    DebugSerial.println(humi);
    DebugSerial.print("Temperature: ");
    DebugSerial.println(temp);
    DebugSerial.print("Distance: ");
    DebugSerial.println(dis);
    delay(1000);

  ThingSpeak.setField(1, dis);
  ThingSpeak.setField(2, temp);
  ThingSpeak.setField(3, humi);

  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  delay(1000); 
}

char ssid[] ="iptime***";   // 본인이 사용하고 있는 wifi(공유기)의 이름을 적어줍니다. 이 와이파이를 이용하여 서버와 데이터를 주고받습니다. 


char pass[] ="11******";   // 공유기의 비밀번호를 적어줍니다. wifi(공유기)를 사용하기 위해 필요한 과정 중 하나입니다. 

 

unsigned long myChannelNumber =110****; // 그다음으로 채널 ID를 입력해 줍니다. 

채널 ID는 자기가 만든 채널로 들어가 보면 채널 이름 밑에 숫자 7자리( 7자리가 아닐 수 있음)가 있는데 그것을 입력해 주면 됩니다. 추후에 Thingspeak를 어떻게 사용하는지에 대해서도 포스팅하도록 하겠습니다.

const char * myWriteAPIKey ="MIZM*******"; // 그다음으로는 Write API Key를 입력해 줍니다.

Write API Key는 채널의 API Keys라는 항목에 있으며 Write API Key와 Read API Key 두 가지 항목이 있는데 여기선 Write API Key의 값을 입력해 줍니다. 

 

app key를 설정하는 부분, SNIPE LoRa Initialization, SNIPE LoRa Set AppKey,  SNIPE LoRa Set Frequency, SNIPE LoRa Set Spreading Factor, SNIPE LoRa Set Rx Timeout은 로라 쉴드를 사용하기 위해 필요한 기초적인 코드입니다. 대부분 setup 부분에 있습니다.

 

WiFi.mode(WIFI_STA); // WIFI 동작 모드를 설정합니다. WIFI_STA, WIFI_AP, WIFI_AP_STA 모드 중에 하나를 사용할 수 있습니다. AP는 Access Point의 줄임말이며 AP mode는 이 보드가 AP와 같은 역할을 하게 됩니다. 보드 자체가 스스로 와이파이 네트워크를 구성하고 클라이언트의 접속을 제공합니다. STA mode는 station mode의 약자로 스마트폰처럼 와이파이에 접속하는 장치들이 여기에 해당합니다. 

 

ThingSpeak.begin(client); // Thingspeak 사용 초기화를 할 때 사용하는 코드입니다. 

 

loop 문에서 맨 처음 나오는 if문은 WiFi의 상태를 확인하여 연결되었으면 넘어가고 연결이 되지 않았으면 WiFi.begin(ssid,pass); 를 통하여 연결할 WIFI AP의 ssid 값과 패스워드를 설정해줍니다. 

 

 

반응형

댓글