Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////tanzimat tempratare////////
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 23
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- ///////////////////////////////
- //////////wifi//////////////////
- #include <WiFi.h>
- ////////////////////////////////
- //////////ESP NOW//////////////
- #include <esp_now.h>
- #define LEDC_CHANNEL_0 0
- #define LEDC_TIMER_13_BIT 13
- #define LEDC_BASE_FREQ 5000
- ///////////////data////////////////
- uint8_t broadcastAddress[] = {0xE8, 0x68, 0xE7, 0x80, 0x5E, 0x11}; // MAC address of the receiver. In my case it is ESP8266
- #define mahtabi1 21
- #define mahtabi2 22
- const int ledPin = 16;
- int buttonPin;//suich esp now
- int currButtonVal = 0, prevButtonVal = 0;
- int brightness = 0;
- bool isDataReceived = false;
- int receivedPotValue = 0;
- typedef struct struct_message { // Structure to send/receive data. Must match the receiver/sender structure
- //char charPlaceHolder[32];
- int intPlaceHolder;
- //float floatPlaceHolder;
- //String stringPlaceHolder;
- //bool boolPlaceHolder;
- } struct_message;
- struct_message myOutGoingData, myIncomingData;
- /////////////////////////
- // Fill-in information from your Blynk Template here
- #define BLYNK_TEMPLATE_ID "TMPLADtP2oUc"
- #define BLYNK_DEVICE_NAME "SZA room blynk"
- #define BLYNK_FIRMWARE_VERSION "0.1.0"
- #define BLYNK_PRINT Serial
- #define BLYNK_DEBUG
- //#define APP_DEBUG
- // Uncomment your board, or configure a custom board in Settings.h
- //#define USE_WROVER_BOARD
- //#define USE_TTGO_T7
- #include "BlynkEdgent.h"
- ////////////////////////////////////////
- BLYNK_WRITE(V0){
- int pinvalue=param.asInt();
- digitalWrite(mahtabi1,pinvalue);
- }
- ///////////////////////////////////////
- BLYNK_WRITE(V2){
- int pinvalue2=param.asInt();
- digitalWrite(mahtabi2,pinvalue2);
- }
- ///////////////////////////////////////////
- BLYNK_WRITE(V3){
- int pinvalue3=param.asInt();
- buttonPin=pinvalue3;
- }
- ///////////////////////////////////////////
- void dama_1(void);
- void setup()
- {
- pinMode(ledPin, OUTPUT);
- pinMode(mahtabi2,OUTPUT);
- pinMode(mahtabi1,OUTPUT);
- Serial.begin(115200);
- delay(100);
- BlynkEdgent.begin();
- //////////////esp now/////////////////
- if (esp_now_init() != ESP_OK) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- esp_now_register_send_cb(OnDataSent); // register the ondatasent callback
- // Register peer
- esp_now_peer_info_t peerInfo;
- memcpy(peerInfo.peer_addr, broadcastAddress, 6);
- peerInfo.channel = 0;
- peerInfo.encrypt = false;
- // Add peer
- if (esp_now_add_peer(&peerInfo) != ESP_OK){
- Serial.println("Failed to add peer");
- return;
- }
- esp_now_register_recv_cb(OnDataRecv); // Register for a callback function that will be called when data is received
- // Setup timer and attach timer to a led pin
- ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
- ledcAttachPin(ledPin, LEDC_CHANNEL_0);
- //////////////////////////////////////
- }
- void loop() {
- BlynkEdgent.run();
- dama_1();
- espcon1();
- }
- //sensor void
- void dama_1() {
- sensors.requestTemperatures();
- Blynk.virtualWrite(V1,sensors.getTempCByIndex(0));
- }
- // Callback when data is sent
- void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
- Serial.print(status == ESP_NOW_SEND_SUCCESS ? "Packet sent successfully with value: " : "Packet sending failed with values: ");
- Serial.println(currButtonVal);
- }
- // Callback when data is received
- void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
- memcpy(&myIncomingData, incomingData, sizeof(myIncomingData)); // copying the incoming data into our structure
- receivedPotValue = myIncomingData.intPlaceHolder; // Here I recieve potentiometer value from ESP8266 in the range [0, 1023]
- receivedPotValue = receivedPotValue < 0 ? 0 : receivedPotValue > 1023 ? 1023 : receivedPotValue; // making sure the raneg [0, 1023]
- isDataReceived = true;
- }
- // Arduino like analogWrite
- // value has to be between 0 and valueMax
- void IMHESP_analogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 255) {
- uint32_t duty = (8191 / valueMax) * min(value, valueMax); // calculate duty, 8191 from 2 ^ 13 - 1
- ledcWrite(channel, duty); // write duty to LEDC
- }
- /////////esp now loop//////////
- void espcon1(){
- currButtonVal = buttonPin ;
- if(currButtonVal != prevButtonVal) {
- prevButtonVal = currButtonVal;
- myOutGoingData.intPlaceHolder = currButtonVal;
- esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myOutGoingData, sizeof(myOutGoingData));
- }
- if(isDataReceived) {
- //isDataReceived = false;
- Serial.print("[RECEIVED]>> ");
- Serial.println(receivedPotValue);
- Serial.println("fhsdh412572542453257cgsdhsdh");
- //receivedPotValue = receivedPotValue <= 15 ? 0 : receivedPotValue; // Just making all less values to zero
- //brightness = int(map(receivedPotValue, 0, 1023, 0, 255));
- //IMHESP_analogWrite(LEDC_CHANNEL_0, brightness);
- Blynk.virtualWrite(V4,receivedPotValue);
- }
- delay(40);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement