Temperature Logger โดยใช้ Arduino

เล่น Arduino มาสัปดาห์นึง ลองทำ Temperature Logger ดู อุปกรณ์มี

  1. Arduino nano
  2. TMP175 I2C Temperature Sensor
  3. ESP8266 Wifi module
  4. 5V to 3.3V regulator module

ต่อ TX จาก Arduino เข้ากับ RX ของ Wifi module ตรงๆ ก็ใช้งานได้ แต่ไม่มั่นใจเลยใช้วงจร voltage divider เป็นตัวต้านทานขนาด 330 โอห์มจำนวน 3 ตัว ส่วนทางฝั่ง TX จาก Wifi module เข้า RX ของ Arduino ก็ต่อตรงเลยไม่มีปัญหาอยู่แล้ว

code arduino

[code language=”cpp”]

#include <SoftwareSerial.h>
#include <Wire.h>

#define SSID "SSID"
#define PASS "PASSWORD"
#define IP "api.thingspeak.com" //"184.106.153.149" // thingspeak.com

String GET = "GET /update?key=[ThingSpeakAPI-Key]&field1=";
SoftwareSerial esp8266(4, 3); // RX, TX
const int I2C_address = 0x48; // I2C write address
byte TempHi; // Variable hold data high byte
byte TempLo; // Variable hold data low byte
float Decimal; // Variable hold decimal value
int TempValue;
char TempString[7]; // Array to hold temperature string
void Cal_Temp();

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
esp8266.begin(9600);
Serial.begin(9600);
sendDebug("AT");
delay(5000);
if(esp8266.find("OK")){
Serial.println("RECEIVED: OK");
connectWiFi();
}
delay(100);
Wire.beginTransmission(I2C_address);
Wire.write(0x01); // Setup configuration register
Wire.write(0x60); // 12-bit
Wire.endTransmission();
}

void loop(){
Wire.beginTransmission(I2C_address);
Wire.write(0x00); // Setup Pointer Register to 0
Wire.endTransmission();
// Read temperature value
Wire.requestFrom(I2C_address, 0x02);
while(Wire.available()) // Checkf for data from slave
{
TempHi = Wire.read(); // Read temperature high byte
TempLo = Wire.read(); // Read temperature low byte
}
Cal_Temp (); // Decimal updated with latest temperature read
String tempC = dtostrf(Decimal,4,1,TempString); // char buffered filled
Serial.print(TempString);
Serial.println(" degree C");
Serial.print(TempHi, HEX);
Serial.println(TempLo, HEX);
updateTemp(tempC);
delay(12000);
}

void updateTemp(String tempC){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if(esp8266.find("Error")){
Serial.print("RECEIVED: Error");
return;
}
cmd = GET;
cmd += tempC;
cmd += "\r\n";
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">")){
Serial.print(">");
Serial.print(cmd);
esp8266.print(cmd);
}else{
sendDebug("AT+CIPCLOSE");
}
if(esp8266.find("OK")){
Serial.println("RECEIVED: OK");
}else{
Serial.println("RECEIVED: Error");
}
}
void sendDebug(String cmd){
Serial.print("SEND: ");
Serial.println(cmd);
esp8266.println(cmd);
}

boolean connectWiFi(){
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(esp8266.find("OK")){
Serial.println("RECEIVED: OK");
return true;
}else{
Serial.println("RECEIVED: Error");
return false;
}
}

void Cal_Temp()
{

TempValue = 0;
TempValue = TempValue | (TempHi);
TempValue = TempValue << 8;
TempValue = TempValue | (0x00FF & TempLo);
TempValue = TempValue >> 4;

Decimal = TempValue * 0.0625;
}
[/code]

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *