The code I've been running is a telnet server that echos text to an Adafruit 1.8" tft display. Here is the code:
Code: Select all
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
//definitions required for CC3000 wifi module
//i.e. pin marked IRQ on Adafruit CC3000 breakout is wired to pin 3
//use standard SPI pins, SCK = 13, MISO = 12, and MOSI = 11
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
//modify these for your network!
#define WLAN_SSID "home"
#define WLAN_PASS "brent4445"
//other security types include WLAN_SEC_UNSEC, WLAN_SEC_WEP, or WLAN_SEC_WPA, WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define LISTEN_PORT 23
//tft display defines
//use standard SPI pins, SCK = 13, MISO = 12, and MOSI = 11
#define cs 9
#define dc 8
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIVIDER);
Adafruit_CC3000_Server server(LISTEN_PORT);
Adafruit_CC3000_Client client;
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, 0);
void setup() {
Serial.begin(9600);
/*
Here's a list of colors!
ST7735_RED, ST7735_MAGENTA, ST7735_YELLOW, ST7735_GREEN, ST7735_BLUE,
ST7735_BLACK, ST7735_WHITE.
*/
//fixes silly pixel offset
tft.initR(INITR_BLACKTAB);
//initial tft display settings
tft.fillScreen(ST7735_BLACK);
tft.setTextColor(ST7735_GREEN);
tft.setTextWrap(true);
tft.setCursor(0,0);
//initialze cc3000 module
tft.println(F("Initializing wifi..."));
if (!cc3000.begin()) while(true) ;
//connect to network
tft.println(F("Connecting..."));
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
tft.println(F("Error connecting!"));
while(true) ;
}
//acquire IP address from DHCP
tft.println(F("Acquiring IP... "));
while (!cc3000.checkDHCP()) delay(500);
//print ip address to serial.
while (!displayConnectionDetails()) delay(1000);
//start server
server.begin();
tft.println(F("Listening..."));
}
void loop() {
//try and grab a client
Adafruit_CC3000_ClientRef client = server.available();
//if a client was in fact grabbed, do stuff
if (client) {
//tft.print(server.read()) method works fine. i.e. buffer hop.
char input[64];
promptUser(input, client, sizeof(input)/sizeof(char));
//print it!
tft.println(input);
}
//long refresh is fine.
delay(1000);
}
//Adafruit's nice little print function
bool displayConnectionDetails(void) {
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) return false;
else {
Serial.print(F("\nIP address: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCP: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNS: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
void promptUser(char *input, Adafruit_CC3000_ClientRef client, int len) {
if (client) {
//wait until receives data
while (!client.available());
//wait ridiculously long time for bytes to accumulate in buffer.
delay(1000);
//read byte into input if available and not newline character
//throw a \0 on the end
int place = 0;
char ch;
for (int i = 0; i < len-1 && client.available() && (ch = client.read()) != '\n'; i++) {
*(input+i) = ch;
place = i;
}
*(input+place+1) = '\0';
}
}