Morning All,
I'm struggling, I'm tryiung to build a remote control using 2 off ESP32's, communicating using the "Now" protocol with the input being a 1x4 membrane matrix pad and the output to 4 off relays.
I can see the correct "data" being received but I suspect I'm not handling the data types properly but I cant get my head round that 🤔
Any help would be appreciated.
TX Code
#include <Arduino.h> #include <Keypad.h> #include <esp_now.h> #include <WiFi.h> uint8_t broadcastAddress1[] = {0x7c,0x9e,0xbd,0x92,0x0E,0x0C};// Replace with the receivers MAC address const byte ROWS = 1; // 1 row const byte COLS = 4; // 4 columns //Keyboard mapping char hexaKeys[ROWS][COLS]= { {'1','2','3','4'},// single row 4 buttor strip }; byte rowPins[ROWS]={12}; byte colPins[COLS]={26,25,14,27}; //Column pins //Int keypad Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); typedef struct test_struct { char y; } test_struct; test_struct example; // callback when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status ) { char macStr[18]; Serial.print("Packet to: "); // Copies the sender mac address to a string snprintf(macStr, sizeof(macStr),"%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1],mac_addr[2], mac_addr[3],mac_addr[4], mac_addr[5]); Serial.print(macStr); Serial.print(" send status:\t"); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success": "Delivery Fail"); } void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); if (esp_now_init()!= ESP_OK) { Serial.println("Error initialising ESP-NOW"); return; } esp_now_register_send_cb(OnDataSent); // register peer esp_now_peer_info_t peerInfo; peerInfo.channel=0; peerInfo.encrypt=false; // register first peer memcpy(peerInfo.peer_addr, broadcastAddress1,6); if (esp_now_add_peer(&peerInfo) != ESP_OK) { Serial.println ("Failed to add peer"); return; } } void loop() { //Read the pushed button char button = customKeypad.getKey(); if (button) { Serial.println(button); example.y = button; Serial.print("example.y: "); Serial.println(example.y); esp_err_t result = esp_now_send(0, (uint8_t *) &example, sizeof(test_struct)); delay(500); } }
RX Code
#include <Arduino.h> #include <esp_now.h> #include <WiFi.h> // Structure example to receive data // Must match the sender structure!!! #define pwr_rly 18 #define patt_sel 19 #define ctl_1 20 #define ctl_2 21 char control1; typedef struct test_struct { char y; } test_struct; // Create a struct_message called example test_struct example; // what you call the structured message is how you will refer to the variables, // for example example.x or example.y //callback function that will be executed when data is received, replaces the usual loop function void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) { memcpy(&example, incomingData, sizeof(test_struct)); Serial.print("Bytes received "); Serial.println(len); Serial.print("button: "); Serial.println(example.y); relay_control(); } void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); pinMode(pwr_rly, OUTPUT); pinMode(patt_sel, OUTPUT); pinMode(ctl_1, OUTPUT); pinMode(ctl_2, OUTPUT); digitalWrite(pwr_rly, LOW); digitalWrite(patt_sel, LOW); digitalWrite(ctl_1, LOW); digitalWrite(ctl_2, LOW); //Init ESP-NOW if (esp_now_init() != ESP_OK) { Serial.println("Error initialising ESP-NOW"); return; } // Once ESP-Now is successfully Init'd, we will register for receive CB to // get recv packet info esp_now_register_recv_cb(OnDataRecv); } void loop() { } void relay_control() { control1 = example.y; Serial.print("control1: "); Serial.println(control1); if (control1 = "1"); //POWER ON/OFF //digitalWrite(pwr_rly, LOW); { Serial.println("#1 match"); if (pwr_rly == HIGH); { digitalWrite(pwr_rly, LOW); } digitalWrite(pwr_rly, HIGH); } }
Bob
From the sound of it, you are transmitting and receiving the value correctly but the magic that's supposed to happen when that value is used just isn't working.
I don't know if this is your problem, but in the bottom section of code you have
void relay_control() { control1 = example.y; Serial.print("control1: "); Serial.println(control1); if (control1 = "1") ; //POWER ON/OFF //digitalWrite(pwr_rly, LOW); { Serial.println("#1 match"); if (pwr_rly == HIGH) ; { digitalWrite(pwr_rly, LOW); } digitalWrite(pwr_rly, HIGH); }
1) Wherever you have placed a semicolon after the if (...) it will terminate the if.
2) control = "1" is an assignment of value "1" to control1, you need to write this as control1=="1" to make it a test of the value of control1 being equal to "1". You can prevent this kind of error in the future by writing the test as "1"==control1. In that case, if you accidentally type "1"=control1 then the compiler will flag it as an error because you cannot assign a new value to a constant.
So you should change that subroutine to something like
void relay_control() { control1 = example.y; Serial.print("control1: "); Serial.println(control1); if (control1 == "1") { Serial.println("#1 match"); if (pwr_rly == HIGH) digitalWrite(pwr_rly, LOW); else digitalWrite(pwr_rly, HIGH); } }
And see if that works.
Anything seems possible when you don't know what you're talking about.
- Evening Will,
- I'll give that a try asap and report on the results.
Thanks for the help.
Bob
Evening Will,
Sorry for the delay, weathers here's been cold, wet and windy so jobs have been taking 3 times as long as they should....
Anyway,
Just modified the code as suggested but the verify fails with
ISO C++ forbids comparison between pointer and integer [-fpermissive]
against the line
if (control1 == "1")
Any thoughts??
Bob
Evening Will,
Just modified the code as suggested but the verify fails with
ISO C++ forbids comparison between pointer and integer [-fpermissive]
against the line
if (control1 == "1")
Any thoughts??
Bob
If the print shows the value as 1, then it's probably because it's trying to compare a char to a String.
So change the =="1" to =='1'. The single quotes will tell the compiler that this is a character (8 bit integer) instead of a String (class object) and hopefully the test will. be acceptable.
By the way, when you reply to a post on the forum, click the reply button in the lower right side of the message. This will create an empty posting with @<whoever you're replying to> at the top. When you post the message, the forum will then send that person an email notifying them of your reply. If you don't click reply or start your posting with the @<somebodt> then no email is sent and you have to wait and hope that they eventually notice your reply to them. That could take minutes, hours, days or last until the Heat Death of the universe depending on their interest level 🙂
Anything seems possible when you don't know what you're talking about.
@will Thanks for the help Will.
All noted and its now working. I changed the data type to "int" and the Relay control looks like this now...
void relay_control() { control1 = example.y; Serial.print("control1: "); Serial.println(control1); if (control1 == '1') { Serial.println("#1 match"); Serial.println (pwr); if (pwr == 0) { digitalWrite(pwr_rly, HIGH); pwr = 1; //Serial.println ("High"); } else { digitalWrite(pwr_rly, LOW); //Serial.println ("Low"); pwr = 0; } } if (control1 == '2') { Serial.println("#2 match"); } if (control1 == '3') { Serial.println("#3 match"); } if (control1 == '4') { Serial.println("#4 match"); }
This now gives me the Power On/Off on Button 1, I just need to get #2 to give me a pulsed output.....
Thanks Again
Bob
Excellent ! Thanks for letting us know it's fixed.
Anything seems possible when you don't know what you're talking about.
Just a thought. Last ESP now I set the mac address of all the chips in the sketch. What a huge improvement. You don't have to scan each chip in use. If a chip gets damaged you don't have to scan a new and reprogram all chips talking to it, you just program in the original address.
There does not seem any restrictions in the address, so b, o, b, w, 0, 1 ( in hex ) gives you 99 easily remembered address and in fact I wrote an macaddress.h file with them all in, just include it.
@hilldweller I hope one of us is misunderstanding. A MAC is burned into the device at the factory, each device has a UNIQUE address. Part of the address identifies the manufacturer. By changing it if in fact you are, that means there are now 2 devices in the world that can send and receive data on the same address. It may never be a problem, but in a worst case situation you can cause serious service disruptions to an innocent party. I don't know what the legal situation is, but if I had to bet I imagine it is a federal. As an end user, you are supposed to manage the IP address to control access to your devices not the MAC. I think there is now a 'local' option that I am unfamiliar with but may do what you want. I hope that is in fact what you are doing and have just used the wrong terminology. The normal way to manage device addressability is in fact one level higher so ronsIPcamera.ronsNetwork.net is at IP 192.168.100.129 which has a MAC of (bunch of numbers). If you burn out the camera and replace it, the name ronsIPcamera does NOT change, it is still mapped to 192.168.100.129 BUT that is now mapped to a new MAC address via a method I am not totally familiar with but I think involves something called ARP
Here is WiKi link to MAC MAC
and here is WiKi link to MAC spoofing MAC Spoofing
here is link to ARP NOTE that this is automatic
Good Luck
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
@hilldweller I hope one of us is misunderstanding. A MAC is burned into the device at the factory, each device has a UNIQUE address. Part of the address identifies the manufacturer.
Good Luck
The ESP has provision for the programmer to change it's MAC address. It's as simple as that. If the consequences were so dire I guess they would not do that. You may have missed the point this is referring to esp-now which is for local control.
@hilldweller Good, now I need to study up on ESP-NOW. I was worried the FBI were about to show up but it sounds like it is under control. 😉
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
@hilldweller Good, now I need to study up on ESP-NOW. I was worried the FBI were about to show up but it sounds like it is under control. 😉
My very first use of it was when I moved house 5 years ago. I needed some extra lights and didn't want to be digging into walls. So 6 ESP8266 and I can control 3 lights from the car, all wall touch control ( with a Nextion ) and for 5 years it's been rock solid. Reset only by power cuts. This was before I stumbled on changing mac address so a bit of extra programming was involved because all the slaves need to know the appropriate macaddress.
I've got it running a segway style robot. It's got very little overhead to slow things down.
@hilldweller I am curious why @dronebot-workshop didn't specify the Local bit when setting his MAC?
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
@hilldweller Sounds like a very handy protocol. I have more than a dozen boards, so I will be experimenting for sure.
First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.
@hilldweller I am curious why @dronebot-workshop didn't specify the Local bit when setting his MAC?
Well I stumbled across it only this year. At first I thought "why is he/she doing this" then I saw the attraction.