Example #1
0
    def handle_read(self):
        try:
            data = self.recv(1024)
            if not data:
                return

            lf_char_index = data.find('\n')

            if lf_char_index == -1:
                # No new line character in data, so we append all.
                self.data += data
            else:
                # We see a new line character in data, so append rest and handle.
                self.data += data[:lf_char_index]
                print "received [%s]" % self.data

                self.send(self.data + '\n')

                # Clear the buffer
                self.data = ""
        except Exception as e:
            BTError.print_error(handler=self,
                                error=BTError.ERR_READ,
                                error_message=repr(e))
            self.data = ""
            self.handle_close()
Example #2
0
    def handle_read(self):
        try:
            data = self.recv(1024)
            if not data: return
            self.data = data

        except Exception as e:
            BTError.print_error(handler=self,
                                error=BTError.ERR_READ,
                                message=repr(e))
            self.data = ""
            self.handle_close()
Example #3
0
    def handle_read(self):

        try:
            data = self.recv(1024)
            if not data:
                return

        except Exception as e:
            print "[Connection Lost]",
            BTError.print_error(handler=self,
                                error=BTError.ERR_READ,
                                error_message=repr(e))
            self.handle_close()
Example #4
0
    def handle_read(self):
        try:
            data = self.recv(1024)
            if not data:
                return

            lf_char_index = data.find('\n')

            if lf_char_index == -1:
                # No new line character in data, so we append all.
                self.data += data
            else:
                # We see a new line character in data, so append rest and handle.
                self.data += data[:lf_char_index]

                if self.data == "blink":
                    print "blink start"
                    led.blink(led_pin, 1, 1, 5)
                    print "blink end"

                elif self.data == "turn LED on":
                    print "turning on LED"
                    self.send("turning on LED")
                    led.on(led_pin)

                elif self.data == "turn LED off":
                    print "turning off LED"
                    self.send("turning off LED")
                    led.off(led_pin)

                elif self.data == "read adc":
                    read_value = adc.read0()
                    print "ADC 0 value read: " + str(read_value)
                    self.send(str(read_value) + '\n')

                else:
                    print "received [%s]" % self.data
                    self.send(self.data + '\n')

                # Clear the buffer
                self.data = ""
        except Exception as e:
            BTError.print_error(handler=self, error=BTError.ERR_READ, error_message=repr(e))
            self.data = ""
            self.handle_close()
Example #5
0
    def handle_read(self):
        try:
            data = self.recv(1024)
            if not data: return

            ln_char_index = data.find("\n")

            if ln_char_index < 0:
                # no newline in data, append to current buffer
                self.data += data
            else:
                # newline found, so append, send, and clear the buffer
                self.data += data[:ln_char_index]
                print "Received [{0}]: {1}".format(self.mac_addr, self.data)

                self.send(data + "\n")
                self.data = ""
        except Exception as e:
            BTError.print_error(handler=self,
                                error=BTError.ERR_READ,
                                message=repr(e))
            self.data = ""
            self.handle_close()
Example #6
0
                    "minute": nowtime.minute,
                    "second": nowtime.second,
                    'temp': int(t),  # real temperature
                    'SN1': SN1,  # NO2
                    'SN2': SN2,  # O3
                    'SN3': SN3,  # CO
                    'SN4': SN4,  # SO2
                    'PM25': PM25,
                    'A_SN1': int(AQI_NO2),  # NO2
                    'A_SN2': int(AQI_O3),  # O3
                    'A_SN3': int(AQI_CO),  # CO
                    'A_SN4': int(AQI_SO2),  # SO2
                    'A_PM25': int(AQI_PM25)
                }

                msg = json.dumps(output)
            elif args.output_format == "csv":
                msg = "Time:{}, {}, {}, {}, {}, {}, {}, {} ,{}, {}, {}, {}  ".format(
                    datetime, t, SN1, SN2, SN3, SN4, AQI_PM25, AQI_NO2, AQI_O3,
                    AQI_CO, AQI_SO2, AQI_PM25)
            try:
                client_handler.send((msg + '\n').encode('ascii'))
            except Exception as e:
                BTError.print_error(handler=client_handler,
                                    error=BTError.ERR_WRITE,
                                    error_message=repr(e))
                client_handler.handle_close()

        # Sleep for 5 seconds
        sleep(2.5)