Please click here to download SSCOM.

TCP Server:

import network
import socket
import time

SSID="yourSSID"
PASSWORD="yourPASSWD"
port=10000
wlan=None
listenSocket=None

def connectWifi(ssid,passwd):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)
  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
  return True

try:
  connectWifi(SSID,PASSWORD)
  ip=wlan.ifconfig()[0]
  listenSocket = socket.socket()
  listenSocket.bind((ip,port))
  listenSocket.listen(1)
  listenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  print ('tcp waitingw...')

  while True:
    print("accepting.....")
    conn,addr = listenSocket.accept()
    print(addr,"connected")

    while True:
      data = conn.recv(1024)
      if(len(data) == 0):
        print("close socket")
        conn.close()
        break
      print(data)
      ret = conn.send(data)
except:
  if(listenSocket):
    listenSocket.close()
  wlan.disconnect()
  wlan.active(False)

1.Open IDE and connect to development board, paste the above example program to new text or open the built-in IDE example program: File->Examples->Communicate->tcpServer.py and change the user's name and code.
2.Click or press F5 to download and run the program. The IP address of the server can be checked in the terminal.
3.Open serial adapt assistant: SSCOM V5.12, choose TCP Client as the port number, change remote of SSCOM to the printed IP address of tcp server of IDE->Terminal. The port number should be same as the port number of examples, click to connect the corresponding ports.
4.After the connection, you can input any message into the edit box, click send. So the server can receive the massage sent from the client.

TCP Client

import network
import socket
import time

SSID="yourSSID"
PASSWORD="yourPASSWD"
host="192.168.3.147"
port=10000
wlan=None
s=None

def connectWifi(ssid,passwd):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)
  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
  return True
try:
  connectWifi(SSID,PASSWORD)
  ip=wlan.ifconfig()[0]
  s = socket.socket()
  s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  s.connect((host,port))
  s.send("hello DFRobot,I am TCP Client")
  while True:
      data = s.recv(1024)
      if(len(data) == 0):
        print("close socket")
        s.close()
        break
      print(data)
      ret = s.send(data)
except:
  if (s):
    s.close()
  wlan.disconnect()
  wlan.active(False)

1.Open IDE and connect to development board, paste the above example program to new text or open the built-in IDE example program: File->Examples->Communicate->tcpClient.py and change the user’s name and code.
2.Open SSCOM, choose TCP Server as the port number, fill in your local IP address to Local and choose a suitable port (Please modify host and port in the example programs accordingly), click listen and wait.
3.Run the example program of IDE, hello DFRobot,I am TCP Client will be sent to SSCOM.
4.Once SSCOM received the message, return to the client.

UDP Server

import socket
import network
import time

port = 10000
SSID="yourSSID"
PASSWORD="yourPASSWD"
wlan=None
s=None

def connectWifi(ssid,passwd):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)

  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
  return True

try:
  if(connectWifi(SSID, PASSWORD) == True):
    s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)  
    ip=wlan.ifconfig()[0]
    s.bind((ip,port))
    print('waiting...')
    while True:
      data,addr=s.recvfrom(1024)
      print('received:',data,'from',addr)
      s.sendto(data,addr)
except:
  if (s):
    s.close()
  wlan.disconnect()
  wlan.active(False)

1.Open IDE and connect to development board, paste the above example program to new text or open the built-in IDE example program: File->Examples->Communicate->udpServer.py and change the user’s name and code.
2.Click or press F5 to download and run the program. The IP address of the server can be checked in the terminal.
3.Open SSCOM, choose UDP as the port number. The udp Client only focus on remote. Change IP address to the printed IP address of terminal. The port number should be same as the port number of examples. Fill in and send message in the SSCOM text editor when connected, results shown as below.

UDP Client

import socket
import network
import time


host='192.168.3.147'
port = 10000
SSID="yourSSID"
PASSWORD="yourPASSWD"
wlan=None
s=None

def connectWifi(ssid,passwd):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)
  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
  return True
try:
  if(connectWifi(SSID,PASSWORD) == True):
    s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)  
    ip=wlan.ifconfig()[0]
    while True:
      s.sendto(b'hello DFRobot\r\n',(host,port))
      time.sleep(1)
except:
  if (s):
    s.close()
  wlan.disconnect()
  wlan.active(False)

1.Open IDE and connect to development board, paste the above example program to new text or open the built-in IDE example program: File->Examples->Communicate->udpServer.py and change the user’s name and code.
2.Open SSCOM, choose UDP as the port number. Change IP address to the printed IP address of terminal and click to connect.
3.Modify host and port in the example programs. Using the IP address of the PC as the host. The port should be as same as the SSCOM.
4.Click or press F5 to download and run the program. The IP address of the server can be checked in the terminal.
5.The client will send Hello DFRobot to server after a successful connection. Then SSCOM will receive the message.

Telnet

Not support it temporarily.

MQTT

Caution: please refer to comprehensive experiment: Internet of things (IoT)

1.Register a new account.
2.Apply for equipment.
3.Replace SSID,PASSWORD CLIENT_ID username, password
4.Run the program.

from umqtt.simple import MQTTClient
from machine import Pin
import network
import time

SSID="yourSSID"
PASSWORD="yourPASSWD"

led=Pin(2, Pin.OUT, value=0)

SERVER = "182.254.130.180"
CLIENT_ID = "yourClientID"
TOPIC = b"yourTopic"
username='yourIotUserName'
password='yourIotPassword'
state = 0
def sub_cb(topic, msg):
    global state
    print((topic, msg))
    if msg == b"on":
            led.value(1)
            state = 0
            print("1")
    elif msg == b"off":
            led.value(0)
            state = 1
            print("0")
    elif msg == b"toggle":
            # LED is inversed, so setting it to current state
            # value will make it toggle
            led.value(state)
            state = 1 - state
def connectWifi(ssid,passwd):
  global wlan
  wlan=network.WLAN(network.STA_IF)
  wlan.active(True)
  wlan.disconnect()
  wlan.connect(ssid,passwd)
  while(wlan.ifconfig()[0]=='0.0.0.0'):
    time.sleep(1)
connectWifi(SSID,PASSWORD)
server=SERVER
c = MQTTClient(CLIENT_ID, server,0,username,password)
c.set_callback(sub_cb)
c.connect()
c.subscribe(TOPIC)
print("Connected to %s, subscribed to %s topic" % (server, TOPIC))
try:
  while 1:
    c.wait_msg()
finally:
  c.disconnect()

results matching ""

    No results matching ""