Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////tanzimat tempratare////////
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // Data wire is plugged into digital pin 2 on the Arduino
- #define ONE_WIRE_BUS 5
- // Setup a oneWire instance to communicate with any OneWire device
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass oneWire reference to DallasTemperature library
- DallasTemperature sensors(&oneWire);
- ///////////////////////////////////////////////////////
- ////////millis////////////////////////////////////////////
- unsigned long int t1=0;
- int interval=5000;
- int x=0;
- /////////////////////
- #include <ESP8266WiFi.h>
- #include <espnow.h>
- uint8_t broadcastAddress[] = {0x94, 0xB9, 0x7E, 0xD9, 0xC2, 0xC0}; //MAC address of the receiver. In my case it is ESP32
- const int ledPin = 4; // i.e. D2 pin
- const int ledPin2 =12;/////////2
- int receivedButtonValue = 0;
- int receivedButtonValue2 = 0;///////2
- bool isDataReceived = false;
- bool isDataReceived2 = false;/////////2
- typedef struct struct_message { // Structure to send/receive data. Must match the receiver/sender structure
- //char charPlaceHolder[32];
- int intPlaceHolder;
- int intPlaceHolder2;///////////2
- //float floatPlaceHolder;
- //String stringPlaceHolder;
- //bool boolPlaceHolder;
- } struct_message;
- struct_message myIncomingData, myOutgoingData;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- Serial.print(sendStatus == 0 ? "Packet sent successfully with value: " : "Packet sending failed with values: ");
- Serial.println(sensors.getTempCByIndex(0));
- }
- // Callback function that will be executed when data is received
- void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
- memcpy(&myIncomingData, incomingData, sizeof(myIncomingData));
- receivedButtonValue = myIncomingData.intPlaceHolder; //Here I recieve the button press value [0, 1] from ESP32
- isDataReceived = true;
- ////////////2
- receivedButtonValue2 = myIncomingData.intPlaceHolder2; //Here I recieve the button2 press value [0, 1] from ESP32
- isDataReceived2 = true;
- }
- void dama_1(void);
- void setup() {
- Serial.begin(115200);
- pinMode(ledPin, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- if (esp_now_init() != 0) { // Init ESP-NOW
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- esp_now_set_self_role(ESP_NOW_ROLE_COMBO); // setting as both sender and receiver. [ESP_NOW_ROLE_CONTROLLER, ESP_NOW_ROLE_SLAVE, ESP_NOW_ROLE_COMBO, ESP_NOW_ROLE_MAX]
- esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0); // Register peer
- esp_now_register_send_cb(OnDataSent); // register the callback on datasent
- esp_now_register_recv_cb(OnDataRecv); // register the callback on receive
- /////////////tempratare///////////////////////////////
- sensors.begin();
- /////////////////////////////////////////////////
- }
- void loop() {
- if(millis() > t1+ interval){
- t1=millis();
- dama_1();///////ersal mqdar dama har 5 saneye
- }
- if (isDataReceived) {
- isDataReceived = false;
- Serial.print("[RECEIVED]>> ");
- Serial.println(receivedButtonValue);
- if(receivedButtonValue == 1) {
- digitalWrite(ledPin, HIGH);
- } else {
- digitalWrite(ledPin, LOW);
- }
- }
- ///////////2
- if (isDataReceived2) {
- isDataReceived2 = false;
- Serial.print("[RECEIVED2]>> ");
- Serial.println(receivedButtonValue2);
- if(receivedButtonValue2 == 1) {
- digitalWrite(ledPin2, HIGH);
- } else {
- digitalWrite(ledPin2, LOW);
- }
- }
- delay(40);
- }
- //sensor void
- void dama_1() {
- // Send the command to get temperatures
- sensors.requestTemperatures();
- //print the temperature in Celsius
- Serial.print("Temperature: ");
- Serial.print(sensors.getTempCByIndex(0));
- Serial.print((char)176);//shows degrees character
- Serial.print("C | ");
- //print the temperature in Fahrenheit
- Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0);
- Serial.print((char)176);//shows degrees character
- Serial.println("F");
- //کد برای ارسال مقدار با پروتو کل ESPNOW
- myOutgoingData.intPlaceHolder = sensors.getTempCByIndex(0); // we MUST send the pot value in the range [0, 1023] because this has been configured at receiving ESP32
- esp_now_send(broadcastAddress, (uint8_t *) &myOutgoingData, sizeof(myOutgoingData));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement