Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By JMS
#36964 Because turning lights on and off is the "hello world" of hardware :) I built a device for the purpose of learning ESPbasic as well as creating something functional.

So I think I am getting my head wrapped around esp8266 basic. I wonder if someone can confirm that my assumptions below are correct (or incorrect).

1. All variables are global
2. All labels are global i.e. you can gosub any [label] no matter which script file you are in
3. The "default" script always runs at start up
4. Currently there is no way to list script files through the web interface
5. Currently there is no way to read the status of an output pin
6. Comments in code start with " ' "
7. When a user goes to the index url they are presented with the last state of the "canvas" the script is not re-run? i.e. just loading http://mylocalesp82266 does nothing other than displaying the screen buffer?

Here is my "Hello World" The hardware device is a an outlet with a relay attached to pin 14 of the esp8266 it is designed to turn my Chrsitmas tree on and off.

Script File: default

Code: Select all'//stat  is the tracking variable for the output pin state it is set here because on power on pin 14  will always be in a known state and from there we track the variable every time we change states
let stat = 0
let pin = 14
load outlet '//load outlet file


Script File: outlet
Code: Select all
[main]
if stat = 1 then goto [ison] '//  if it's on display the lit Christmas tree image with a turn off button
if stat = 0 then goto [isoff]'//if it is off display the unlit Christmas tree image with a turn on button
let stat = 0
goto [main]
wait

[isoff] '//Display the unlit Christmas tree image with a turn on button
cls
'//The wprint below  makes the image zoom to fill the mobile screen size
wprint "<meta name='viewport' content='width=device-width, initial-scale=2'>"
image "treeoff.png"
wprint "<br>"
button "Turn on" [turnon]
wait

[ison] '// Display the lit Christmas tree image with a turn off button
cls
wprint "<meta name='viewport' content='width=device-width, initial-scale=2'>"
image "treeon.png"
wprint "<br>"
button "Turn off" [turnoff]
wait

[turnon] '//turn it on
po pin 1
let stat = 1
goto [main]

[turnoff]  '//turn it off
let stat = 0
po pin 0
goto [main]


Image Files:
Tree off
Image
Tree on
Image

I know this code is a bit crude any comments or suggestions would be greatly appreciated.

Thanks,
J
User avatar
By Mmiscool
#36995 This is very nice and straight to the point.

Branch labels can on;y be called from the loaded program.

Variables are all global.

And yes. The html buffer is send upon requesting a page from the esp.

And I am glad to see a Christmas project. Would be nice to post a video and some pics of your circuit.
User avatar
By JMS
#37010 Here is the basic schematic:

Image

I had an enclosure very similar to this one laying around. The original relay guts for this device had been long ago removed giving ample room for the 5v relay and a 5v power supply. The esp8266 is mounted externally on the rear of the device. I mounted a perfboard with female headers to the rear of the device and all connections are made to the perfboard so the esp can be easily "unplugged". The enclosure still had the mains plug on the back and the mains receptacle on the front making for a very clean build.
Image

Crestron makes a bunch of boxes with a similar form factor if you can find any on ebay listed as broken or non functioning they make great project boxes for interfacing with mains power. In this particular device the original relay was 24v and it expected to have an external 24v DC source. By integrating a 5v power supply into the device I was able to make it completely self contained. I do plan to add a momentary push button to allow relay state change locally.

-J
User avatar
By Mmiscool
#37018 You can use the flash button on the esp.

It is connected to gpio 0.

It doesn't have any interrupts yet so you will have to use a timer to check. Mabe every 5 seconds for push button press.