Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By Mmiscool
#50126 There has been a bunch of chatter recently about having a sing server going trough a firewall communicating with devices inside a local network.

I wanted to post a simple example of how to do this using nothing but esp basic for both the "Master" and "Slave" units.

You will have to port forward both port 80 and 81 to the "Master" device. You will also have to change the ip addressed for the TartgetIP and the ones in the listbox to match what you have on your own local network.

This will toggle the led on a node mcu of the slave device using button from the web page of the master.

The code for the slave device includes the capability of po, pwo, pi, pwi, and ai.

For the master unit
Code: Select allTartgetIP = "172.16.0.117"

listbox TartgetIP, "172.16.0.117,172.16.0.118", 5

button "On", [on]
button "Off", [off]
wait

[on]
print wget(TartgetIP & "/msg?pin=2&stat=0&action=po")
wait


[off]
print wget(TartgetIP & "/msg?pin=2&stat=1&action=po")
wait




For the slave
Code: Select allmsgbranch [mybranch]
print "You can send msgs to the esp and do things based on accessing a URL"
wait


[mybranch]
MyReturnMsg = "Not a valid msg received"
pinNo = val(msgget("pin"))
pinStatus = val(msgget("stat"))
pinAction = msgget("action")

if pinAction == "po" then
   io(po,pinNo,pinStatus)
   MyReturnMsg = "good"
end if
if pinAction == "pi" then
   MyReturnMsg = io(pi,pinNo)
end if
if pinAction == "pwo" then
   io(pwo,pinNo,pinStatus)
   MyReturnMsg = "good"
end if
if pinAction == "pwi" then
   MyReturnMsg = io(pwi,pinNo)
end if
if pinAction == "ai" then
   MyReturnMsg = io(ai)
end if
msgreturn MyReturnMsg
wait
User avatar
By forlotto
#50130 This is essentially what I was building when we talked not exactly the same but very similar.

Excellent example!

