Send real time to picaxe with ESP8266

beb101

Senior Member
If you are behind a wireless router, you can use a $3 ESP8266-01 for your RTC. Flash the ESP with the Lua nodeMCU
firmware and upload these two scripts to the ESP:
Code:
-- File name: init.lua
print("set up wifi mode")
wifi.setmode(wifi.STATIONAP)
wifi.sta.config("ret13x","XXXXXXXXX")
 --here SSID and PassWord should be modified according your wireless router
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function() 
    if wifi.sta.getip()== nil then 
    	print("IP unavaiable, Waiting...") 
    else 
    	tmr.stop(1)
    	print("Config done, IP is "..wifi.sta.getip())
    	--dofile("yourfile.lua")
    end 
 end)

-- File name: daytime.lua
-- tested on NodeMCU 0.9.5 build 20150126
-- Queries NIST DayTime server for a date time string
-- Fixed format returned:
-- JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM. 
-- Ex: 57053 15-01-31 20:14:43 00 0 0 703.0 UTC(NIST) *
--   JJJJJ - the modified Julian Date ( MJD is the Julian Date minus 2400000.5)
--   YR-MO-DA - the Date
--   HH:MM:SS - the Time 
--   TT -  USA is on Standard Time (ST) or (DST) : 00 (ST)
--   L - Leap second at the end of the month (0: no, 1: +1, 2: -1)
--   H - Health of the server (0: healthy, >0: errors)
--   msADV - time code advanced in ms for network delays
--   UTC(NIST) - the originator of this time code
--   OTM - on-time marker : * (time correct on arival)

print('daytime.lua started')
conn = nil
conn=net.createConnection(net.TCP, 0) 
-- show the server returned payload
conn:on("receive", function(conn, payload) 
			print('\nreceived')
                        print(payload)
                        print('extracted Date and Time')
			print('20'..string.sub(payload,8,14)..
			      string.sub(payload,15,24))
                   end) -- function(conn,payload)
-- show disconnection
conn:on("disconnection", function(conn, payload) print('\nDisconnected') end)
--connect                                   
conn:connect(13,'utcnist2.colorado.edu') -- DayTime protocol sends immediately on connect
You communicate with the ESP via a serial port. init.lua will automatically run on a powercycle or
soft restart and will obtain an IPaddress. The daytime script is executed by issuing the
command: dofile("daytime.lua") which will return the date/time string, for example, 2015-02-28 16:44:07.
So, you send the command and the ESP will respond via the print statement. The NIST server will
lock you out if you try to query it more frequently than every 5 sec; so, not exactly a RTC clock, but close.
Also, the extraneous print statements and comments should probably be removed from the scripts.

I did a forum search for the ESP8266, but didn't find much except for manuka's post,

http://www.picaxeforum.co.uk/showthread.php?26811-ESP8266-WiFi-serial-module

I am using the Lua firmware because it offers so much more than the AT command set and is
very stable. It has the further advantage of off loading much of the grunt processing from the
host MCU and also has a file system for storing scripts. The disadvantage is that you need to learn
a bit of Lua. so, in case anyone is interested in the ESP with Lua here are some links,

nodeMCU forum:
http://www.esp8266.com/viewforum.php?f=17

nodeMCU firmware(see pre_build) :
https://github.com/nodemcu/nodemcu-firmware

API instruction set:
https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en

For development, I use Lua Loader and a PC console TCP client for testing networking:
http://benlo.com/esp8266/index.html#LuaLoader
 

eggdweather

