Pages

Wednesday, March 1, 2017

Back to Basic - Basic language on the ESP8266


When I was a young guy of about 17 years old (we are talking 1976) I bought my first computer which was basically a bag of chips with a calculator keyboard and a calculator-like display. It was based on a RCA 1802 microprocessor and could only be programmed in machine language (thats even lower as assembly).



Later I moved on to the Commodore PET which was the predecessor of the Commodore 64. Just look at that beauty. At that time computers still looked like computers !! The Pet (personal Electronic Transactor) could be programmed in Basic which was a huge leap forward. I did marvels in Basic. I wrote a simulator for an assembly language that was ported to a Burroughs Mainframe and was used at the HTS in Eindhoven for study purposes. I wrote a database program (Elektronische Kaartenbak), a spreadsheet program (Volderscalc) and a complete accounting package (VOLAD) in Basic and all this on my faithfull PET with only 32K of memory.

And now in the age of Arduino and ESP-8266 I had to learn a whole bunch of new languages: C++, Lua, Python and HTML.

Basic as a language is nowadays looked upon as inferior by many programmers. The reason for this is that Basic has Goto statements. This makes it possible to jump from one part of the program to another part at any instance. To many programmers this kills structured programming and makes it possible to code like spaghetti.

So I was totally pleased when suddenly there was a Basic for the ESP-8266. And if I may say so not an ordinary Basic but a real splendid one. ESP8266-Basic has so many features build in that it is possible to read a thermometer sensor and display the values on a webpage in just 2 lines of code: amazing.

So lets do it: lets program in Basic on the famous ESP8266.

Installing ESP8266-Basic.

Before we can start programming in Basic we need to install the language on the ESP-8266 itself. Well that's a piece of cake. First download the ESP Flasher from this page:
https://www.esp8266basic.com/download.html

Put the Flasher in a sub-directory on your computer and start it.
The Flasher indicates the Com-Port on which it found the ESP8266 and you will have to tell it the amount of memory the ESP8266 has. For the ESP8266-01 choose 512K or 1M. For the NodeMCU and its varieties types use 4M.


Next choose Flash Firmware and the led on the ESP8266 will start to blink.





On screen you will see the flashing progres and after a while flashing is ready and the info on your screen will look as the picture above.

Wireles Station or stand-alone.

When Basic has been flashed into the ESP we can start using it.
At first ESP8266-Basic is not connected to your router because (off course) it does not know your routers nae and password to get access.

So ESP8266-Basic has to be connected to as an Access Point.

Start Wifi on your phone or tablet and look for the accesspoints. One will be called ESP followed by some number (which is actually the MAC adress of your ESP8266). Activate this accesspoint on your device and make sure it is connected to your phone or tablet.

For properly using ESP-8266 Basic you will need to use Firefox or Chrome as browser on your Android device.


Now start the browser and surf to 192.168.4.1 and you will see the start screen of ESP-8266 Basic.

As you can see I am using ESP8266-Basic 3.0 Alpha 51.
Now I am experiencing a small problem with this version and that is that not all menu items are directly available. So I found a way around this. Just follow the steps below and you're all safe.

First choose the menu item EDIT.

Edit starts with a program called default.bas which is not available in the ESP-8266. So we will make our first program. In the program area just type the first program line:

print "hello"

Next step is to click on Save. and that's it. You wrote your first program. Which is at the same time the workaround. All Menu items are available now.


Now if you click on Run Basic will run the program which just prints the word hello on your screen.

That's it.

The other menu functions

As you have seen there are several menu items available.

VARS
will give you a list of used variables and all the ESP pin states !!

EDIT
is used to edit programs like we just did above

RUN
will start the last Edited and saved program

DEBUG
is used to help you debug any faults in your program with a pause function, showing you which line is executed and at the same time showing a list of variables.






FILE MANAGER
is just what it suggests. You can browse your computer or Android device for files and upload them to the ESP. And you can choose in the file list the program you want to Edit, Delete or Rename. When choosing a program and choosing Edit you will be directed to the Edit screen.
The Filemanager menu was only available after I wrote and saved my default.bas program





SETTINGS
Settings is an important item. In settings you can put the name of your Access point and Password so you can access ESP8266-Basic from any device (computer, laptop or Android device) in your network. As I find my phones screen to small for daily programming I program ESP8266-Basic from my computer.
The Settings menu was only accessible after I wrote and saved my default.bas program.


Another important setting is wether ESP-Basic has to start the default.bas program directly at starting up. With this setting your program will automatically run each time the ESP is powered up.

Access from within your network.




After filling in your Accesspoints name and password in the SETTINGS page ESP-Basic will come up in your router as a new device.



Look in your router for the IP adress for the ESP and start a new window with the IP-adress as URL. So now we are ready to go.

Now why would I program the ESP from my desktop machine. Well it makes it easy to open multiple windows, search for Basic programs copy them and paste them in the ESP8266-BASIC EDIT window. Makes programming very easy.

Dallas 18b20




The Dallas 18b20 is the ideal thermometer. It has a wide range of measurement from -55 degress Celsius to +125 degrees Celsius. Its accuracy is 0.5 degrees Celsius and it is waterproof. So theoretically you can put it in aboiling kettle and measure the temperature. I have seen tinkerers putting one in a freezer to check the temperature. As it is waterproof you can use it for measuring outside temperature.

It uses a specific communiaction protocol and communicates over just 1 wire. There is a lot to tell about this wonder-thermometer but I'll save that for a rainy day.

Most important is that it easily communicates with ESP8266-Basic.

Measuring temperature with the Dallas 18b20

