diff --git a/random variables/README.md b/random variables/README.md new file mode 100644 index 0000000..1a74a2d --- /dev/null +++ b/random variables/README.md @@ -0,0 +1,10 @@ +# random variables demo +make sure you input the correct mac address in line 8 of the tx file. + +`uint8_t receiverMACAddress[] = {0x08, 0xF9, 0xE0, 0x6C, 0x75, 0x84};` + +you dont necessarily need an esp32 or esp8266 to use these files, im too lazy to rename them, and thats the boards that i used that i know works with the code, because i totally coded them. + +in the esp32 tx file, i used an ssd1306 128x64 oled display, if you dont have one of these, use your brain or ai to remove the display code. + +in the rx file, theres nothing special about it. \ No newline at end of file diff --git a/random variables/esp32_esp-now_tx.ino b/random variables/esp32_esp-now_tx.ino new file mode 100644 index 0000000..6dfb585 --- /dev/null +++ b/random variables/esp32_esp-now_tx.ino @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include + +// Define the MAC address of the receiver (ESP8266) +uint8_t receiverMACAddress[] = {0x08, 0xF9, 0xE0, 0x6C, 0x75, 0x84}; + +// Structure to send data +typedef struct struct_message { + char a[32]; + int b; + float c; + bool d; +} struct_message; + +// Create a struct_message called myData +struct_message myData; + +// OLED display width and height, in pixels +#define SCREEN_WIDTH 128 +#define SCREEN_HEIGHT 64 + +// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) +#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) +Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); + +// Callback function when data is sent +void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { + Serial.print("\r\nLast Packet Send Status: "); + String statusMessage = status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"; + Serial.println(statusMessage); + + // Update the display with the status message + display.clearDisplay(); + display.setTextSize(1); // Normal 1:1 pixel scale + display.setTextColor(SSD1306_WHITE); // Draw white text + display.setCursor(0, 0); + display.println(statusMessage); + display.display(); +} + +void setup() { + // Initialize Serial Monitor + Serial.begin(115200); + + // Initialize OLED display + if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64 + Serial.println(F("SSD1306 allocation failed")); + for(;;); + } + display.display(); + delay(2000); // Pause for 2 seconds + + // Clear the buffer + display.clearDisplay(); + + // Set device as a Wi-Fi Station + WiFi.mode(WIFI_STA); + + // Init ESP-NOW + if (esp_now_init() != ESP_OK) { + Serial.println("Error initializing ESP-NOW"); + return; + } + + // Register send callback + esp_now_register_send_cb(onDataSent); + + // Register peer + esp_now_peer_info_t peerInfo; + memset(&peerInfo, 0, sizeof(peerInfo)); + memcpy(peerInfo.peer_addr, receiverMACAddress, 6); + peerInfo.channel = 0; + peerInfo.encrypt = false; + + // Add peer + if (esp_now_add_peer(&peerInfo) != ESP_OK) { + Serial.println("Failed to add peer"); + return; + } + + // Seed random number generator + randomSeed(analogRead(0)); +} + +void loop() { + // Generate random values + snprintf(myData.a, sizeof(myData.a), "Value: %d", random(0, 1000)); + myData.b = random(0, 100); + myData.c = random(0, 1000) / 100.0; + myData.d = random(0, 2); + + // Send message via ESP-NOW + esp_err_t result = esp_now_send(receiverMACAddress, (uint8_t *) &myData, sizeof(myData)); + + if (result == ESP_OK) { + Serial.println("Sent with success"); + } else { + Serial.println("Error sending the data"); + } + + delay(2000); +} diff --git a/random variables/esp8266_esp-now_rx.ino b/random variables/esp8266_esp-now_rx.ino new file mode 100644 index 0000000..41b9053 --- /dev/null +++ b/random variables/esp8266_esp-now_rx.ino @@ -0,0 +1,52 @@ +#include +extern "C" { + #include +} + +// Structure to receive data +typedef struct struct_message { + char a[32]; + int b; + float c; + bool d; +} struct_message; + +struct_message myData; + +// Callback function that will be executed when data is received +void onDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) { + memcpy(&myData, incomingData, sizeof(myData)); + Serial.print("Bytes received: "); + Serial.println(len); + Serial.print("Char: "); + Serial.println(myData.a); + Serial.print("Int: "); + Serial.println(myData.b); + Serial.print("Float: "); + Serial.println(myData.c); + Serial.print("Bool: "); + Serial.println(myData.d); +} + +void setup() { + // Initialize Serial Monitor + Serial.begin(115200); + + // Set device as a Wi-Fi Station + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + + // Init ESP-NOW + if (esp_now_init() != 0) { + Serial.println("Error initializing ESP-NOW"); + return; + } + + // Register for a callback function that will be called when data is received + esp_now_set_self_role(ESP_NOW_ROLE_SLAVE); + esp_now_register_recv_cb(onDataRecv); +} + +void loop() { + // Nothing to do here +}