Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of huzzah_helloWorld by
Diff: main.cpp
- Revision:
- 1:e4753022f351
- Parent:
- 0:57cec3469a80
- Child:
- 2:f01835b2e4f7
--- a/main.cpp Thu Oct 22 16:18:21 2015 +0000
+++ b/main.cpp Fri Mar 18 05:13:25 2016 +0000
@@ -1,25 +1,100 @@
#include "mbed.h"
-
+#include <string>
+#include <sstream>
+#include <stdlib.h>
+
Serial pc(USBTX, USBRX);
Serial esp(p28, p27); // tx, rx
DigitalOut reset(p26);
DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
DigitalOut led4(LED4);
+DigitalIn pb(p10);
+PwmOut light(p21);
+
+Ticker alarmChecker;
+bool alarmFired = false;
+int alarmHour = 23;
+int alarmMin = 49;
+int alarmDur = 1;
+string data;
Timer t;
+time_t rtc;
+struct tm *tp;
+
int count,ended,timeout;
char buf[2024];
char snd[1024];
+char val[2];
-char ssid[32] = "SwagInABag"; // enter WiFi router ssid inside the quotes
-char pwd [32] = "yoloswag"; // enter WiFi router password inside the quotes
+char ssid[32] = "ssid"; // enter WiFi router ssid inside the quotes
+char pwd [32] = "password"; // enter WiFi router password inside the quotes
-void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
+void SendCMD(),getreply(),ESPhttp(),ESPsetbaudrate(), sendConfig(), ESPreset(), sendNTPFunction(), updateNTP(), getUtime(), startSunrise(float d);
+
+void alarmCheck(){
+ rtc = time(NULL);
+ tp = localtime(&rtc);
+ led1=1;
+ if(!alarmFired){
+ printf("hr: %d min:%d\r\n", tp->tm_hour, tp->tm_min);
+ int daystamp = tp->tm_hour * 3600 + tp->tm_min * 60;
+ int alarmstamp = alarmHour * 3600 + alarmMin * 60 - (alarmDur * 60);
+ pc.printf("%d - %d = %d", alarmstamp, daystamp, alarmstamp - daystamp);
+ if( (alarmstamp - daystamp) <= 0){
+ // time to start the wake up sequence
+ startSunrise((float)(alarmDur*60));
+ alarmFired = true;
+ }
+ } else{
+ if(tp->tm_hour == 0)
+ alarmFired = false;
+ }
+ wait(1);
+ led1=0;
+
+}
+
+void flush(){
+ while(esp.readable()) {
+ char tmp = esp.getc();
+ data += tmp;
+ pc.putc(tmp);
+ }
+}
void dev_recv()
{
led1 = !led1;
+ /*
while(esp.readable()) {
- pc.putc(esp.getc());
+ char tmp = esp.getc();
+ data += tmp;
+ pc.putc(tmp);
+ }
+ */
+ timeout=2;
+ getreply();
+ pc.printf("%s\n\n", buf);
+ char * location = strstr(buf, "GET /?");
+ if(location != NULL){
+ //pc.printf("%d\n", location - buf);
+ char * hour = strstr(buf, "h=");
+ char * min = strstr(buf, "m=");
+ char * dur = strstr(buf, "d=");
+ if(hour != NULL && min != NULL && dur != NULL){
+ strncpy(val, hour+2,2);
+ alarmHour = atoi(val) %24;
+ strncpy(val, min+2,2);
+ alarmMin = atoi(val) %60;
+ strncpy(val, dur+2,2);
+ alarmDur = atoi(val);
+
+ pc.printf("New config: hrs %d min %d duration %d", alarmHour, alarmMin, alarmDur);
+ sendConfig();
+
+ }
}
}
@@ -34,103 +109,94 @@
int main()
{
+
+ pb.mode(PullDown);
+ light.period(0.005); //200Hz to eliminate flicker
reset=0; //hardware reset for 8266
- pc.baud(9600); // set what you want here depending on your terminal program speed
+ pc.baud(115200); // set what you want here depending on your terminal program speed
pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
wait(0.5);
reset=1;
- timeout=2;
+ timeout=1;
getreply();
-
- esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time.
+
+ // RESET wifi module and and connect to the wifi
+ ESPreset();
+
+ // send the NTP function to wifi module
+ sendNTPFunction();
+
+
+
+
+ while(tp->tm_year ==0){
+ updateNTP();
+ wait(3);
+ getUtime();
+ wait(1);
+ }
+
+ led3=1;
+
+ for(float i=0.0;i<1.0;i+=0.1){
+ light=i;
+ wait(0.5);
+ }
+ light = 0;
- //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ******************
-
- ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
+ //ESPreset();
-
-
- pc.attach(&pc_recv, Serial::RxIrq);
+ ESPhttp();
+
+ //pc.attach(&pc_recv, Serial::RxIrq);
esp.attach(&dev_recv, Serial::RxIrq);
- // continuosly get AP list and IP
+ rtc = time(NULL);
+ pc.printf("%s\r\n", ctime(&rtc));
+ led1=0;led2=0;led3=0;led4=0;
+ tp = localtime(&rtc);
+ printf("hr: %d min:%d\r\n", tp->tm_hour, tp->tm_min);
+ alarmChecker.attach(&alarmCheck, 60);
while(1) {
sleep();
}
}
+
+void startSunrise(float d){
+ for(float i=0.0; i < 1.0; i += (1.0/d)){
+ light = i;
+ pc.printf("%f\n", i);
+ wait(1);
+ }
+ light = 1.0;
+
+ wait(120);
+ light = 0;
+
+}
// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
void ESPsetbaudrate()
{
- strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
+ strcpy(snd, "uart.setup(0, 9600, 8, 0, 1, 0)\r\n"); // change the numeric value to the required baudrate
SendCMD();
+ timeout=2;
+ getreply();
+ pc.printf(buf);
}
-
-// +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
-void ESPconfig()
-{
- wait(5);
- pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
- strcpy(snd,".\r\n.\r\n");
- SendCMD();
- wait(1);
+void ESPreset(){
pc.printf("---------- Reset & get Firmware ----------\r\n");
strcpy(snd,"node.restart()\r\n");
SendCMD();
- timeout=5;
- getreply();
- pc.printf(buf);
-
- wait(2);
-
- pc.printf("\n---------- Get Version ----------\r\n");
- strcpy(snd,"print(node.info())\r\n");
- SendCMD();
- timeout=4;
- getreply();
- pc.printf(buf);
-
- wait(3);
-
- // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
- pc.printf("\n---------- Setting Mode ----------\r\n");
- strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
- SendCMD();
- timeout=4;
+ timeout=2;
getreply();
pc.printf(buf);
-
- wait(2);
-
-
-
- pc.printf("\n---------- Listing Access Points ----------\r\n");
- strcpy(snd, "function listap(t)\r\n");
- SendCMD();
- wait(1);
- strcpy(snd, "for k,v in pairs(t) do\r\n");
- SendCMD();
- wait(1);
- strcpy(snd, "print(k..\" : \"..v)\r\n");
- SendCMD();
- wait(1);
- strcpy(snd, "end\r\n");
- SendCMD();
- wait(1);
- strcpy(snd, "end\r\n");
- SendCMD();
- wait(1);
- strcpy(snd, "wifi.sta.getap(listap)\r\n");
- SendCMD();
- wait(1);
- timeout=15;
- getreply();
- pc.printf(buf);
-
- wait(2);
-
+
+ ESPsetbaudrate();
+
+
pc.printf("\n---------- Connecting to AP ----------\r\n");
pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
strcpy(snd, "wifi.sta.config(\"");
@@ -139,7 +205,7 @@
strcat(snd, pwd);
strcat(snd, "\")\r\n");
SendCMD();
- timeout=10;
+ timeout=3;
getreply();
pc.printf(buf);
@@ -148,7 +214,7 @@
pc.printf("\n---------- Get IP's ----------\r\n");
strcpy(snd, "print(wifi.sta.getip())\r\n");
SendCMD();
- timeout=3;
+ timeout=2;
getreply();
pc.printf(buf);
@@ -156,19 +222,24 @@
pc.printf("\n---------- Get Connection Status ----------\r\n");
strcpy(snd, "print(wifi.sta.status())\r\n");
+ pc.printf("%s", snd);
SendCMD();
- timeout=5;
+ timeout=2;
getreply();
pc.printf(buf);
+
+
+}
- pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
- pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
- pc.printf(" It saves the SSID and password settings internally\r\n");
- wait(10);
-
-
+// +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
+void ESPhttp()
+{
+
pc.printf("\n---------- Setting up http server ----------\r\n");
- strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
+
+ sendConfig();
+
+ strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
SendCMD();
wait(1);
strcpy(snd, "srv:listen(80,function(conn)\r\n");
@@ -177,10 +248,12 @@
strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
SendCMD();
wait(1);
+
strcpy(snd, "print(payload)\r\n");
SendCMD();
wait(1);
+
strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
SendCMD();
wait(1);
@@ -189,11 +262,31 @@
SendCMD();
wait(1);
- strcpy(snd, "conn:send(\"<h1> Hi James, NodeMcu.</h1>\")\r\n");
- SendCMD();
+ strcpy(snd, "conn:send(\"<h1>Configure Sunrise Alarm</h1><br><br><form method='get'>Wake up at: <input name=h value='\")\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "conn:send(hour)\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "conn:send(\"'> Hours and <input name=m value='\")\r\n");
+ SendCMD();
wait(1);
- strcpy(snd, "conn:send(\"<h2> test</h2>\")\r\n");
+ strcpy(snd, "conn:send(min)\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "conn:send(\"'> minutes <br> <br> Start sunrise <input name=d value='\")\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "conn:send(dur)\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "conn:send(\"'> minutes before <input type=submit value='submit'></form>\")\r\n");
SendCMD();
wait(1);
@@ -210,15 +303,15 @@
wait(1);
strcpy(snd, "end)\r\n");
SendCMD();
- wait(1);
- timeout=17;
- getreply();
- pc.printf(buf);
- pc.printf("\r\nDONE");
+
+ flush();
+
+ pc.printf("\r\nDONE\r\n");
}
void SendCMD()
{
+ pc.printf("%s", snd);
esp.printf("%s", snd);
}
@@ -241,4 +334,128 @@
}
}
-
\ No newline at end of file
+void sendConfig()
+{
+ stringstream strs;
+ stringstream strs1;
+ stringstream strs2;
+
+ strcpy(snd, "hour=");
+ SendCMD();
+ strs << alarmHour;
+ string temp_str = strs.str();
+ char const* pchar = temp_str.c_str();
+ strcpy(snd, pchar);
+ SendCMD();
+ strcpy(snd, "\r\n");
+ SendCMD();
+ wait(1);
+
+
+ strcpy(snd, "min=");
+ SendCMD();
+ strs1 << alarmMin;
+ temp_str = strs1.str();
+ pchar = temp_str.c_str();
+ strcpy(snd, pchar);
+ SendCMD();
+ strcpy(snd, "\r\n");
+ SendCMD();
+ wait(1);
+
+
+
+ strcpy(snd, "dur=");
+ SendCMD();
+ strs2 << alarmDur;
+ temp_str = strs2.str();
+ pchar = temp_str.c_str();
+ strcpy(snd, pchar);
+ SendCMD();
+ strcpy(snd, "\r\n");
+ SendCMD();
+ wait(1);
+}
+
+void sendNTPFunction()
+{
+ wait(15);
+ strcpy(snd, "timezone = -4\r\n");
+ SendCMD();
+ wait(2);
+ strcpy(snd, "request=string.char(227,0,6,236,0,0,0,0,0,0,0,0,49,78,49,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\r\n");
+ SendCMD();
+ wait(4);
+
+ strcpy(snd, "time = 0\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "sk=net.createConnection(net.UDP, 0)\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "sk:on(\"receive\", function(sck, payload)\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "print(\"YAY!\")\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "local highw,loww,ntpstamp\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "highw = payload:byte(41) * 256 + payload:byte(42)\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "loww = payload:byte(43) * 256 + payload:byte(44)\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "ntpstamp=( highw * 65536 + loww ) + ( timezone* 3600)\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "time = ntpstamp - 1104494400 - 1104494400\r\n");
+ SendCMD();
+ wait(1);
+ strcpy(snd, "print(time)\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "sck:close()\r\n");
+ SendCMD();
+ wait(1);
+
+ strcpy(snd, "end )\r\n");
+ SendCMD();
+ wait(1);
+
+ flush();
+ wait(15);
+ //updateNTP();
+}
+void updateNTP(){
+ flush();
+ strcpy(snd, "sk:connect(123,\"104.232.3.3\")\r\n");
+ SendCMD();
+ wait(3);
+ strcpy(snd, "sk:send(request)\r\n");
+ SendCMD();
+ wait(4);
+ timeout=3;
+ getreply();
+ pc.printf(buf);
+ //getUtime();
+}
+
+void getUtime(){
+ flush();
+ strcpy(snd, "print(time)\r\n");
+ SendCMD();
+ wait(1);
+ timeout=5;
+ getreply();
+ pc.printf(buf);
+ int utime = atoi(buf);
+ if(utime != NULL)
+ set_time(utime);
+ time_t seconds = time(NULL);
+ pc.printf("Time as a basic string = %s", ctime(&seconds));
+}
\ No newline at end of file
