The website shows the current state of the room, to be updated at a given time interval (every 5 seconds in this case). In the Python version of the code (the first code segment) the microcontroller is hooked into a host computer. The host computer has an API called PYserial installed (http://pyserial.sourceforge.net/). This allows you to read from the serial port, or in our case to read from a USB to serial converter, which is recognized automatically. What the program does is reads from the serial port every five seconds. It reads a string in the format ”/ int int int int int int\n\r”. Each of the ints represents the state of one of the sensors in the order Lights, Left Window, Right Window, Door, IR Sensors, and Vibration sensor. Then, using this information, the program creates a simple HTML webpage which can be viewed at http://ieee.udarknet.com.
Thanks to afterburn for help putting the finishing touches on the python code below. If you have any questions about the code, feel free to contact CattyKid
#!/usr/bin/python #Copyright 2010 - Eric McGinnis, Yingbo Wang, Stephen Janansky # import string, sys, serial, time, datetime #First we want to read from the port. #Initialize the port and set the baud rate s = serial.Serial('/dev/ttyUSB0',9600,timeout=20) while 1: s.flushInput() time.sleep(5) line = s.readline() line = s.readline() line = ' / 1 0 1 1 0 1\n\r' output = open('/var/www/index.html','w') output.write("<html>\n\ <title>Current Status of the IEEE Lounge</title>\n") output.write("<head>\n") output.write("<script>\n\ var limit=\"0:05\"\n\ if (document.images){\n\ var parselimit=limit.split(\":\")\n\ parselimit=parselimit[0]*60+parselimit[1]*1\n\ }\n\ function beginrefresh(){\n\ if (!document.images)\n\ return\n\ if (parselimit==1)\n\ window.location.reload()\n\ else{\n\ parselimit-=1\n\ curmin=Math.floor(parselimit/60)\n\ cursec=parselimit%60\n\ if (curmin!=0)\n\ curtime=curmin+\" minutes and \"+cursec+\" seconds left until page refresh!\"\n\ else\n\ curtime=cursec+\" seconds left until page refresh!\"\n\ window.status=curtime\n\ setTimeout(\"beginrefresh()\",1000)\n\ }\n\ }\n\ window.onload=beginrefresh\n\ </script>"); output.write("</head>\n<body>\n <h1>\n\ Current Status of the IEEE Lounge\n\ </h1>\n") if len(line)==0: output.write("Sorry! The sensor is NOT working! :( Please send help...<br><br></body>") else: output.write("<table border=\"2\">\n\ <tr>\n") if line[3] == '1': output.write("<td BGCOLOR=\"#00ff00\">The Lights are ON.") else: output.write("<td BGCOLOR=\"#ff0000\">The Lights are OFF.") output.write("</td>\n") if line[5] == '1': output.write("<td BGCOLOR=\"#00ff00\">The Left Window is OPEN.") else: output.write("<td BGCOLOR=\"#ff0000\">The Left Window is CLOSED.") output.write("</td>\n") if line[7] == '1': output.write("<td BGCOLOR=\"#00ff00\">The Right Window is OPEN.") else: output.write("<td BGCOLOR=\"#ff0000\">The Right Window is CLOSED.") output.write("</tr></td>\n") if line[9] == '1': output.write("<tr><td BGCOLOR=\"#00ff00\">The Door is OPEN.") else: output.write("<td BGCOLOR=\"#ff0000\">The Lights Door is CLOSED.") output.write("</td>\n") if line[11] == '1': output.write("<td BGCOLOR=\"#00ff00\">The IR Sensors detect motion.") else: output.write("<td BGCOLOR=\"#ff0000\">The IR Sensors do not detect motion.") output.write("</td>\n") if line[13] == '1': output.write("<td BGCOLOR=\"#00ff00\">The Table is SHAKING, someone is doing work!") else: output.write("<td BGCOLOR=\"#ff0000\">The Table is NOT SHAKING.") output.write("</tr></td>\n\ </body>\n\ <h4>\n") currentTime = datetime.datetime.now() output.write("These statistics were last updated ") output.write(currentTime.ctime()) output.write(" EST"); output.write("</html>\n") output.close()
The C code is included below for reference. Using is it not recommended since the Python code is a simpler (even for someone with little to no python experience) and more reliable with less system overhead.
In the C version, Minicom runs on the host computer. The C program below runs on the host computer. Minicom dumps info received over serial from the microcontroller into a text file. The C program reads this text file information by opening the file and an input stream, opens an output stream file index.html, then outputs to it. The simple website is completely re-written by the C program every time the program runs.
#include <iostream> #include <fstream> #include <ctime> #include <stdio.h> using namespace std; int main(void) { ofstream output; char buffer[6]; ifstream input; input.open("serial.txt"); input >> buffer[0]; for (int i=0; i < 6; i++){ input >> buffer[i]; cout << buffer[i] << ' '; } cout << endl; input.close(); output.open("statusfile.html"); output << "<html>\n\ <h1>\n\ This is out page\n\ </h1>\n\ <body>\n\ <table border=\"2\">\n\ <tr>\n\ <td>The Lights are..."; if (buffer[0] == '1') output << "ON"; else output << "OFF"; output << "</td>\n\ <td>The Left window is..."; if (buffer[1] == '1') output << "OPEN"; else output << "CLOSED"; output << "</td>\n\ <td>The Right Window is..."; if (buffer[2] == '1') output << "OPEN"; else output << "CLOSED"; output << "</td>\n\ <td>The Door is..."; if (buffer[3] == '1') output << "OPEN"; else output << "CLOSED"; output << "</td>\n\ <td>The IR sensors are..."; if (buffer[4] == '1') output << "ACTIVATED"; else output << "IDLE"; output <<"</td>\n\ <td>The Table is..."; if (buffer[5] == '1') output << "SHAKING"; else output << "STEADY"; output << "</td>\n\ </body>\n\ <h4>\n\ These statistics were last updated: "; time_t now; time(&now); output << ctime(&now); output << "</html>\n"; output.close(); return 1; }