Senior Member
Code:
#include <SoftwareSerial.h>
   #define SSID "xxxxxxxx"
   #define PASS "xxxxxxxx"
   #define DST_IP "220.181.111.85" //baidu.com
   SoftwareSerial dbgSerial(10, 11); // RX, TX
   void setup()
   {
     // Open serial communications and wait for port to open:
     Serial.begin(57600);
     Serial.setTimeout(5000);
     dbgSerial.begin(9600); //can't be faster than 19200 for softserial
     dbgSerial.println("ESP8266 Demo");
     //test if the module is ready
     Serial.println("AT+RST");
     delay(1000);
     if(Serial.find("ready"))
     {
       dbgSerial.println("Module is ready");
     }
     else
     {
       dbgSerial.println("Module have no response.");
       while(1);
     }
     delay(1000);
     //connect to the wifi
     boolean connected=false;
     for(int i=0;i<5;i++)
     {
       if(connectWiFi())
       {
         connected = true;
         break;
       }
     }
     if (!connected){while(1);}
     delay(5000);
     //print the ip addr
     /*Serial.println("AT+CIFSR");
     dbgSerial.println("ip address:");
     while (Serial.available())
     dbgSerial.write(Serial.read());*/
     //set the single connection mode
     Serial.println("AT+CIPMUX=0");
   }
   void loop()
   {
     String cmd = "AT+CIPSTART=\"TCP\",\"";
     cmd += DST_IP;
     cmd += "\",80";
     Serial.println(cmd);
     dbgSerial.println(cmd);
     if(Serial.find("Error")) return;
     cmd = "GET / HTTP/1.0\r\n\r\n";
     Serial.print("AT+CIPSEND=");
     Serial.println(cmd.length());
     if(Serial.find(">"))
     {
       dbgSerial.print(">");
       }else
       {
         Serial.println("AT+CIPCLOSE");
         dbgSerial.println("connect timeout");
         delay(1000);
         return;
       }
       Serial.print(cmd);
       delay(2000);
       //Serial.find("+IPD");
       while (Serial.available())
       {
         char c = Serial.read();
         dbgSerial.write(c);
         if(c=='\r') dbgSerial.print('\n');
       }
       dbgSerial.println("====");
       delay(1000);
     }
     boolean connectWiFi()
     {
       Serial.println("AT+CWMODE=1");
       String cmd="AT+CWJAP=\"";
       cmd+=SSID;
       cmd+="\",\"";
       cmd+=PASS;
       cmd+="\"";
       dbgSerial.println(cmd);
       Serial.println(cmd);
       delay(2000);
       if(Serial.find("OK"))
       {
         dbgSerial.println("OK, Connected to WiFi.");
         return true;
         }else
         {
           dbgSerial.println("Can not connect to the WiFi.");
           return false;
         }
       }
This is an Arduino variant, I believe you should find apart from connect.wifi() status little difficulty in converting it PICAXE basic, all comms to the ESP8266 are straightforward serial commands e.g. "AT" commands You could make assumptions about the connected state thereby bypassing the connect.wifi() status tests.

e.g. Serial.find("ready")) becomes Serin 0, T9600_8,("ready")

At £5 they are quite cheap and enable UDP like transfers to be made.
 
Last edited:

techElder

Well-known member
beb101, your subject line to your OP is "Send real time to picaxe with ESP8266 ".

Where's the beef?

(Quote from an old American hamburger joint [Wendy's] commercial meaning where's the substance to your comment.)
 

beb101

Senior Member
@srnet
You do your Lua development and testing external to the Picaxe using one of the Lua uploaders.
These are just fancy terminal programs that ease development. After the Lua program works and
the script file is saved on the ESP, hookup the ESP to the Picaxe serial port and send the
dofile("yourfile.lua") command to the ESP. Whatever the result of the file execution on
the ESP is can then be sent back to the Picaxe via the Lua print statement. Even an 08m2 can handle
this sort thing (i think). With the ESP-12 module all of the pins are exposed and you can do
some fancy things with practically no burden on the host (see the API command set)

I moved away from the AT firmware because of the extensive string handling necessary .
I wrote an ESP AT driver for a Netduino running the .NET Micro framework and it was very
messy due to the command/response paradigm. With the Lua firmware you essentially have a
programmable serial WiFi bridge. However, the programmable part depends upon learning some Lua.
One added benefit of the latest nodeMCU firmware is double precision floating point support so it could
be used simply as a Picaxe floating point co-processor. I haven't looked into this, but For example,
Code:
a= 133.2334111    <-- send
> b =5776.113      <-- send
> print(a*b)
769571.23788905  <-- sent back

> print(b/a)
43.353337217079
 

beb101

Senior Member
@Texasclodhopper,
Well I guess the substance is that if you send dofile("daytime.lua") from a Picaxe serial port connected to an ESP running nodeMcu firmware with "dytime.lua" saved in its flash memory you will get back the date and time, restricted to sending the command every 5 sec. The time will only be accurate to approx. 5 sec., but what the heck. Hence, a RTC accurate to about 5 sec. The post was really about the ESP8266 with application to getting getting the time whenever you want it as in the other post.

P.S. sorry for the lawyerlike first sentence
 
Top