diff --git a/lopy/main.py b/lopy/main.py index 14bdb0a..a298d6e 100644 --- a/lopy/main.py +++ b/lopy/main.py @@ -1,13 +1,18 @@ import pycom import machine +import time import tiny_http from network import WLAN -SSID = "SSID" -PASS = "PASS" - pycom.heartbeat(False) +SSID = "created-iot-workshop" +PASS = "created2018" +IP = "10.0.0.254" +PORT = 5000 +ENDPOINT = "/putldrdata/" +CHANNEL = 1 + wlan = WLAN(mode=WLAN.STA) print("MAIN: WLAN Initialised") @@ -20,4 +25,18 @@ print("Connected to network") # Example tiny_http usage -# print(tiny_http.get_request("192.168.0.10", 5000, "/")) +x = tiny_http.TinySocket(IP, PORT) +x.connect() +x.send_request("/") +print(x.get_response()) +x.close() + +adc = machine.ADC() +read_pin = adc.channel(pin="P16") + +while True: + x.connect() + x.send_request(ENDPOINT + str(CHANNEL) + "/" + str(read_pin())) + print(x.get_response()) + x.close() + time.sleep(1) diff --git a/lopy/tiny_http.py b/lopy/tiny_http.py index d223703..1fc0911 100644 --- a/lopy/tiny_http.py +++ b/lopy/tiny_http.py @@ -14,27 +14,39 @@ def parse_return_data(data_b): newline_count = 0 return data_b[start:] -def get_request(host, port, endpoint): - """ Sends a GET request to host:port/endpoint and returns the body - of the response. Please note, endpoint should begin with a slash "/" - """ - # create a socket and connect to the server - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) - s.settimeout(5) # seconds - try: - s.connect(socket.getaddrinfo(host, port)[0][-1]) - except OSError: - print("Caught OSError trying to connect to server, are you sure the server is up?") - print("Connected to server") - s.send(bytes( - "GET {} HTTP/1.1\nHost: {}:{}\n\n".format(endpoint, - host, - port), - "utf-8")) - total = b"" - data = None - while data != b"": - data = s.recv(100) - total += data - s.close() - return parse_return_data(total) +class TinySocket(): + def __init__(self, host, port): + self.host = host + self.port = port + + def connect(self): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) + self.sock.settimeout(5) # seconds + + try: + #s.connect(socket.getaddrinfo(host, port)[0][-1]) + self.sock.connect((self.host, self.port)) + except OSError: + print("Caught OSError trying to connect to server, are you sure the server is up?") + + def send_request(self, endpoint): + """ Sends a GET request to host:port/endpoint and returns the body + of the response. Please note, endpoint should begin with a slash "/" + """ + data = bytes("GET {} HTTP/1.1\nHost: {}:{}\n\n".format + (endpoint, self.host, self.port), "utf-8") + + self.sock.send(data) + + def get_response(self): + total = b"" + data = None + + while data != b"": + data = self.sock.recv(128) + total += data + + return parse_return_data(total) + + def close(self): + self.sock.close() diff --git a/server/main.py b/server/main.py index ae7f4ca..6104e69 100644 --- a/server/main.py +++ b/server/main.py @@ -1,8 +1,11 @@ import time -from flask import Flask +import json +from flask import Flask, render_template, send_from_directory + +num_channels = 20 app = Flask(__name__) -ldr_data = [] +ldr_data = [[] for i in range(num_channels)] @app.route('/') def hello_world(): @@ -11,16 +14,29 @@ def hello_world(): """ return "hello" -@app.route('/putldrdata/') -def put_ldr_data(data): +@app.route('/putldrdata//') +def put_ldr_data(channel, data): """ Stores the LDR value with the current time """ - ldr_data.append((time.time(), data)) - return "OK" + ldr_data[channel].append((time.time(), data)) + return "OK, channel: " + str(channel) + " data: " + str(data) -@app.route('/getldrdata') -def get_ldr_data(): +@app.route('/getldrdata/') +def get_ldr_data(channel): """ Returns all LDR data as a string """ - return str(ldr_data) + if channel >= 0 and channel < num_channels: + return str(ldr_data[channel]) + + else: + return "channel " + str(channel) + " out of range" + +@app.route('/logger') +def logger(): + """ Returns html file for logger char """ + return render_template('logger.html') + +@app.route('/logger/') +def send_js(path): + return send_from_directory('templates', path) if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)