创建Python服务器以从Arduino CC3000客户端接收数据

时间:2017-08-08 02:40:37

标签: python python-3.x arduino-uno

我正在尝试通过运行Windows 10的笔记本电脑的端口65上的LAN连接将数据从我的Arduino发送到我的电脑。

目前,由于我的主要障碍,我还没有到达数据部分:我无法让CC3000连接到我的Python服务器。

来自Arduino的调试正是你所期望的,如果它无法连接,但是你走了:(显然有一段时间它放弃了找到我的电脑的IP地址)

Initializing CC3000.............Initialized!
Requesting Connection to WiFi Network....Connected!
Requesting DHCP...
Failed!
Displaying Connection Details...Success!
IP Addr: 192.168.0.113
Netmask: 255.255.255.0
Gateway: 192.168.0.1
DHCPsrv: 192.168.0.1
DNSserv: 192.168.0.1
Success!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
0.0.0.0Failed!
Connecting to client............
Determining IP of host...
Failed!
Failed!
Failed!

这是我的Arduino代码(我有一个Linksprite CC3000,但我使用的是Adafruit CC3000库):

#include <Adafruit_CC3000.h>

#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include <stdlib.h>

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3  // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT 5 // Apparently these can be any two pins
#define ADAFRUIT_CC3000_CS 10  // But I wouldn't change these...
// Use hardware SPI for the remaining pins (On a Mega 2560, SCK = 52, MISO = 50, and MOSI = 51)
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);

#define WLAN_SSID "ARK_TP-LINK_2.4GHz_FFBC77"
#define WLAN_PASS "AHomeRWirelessK1!"
#define WLAN_SECURITY WLAN_SEC_WPA2


String readString = String(100); //string for fetching data from address
uint32_t ip = ( 192 << 24 ) & ( 168 << 16 ) & ( 0 << 8 ) & ( 115 ); // This translates into the ip address we need
                          // 323223552X; 192.168.0.X

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.print(F("Initializing CC3000............."));
  if (!cc3000.begin())
  {
    Serial.println(F("Failed! Check your wiring?"));
    while(1);
  }
  Serial.println("Initialized!");
  //Connect to the Wireless Access Point
  Serial.print("Requesting Connection to WiFi Network....");
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }

  Serial.println(F("Connected!"));
  int DHCPTimeout = 0;
  bool failed = false;
  Serial.print(F("Requesting DHCP..."));
  Serial.println("\n" + (!cc3000.checkDHCP()));
  while (!cc3000.checkDHCP() && DHCPTimeout < 16) 
  { //Obtain IP addeess
    Serial.println("Failed!");
    delay(1000);
    DHCPTimeout = DHCPTimeout + 1;
    if (DHCPTimeout == 15)
    {
      failed = true;
    }
  }
  if (failed)
  {
    Serial.println("Timed Out...");
  } else
  {
    Serial.print("Displaying Connection Details...");
    while (! displayConnectionDetails())
    {
      Serial.println("Failed!");
      delay(1000);
    }
    Serial.println("Success!");
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Connecting to client............");
  Serial.println("Determining IP of host...");
  uint32_t ip;
  while (!cc3000.getHostByName("name_of_LAN_PC", &ip))
  {
    Serial.println("Failed!");
    delay(1000);
  }
  cc3000.printIPdotsRev(ip);
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 65);
  if (client.connected()){
    Serial.println("Success!");
    Serial.println("Saying hi...");
    client.println("hi");
    client.println("");
    if (client.available()){
      char c = client.read();
      Serial.print(c);
    }
  } else{
    Serial.println("Failed!");
  }
  client.close();
  delay(1000);
}
bool displayConnectionDetails(void)
{
  uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

  if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  {
    //Serial.println(F("Unable to retrieve the IP Address!\r\n"));
    return false;
  }
  else
  {
    Serial.print("Success!");
    Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
    Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
    Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
    Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
    Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
    Serial.println();
    return true;
  }
}

当然,相比之下,Python代码非常简单:

import socket
import time
import smtplib as smtp

def send_email(subject, message, to):       #Send an email
    fro = "unoarduino77@gmail.com"
    print("Sending Email:",subject)
    server = smtp.SMTP("smtp.gmail.com", 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(fro, "alphabetagammadelta")
    header = "To:" + to + "\n" + "From: " + fro + "\n" + "Subject:" + subject + "\n"
    #header for message contains to, from, and subject
    msg = header + "\n" + message + " \n\n"
    #formulate packet using header and message
    server.sendmail(fro, to, msg)
    server.close()
def createServer():                         #Create a server to recieve
    global sock                             #data from the Arduino
    sock = socket.socket()
    host = socket.gethostname()
    print("Initializing Server")
    print("Host:", host)
    port = 65
    print("Port:", port)
    sock.bind((host, port))
    #print(sock.bind((host, port)))
    sock.listen(5)
    while 1:
        c, addr = sock.accept()
        c.send("Connected!")
        print("Connected to "+addr[0]+":"+str(addr[1]))
    print(c.recv(1024))
createServer()

1 个答案:

答案 0 :(得分:0)

如果您的主要目标是建立从arduino到python的通信,请查看此页面:

https://playground.arduino.cc/Interfacing/Python

您可以通过串行接口

在python代码和Arduino之间进行通信