Go Down

Topic: [Solved] socket.error [error 146] connect refused (Read 2509 times) previous topic - next topic

dyu

I try to upload the values of humidity and temperature to thingspeak, but the something be wrong..

while the things.py (on Openwrt side) be executed, the result be showed as below.


root@mylinkit:~# python things.py
Traceback (most recent call last):
 File "things.py", line 31, in <module>
   p0 = value.get("PM2.5")
 File "/usr/lib/python2.7/bridge/bridgeclient.py", line 76, in get
   json = self.socket_open()
 File "/usr/lib/python2.7/bridge/bridgeclient.py", line 59, in socket_open
   self.json = TCPJSONClient('127.0.0.1', 5700)
 File "/usr/lib/python2.7/bridge/tcp.py", line 65, in __init__
   TCPClient.__init__(self, address, port)
 File "/usr/lib/python2.7/bridge/tcp.py", line 38, in __init__
   client.connect((address, port))
 File "/usr/lib/python2.7/socket.py", line 224, in meth
   return getattr(self._sock,name)(*args)
socket.error: [Errno 146] Connection refused



I do not know how to solve it, please help me, thank you so much.


Aurdino side..............................

// MCU To MPU
#include <Bridge.h>

// DHT
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup() {  
Serial.begin(9600);
dht.begin();
Bridge.begin();
}

void loop() {  
// Get Temperature and Humidity
float h = dht.readHumidity();
float t = dht.readTemperature();

// Display Temperature and Humidity Value
Serial.print("DHT-Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("DHT-Temperature: ");
Serial.print(t);
Serial.println(" *C\t");

// MCU To MPU
Bridge.put("Humidity", String(h));
Bridge.put("Temperature", String(t));

delay(1000);
}


Openwrt side...............................
import sys  
import time  
import httplib, urllib

sys.path.insert(0, '/usr/lib/python2.7/bridge/')  
from bridgeclient import BridgeClient as bridgeclient  
value = bridgeclient()  
thinkSpeakApiKey = "PZ9BWVYAGTX3VUNU"

def post_to_thingspeak(payload):  
  headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
  not_connected = 1
  while (not_connected):
      try:
          conn = httplib.HTTPConnection("api.thingspeak.com:80")
          conn.connect()
          not_connected = 0
      except (httplib.HTTPException, socket.error) as ex:
          print "Error: %s" % ex
          time.sleep(10)  # sleep 10 seconds

  conn.request("POST", "/update", payload, headers)
  response = conn.getresponse()
  print( response.status, response.reason, payload, time.strftime("%c"))
  data = response.read()
  conn.close()

while True:  
  h0 = value.get("Humidity")
  t0 = value.get("Temperature")
  print "Humi: " + h0
  print "Temp: " + t0
  params = urllib.urlencode({'field1': t0, 'field2': h0, 'key': thinkSpeakApiKey})
  post_to_thingspeak(params)
  time.sleep(5)

dyu

I got the answer myself. Only with a sketch and python script is the bridge working. It can work.

Go Up