Created
October 26, 2018 13:32
-
-
Save ValeriyDP/1a02a0772f197186f34918feba3ec8b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ESP8266httpUpdate.h> | |
#define USE_SERIAL Serial | |
ESP8266WiFiMulti WiFiMulti; | |
int dotLength = 100; | |
int dashLength = dotLength * 3; | |
int elemPause = dotLength; | |
int Spaces = dotLength * 3; | |
int wordPause = dotLength * 7; | |
const int FW_VERSION = 3; | |
const char* fingerprint = "AD 1E 0A D7 91 9E 7C 5D 43 25 A9 91 AB 28 90 9E 9F 13 24 C9"; | |
const char* fwUrlBase = "https://839d.k.time4vps.cloud/firmware/"; | |
const char* fwDevice = "deviceled1"; | |
char stringToMorseCode[] = "Some new message"; | |
void setup() { | |
USE_SERIAL.begin(115200); | |
USE_SERIAL.setDebugOutput(true); | |
pinMode(LED_BUILTIN, OUTPUT); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
USE_SERIAL.println(); | |
for (uint8_t t = 4; t > 0; t--) { | |
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); | |
USE_SERIAL.flush(); | |
delay(1000); | |
} | |
WiFi.mode(WIFI_STA); | |
WiFiMulti.addAP("dev-pro", "Dev-Pro@Kharkov"); | |
} | |
// if ((WiFiMulti.run() == WL_CONNECTED)) { | |
// WiFiClient client; | |
// // The line below is optional. It can be used to blink the LED on the board during flashing | |
// // The LED will be on during download of one buffer of data from the network. The LED will | |
// // be off during writing that buffer to flash | |
// // On a good connection the LED should flash regularly. On a bad connection the LED will be | |
// // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second | |
// // value is used to put the LED on. If the LED is on with HIGH, that value should be passed | |
// // ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); | |
// // t_httpUpdate_return ret = ESPhttpUpdate.update("http://839d.k.time4vps.cloud", 2018, "/v0.01-alpha/firmware.bin"); | |
// // Or: | |
// //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin"); | |
// t_httpUpdate_return ret = ESPhttpUpdate.update("https://839d.k.time4vps.cloud/firmware/v0.01-alpha/firmware.bin", "", fingerprint); | |
// switch (ret) { | |
// case HTTP_UPDATE_FAILED: | |
// USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | |
// break; | |
// case HTTP_UPDATE_NO_UPDATES: | |
// USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES"); | |
// break; | |
// case HTTP_UPDATE_OK: | |
// USE_SERIAL.println("HTTP_UPDATE_OK"); | |
// break; | |
// } | |
// } | |
// String getMAC() | |
// { | |
// uint8_t mac[6]; | |
// char result[14]; | |
// snprintf(result, sizeof(result), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | |
// return String( result ); | |
// } | |
void checkForUpdates() { | |
// String mac = getMAC(); | |
String fwURL = String( fwUrlBase ); | |
fwURL.concat( fwDevice ); | |
String fwVersionURL = fwURL; | |
fwVersionURL.concat( ".vrn.txt" ); | |
Serial.println( "Checking for firmware updates." ); | |
Serial.print( "MAC address: " ); | |
Serial.println( fwDevice ); | |
Serial.print( "Firmware version URL: " ); | |
Serial.println( fwVersionURL ); | |
HTTPClient httpClient; | |
httpClient.begin(fwVersionURL, fingerprint); | |
int httpCode = httpClient.GET(); | |
if( httpCode == 200 ) { | |
String newFWVersion = httpClient.getString(); | |
Serial.print( "Current firmware version: " ); | |
Serial.println( FW_VERSION ); | |
Serial.print( "Available firmware version: " ); | |
Serial.println( newFWVersion ); | |
int newVersion = newFWVersion.toInt(); | |
if( newVersion > FW_VERSION ) { | |
Serial.println( "Preparing to update" ); | |
String fwImageURL = fwURL; | |
fwImageURL.concat( ".bin" ); | |
Serial.println(fwImageURL); | |
t_httpUpdate_return ret = ESPhttpUpdate.update(fwImageURL, "", fingerprint); | |
switch (ret) { | |
case HTTP_UPDATE_FAILED: | |
USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | |
break; | |
case HTTP_UPDATE_NO_UPDATES: | |
USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES"); | |
break; | |
case HTTP_UPDATE_OK: | |
USE_SERIAL.println("HTTP_UPDATE_OK"); | |
break; | |
} | |
} else { | |
Serial.println( "Already on latest version" ); | |
} | |
} else { | |
Serial.print( "Firmware version check failed, got HTTP response code " ); | |
Serial.println( httpCode ); | |
} | |
httpClient.end(); | |
} | |
// DOT | |
void MorseDot() { | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(dotLength); // hold in this position | |
} | |
// DASH | |
void MorseDash() { | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(dashLength); // hold in this position | |
} | |
// Turn Off | |
void LightsOff(int delayTime) { | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(delayTime); | |
} | |
void GetChar(char tmpChar) { | |
// Take the passed character and use a switch case to find the morse code for that character | |
switch (tmpChar) { | |
case 'a': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'b': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'c': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'd': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'e': | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'f': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'g': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'h': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'i': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'j': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'k': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'l': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'm': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'n': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'o': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'p': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 'q': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'r': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 's': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
case 't': | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'u': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'v': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'w': | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'x': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'y': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
break; | |
case 'z': | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDash(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
MorseDot(); | |
LightsOff(elemPause); | |
break; | |
default: | |
// If a matching character was not found it will default to a blank space | |
LightsOff(Spaces); | |
} | |
} | |
void loop() { | |
if ((WiFiMulti.run() == WL_CONNECTED)) { | |
checkForUpdates(); | |
} | |
for (uint8_t i = 0; i < sizeof(stringToMorseCode) - 1; i++) { | |
// Get the character in the current position | |
char tmpChar = stringToMorseCode[i]; | |
// Set the case to lower case | |
tmpChar = toLowerCase(tmpChar); | |
// Call the subroutine to get the morse code equivalent for this character | |
GetChar(tmpChar); | |
} | |
LightsOff(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment