简体   繁体   中英

Creating a Python server to recieve data from Arduino CC3000 client

I am attempting to send data from my Arduino to my computer via LAN connection on port 65 of my laptop running Windows 10.

Right now, I haven't reached the data part due to my major roadblock: I cannot get the CC3000 to connect to my Python server.

The debug from the Arduino is just what you would expect if it couldn't connect, but here you go: (apparently after a while it gives up on finding my computer's IP address)

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!

Here is my Arduino code (I have a Linksprite CC3000, but I'm using the Adafruit CC3000 library):

#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;
  }
}

Of course, the Python code is extremely simple in comparison:

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()

If your main objective is to establish communication from the arduino to python check out this page:

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

You can communicate between your python code and Arduino via the serial Interface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM