#include<SoftwareSerial.h>SoftwareSerialmySerial(12,13); //Uno Rx Tx (12 13) = SoftwareSerialconstchar* Stream_mode ="M 00\r\n";String str;voidsetup() {Serial.begin(9600); //시리얼 통신 초기화Serial.println("SST GAS Sensor");mySerial.begin(9600);mySerial.print(Stream_mode);} voidloop() { /* Response ex) O XXX T XXX P XXX % XXX e xxx O XXX: PPO2 T XXX: temperature P XXX: barometric pressurec % XXX: O2 e XXX: Sensor Status */if(mySerial.available()>0) { str =""; str =mySerial.readStringUntil('\n'); Serial.println(str); }}
#include<SoftwareSerial.h>SoftwareSerialmySerial(12,13); //Uno Rx Tx (12 13) = SoftwareSerialconstchar* Poll_mode ="M 01\r\n";constchar* O2_Read ="%\r\n";String str;voidsetup() {Serial.begin(9600); //시리얼 통신 초기화mySerial.begin(9600);Serial.println("SST GAS Sensor");delay(1000);mySerial.print(Poll_mode);} voidloop() { /* Response ex) O XXX T XXX P XXX % XXX e xxx O XXX: PPO2 T XXX: temperature P XXX: barometric pressurec % XXX: O2 e XXX: Sensor Status */mySerial.print(O2_Read);delay(1000);if(mySerial.available()>0) { str =""; str =mySerial.readStringUntil('\n'); Serial.println(str); }}
7.Thingspeak 활용 방법
Step1. 아두이노 IDE에 Thingspeak 라이브러리 추가
Step2. Wi-Fi ssid, pass 확인 및 변경
code Thingspeak ssid, password 부분 자신이 사용할 Wi-Fi ssid, pass로 변경
Step3. Thingpeak channelnumber 및 api key 확인
Thingspeak -> channel -> my channel -> channel setting -> channel id 확인 후 code 변경
Thingspeak -> channel -> my channel -> Api keys -> Write Api key -> key 확인 후 code 변경
소스 코드
#include<Arduino.h>#include"ThingSpeak.h"#include<Wire.h>#include<WiFi.h>constchar* Poll_mode ="M 01\r\n";constchar* O2_Read ="%\r\n";constchar* ssid ="your network SSID"; // your network SSID (name) constchar* password ="your network password"; // your network passwordWiFiClient client;unsignedlong myChannelNumber = your channel number;constchar* myWriteAPIKey ="your channel api key";// Timer variablesString str;unsignedlong lastTime =0;unsignedlong timerDelay =10000;voidsetup(){Serial.begin(9600);WiFi.mode(WIFI_STA); ThingSpeak.begin(client); // Initialize ThingSpeak}voidloop(){float o2_value_to;Serial1.print(O2_Read);delay(1000);if(Serial1.available()>0) { str =""; str =Serial1.readStringUntil('\n'); Serial.println(str);int o2_length =str.length(); String value_o2 =str.substring(o2_length-6, o2_length); o2_value_to =value_o2 .tofloat(); }if ((millis() - lastTime) > timerDelay) { // Connect or reconnect to WiFiif(WiFi.status() != WL_CONNECTED) {Serial.print("Attempting to connect");while(WiFi.status() != WL_CONNECTED){WiFi.begin(ssid, password); delay(2000); } Serial.println("\nConnected."); } // pieces of information in a channel. Here, we write to field 1.ThingSpeak.setField(1, o2_value_to);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)); } lastTime =millis(); }