#!/usr/bin/env python
# -*- coding: utf-8 -*-

#import libraries
import serial
from time import sleep

try:

  # declare variables
  port = 'COM5'
  baud = 9600
  
  # open serial connection
  ser = serial.Serial(port=port, baudrate=baud,timeout=3)
  
  # wait for the Arduino to restart
  sleep(2)
  
  # send command 
  ser.write("s")
  
  # read 1 character from the serial port
  comment = ser.read(1)
  reply="No connection"
  if comment =='1':
    reply = 'Switch closed'
  if comment =='2':
    reply = 'Switch open'
  
  # close the serial port
  ser.close()

except:
  reply="Fatal error"


  
# show the HTML page
print "Content-Type: text/html"
print """
<head>
<title>Read switch over the internet</title>
<meta name="viewport" content="width=200" content="initial-scale=1">
</head>
<html>
<body> 
%s
</body>
</html>
""" % reply
