728x90
TFMINI LIDAR (TOF) Laser Ranger Distance Sensor
ARDUINO + ETHERNET SHIELD
아두이노 보드에 이더넷 쉴드를 결합하여 랜선을 연결하여 인터넷 사용이 가능합니다.
LTE 라우터와 연결하여 외부에서도 인터넷을 사용할 수 있습니다.
라이다 거리센서와 결합하여 거리데이터를 서버로 업로드합니다.
ThingSpeak 서버를 이용하여 데이터를 그래프로 표시합니다.
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "S7UUGFIPSQIS9H5I";
const int updateThingSpeakInterval = 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
// Setup software serial port
SoftwareSerial mySerial(2, 3); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
Serial.println("IP address: ");
Serial.println(Ethernet.localIP());// 아이피 주소를 출력합니다.
}
delay(1000);
}
void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(115200);
// wait for serial port to connect. Needed for native USB port only
while (!Serial);
Serial.println ("Initializing...");
// Step 2: Initialize the data rate for the SoftwareSerial port
mySerial.begin(115200);
// Start Ethernet on Arduino
startEthernet();
}
/* For Arduinoboards with multiple serial ports like DUEboard, interpret above two pieces of code and
directly use Serial1 serial port*/
int dist;//actual distance measurements of LiDAR
int strength;//signal strength of LiDAR
int check;//save check value
int i;
int uart[9];//save data measured by LiDAR
const int HEADER=0x59;//frame header of data package
int timeout = 0;
void updateThingSpeak(String tsData)
{
// Serial.println("updateThingSpeak");
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
//Serial.println("Connecting to ThingSpeak...");
//Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void loop() {
if (mySerial.available())//check if serial port has data input
{
if(mySerial.read()==HEADER)//assess data package frame header 0x59
{
uart[0]=HEADER;
if(mySerial.read()==HEADER)//assess data package frame header 0x59
{
uart[1]=HEADER;
for(i=2;i<9;i++)//save data in array
{
uart[i]=mySerial.read();
}
check=uart[0]+uart[1]+uart[2]+uart[3]+uart[4]+uart[5]+uart[6]+uart[7];
if(uart[8]==(check&0xff))//verify the received data as per protocol
{
dist=uart[2]+uart[3]*256;//calculate distance value
strength=uart[4]+uart[5]*256;//calculate signal strength value
Serial.print("dist = ");
Serial.print(dist);//output measure distance value of LiDAR
//Serial.print('\t');
//Serial.print("strength = ");
//Serial.print(strength);//output signal strength value
Serial.print('\n');
}
}
}
}
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
// Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
//Serial.println("...disconnected");
//Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1="+String(dist)+"&field2="+String(strength));
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {
startEthernet();
}
lastConnected = client.connected();
delay(1);
}
Node-RED Realtime Data Collect
LTE M2M ROUTER
인터넷이 없는 곳에서도 LET 모바일 인터넷을 사용할 수 있습니다.
고품질 LTE망을 통해 자유로운 인터넷 환경을 제공합니다.
http://biz-lguplus.kr/content.php?pagecode=m31
참조
https://m.blog.naver.com/cherrychance/221761135441
Update ThingSpeak Server
'임베디드 보드 > 아두이노' 카테고리의 다른 글
미니 LED 전광판 아두이노 제어 (0) | 2020.09.23 |
---|---|
PWM splitter (0) | 2020.09.17 |
WEMOS D1 전류/전압 측정, ThingSpeak 데이터 모니터링 (0) | 2020.08.21 |
전압/전류 Data 전송 From Arduino to Node-RED (0) | 2020.08.13 |
아두이노 전류량/ 전압 체크 (1) | 2020.08.12 |