Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Santos2, for ESP Wemos mini 2. program. In Notion.
- //First get rid of DHT stuff
- //Wemos Min1 2, sender, currently C0M11
- #include <ESP8266WiFi.h>
- #include <espnow.h>
- // #include <Adafruit_Sensor.h>
- // #include <DHT.h>
- // REPLACE WITH THE MAC Address of your receiver
- uint8_t broadcastAddress[] = {0x18,0xFE,0x34,0xF9,0x2E,0x4A} ;
- //{0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- // Digital pin connected to the DHT sensor
- // #define DHTPIN 5 // Uncomment the type of sensor in use:
- //#define DHTTYPE DHT11 // DHT 11
- // #define DHTTYPE DHT22 // DHT 22 (AM2302)//#define DHTTYPE DHT21 // DHT 21 (AM2301)
- // DHT dht(DHTPIN, DHTTYPE);
- // Define variables to store DHT readings to be sent
- float temperature;
- float humidity;
- // Define variables to store incoming readings
- float incomingTemp;
- float incomingHum;
- // Updates DHT readings every 10 seconds
- const long interval = 10000;
- unsigned long previousMillis = 0; // will store last time DHT was updated
- // Variable to store if sending data was successful
- String success;
- //Structure example to send data
- //Must match the receiver structure
- typedef struct struct_message {
- float temp;
- float hum;
- } struct_message;
- // Create a struct_message called DHTReadings to hold sensor readings
- struct_message DHTReadings;
- // Create a struct_message to hold incoming sensor readings
- struct_message incomingReadings;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- Serial.print("Last Packet Send Status: ");
- if (sendStatus == 0){
- Serial.println("Delivery success");
- }
- else{
- Serial.println("Delivery fail");
- }
- }
- // Callback when data is received
- void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
- memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
- Serial.print("Bytes received: ");
- Serial.println(len);
- incomingTemp = incomingReadings.temp;
- incomingHum = incomingReadings.hum;
- }
- void getReadings(){
- // Read Temperature
- temperature = 1112.3 ; //dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- //float t = dht.readTemperature(true);
- if (isnan(temperature)){
- Serial.println("Failed to read from DHT");
- temperature = 0.0;
- }
- humidity = 1145.6; //dht.readHumidity();
- if (isnan(humidity)){
- Serial.println("Failed to read from DHT");
- humidity = 0.0;
- }
- }
- void printIncomingReadings(){
- // Display Readings in Serial Monitor
- Serial.println("INCOMING READINGS");
- Serial.print("Temperature: ");
- Serial.print(incomingTemp);
- Serial.println(" ºC");
- Serial.print("Humidity: ");
- Serial.print(incomingHum);
- Serial.println(" %");
- }
- void setup() {
- // Init Serial Monitor
- Serial.begin(115200);
- // Init DHT sensor
- // dht.begin(); //==
- // 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;
- }
- // Set ESP-NOW Role
- esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
- // Once ESPNow is successfully Init, we will register for Send CB to
- // get the status of Trasnmitted packet
- esp_now_register_send_cb(OnDataSent);
- // Register peer
- esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
- // Register for a callback function that will be called when data is received
- esp_now_register_recv_cb(OnDataRecv);
- }
- void loop() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- // save the last time you updated the DHT values
- previousMillis = currentMillis;
- //Get DHT readings
- getReadings();
- //Set values to send
- DHTReadings.temp = temperature;
- DHTReadings.hum = humidity;
- // Send message via ESP-NOW
- esp_now_send(broadcastAddress, (uint8_t *) &DHTReadings, sizeof(DHTReadings));
- // Print incoming readings
- printIncomingReadings();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment