#include <WiFi.h>
#include <HTTPClient.h>
#include <MD_MAX72xx.h> // Include the MD_MAX72XX library
// Replace with your network credentials
const char* ssid = "GURITA5G";
const char* password = "88888888";
// Replace with your Laravel API URL
const char* serverName = "https://tambal.in/esp32.php";
// MAX7219 configuration
#define MAX_DEVICES 4 // Number of MAX7219 devices
#define CS_PIN 5 // CS pin connected to MAX7219
// Create an instance of the MD_MAX72XX library
MD_MAX72XX matrix = MD_MAX72XX(CS_PIN, MAX_DEVICES);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize MAX7219 matrix
matrix.begin();
matrix.setIntensity(0); // Set brightness (0-15)
matrix.setRotation(0, 1); // Adjust rotation as needed
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
// Parse JSON response
int priceIndex = response.indexOf("price");
String priceString = response.substring(priceIndex + 7, response.indexOf("}", priceIndex));
float price = priceString.toFloat();
// Display price on MAX7219 matrix
displayPrice(price);
} else {
Serial.print("Error on HTTP request: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
delay(60000); // Fetch data every 60 seconds
}
void displayPrice(float price) {
String priceString = String(price, 2);
matrix.clear(); // Clear the display
matrix.setCursor(0, 0);
matrix.print(priceString);
matrix.write(); // Write the buffer to the displays
}