Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Latest changes with Simon #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions lopy/main.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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)
60 changes: 36 additions & 24 deletions lopy/tiny_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
34 changes: 25 additions & 9 deletions server/main.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -11,16 +14,29 @@ def hello_world():
"""
return "hello"

@app.route('/putldrdata/<int:data>')
def put_ldr_data(data):
@app.route('/putldrdata/<int:channel>/<int:data>')
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/<int:channel>')
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/<path:path>')
def send_js(path):
return send_from_directory('templates', path)

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)