Forráskód Böngészése

initial upload (0.32)

Stephan Mühl 6 éve
commit
47a5344797
8 módosított fájl, 433 hozzáadás és 0 törlés
  1. 2 0
      .gitattributes
  2. 6 0
      .gitignore
  3. 6 0
      _gitignore.txt
  4. 67 0
      _travis.yml
  5. 46 0
      lib/README
  6. 38 0
      platformio.ini
  7. 257 0
      src/AWTRIXController.cpp
  8. 11 0
      test/README

+ 2 - 0
.gitattributes

@@ -0,0 +1,2 @@
+# Auto detect text files and perform LF normalization
+* text=auto

+ 6 - 0
.gitignore

@@ -0,0 +1,6 @@
+.pio
+.pioenvs
+.piolibdeps
+.vscode/.browse.c_cpp.db*
+.vscode/c_cpp_properties.json
+.vscode/launch.json

+ 6 - 0
_gitignore.txt

@@ -0,0 +1,6 @@
+.pio
+.pioenvs
+.piolibdeps
+.vscode/.browse.c_cpp.db*
+.vscode/c_cpp_properties.json
+.vscode/launch.json

+ 67 - 0
_travis.yml

@@ -0,0 +1,67 @@
+# Continuous Integration (CI) is the practice, in software
+# engineering, of merging all developer working copies with a shared mainline
+# several times a day < https://docs.platformio.org/page/ci/index.html >
+#
+# Documentation:
+#
+# * Travis CI Embedded Builds with PlatformIO
+#   < https://docs.travis-ci.com/user/integration/platformio/ >
+#
+# * PlatformIO integration with Travis CI
+#   < https://docs.platformio.org/page/ci/travis.html >
+#
+# * User Guide for `platformio ci` command
+#   < https://docs.platformio.org/page/userguide/cmd_ci.html >
+#
+#
+# Please choose one of the following templates (proposed below) and uncomment
+# it (remove "# " before each line) or use own configuration according to the
+# Travis CI documentation (see above).
+#
+
+
+#
+# Template #1: General project. Test it using existing `platformio.ini`.
+#
+
+# language: python
+# python:
+#     - "2.7"
+#
+# sudo: false
+# cache:
+#     directories:
+#         - "~/.platformio"
+#
+# install:
+#     - pip install -U platformio
+#     - platformio update
+#
+# script:
+#     - platformio run
+
+
+#
+# Template #2: The project is intended to be used as a library with examples.
+#
+
+# language: python
+# python:
+#     - "2.7"
+#
+# sudo: false
+# cache:
+#     directories:
+#         - "~/.platformio"
+#
+# env:
+#     - PLATFORMIO_CI_SRC=path/to/test/file.c
+#     - PLATFORMIO_CI_SRC=examples/file.ino
+#     - PLATFORMIO_CI_SRC=path/to/test/directory
+#
+# install:
+#     - pip install -U platformio
+#     - platformio update
+#
+# script:
+#     - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N

+ 46 - 0
lib/README

@@ -0,0 +1,46 @@
+
+This directory is intended for project specific (private) libraries.
+PlatformIO will compile them to static libraries and link into executable file.
+
+The source code of each library should be placed in a an own separate directory
+("lib/your_library_name/[here are source files]").
+
+For example, see a structure of the following two libraries `Foo` and `Bar`:
+
+|--lib
+|  |
+|  |--Bar
+|  |  |--docs
+|  |  |--examples
+|  |  |--src
+|  |     |- Bar.c
+|  |     |- Bar.h
+|  |  |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
+|  |
+|  |--Foo
+|  |  |- Foo.c
+|  |  |- Foo.h
+|  |
+|  |- README --> THIS FILE
+|
+|- platformio.ini
+|--src
+   |- main.c
+
+and a contents of `src/main.c`:
+```
+#include <Foo.h>
+#include <Bar.h>
+
+int main (void)
+{
+  ...
+}
+
+```
+
+PlatformIO Library Dependency Finder will find automatically dependent
+libraries scanning project source files.
+
+More information about PlatformIO Library Dependency Finder
+- https://docs.platformio.org/page/librarymanager/ldf.html

+ 38 - 0
platformio.ini

@@ -0,0 +1,38 @@
+; PlatformIO Project Configuration File
+;
+;   Build options: build flags, source filter
+;   Upload options: custom upload port, speed and extra flags
+;   Library options: dependencies, extra library storages
+;   Advanced options: extra scripting
+;
+; Please visit documentation for the other options and examples
+; http://docs.platformio.org/page/projectconf.html
+
+[platformio]
+env_default = nodemcuv2
+
+[common]
+lib_deps_builtin =
+    SPI
+    
+lib_deps =
+    PubSubClient
+    Adafruit NeoPixel
+    https://github.com/awtrix/Adafruit-GFX-Library
+    https://github.com/awtrix/FastLED_NeoMatrix
+    ArduinoJson
+    FastLED
+    https://github.com/QuentinCG/Arduino-Light-Dependent-Resistor-Library/releases/download/1.0.0/LightDependentResistor_v1_0_0.zip
+
+[env:nodemcuv2]
+platform = espressif8266
+#board = nodemcuv2
+board = d1_mini
+build_flags = -w -Wl,-Teagle.flash.4m.ld -DMQTT_MAX_PACKET_SIZE=3000
+board_build.f_cpu = 80000000L 
+framework = arduino
+monitor_speed = 115200
+upload_speed = 921600
+lib_deps =
+    ${common.lib_deps_builtin}
+    ${common.lib_deps}

