def test_get_wrong_key(channel_param): if channel_param.access == "public": pytest.skip() ch = Channel(id=channel_param.id, api_key="wrong_key") with pytest.raises(requests.exceptions.HTTPError) as excinfo: ch.get() excinfo.match(r"400 .*")
def test_get_without_key(channel_param): ch = Channel(id=channel_param.id) if channel_param.access == "private": with pytest.raises(requests.exceptions.HTTPError) as excinfo: ch.get() excinfo.match(r"400 .*") else: result = json.loads(ch.get()) assert type(result) == dict
def test_channel(channel_param, servers): ch = Channel(channel_param.id, server_url=servers) assert ch.id == channel_param.id assert ch.api_key is None if servers is None: ch.server_url == "https://api.thingspeak.com" else: assert ch.server_url == servers ch = Channel(channel_param.id, api_key=channel_param.api_key) assert ch.api_key is channel_param.api_key
def test_update(channel_param): ch = Channel(id=channel_param.id, api_key=channel_param.api_key) if channel_param.write: ch.update({"field1": 1}) else: with pytest.raises(requests.exceptions.HTTPError) as excinfo: ch.update({"field1": 1}) excinfo.match(r"400 .*")
def main_loop(self): # Get API keys from file try: with open("api_write_key.txt", "r") as keyfile: write_key = keyfile.read().strip() with open("api_read_key.txt", "r") as keyfile: read_key = keyfile.read().strip() except FileNotFoundError as e: print("Could not open keyfiles.") exit(1) # Get ThingSpeak channel object self.thingspeak_chan = Channel(1222699, write_key=write_key, read_key=read_key) self.server_conn = Connection(self.thingspeak_chan, self.address, "control_server") self.server_conn.established.wait() while True: next_person = False #set up transaction id which keeps track of order of signals. self.tid = 0 badge_id = self.nfc_reader.read_card() handle_badge_tap(badge_id) while next_person == False: data = self.server_conn.recv() try: rsp = Message.from_bytes(data) except: print(f"Received invalid message \"{data}\"") exit(1) if type(rsp) == InformationRequestMessage: handle_information_request(rsp) elif type(rsp) == AccessResponseMessage: next_person = handle_access_response(rsp) elif type(rsp) == DoorStateUpdateMessage: handle_door_state_update(rsp) else: print(f"Received invalid message \"{data}\"") exit(1) self.tid += 1
def test_missing_id(): with pytest.raises(TypeError) as excinfo: Channel() assert "missing 1 required positional argument" in str(excinfo.value)
def test_update_no_key(channel_param): ch = Channel(id=channel_param.id) with pytest.raises(ValueError) as excinfo: ch.update({"field1": 1}) excinfo.match("Missing api_key")
def test_get_with_key(channel_param): ch = Channel(id=channel_param.id, api_key=channel_param.api_key) result = json.loads(ch.get()) assert type(result) == dict
from thingspeak import Channel #Thingspeak stuff from thingspeak import ThingSpeakAPI, ProtoHTTPS #Thingspeak stuff import credentials #For the API_KEY room = 'room' active_channel = room room_temp = 'Temperature' room_humidity = 'RelativeHumidity' room_lum = 'Luminosity' channels = [ Channel(active_channel, credentials.API_KEY, [room_temp, room_humidity, room_lum]) ] thing_speak = ThingSpeakAPI(channels, protocol_class=ProtoHTTPS, log=True) import dht import machine d_pin = dht.DHT11(machine.Pin(5)) #Inialize pin D1 of nodemcu lum_pin = machine.ADC(0) def get_data(): d_pin.measure() lum_voltage = lum_pin.read() * (3.3 / 1024.0) returner = [d_pin.temperature(), d_pin.humidity(), lum_voltage]
from os import getenv from nut2 import PyNUTClient from thingspeak import Channel CHANNEL_ID = int(getenv("THINGSPEAK_CHANNEL_ID")) API_KEY = getenv("THINGSPEAK_API_KEY") UPS_NAME = getenv("NUT_UPS_NAME") channel = Channel(CHANNEL_ID, api_key=API_KEY) client = PyNUTClient() voltage = float(client.get_var(UPS_NAME, 'input.voltage')) print(voltage, channel.update({"field1": voltage}))
from thingspeak import Channel from include.secrets import THINGSPEAK_WRITE_KEYS channel_esp32 = 'Sudhama' channel_esp32_D = 'Sridama' channel_esp32_P = 'Gauranga' active_channel = channel_esp32 field_moisture = 'Moisture' field_temperature = 'Temperature' field_humidity = 'Humidity' channels = [ Channel(channel_esp32, THINGSPEAK_WRITE_KEYS[channel_esp32], [field_moisture, field_temperature, field_humidity]), Channel(channel_esp32_D, THINGSPEAK_WRITE_KEYS[channel_esp32_D], [field_moisture, field_temperature, field_humidity]), Channel(channel_esp32_P, THINGSPEAK_WRITE_KEYS[channel_esp32_P], [field_moisture, field_temperature, field_humidity]) ]