Skip to content

Instantly share code, notes, and snippets.

@martinayotte
Last active June 5, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinayotte/a399c2a4bc291dc1c25a7bf607214a17 to your computer and use it in GitHub Desktop.
Save martinayotte/a399c2a4bc291dc1c25a7bf607214a17 to your computer and use it in GitHub Desktop.
/*
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <ArduinoOTA.h>
#include <ESP8266HTTPClient.h>
#include <FS.h>
extern "C" {
#include "user_interface.h"
}
const char *password = "MyPassword";
char HostName[32];
boolean tcp_server_started = false;
WiFiServer tcpserver(23); // Telnet Port
WiFiClient client;
int i = 0;
char buf[128];
bool connectWifi(const char *ssid, const char *password)
{
int timeout = 60;
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
timeout--;
if (timeout <= 0) {
Serial.printf("\r\nWiFi connect aborted !\r\n");
return false;
}
}
Serial.printf("\r\nWiFi connected !\r\n");
showIP(Serial);
return true;
}
void showIP(Print& p)
{
String str = "Hostname : ";
str += wifi_station_get_hostname();
str += "\r\nLocal MAC Addr : ";
str += WiFi.macAddress();
str += "\r\nLocal IP Addr: ";
str += WiFi.localIP().toString();
str += "\r\nLocal Gateway: ";
str += WiFi.gatewayIP().toString();
str += "\r\nSoftAP MAC Addr: ";
str += WiFi.softAPmacAddress();
str += "\r\nSoftAP IP Addr: ";
str += WiFi.softAPIP().toString();
str += "\r\nDNS IP Addr: ";
str += WiFi.dnsIP().toString();
p.println(str);
}
void GetNYTimes(Print& p)
{
long total_size = 0;
uint8_t buff[1024] = { 0 };
HTTPClient http;
http.begin("http://10.111.111.11/nytimes-full.txt");
// http.begin("http://www.nytimes.com/");
// http.begin("http://www.eetimes.com/");
int httpCode = http.GET();
if (httpCode < 0) {
p.print("[HTTP] GET... failed, error: ");
p.println(http.errorToString(httpCode));
return;
}
p.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode != HTTP_CODE_OK) {
return;
}
if (!SPIFFS.begin()) {
p.println("SPIFFS failed to mount !\r\n");
return;
}
String filename = "/nytimes.txt";
File f = SPIFFS.open(filename.c_str(), "w");
if (!f) {
String str = "Can't open '" + filename + "' !\r\n";
p.println(str);
return;
}
int len = http.getSize();
WiFiClient* stream = http.getStreamPtr();
// read all data from server
while (http.connected() && (len > 0 || len == -1)) {
// get available data size
size_t size = stream->available();
if (size) {
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
String msg = String("Size=") + c + "\r\n";
p.print(msg);
// f.print(msg);
// p.write(buff, c);
f.write((uint8_t *)buff, c);
total_size += c;
if (len > 0) {
len -= c;
}
}
delay(10);
}
f.close();
p.printf("Download completed (total_size=%ld) !\r\n", total_size);
}
void setup() {
Serial.begin(115200);
Serial.println();
sprintf(HostName, "MyESPAP-%06X", ESP.getChipId());
Serial.printf("Hello from %s !\r\n", HostName);
delay(1000);
wifi_station_set_hostname(HostName);
WiFi.softAP(HostName, password);
WiFi.mode(WIFI_AP_STA);
if (WiFi.SSID().length() > 0) {
// connectWifi(WiFi.SSID(), "");
// wifi_station_connect();
WiFi.reconnect();
}
ArduinoOTA.onStart([]() {
Serial.println("OTA Start");
});
ArduinoOTA.onEnd([]() {
Serial.println("\nOTA End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
}
boolean parseCommand(Print &p, const char *buf) {
if (strcmp(buf, "hello") == 0) {
p.printf("Hello from %s !!!\r\n( Build %s %s )\r\n", HostName, __DATE__, __TIME__);
}
else if (strcmp(buf, "wifi") == 0) {
WiFi.printDiag(p);
}
else if (strncmp(buf, "connect", 7) == 0) {
String ssid = "";
String pwd = "";
if (strlen(buf) > 8)
ssid = &buf[8];
int sep = ssid.indexOf(" ");
if (sep > 0) {
pwd = ssid.substring(sep + 1);
ssid = ssid.substring(0, sep);
p.printf("SSID=%s PWD=%s\r\n", ssid.c_str(), pwd.c_str());
if (connectWifi(ssid.c_str(), pwd.c_str()))
WiFi.printDiag(p);
}
}
else if (strcmp(buf, "showip") == 0) {
showIP(p);
}
else if (strcmp(buf, "nyt") == 0) {
GetNYTimes(p);
}
else if (strcmp(buf, "reset") == 0) {
ESP.reset();
}
else if (strcmp(buf, "spifm") == 0) {
if (!SPIFFS.begin()) {
p.println("SPIFFS failed to mount !\r\n");
}
else {
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
p.print(dir.fileName());
p.print(" - ");
p.println(dir.fileSize());
}
}
}
else if (strncmp(buf, "spifr", 5) == 0) {
String filename = "/Tourlou.txt";
if (strlen(buf) > 6)
filename = &buf[6];
File f = SPIFFS.open(filename.c_str(), "r");
if (!f) {
String str = "Can't open '" + filename + "' !\r\n";
p.println(str);
}
else {
char buf[1024];
long siz = f.size();
while(siz > 0) {
size_t len = std::min((long)(sizeof(buf) - 1), siz);
f.read((uint8_t *)buf, len);
buf[len] = 0;
p.println(buf);
siz -= sizeof(buf) - 1;
}
f.close();
}
}
else if (strncmp(buf, "spifh", 5) == 0) {
String filename = "/Tourlou.txt";
if (strlen(buf) > 6)
filename = &buf[6];
File f = SPIFFS.open(filename.c_str(), "r");
if (!f) {
String str = "Can't open '" + filename + "' !\r\n";
p.println(str);
}
else {
char buf[1024];
long siz = f.size();
while(siz > 0) {
size_t len = std::min((long)(sizeof(buf) - 1), siz);
f.read((uint8_t *)buf, len);
buf[len] = 0;
for (int i = 0; i < strlen(buf); i++) {
p.print(buf[i], HEX);
}
siz -= sizeof(buf) - 1;
}
f.close();
}
}
else
return false;
return true;
}
void loop() {
ArduinoOTA.handle();
if (!tcp_server_started && WiFi.status() == WL_CONNECTED) {
tcpserver.begin();
tcp_server_started = true;
ArduinoOTA.setHostname(HostName);
ArduinoOTA.begin();
MDNS.addService("telnet", "tcp", 23);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial.write(inByte);
buf[i] = (char)inByte;
if (inByte == '\r' or inByte == '\n' or i > (sizeof(buf) - 1)) {
buf[i] = 0;
i = 0;
Serial.print("echo = ");
Serial.println(buf);
parseCommand(Serial, buf);
}
else
i++;
}
if (client) {
/* if (client.status() == CLOSED) {
client.stop();
Serial.println("connection closed !");
}
if (client.connected() && client.available()) {
client.setNoDelay(1);
String req = client.readStringUntil('\r');
client.flush();
if (req.equals("exit")) {
client.stop();
}
else if (!parseCommand(client, req.c_str())) {
if (req.length() > 0)
client.println("Unknown command !");
else
client.print(">");
}
}*/
}
else {
// client = tcpserver.available();
}
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment