Skip to content

Instantly share code, notes, and snippets.

@jrjbertram
Created September 1, 2014 14:08
Show Gist options
  • Select an option

  • Save jrjbertram/a9154bddc11989fde863 to your computer and use it in GitHub Desktop.

Select an option

Save jrjbertram/a9154bddc11989fde863 to your computer and use it in GitHub Desktop.
fast back to back sendtos crash arduino due
#include <ccspi.h>
#include <Adafruit_CC3000.h>
#include <SPI.h> // neede for some reason here too
#include <Arduino.h>
#include "utility/socket.h"
#define ENABLE_NET 1
uint16_t _port = 5005; // picked at random
uint32_t _addr = 0x6401a8C0; // 192.168.1.100... laptop addr. Byte swapped correctly!!
int _socket;
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT 5 // Can be any pin
#define ADAFRUIT_CC3000_CS 10 // Can be any pin
#include "wifi_credentials.h" // defines WLAN_SSID and WLAN_PASS strings
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define LISTEN_PORT 7 // What TCP port to listen on for connections. The echo protocol uses port 7.
#ifdef ENABLE_NET
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed
Adafruit_CC3000_Server echoServer(LISTEN_PORT);
#endif
void setup() {
Serial.begin(115200);
Serial.println( "test" );
#ifdef ENABLE_NET
Serial.println(F("\nInitializing wifi..."));
if (!cc3000.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while (1);
}
Serial.println(F("\nInitialized..."));
//uint16_t firmware = checkFirmwareVersion();
//if (firmware < 0x113) {
// Serial.println(F("Wrong firmware version!"));
// for(;;);
//}
//displayMACAddress();
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while (1);
}
Serial.println(F("Connected!"));
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
// Start listening for connections
echoServer.begin();
// Create the UDP socket
int soc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (soc < 0) {
Serial.println("socket() call failed");
while ( 1 ) ;
}
sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(_port);
address.sin_addr.s_addr = 0; // bind to DHCP-assigned address
socklen_t len = sizeof(address);
if (bind(soc, (sockaddr*) &address, sizeof(address)) < 0) {
Serial.println("bind() call failed");
while ( 1 ) ;
}
// int ip = 0xC0A80164;
// Serial.print(F("\n\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");
// int replies = cc3000.ping(ip, 5);
// Serial.print(replies); Serial.println(F(" replies"));
_socket = soc;
#endif
}
void sendchar( uint8_t data )
{
#ifdef ENABLE_NET
sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(_port);
address.sin_addr.s_addr = _addr;
socklen_t len = sizeof(address);
int n = sendto(_socket, &data, 1, 0, (sockaddr*)&address, len);
if ( n < 0 )
{
Serial.println( "sendto error" );
}
#endif
}
uint8_t message[] = "hey there\n";
void loop() {
if ( millis() % 5000 == 0 ) // every 5 seconds
{
// put your main code here, to run repeatedly:
Serial.println( "printing" );
for ( int i = 0; message[i] != '\0'; i++ )
{
//delay(1); // need a delay or it will crash
sendchar( message[i] );
}
}
}
/**************************************************************************/
/*!
@brief Displays the driver mode (tiny of normal), and the buffer
size if tiny mode is not being used
@note The buffer size and driver mode are defined in cc3000_common.h
*/
/**************************************************************************/
void displayDriverMode(void)
{
#ifdef CC3000_TINY_DRIVER
Serial.println(F("CC3000 is configure in 'Tiny' mode"));
#else
Serial.print(F("RX Buffer : "));
Serial.print(CC3000_RX_BUFFER_SIZE);
Serial.println(F(" bytes"));
Serial.print(F("TX Buffer : "));
Serial.print(CC3000_TX_BUFFER_SIZE);
Serial.println(F(" bytes"));
#endif
}
/**************************************************************************/
/*!
@brief Tries to read the CC3000's internal firmware patch ID
*/
/**************************************************************************/
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
#ifndef CC3000_TINY_DRIVER
if (!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
version = major; version <<= 8; version |= minor;
}
#endif
return version;
}
/**************************************************************************/
/*!
@brief Tries to read the 6-byte MAC address of the CC3000 module
*/
/**************************************************************************/
void displayMACAddress(void)
{
uint8_t macAddress[6];
if (!cc3000.getMacAddress(macAddress))
{
Serial.println(F("Unable to retrieve MAC Address!\r\n"));
}
else
{
Serial.print(F("MAC Address : "));
cc3000.printHex((byte*)&macAddress, 6);
}
}
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(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;
}
}
#! /usr/bin/python3
import sys
import socket
import select
UDP_IP = "0.0.0.0"
UDP_PORT = 5005
addr = ( "192.168.1.200", 31000 )
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
# Check for input from either stdin or our socket
activity = select.select([sys.stdin, sock], [], [], 0)[0]
if sys.stdin in activity:
line = sys.stdin.readline()
if line:
#print "sending line ", line, " to ", addr, " port ", UDP_PORT
sock.sendto( line, addr )
if sock in activity:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data, " from ", addr
sys.stdout.write( data )
sys.stdout.flush()
#define WLAN_SSID "your" // cannot be longer than 32 characters!
#define WLAN_PASS "login"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment