Skip to content

Instantly share code, notes, and snippets.

@SimedruF
Last active October 17, 2021 10:16
Show Gist options
  • Save SimedruF/049fbec5eed5c1b21fd63e89e0ee64f1 to your computer and use it in GitHub Desktop.
Save SimedruF/049fbec5eed5c1b21fd63e89e0ee64f1 to your computer and use it in GitHub Desktop.
ESp32_DEVKIT_receiver_esp_now.ino
/*
Florin Simedru
Complete project details at https://automatic-house.blogspot.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
/********************************************************************************************************************
* Preferences--> Aditional boards Manager URLs : https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
* Board Settings:
* Board: "ESP32 Wrover Module"
* Upload Speed: "921600"
* Flash Frequency: "80MHz"
* Flash Mode: "QIO"
* Partition Scheme: "Hue APP (3MB No OTA/1MB SPIFFS)"
* Core Debug Level: "None"
* COM Port: Depends *On Your System*
*
*/
#include <esp_now.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
const char* ssid = "WIFI_SSID";
const char* password = "password";
// For the breakout, you can use any 2 or 3 pins
// These pins will also work for the 1.8" TFT shield
#define TFT_MOSI 23 // SDA Pin on ESP32
#define TFT_SCLK 18 // SCL Pin on ESP32
#define TFT_CS 5 // Chip select control pin
#define TFT_DC 2 // Data Command control pin
#define TFT_RST 4 // Reset pin (could connect to RST pin)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 160 // OLED display height, in pixels
/* TFT ST7735 configuration */
Adafruit_ST7735 display = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
String temperature;
String humidity;
String pressure;
unsigned long previousMillis = 0;
const long interval = 5000;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;
// Create a struct_message called myData
struct_message myData;
const char* ntpServer = "europe.pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
/* Function to get timeinfo from ntpServer*/
void printLocalTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %y %H:%M:%S");
display.println(&timeinfo, "%a, %b %d %y %H:%M:%S");
}
// callback function that will be executed when data is received from ESP-NOW sender
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int 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);
Serial.println();
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station and access point
WiFi.mode(WIFI_AP_STA);
Serial.println(WiFi.macAddress());
Serial.print("Hello! ST7735 TFT Test");
// Use this initializer if you're using a 1.8" TFT
display.initR(INITR_GREENTAB); // Init ST7735 display 128x160 pixel
display.setRotation(3);
display.fillScreen(ST77XX_BLACK);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
// Display connection status
display.setTextSize(1);
display.setTextColor(ST77XX_WHITE);
display.setCursor(10,10);
display.print("Connecting to ");
display.print(ssid);
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
display.setTextSize(1);
display.setCursor(10, 20);
display.print("Connected to WiFi with IP:");
display.print(WiFi.localIP());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
display.setTextSize(1);
display.setCursor(10, 40);
display.print("Error initializing ESP-NOW ");
display.print("return");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
//init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
delay(2000);
}
void PrintInfoServer() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED ){
// copy data received from ESP-NOW
temperature = myData.b;
humidity = myData.c;
pressure = myData.d;
Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity + " % - Pressure: " + pressure + " hPa");
display.fillScreen(ST77XX_BLACK);
// display temperature
display.setTextSize(1);
display.setTextColor(ST77XX_WHITE);
display.setCursor(10,10);
display.print("T: ");
display.print(temperature);
display.print(" ");
display.setTextSize(1);
display.write(248);
display.setTextSize(1);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(10, 20);
display.print("H: ");
display.print(humidity);
display.print(" %");
// display pressure
display.setTextSize(1);
display.setCursor(10, 30);
display.print("P:");
display.print(pressure);
display.setTextSize(1);
display.setCursor(50, 30);
display.print("hPa");
// display wifi
display.setTextSize(1);
display.setTextColor(ST77XX_WHITE);
display.setCursor(10,40);
display.print("wifi: ");
display.print(ssid);
display.print(" ");
display.setTextSize(1);
display.setTextColor(ST77XX_GREEN);
display.setCursor(10,50);
printLocalTime();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
void loop() {
display.invertDisplay(false);
PrintInfoServer();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment