Last active
April 29, 2025 23:33
-
-
Save soap/d5d8c9b27a490651295db7456436ef45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <DHT.h> // Adafruit library | |
#include <WiFi.h> // Core Arduino WiFi | |
#include <HTTPClient.h> | |
#define dhtPin 4 | |
#define buzzerPin 19 | |
#define SCREEN_WIDTH 128 | |
#define SCREEN_HEIGHT 64 | |
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); | |
DHT dht11(dhtPin, DHT11); // pin and type of sensor | |
// ตั้งค่า WiFi | |
const char* ssid = "WiFi SSID"; // ข้อมูลจากครู | |
const char* password = "Password"; // ข้อมูลจากครู | |
// ตั้งค่า API endpoint ของ Web Server | |
const char* serverName = "http://192.168.1.31:8000/api/sensor"; // ข้อมูลจากครู" | |
// Node ID (device_id ที่ Laravel ใช้) | |
const char* deviceId = "d00x"; // ข้อมูลจากครู | |
float humid, temp_c; | |
void setup() { | |
Serial.begin(115200); // ใช้ความเร็วสูงขึ้น | |
//------------- เชื่อมต่อ WiFi | |
WiFi.begin(ssid, password); | |
Serial.print("Connecting to WiFi..."); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(" connected!"); | |
//------------- จบส่วนเชื่อมต่อ WiFi ---------------------- | |
dht11.begin(); | |
if( !oled.begin(SSD1306_SWITCHCAPVCC, 0x3C) ) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for(;;); | |
} | |
oled.clearDisplay(); | |
oled.setTextSize(1); | |
oled.setTextColor(SSD1306_WHITE); | |
oled.setCursor(5, 10); // x=5, y=10 | |
oled.println("Ready"); | |
oled.display(); | |
delay(1000); | |
} | |
void loop() { | |
temp_c = dht11.readTemperature(); | |
humid = dht11.readHumidity(); | |
// แสดงบนจอ OLED | |
oled.clearDisplay(); | |
oled.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE); | |
oled.setCursor(5,5); | |
oled.print("T : "); | |
oled.print(temp_c, 1); | |
oled.print(" "); | |
// oled.print((char)247); | |
// วาดวงกลมเล็กแทนองศา | |
int16_t x = oled.getCursorX(); // X หลังจากพิมพ์เลข | |
int16_t y = oled.getCursorY(); // Y หลังจากพิมพ์เลข | |
oled.drawCircle(x + 3, y + 2, 2, SSD1306_WHITE); // วาดวงกลมเล็ก | |
oled.setCursor(x + 8, y); // เลื่อนไปต่อ | |
oled.print("C"); | |
oled.setCursor(5, 20); | |
oled.print("H : "); | |
oled.print(humid, 1); | |
oled.println("%"); | |
oled.display(); | |
if ((WiFi.status() == WL_CONNECTED)) { | |
HTTPClient http; | |
// เริ่มต้น POST | |
http.begin(serverName); | |
http.addHeader("Content-Type", "application/json"); | |
// สร้าง JSON payload | |
String payload = "{"; | |
payload += "\"device_id\":\"" + String(deviceId) + "\","; | |
payload += "\"temperature\":" + String(temp_c, 1) + ","; | |
payload += "\"humidity\":" + String(humid, 1); | |
payload += "}"; | |
Serial.println("Sending payload:"); | |
Serial.println(payload); | |
int httpResponseCode = http.POST(payload); | |
if (httpResponseCode > 0) { | |
String response = http.getString(); | |
Serial.println(httpResponseCode); | |
Serial.println(response); | |
} else { | |
Serial.print("Error on sending POST: "); | |
Serial.println(http.errorToString(httpResponseCode)); | |
} | |
http.end(); | |
} else { | |
Serial.println("WiFi Disconnected"); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment