[SOLVED] ESP8266 - NOOB trying to compare 2 variables - pointers&data confusion

Guys,

for my smart home project, i have setup my little nodejs server which communicates with the clients over socket.io

the clients can turn on/off virtual switches on the server which in turn emits the message to a raspberrypi which opens/closes some relays.

I am trying to replicate the same system with an ESP8266 (it is a relay module, but in reality i only need to program the ESP) so that i can go wireless

the server emits a message which is a json
(sorry if the formatting is a bit messy and probably not ideal, can't really change it now without changing my nodejs server... :wink: )

["serveremit",{"Client":"192.168.xxx.yyy"},{"Index":"1"},{"Link":"LIGHTS"},{"Item":"LR Ceiling Light"},{"Status":true},{}]

and i want the ESP to receive the message and process it

I want to check whether if myIndex and root_2_Index are a match (and I will need to do the same with myItem and root_4_Item)
if they are it means that the ESP knows the message is for himself and not for one of the other ESP's around the house)

if i Serial.print the variables and i get the same output on the monitor

unfortunately if i run a check like "if (myIndex == root_2_Index)" i never get a positive answer

the variables in question are:

myIndex and myItem (the ones which are coming from WiFiManagerParameter, the fantastic library from Tzapu)
and
root_2_Index and root_4_Item which are the ones i am receiving from my nodejs server (imported through the ArduinoJson and WebSocketsClient libraries)

I tried to change a bit the code, i suspect my issue is that there is a mismatch with the variable types and probably I am using pointers instead of variable.
But my background is mechanical and i am struggling with the underlying principles.

Could i kindly ask to take a look at the code and let me have some "pointers" (pun not intended) to where i should go to fix the issue? I don't mind studying it a bit more deeply, but i guess i am too far to even understand what the issue might be at the moment...

thanks a lot

M

8266-blink-02.ino (7.35 KB)

Classic issue. You will need to use strcmp to compare myIndex to root_2_Index.

Thanks Wildbill,

i did try a couple of permutations with strcmp but i never made it to make it work

for instance, right now i just added:

  int testindex = strcmp(myIndex,root_2_Index);
  Serial.println("strcmp testindex: ");
  Serial.println(testindex);

but when the ESP gets the message the screen spits out a bunch of nonsense to me (see below) and then crashes.
I saw that strings are taking a lot of memory, could this be an issue?

thanks a lot
M

crash:

3ffffd30:  00000000 00000000 4bc6a7f0 3ffef66d

3ffffd40:  00000000 3ffef604 3ffffda0 40214769

3ffffd50:  00000000 401047b7 3ffed6a8 401000ff

3ffffd60:  00000002 3ffef604 3ffffda0 402144e8

3ffffd70:  00004505 00000002 3ffee17c 40102280

3ffffd80:  3ffea560 00000000 0000000c 00000000

3ffffd90:  3ffef5e0 00000002 3ffef604 4021456f

3ffffda0:  3fff06b4 7fffffff 40213d28 40214750

3ffffdb0:  3ffef66b 00000002 00040000 40101508

3ffffdc0:  3ffea560 0000002d 00000000 0000009f

3ffffdd0:  3ffef604 3ffef5e0 3ffef604 40214729

3ffffde0:  40102464 3fffc200 00000022 3fffc278

3ffffdf0:  4000dd24 00000030 00000010 ffffffff

3ffffe00:  4000066d 010d9582 00418937 0000009f

3ffffe10:  3ffef604 3ffef604 3ffef5e0 40214749

3ffffe20:  000000b6 3fffc6fc 5022b0ee 40215449

3ffffe30:  00418937 004fcbff 3ffefa88 00000030

3ffffe40:  00004bc6 00000000 00000000 fffffffe

3ffffe50:  00000000 3fffc6fc 00000000 3ffef738

3ffffe60:  00000000 3fff0b4c 3ffefa88 00000030

3ffffe70:  00000000 00000000 00000000 fffffffe

That looks like a fragment of a crash dump (Not that I've ever used any of the ESP range).

What do you get from Serial print before the crash?

this is what i get:

if it can be of any help "serverreloadlinks" is another event being emitted on the socket.io but it is not the one carrying the Index/Item information
the event that is carrying the information is "serveremit"

thanks a lot
M

[IOc] get event: ["serverreloadlinks","reload"]
myIndex NOT = root 2 Index

myIndex NOT = root 4 Item


--------------- CUT HERE FOR EXCEPTION DECODER ---------------


Exception (28):

epc1=0x4000bdcb epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000



>>>stack>>>



ctx: cont

sp: 3ffff930 end: 3fffffc0 offset: 0190.....

That's the reason for the crash. Your processmessage function assumes it's going to get your serveremit JSON data. When it gets serverreloadlinks instead, it tries to access things like doc[2] that don't exist.

You need to check what you have before you access anything in doc.

SUPER SUPER THANKS

it works

I am guessing that receving the two server events at the same time caused the issue

I added an IF statement which basically ignored the messages not meant to be analysed and now it works

i really don't know how to thank you... thanks

M

  int testroot = strcmp("serveremit",root_0);
  Serial.println("strcmp testroot :");
  Serial.println(testroot);
  
  if (testroot == 0) {
	  Serial.println("strcmp testroot OK");
	  
	  int testindex = strcmp((char*)myIndex,(char*)root_2_Index);
	  Serial.println("strcmp testindex :");
	  Serial.println(testindex);

	  int testitem = strcmp((char*)myItem,(char*)root_4_Item);
	  Serial.println("strcmp testitem :");
	  Serial.println(testitem);
	  
	  if ((testindex == 0) && (testitem == 0)) {
		Serial.println("process signal");
	  } 
	  else {
		Serial.println("message not for us");
	  }

  } 
  else if (testroot != 0) {
	Serial.println("strcmp testroot OK");
  }

nikolai_carnaghi:
I am guessing that receving the two server events at the same time caused the issue

I don't think so. It was simply that the same code was called for both payloads and that code doesn't handle "serverreloadlinks" properly.

thanks a lot... much appreciated

M

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.