개발자/Arduino

Nano 33 IoT 데이터 서버 전송 Get 방식

지구빵집 2020. 10. 13. 19:06
반응형

 

 

가을은 점점 가득 찬다. 곱게 물들어가는 날이 빠르기만 하다. 무엇을 놓치고 사는 게 중요한 게 아니라 무엇을 가지고 살아가는지 돌아볼 일이다.

 

스마트 팩토리 센서 데이터 전송으로 한참을 고생하다가 결국 출장을 가서 겨우 코드를 만들어 왔다. 어차피 할 거라서 잘해야겠다.라고 생각할 필요도 없다. 무슨 일을 하느냐가 중요한 게 아니라 일을 어떻게 하느냐가 중요하다고 말은 한다. 말이 의미하는 그 겉멋에 감동받게 되지만 속지 않아야 한다. 아무리 그래도 무슨 일을 하는지가 더 중요하다. 돈을 버는 일인지, 낭비를 하는 일인지, 자신을 성장시키는지 여하튼 어떻게 하더라도 일 자체가 자지고 있는 특성은 변함이 없다. 그래서 그런지 무엇 하나 되더라도 너무 어렵게 된다. 

 

데이터가 전혀 들어오지 않는다고 해서 출장을 왔다. 결국 문제를 알아내긴 했지만 왠지 썩 좋은 해결책으로 보이지 않는다. 아래는 돌아가는 소스코드를 올려둔다. 왜 돌아가는지도 모르는 코드라서.^^ 본격적으로 데이터를 보내는 일이 남았다. 이전까지는 대충 보냈거든.

 

 

/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using the WiFi module.

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)

 created 13 July 2010
 by dlf (Metodo2 srl)
 modified 31 May 2012
 by Tom Igoe
 */


#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>

#include "arduino_secrets.h" 
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "jarvis.wepnp.com";    // name address for Google (using DNS)
//char server[] = "www.google.com";

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
    
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    root["machine"] = "gps";
    root["temperature"] = 350;
    root["humidity"] = 34;
    root["power"] = 0;
    root["count"] = 3;
    
    client.print("GET /API/v2/kpi/data/insert?data={%22machine%22:%22");
    client.print("print-02");
    client.print("%22,%22temperature%22:");
    client.print(350);
    client.print(",%22humidity%22:");
    client.print(34);
    client.print(",%22power%22:");
    client.print(0);
    client.print(",%22count%22:");
    client.print(3);
    client.println("} HTTP/1.1");
    //root.printTo(client);
    //client.print(" HTTP/1.1");
    client.println("Host: jarvis.wepnp.com");
    client.println("Authorization: Basic YmVsbGFuZWxsYTplU21aNmJvMjFjVGt1dnA3bXM2ZFI0Snlabw==");
    //client.println("Content-Type: application/x-www-form-urlencoded");
    //client.println("Connection: close");
    client.println();
    //client.print("data="); 
    
    //JsonObject& root = jsonBuffer.createObject();
    //root.printTo(Serial);    
    //root.printTo(client);

    //data send
        
  }
}

void loop() {
 
  
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

 

결과 화면을 참고하도록 올려둔다.

 

 

코드 실행화면 

 

 

 

간절히 원하면 지금 움직여야 한다. 움직이지 않아도 되고, 아니 움직이지 않는게 좋을 때도 있어.

 

 

 

 

반응형