def listen(self, ip=None, port=8005): do_connect(self.ssid, self.password, ip) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Pick a port for port forwarding purposes...using Google wifi.... s.bind(('', port)) s.listen(1) counter = 0 while True: conn, addr = s.accept() print('Got connection #{} from {}' .format(counter, addr)) counter += 1 request = conn.recv(1024) request = str(request) # Commands for controlling actuator. open_door = request.find('open') close_door = request.find('close') exit_listen = request.find('exit') hello_listen = request.find('hello') stop_listen = request.find('stop') if (hello_listen != -1 and hello_listen < 10): self._send_response(conn, 'hello') elif (open_door != -1 and open_door < 10): self._setMovementTime(request) self._turn_pin_off_just_in_case(CLOSE_PIN) self._do_actuator(OPEN_PIN) self._send_response( conn, 'opening door for ' + str(self.amount_of_movement_ms) + ' ms') elif (close_door != -1 and close_door < 10): self._setMovementTime(request) self._turn_pin_off_just_in_case(OPEN_PIN) self._do_actuator(CLOSE_PIN) self._send_response( conn, 'closing door for '+str(self.amount_of_movement_ms) + ' ms') elif (stop_listen != -1 and stop_listen < 10): self.timer.deinit() self._turn_pin_off_just_in_case(OPEN_PIN) self._turn_pin_off_just_in_case(CLOSE_PIN) self._send_response(conn, 'stopped') elif (exit_listen != -1 and exit_listen < 10): self.timer.deinit() self._turn_pin_off_just_in_case(OPEN_PIN) self._turn_pin_off_just_in_case(CLOSE_PIN) conn.close() print('received a request to exit, buh-bye') break else: self._send_response( conn, 'The command received was not valid.') conn.close() s.close()
def send_reading(self, v1, v2, i1, i2, power): do_connect(self.ssid, self.password) # .sv timestamp: http://bit.ly/2MO0XNt #data = '{'+'"V1":{},"V2":{},"I1":{},"I2":{},"P":{},".sv":"timestamp"'.format(v1,v2,i1,i2,power) +'}' data = '{' + '"V1":{},"V2":{},"I1":{},"I2":{},"P":{}'.format( v1, v2, i1, i2, power) + ',"timestamp": {".sv":"timestamp"}}' print(data) path = 'https://iot-test-1e426.firebaseio.com/' + self.device_name + '/' + self.userID + '/.json' print(path) response = requests.post(path, data=data) print('response: {}'.format(response.text))
import mqtt_connect as mqtt from mcp3008 import MCP3008 # Enter Wifi SSID and PW SSID = 'CPGVentures' PW = 'Soulshine2019' # Enter time interval in seconds for publishing data to broker publish_time = 30 # Test sensors before connecting to WIFI and Broker prph.led_blink(10, 500) # 10 x 0.5s Blink on startup mcp = MCP3008() tc_water, tf_water = prph.tmp36(mcp.read(0)) print('Temp Celsius = {}c'.format(tc_water)) print('Temp Fahrenheit = {}f'.format(tf_water)) prph.redled(True) t2320, h2320 = prph.am2320(4) print('AM2320 Temp = {}c'.format(t2320)) print('AM2320 Humidity = {}%'.format(h2320)) while wifi.do_connect(SSID, PW): # Pass in the SSID and PW try: mqtt.broker_connect(publish_time, tc_water) except OSError: print('Broker connection lost going to Deep Sleep mode') prph.deepsleep(60) prph.led_blink(20, 250) # 20 x 0.25s fast Blink if no connection print('Going to Deep Sleep mode') prph.deepsleep(60)
import loader from wb_test import * import wifi_connect import utime wifi_connect.do_connect() from wb_publish import *
import sys import machine import network import ws2812 as ws2812 import wifi_connect as wifi_connect #wifi Wifi_info = (["www.qiwen.cn","miao13006331630"],["WHDC","whdc5325"],["MKNET","83842911"]) Mywifi = [Wifi_info[2][0], Wifi_info[2][1]] #Pin Led_PIN = 2 Led_NUM = 30 try: wifi_connect.do_connect(Mywifi[0],Mywifi[1]) ws2812.init(data_pin=Led_PIN, leds_num=Led_NUM) ws2812.demo4() except: wifi_connect.wifioff()
import network from wifi_connect import do_connect wlan_sta = network.WLAN(network.STA_IF) wlan_sta.disconnect() do_connect("happyday", "poi098lkj")
import wifi_connect as wifi import simple_socket as sock import gc with open('ssid.info', 'r') as f: wifi_name = f.readline().split('\n')[0] wifi_pwd = f.readline().split('\n')[0] wifi.do_connect(wifi_name, wifi_pwd) def main(): # pass while True: sock.listen() gc.collect() if __name__ == '__main__': main()
def listen(self, port): do_connect(self.ssid, self.password) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('', port)) s.listen(1) counter = 0 while True: conn, addr = s.accept() print('Got connection #{} from {}'.format(counter, addr)) counter += 1 request = conn.recv(1024) request = str(request) # Figure out if the URL is for controlling valve(s) start_watering = request.find('water_on') stop_watering = request.find('water_off') valve_off = request.find('valve_off') exit_listen = request.find('exit') watering_time = request.find('water_time') hello_listen = request.find('hello') # The check is a follow up to the request.find()... if (hello_listen != -1 and hello_listen < 10): # Command sent to start watering. valve_str = self._get_valve_str() min_str = "minute" if self.watering_mins == 1 else "minutes" return_str = 'Hello - the watering time is set to {} {}. \n Valves: {}.'.format( self.watering_mins, min_str, valve_str) self._send_response(conn, return_str) if (start_watering != -1 and start_watering < 10): # Command sent to start watering. self._send_response(conn, 'start watering') self._cycle_through_valves() elif (stop_watering != -1 and stop_watering < 10): # Command sent to stop watering. self._send_response(conn, 'stop watering') # THere's only one valve on at a time. self._turn_off_valve() elif (valve_off != -1 and valve_off < 10): # See if the valve string is in the dictionary of valves. return_str = "" key = self._remove_valve(request[valve_off:]) if len(key) != 0: return_str = "Will not turn the water on for the {} valve ".format( key) else: return_str = "Not a valid valve name." valve_str = self._get_valve_str() return_str += '\n Valves that are on include {}'.format( valve_str) self._send_response(conn, return_str) elif (watering_time != -1 and watering_time < 10): # The first part of the request has been truncated. I send # the request string at the location where the string # water_time starts. is_watering_time_set = self._set_watering_time( request[watering_time:]) return_str = "" if (is_watering_time_set): min_str = "minute" if self.watering_mins == 1 else "minutes" return_str = "Changing the watering time to {} {}.".format( self.watering_mins, min_str) else: return_str = "The watering time must be between {} minutes and {} minutes. The watering time is still {} minutes.".format( MIN_WATERING_TIME, MAX_WATERING_TIME, self.watering_mins) self._send_response(conn, return_str) elif (exit_listen != -1 and exit_listen < 10): self._turn_off_valve() conn.close() print('received a request to exit, buh-bye') break else: print('received packet. Was not a water request') conn.close() s.close()