There is a ton on information about the Dallas 18b20 on the internet. And we do not need it at all for using it with ESP8266-Basic. You just have to remember one thing and that is that the Dallas 18b20 dataline NEEDS to be attached to D4 (GPIO2) on the NodeMCU board.


[EDIT] Oops !!! Just saw a mistake on the breadboard layout down here. I forgot to attach the 5 volt lead from the NodeMCU to the breadboard.So put a small lead from Vin (bottom right) to the VCC line.




Now that is done we need to write our Basic program. And here comes the neat part. We only need two lines of code:



So let us look at the program:

temnow = temp(0)
wprint temnow


First we read the Dallas thermometer with the command temp(0) and put it in the variable temnow
the second line puts the variable on the screen.

That's it.



The above picture is the result when we run this program.
The temperature with an accuracy of 4 decimals in just 2 lines of code.

Now this accuracy is fine but I want just 1 decimal for better readaility. Well here we go:



temnow = temp(0)
temnow = int(temnow*10)/10
wprint temnow


Save the program and run it.




The second line in our program multiplies the temnow variable with 10 and makes an integer from it. Next it is divided by 10 again and there we have our value with just 1 decimal.

A nice looking webpage

Now look at the next picture of a webpage.



It is the same temperature reading but now in a nice web-page with a background color, a header for the page info. The temperature reading in a contrasting color and a bar that indicates wether the temperature is lowering or rising. And 2 buttons of which one takes another reading and the other one stops this program.

Man, this must be complicated to program......... NOT

 memclear  
 cls  
 temnow = temp(0)  
 temnow = int(temnow * 10)  
 temnow = temnow / 10  
 wprint "<!DOCTYPE html>"   
 wprint "<html> <body>"  
 wprint |<body style="background-color:powderblue;">|  
 wprint |<H1><span style="color: red;">|  
 wprint "Thermometer"  
 wprint "</H1>"  
 wprint "</span>"  
 wprint |<H2><span style="color: blue;">|  
 wprint "Temperature = "  
 wprint temnow  
 wprint "</span>"  
 wprint "</H2>"  
 meter temnow, 0, 40  
 wprint "<br>"  
 wprint "<br>"  
 button "Again", [again]  
 wprint "............"  
 button "Stop", [quit]  
 timer 1000, [refresh]  
 wait  
 [refresh]  
 temnow = temp(0)  
 Wait  
 [again]  
 timer 0  
 run  
 wait  
 [quit]  
 end  


Just look at this source code. Most of it is HTML commands for formatting the page layout.
Putting a button on your web-page is done by the command:

button "Again", [again]

and the program just jumps to the label [again] if you press that button.

I think you can figure out the rest of the program for yourselves as it is dead-easy.

Neopixels

Remember the Neopixels. Those charming RGB leds for which you need just 1 dataline to control hundreds of them. Well programming them in ESP8266-Basic is a piece of cake.

But before you start using the Neopixels with ESP8266-Basic I urge you to read the story I wrote about the neopixels. You can switch to that story in a new tab by clicking here.
http://lucstechblog.blogspot.nl/2015/10/neopixels-ws2812-intro.html

 



First set up your breadboard like the above picture and connect the Neopixel data line to the NodeMCU D8 line. ESP8266-Basic uses this pin as its default pin for accessing the neopixels. You can alter this with the neo.setup() command. If you want to connect the neopixels to D7 for example you start the ptogram with the next line:

neo.setup(d7)

In the example below I used 7 Neopixels.

Now type in the editor the next program and save it:




neo.cls()

for x=0 to 6
neo(x,0,0,255,0)
delay 1000
next x

The Neopixel command neo() uses the following syntax:
the first parameter (x in this case) is the number of the neopixel in the chain starting at 0
The next 3 figures (0,0,255) represent the RGB code which is in this case blue
The last figure 0 in this case tells Basic to immediately change the color.

At first the Neopixels will be off. Then one by one they will light up in blue until they all display blue and the program stops.

Anybody who is familiar with programming will appreciate how easy it is to do amazing things with ESP-Basic.

IFTTT

A lot of automation can be done with IFTTT (If This Then That). IFTTT is much used by ESP-8266 enthousiasts as it easily can be used to trigger all kinds of events for IOT (Internet of Things) projects. One of the upcoming stories is how to use IFTTT to send a Twitter message if it rains using my rain-detector described in the story you can read by clicking here.

Adressing IFTTT can be a real pain in some of the programming languages.

In ESP8266-Basic it is just a few lines of code:

iftttkey = "XXXYYYZZZXXXAA"
trig = "maker.ifttt.com/trigger/givenotice/with/key/" & iftttkey
print wget(trig)

And that really is all !!!!



Making an API call has never been easier in any programming language (as far as I know).


Final Words

ESP8266-Basic is really easy to work with and well documented.


Essentially it is Basic with loads of built in libraries. This makes sure that we can focus on the actual programming and not on putting all kinds of code together for just getting some sensors to work.....

The above shown examples are just a very small part of what ESP8266-Basic is capable off. I haven't discussed Telnet, Serial communications, Timers, File I/O, using the ESP I/O pins, Oled-LCD and TFT screen drivers, web-interface commands, email, web-graphics, mathematic and string functions, i2c, SPI, UDP and even Infrared control.

I sincerely thank the maker MMISCOOL for bringing such a great product to the ESP8266. And I am certainly going to use ESP8266-Basic in my future projects.

You can find the official ESP8266-Basic website here: https://www.esp8266basic.com/
The official webpage also contains examples and the complete manual :  https://www.esp8266basic.com/language-reference.html
And you can find a ton of information, projects and examples  on the ESP8266-Basic Forum: http://www.esp8266.com/viewforum.php?f=38

So till next time
Have fun

Luc Volders