
def read_sensor():
  global temp, hum
  temp = hum = 0
  try:
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
      msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum))
      # temperature to Fahrenheit: temp = temp * (9/5) + 32.0
      # calibrate temperature
      temp = temp - 4.0
      hum = round(hum, 2)
      return(msg)
    else:
      return('Invalid sensor readings.')
  except OSError as e:
    return('Failed to read sensor.')

def web_page():
  html = """{
   "sensor1":
      {
         "temperature":"""+str(temp)+""",
         "humidity":"""+str(hum)+"""
      }
}"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  print('Content = %s' % str(request))
  sensor_readings = read_sensor()
  print(sensor_readings)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: application/json\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  conn.close()


def read_sensor():
  global temp, hum
  temp = hum = 0
  try:
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    if (isinstance(temp, float) and isinstance(hum, float)) or (isinstance(temp, int) and isinstance(hum, int)):
      msg = (b'{0:3.1f},{1:3.1f}'.format(temp, hum))
      # temperature to Fahrenheit: temp = temp * (9/5) + 32.0
      # calibrate temperature
      temp = temp - 4.0
      hum = round(hum, 2)
      return(msg)
    else:
      return('Invalid sensor readings.')
  except OSError as e:
    return('Failed to read sensor.')

def web_page():
  html = """{
   "sensor1":
      {
         "temperature":"""+str(temp)+""",
         "humidity":"""+str(hum)+"""
      }
}"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  print('Content = %s' % str(request))
  sensor_readings = read_sensor()
  print(sensor_readings)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: application/json\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  conn.close()

# temperature to Fahrenheit: temp = temp * (9/5) + 32.0 # calibrate temperature {    "sensor1":       {          "temperature": ,          "humidity": } }