+ 257 - 0
src/AWTRIXController.cpp

@@ -0,0 +1,257 @@
+#include <ESP8266WebServer.h>
+#include <ESP8266HTTPUpdateServer.h>
+#include <ESP8266WiFi.h>
+#include <WiFiClient.h>
+#include <ESP8266mDNS.h>
+#include <DNSServer.h>
+#include <PubSubClient.h>
+#include <FS.h>
+#include <ArduinoJson.h>
+#include <Adafruit_GFX.h>
+#include <FastLED.h>
+#include <FastLED_NeoMatrix.h>
+#include <Fonts/TomThumb.h>
+#include <LightDependentResistor.h>
+
+////////////////////////////////////////////////////////////////
+///////////////////////// Config begin /////////////////////////
+// Wifi Config
+const char *ssid = "xxxxx";
+const char *password = "xxxxx";
+char *awtrix_server = "192.168.178.39";
+
+/// LDR Config
+#define LDR_RESISTOR 10000 //ohms
+#define LDR_PIN A0
+#define LDR_PHOTOCELL LightDependentResistor::GL5516
+
+/// Matrix Config
+#define MATRIX_PIN D2
+//uncomment following line to use Matrixtype 2
+//#define MATRIX_MODEV2
+
+///////////////////////// Config end /////////////////////////
+//////////////////////////////////////////////////////////////
+
+//////////////////////////////////////////////////////////////
+//////////////////////// Don't touch /////////////////////////
+char *topics = "awtrixmatrix/";
+#define NUMMATRIX (32 * 8)
+CRGB leds[NUMMATRIX];
+String version = "0.32";
+#ifdef MATRIX_MODEV2
+FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG);
+#endif
+#ifndef MATRIX_MODEV2
+FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(leds, 32, 8, NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG);
+#endif
+
+WiFiClient espClient;
+PubSubClient client(espClient);
+ESP8266WebServer server(80);
+ESP8266HTTPUpdateServer httpUpdater;
+LightDependentResistor photocell(LDR_PIN, LDR_RESISTOR, LDR_PHOTOCELL);
+
+unsigned long startTime = 0;
+unsigned long endTime = 0;
+unsigned long duration;
+
+String GetChipID()
+{
+	return String(ESP.getChipId());
+}
+
+int GetRSSIasQuality(int rssi)
+{
+	int quality = 0;
+
+	if (rssi <= -100)
+	{
+		quality = 0;
+	}
+	else if (rssi >= -50)
+	{
+		quality = 100;
+	}
+	else
+	{
+		quality = 2 * (rssi + 100);
+	}
+	return quality;
+}
+
+void handleNotFound()
+{
+	server.sendHeader("Location", String("/update"), true);
+	server.send(302, "text/plain", "");
+}
+
+void callback(char *topic, byte *payload, unsigned int length)
+{
+	String s_payload = String((char *)payload);
+
+	String s_topic = String(topic);
+	int last = s_topic.lastIndexOf("/") + 1;
+	String channel = s_topic.substring(last);
+
+	DynamicJsonBuffer jsonBuffer;
+	JsonObject &json = jsonBuffer.parseObject(s_payload);
+
+	if (channel.equals("show"))
+	{
+		matrix->show();
+	}
+	else if (channel.equals("clear"))
+	{
+		matrix->clear();
+	}
+	else if (channel.equals("drawText"))
+	{
+		if (json["font"].as<String>().equals("big"))
+		{
+			matrix->setFont();
+			matrix->setCursor(json["x"].as<int16_t>(), json["y"].as<int16_t>() - 1);
+		}
+		else
+		{
+			matrix->setFont(&TomThumb);
+			matrix->setCursor(json["x"].as<int16_t>(), json["y"].as<int16_t>() + 5);
+		}
+		matrix->setTextColor(matrix->Color(json["color"][0].as<int16_t>(), json["color"][1].as<int16_t>(), json["color"][2].as<int16_t>()));
+		String text = json["text"];
+		matrix->print(text);
+	}
+	else if (channel.equals("drawBMP"))
+	{
+		int16_t h = json["height"].as<int16_t>();
+		int16_t w = json["width"].as<int16_t>();
+		int16_t x = json["x"].as<int16_t>();
+		int16_t y = json["y"].as<int16_t>();
+
+		for (int16_t j = 0; j < h; j++, y++)
+		{
+			for (int16_t i = 0; i < w; i++)
+			{
+				matrix->drawPixel(x + i, y, json["bmp"][j * w + i].as<int16_t>());
+			}
+		}
+	}
+	else if (channel.equals("drawLine"))
+	{
+		matrix->drawLine(json["x0"].as<int16_t>(), json["y0"].as<int16_t>(), json["x1"].as<int16_t>(), json["y1"].as<int16_t>(), matrix->Color(json["color"][0].as<int16_t>(), json["color"][1].as<int16_t>(), json["color"][2].as<int16_t>()));
+	}
+	else if (channel.equals("drawPixel"))
+	{
+		matrix->drawPixel(json["x"].as<int16_t>(), json["y"].as<int16_t>(), matrix->Color(json["color"][0].as<int16_t>(), json["color"][1].as<int16_t>(), json["color"][2].as<int16_t>()));
+	}
+	else if (channel.equals("setBrightness"))
+	{
+		matrix->setBrightness(json["brightness"].as<int16_t>());
+	}
+	else if (channel.equals("speedtest"))
+	{
+		matrix->setFont(&TomThumb);
+		matrix->setCursor(0, 7);
+
+		endTime = millis();
+		duration = endTime - startTime;
+		if (duration > 55 || duration < 45)
+		{
+			matrix->setTextColor(matrix->Color(255, 0, 0));
+		}
+		else
+		{
+			matrix->setTextColor(matrix->Color(0, 255, 0));
+		}
+		matrix->print(duration);
+		startTime = millis();
+	}
+	else if (channel.equals("getMatrixInfo"))
+	{
+		StaticJsonBuffer<200> jsonBuffer;
+		JsonObject& root = jsonBuffer.createObject();
+		root["version"] = version;
+		root["wifirssi"] = String(WiFi.RSSI());
+		root["wifiquality"] =GetRSSIasQuality(WiFi.RSSI());
+		root["wifissid"] =WiFi.SSID();
+		root["getIP"] =WiFi.localIP().toString();
+		String JS;
+		root.printTo(JS);
+		client.publish("matrixInfo", JS.c_str());
+	}
+	else if (channel.equals("getLUX"))
+	{
+		StaticJsonBuffer<200> jsonBuffer;
+
+		client.publish("matrixLux", String(photocell.getCurrentLux()).c_str());
+	}
+}
+
+void reconnect()
+{
+	// Loop until we're reconnected
+	while (!client.connected())
+	{
+
+		// Attempt to connect
+		if (client.connect(("AWTRIXController_" + GetChipID()).c_str(),"matrixstate",0,false,"diconnected"))
+		{
+			// ... and resubscribe
+			client.subscribe((String(topics) + "#").c_str());
+			// ... and publish
+			client.publish("chipid", GetChipID().c_str(), true);
+			client.publish("ip", WiFi.localIP().toString().c_str(), true);
+			client.publish("matrixstate", "connected");
+		}
+		else
+		{
+			// Wait 5 seconds before retrying
+			delay(5000);
+		}
+	}
+}
+
+void setup()
+{
+	matrix->begin();
+	matrix->setTextWrap(false);
+	matrix->setBrightness(127);
+	matrix->setFont(&TomThumb);
+	matrix->setCursor(0, 7);
+	matrix->print("WIFI...");
+	matrix->show();
+	WiFi.mode(WIFI_STA);
+	WiFi.begin(ssid, password);
+	while (WiFi.status() != WL_CONNECTED)
+	{
+		delay(500);
+	}
+
+	MDNS.begin("AWTRIXController");
+
+	photocell.setPhotocellPositionOnGround(false);
+	FastLED.addLeds<NEOPIXEL, MATRIX_PIN>(leds, NUMMATRIX).setCorrection(TypicalLEDStrip);
+	matrix->clear();
+	matrix->setCursor(0, 7);
+	matrix->print("READY!");
+	matrix->show();
+
+	httpUpdater.setup(&server);
+	server.onNotFound(handleNotFound);
+	server.begin();
+
+	client.setServer(awtrix_server, 7001);
+	client.setCallback(callback);
+}
+
+void loop()
+{
+	if (!client.connected())
+	{
+		reconnect();
+	}
+	client.loop();
+
+	server.handleClient();
+	MDNS.update();
+}

+ 11 - 0
test/README

@@ -0,0 +1,11 @@
+
+This directory is intended for PIO Unit Testing and project tests.
+
+Unit Testing is a software testing method by which individual units of
+source code, sets of one or more MCU program modules together with associated
+control data, usage procedures, and operating procedures, are tested to
+determine whether they are fit for use. Unit testing finds problems early
+in the development cycle.
+
+More information about PIO Unit Testing:
+- https://docs.platformio.org/page/plus/unit-testing.html