代码之家  ›  专栏  ›  技术社区  ›  Federico

python“str”对象不支持项分配

  •  0
  • Federico  · 技术社区  · 6 年前

    我正在为一个将数据发送到thingspeak的嵌入式系统开发一个python脚本。

    为了做到这一点(以及研究python),我使用了线程模块和 ThingSpeak 使用模块。

    基本上,我的脚本创建两个线程:

    • 每10秒向thingspeak发送一次数据。
    • 一个用于切换LED。

    除了在ThingsPeak上发布以外,一切都正常。注意:在thingspeak上不带线程的发布可以工作。

    我得到这个错误: “typeerror:'str'对象不支持项分配”

    Traceback (most recent call last):
      File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
        self.run()
      File "/usr/lib/python2.7/threading.py", line 754, in run
        self.__target(*self.__args, **self.__kwargs)
      File "thingSpeak.py", line 57, in ThingSpeak_Thread
        response = thingSpeakHandler.update(jsonMessage)
      File "/usr/lib/python2.7/site-packages/thingspeak/thingspeak.py", line 110, in update
        data['api_key'] = self.write_key
    TypeError: 'str' object does not support item assignment
    

    我认为这个错误与线程的添加有关。

    这是我的代码:

    import onionGpio
    import time
    import thingspeak
    import sys
    import threading
    import random
    import thread
    import json
    
    # Declare variables
    heartBeatDelaySec   = 1.0
    heartBeatLedStatus  = 0
    heartBeatDisable    = False
    
    thingSpeakDelaySec  = 10
    
    thingSpeakChannel = 98765
    thingSpeakWriteKey = '1234556'
    thingSpeakReadKey = '123456'
    
    
    def HeartBeatThread( name, delay, runEvent ):
      global heartBeatLedStatus
    
      print ("Task " + name + " started")
    
      while runEvent.is_set():
        blueLed.setValue( heartBeatLedStatus )
    
        if( heartBeatLedStatus == 0 ):
          heartBeatLedStatus = 1
        else:
          heartBeatLedStatus = 0
    
        time.sleep(delay)
    
      # Shut-down Led
      blueLed.setValue( 1 )
    
    
    def ThingSpeak_Thread( name, delay, thingSpeakChannel, thingSpeakWriteKey, runEvent ):
    
      thingSpeakHandler = thingspeak.Channel( id=thingSpeakChannel, write_key=thingSpeakWriteKey, fmt='json' )
    
      while runEvent.is_set():
        temp = random.randint(12, 38)
        rH   = random.randint(30, 68)
    
        # Build JSON with data
        data = {}
        data['1'] = temp
        data['2'] = rH
        jsonMessage = json.dumps(data)
    
        print( "Publishing: " + jsonMessage )
    
        response = thingSpeakHandler.update(jsonMessage)
    
        time.sleep(delay)
    
    
    # Led Initialize
    blueLed   = onionGpio.OnionGpio(15)
    greenLed  = onionGpio.OnionGpio(16)
    redLed    = onionGpio.OnionGpio(17)
    
    blueLed.setOutputDirection(1)
    greenLed.setOutputDirection(1)
    redLed.setOutputDirection(1)
    
    blueLed.setValue(1)
    greenLed.setValue(1)
    redLed.setValue(1)
    print ("GPIO configured")
    
    
    # Initialize Threads
    runEvent = threading.Event()
    runEvent.set()
    
    heartBeatThreadHandler = threading.Thread(target = HeartBeatThread, args = ("Heart Beat", heartBeatDelaySec, runEvent))
    
    thingSpeakThreadHandler = threading.Thread(target = ThingSpeak_Thread, args = ("ThingSpeak", thingSpeakDelaySec, thingSpeakChannel, thingSpeakWriteKey, runEvent))
    
    # Start Threads
    heartBeatThreadHandler.start()
    thingSpeakThreadHandler.start()
    
    try:
      while( True ):
        time.sleep(.01)
    except:
      print ("Attempting to close threads. Max wait = " + str(max(heartBeatDelaySec, thingSpeakDelaySec)) + "sec")
      runEvent.clear()
      heartBeatThreadHandler.join()
      thingSpeakThreadHandler.join()
      print ("Threads successfully closed")
    

    感谢您的帮助。

    最好的问候, 费德里克

    1 回复  |  直到 6 年前
        1
  •  1
  •   netolyrg    6 年前

    json.dumps() ThingSpeak_Thread docs thingSpeakHandler.update() dict jsonMessage = json.dumps(data) data update()