Thanks one thing I will note the two differences:
1. msgget is a function
2. you are now specifying val.
- also well documented is the use of io(po,pin#,pinval) is a new requirement for v3.x
Different than previous versions, these subtle changes threw my simple mind off course.
Maybe I have the IQ of an ant.
Maybe it is just knowing syntax and knowing how it is the most common bug to face.
It takes a lot for me to assume there is a change without actually knowing it guess and check is a huge time waster without any solid pointers to the difference in syntax if you know what I mean.
User avatar
By forlotto
#50135 I know this may suck but it is the shortest possible version of what I am doing more greater than 20 lines.

Brief explanation:
It essentially spits out web page responses needed for my phone application the html code is just for a black background and green text as well as appropriate spacing and done in a manner that tracks pin status each time an operation is completed. I like to start off as having the variable on in the event that there is a power outage the device turns back on. I am using a fan and a relay for hardware.

Now there have been variations on how to do this through the versions that 1.x you could do it one way but this did not work for 2.x so I found another way in version 3.x I have not found a possible variation.

In any event this is a great feature to lose the ability to display different web pages built with msgbranch extends far beyond my little example given here one can only dream up possibility here it is nearly endless.

Bare with me this is long and I apologize for the length of it! Just humor me please.

I suggest you select this all and paste it to notepad for better reading:

NOTE THE CODE I TRIED TO ADD BOLD TAGS TO With an arrorow <------------------ is the difference between the operation in 1.x and 2.x as far as 3.x I have not gotten it to work correctly could be something dumb I am not aware of in 3.x but if so it is likely not documented well and it may be helpful to have it documented.

Also Code has been created for all 3 versions noting the differences between versions!
Code: Select all
-----------
v1 code
----------

Let 12SX = 1
 msgbranch [mybranch]
cls
    print "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center>SERVER STATUS=200 OK "

Wait
[mybranch]
cls
    MyReturnMsg = "Not a valid msg received"
    msgget "pin" pinNo
    msgget "stat" pinStatus
    msgget "action" pinAction
    if pinAction == "po" then gosub [pomypin]
    if pinAction == "s" then gosub [readmyit]
    cls
    print "SERVER: 200 STATUS OK </body></center></html>"
    msgreturn myreturnmsg
    wait
  [pomypin]
cls
po pinNo pinStatus
if pinNo == "12" then 12SX = pinStatus [b]'<------------- This line tracks variables fine in 1.x versions!!!![/b]
if pinNo == "12" then OL = "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center><p>.</p>FAN1"
myreturnmsg = ""
if pinStatus = 1 then myreturnmsg = OL & ": was turned OFF successfully </body></center></html>" else MyReturnMsg = OL & ":

was turned ON successfully</body></center></html>"
return
[readmyit]
cls
let myreturnmsg = ""
myreturnmsg = myreturnmsg & "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center>"
myreturnmsg = myreturnmsg & ".:: FAN1 Status ::.<p></p><p></p>"
if 12SX = 1 then myreturnmsg = myreturnmsg & "<p>UNIT: ON</p></body></center></html>" else myreturnmsg = myreturnmsg &

"<p>UNIT: OFF</p></body></center></html>"
return

-----------
Version 2 Code you could no longer use this line of code to track things beyond the first tracking: if pinNo == "12" then 12SX =

pinStatus
So below is the workaround code!
------------
Let 12SX = 1
 msgbranch [mybranch]
cls
    print "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center>SERVER STATUS=200 OK "

Wait
[mybranch]
cls
   myretunmsg = "Not a valid msg received"
    msgget "pin" pinNo
    msgget "stat" pinStatus
    msgget "action" pinAction
    if pinAction == "po" then gosub [pomypin]
    if pinAction == "s" then gosub [readmyit]
    cls
    print "SERVER: 200 STATUS OK </body></center></html>"
    msgreturn myreturnmsg
    wait
  [pomypin]
cls
po pinNo pinStatus
if pinno == "12" then 12SX = io(laststat,12) [b]'<----------------This line fixed something that changed in the ability to consistantly track [/b]

variables!!!!!
if pinNo == "12" then OL = "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center><p>.</p>FAN1"
myreturnmsg = ""
if pinStatus = 1 then myreturnmsg = OL & ": was turned OFF successfully </body></center></html>" else myretunmsg = OL & ": was

turned ON successfully</body></center></html>"
return
[readmyit]
cls
let myreturnmsg = ""
myreturnmsg = myreturnmsg & "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center>"
myreturnmsg = myreturnmsg & ".:: FAN1 Status ::.<p></p><p></p>"
if 12SX = 1 then myreturnmsg = myreturnmsg & "<p>UNIT: ON</p></body></center></html>" else myreturnmsg = myreturnmsg &

"<p>UNIT: OFF</p></body></center></html>"
return


------------
V3 Code DONT WORK at all even using your latest example I cannot build web page response from code possibly it is my own error

IDK but this is why I post and keep trying!
------------
let 12SX = 1
io(po,12,1)
msgbranch [mybranch]
cls
wprint "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center>SERVER STATUS=200 OK "
wait

[mybranch]
cls
pinno = val(msgget("pin"))
pinstatus = val(msgget("stat"))
pinaction = msgget("action")
if pinaction == "po" then gosub [pomypin]
if pinaction == "s" then gosub [readmyit]
cls
wprint "Sever: 200 OK </body></center></html>"
msgreturn myreturnmsg
    wait

[pomypin]
cls
io(po,pinno,pinstatus)
if pinno == "12" then 12SX = io(laststat,12)
if pinno == "12" then OL = "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center><p>.</p>FAN1"
let myreturnmsg = ""
if 12SX = 1 then myreturnmsg = OL & ": was turned ON successfully </body></center></html>" else myreturnmsg = OL & ": was

turned OFF successfully</body></center></html>"
return

[readmyit]
cls
let myreturnmsg = ""
myreturnmsg = myreturnmsg & "<html><head></head><text><body bgcolor=000000 text=C6ED2C><center>"
myreturnmsg = myreturnmsg & ".:: FAN1 Status ::.<p></p><p></p>"
if 12SX = 1 then myreturnmsg = myreturnmsg & "<p>UNIT: ON</p></body></center></html>" else myreturnmsg = myreturnmsg &

"<p>UNIT: OFF</p></body></center></html>"
return

'These are the Commands I use

'-----------------------------------------------------
'Commands Below
'-----------------------------------------------------
'On1
'http://LOCAL IP/msg?pin=12&stat=1&action=po This displays a page showing Fan turned on successfully
'Off1
'http://LOCAL IP/msg?pin=12&stat=0&action=po This displays a page showing Fan turned off successfully
'Status
'http://LOCAL IP/msg?action=s This displays a page showing if the fan is OFF or ON AKA status page.



Now if you take the time to notice there have been larger differences in how msgbranch works than you think and without appropriate testing of the versions and each of these codes you will not likely be able to distinguish the difference.

The bummer is these were not my actual working codes but I made them in a fashion to hopefully resemble as closely as to what I was doing to shorten things up substantially and I hope that in the process I did not mistype something. If I did my apologies! But I tried to make them as simplistic as possible green text black background full html page responses for each action and pin tracking.

I hope this helps I know it is a huge thing to ask but I really need to confirm things are changing and it is not just me causing problems I've tried to get version 3.x to work and it gives me No Message Branch Defined or something along that line.