Skip to content

Instantly share code, notes, and snippets.

@barii
Created January 4, 2025 10:09
Show Gist options
  • Save barii/61af5639432c4a9de36a4926ed95db74 to your computer and use it in GitHub Desktop.
Save barii/61af5639432c4a9de36a4926ed95db74 to your computer and use it in GitHub Desktop.
receiver
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <esp_now.h>
#include <esp_wifi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address of the OLED
#define OLED_SDA 21 // GPIO for SDA
#define OLED_SCL 22 // GPIO for SCL
#define REMOTE_IP "192.168.0.1" // Input the remote server you want to connect to
#define REMOTE_PORT 8888 // Input the remote port provided
Adafruit_SSD1306 *display;
int timeSinceMessage = 0;
float battery = 0.0;
const char* mac = 0;
const char* ssid_Router = "wifi-name"; // Enter the router name
const char* password_Router = "wifi-password"; // Enter the router password
typedef struct {
char location[32];
char mac[18];
float cellPercent;
float cellVoltage;
} esp_now_message;
volatile bool newMessageReceived = false;
esp_now_message incomingMessage;
void setup_esp_now() {
WiFi.mode(WIFI_STA);
WiFi.STA.begin();
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(esp_now_recv_cb_t(onReceive));
}
char* readMacAddress() {
uint8_t baseMac[6];
esp_err_t ret = esp_wifi_get_mac(WIFI_IF_STA, baseMac);
if (ret == ESP_OK) {
Serial.printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
char* macStr = (char*)malloc(18);
snprintf(macStr, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
baseMac[0], baseMac[1], baseMac[2],
baseMac[3], baseMac[4], baseMac[5]);
return macStr; // Return the allocated string
} else {
Serial.println("Failed to read MAC address");
return NULL; // Memory allocation failed
}
}
void setup_wifi() {
Serial.print("Connecting to Wi-Fi... ");
WiFi.begin(ssid_Router, password_Router);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("\nWiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
delay(1000);
mac = readMacAddress();
Serial.println("mac: ");
Serial.println(mac);
setup_esp_now();
setup_wifi();
display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Wire.begin(OLED_SDA, OLED_SCL);
if (!display->begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display->clearDisplay();
display->display();
}
// Callback function for received ESP-NOW messages
void onReceive(const uint8_t *mac, const uint8_t *data, int len) {
memcpy(&incomingMessage, data, sizeof(incomingMessage));
Serial.println("Received ESP-NOW message:");
Serial.printf("Location: %s\n", incomingMessage.location);
Serial.printf("MAC Address: %s\n", incomingMessage.mac);
Serial.printf("Battery Voltage: %.2f\n", incomingMessage.cellVoltage);
Serial.printf("Battery Percentage: %.2f\n", incomingMessage.cellPercent);
newMessageReceived = true;
timeSinceMessage = 0;
}
// Function to send a POST request
void sendPostRequest() {
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient http;
http.begin(client, "https://log-abcdefgh-uc.a.run.app/log");
String payload = "{\"location\":\"" + String(incomingMessage.location) +
"\",\"device\":\"" + String(incomingMessage.mac) +
"\",\"battery\":\"" + String(incomingMessage.cellPercent, 2) + "\"}";
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
http.end();
} else {
Serial.println("WiFi Disconnected");
}
}
void loop() {
if (newMessageReceived) {
sendPostRequest();
newMessageReceived = false; // Reset the flag
}
handle_oled(timeSinceMessage);
timeSinceMessage++;
delay(1000);
}
void handle_oled(int c) {
int hours = c / 3600;
int minutes = (c % 3600) / 60;
int seconds = c % 60;
display->clearDisplay();
display->setTextSize(1);
display->setTextColor(SSD1306_WHITE);
display->setCursor(0, 0);
display->println(mac);
display->println(WiFi.localIP());
display->print("Channel "); display->println(WiFi.channel());
display->print("Battery: "); display->print(incomingMessage.cellPercent); display->println("%");
display->println("");
display->println("Last message: ");
if (hours < 10) display->print("0");
display->print(hours);
display->print(":");
if (minutes < 10) display->print("0");
display->print(minutes);
display->print(":");
if (seconds < 10) display->print("0");
display->println(seconds);
display->display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment