Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino
- #include <Arduino.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <FS.h>
- // Ethernet
- #include <EthernetENC.h>
- #define USE_CUSTOM_ETHERNET true
- #include <EthernetWebServer.h>
- // Settings
- #include "settings/FixedSizeStringEepromConverter.h"
- #include "settings/DefaultEepromConverter.h"
- #include "settings/EEPROMVariable.h"
- #include <EEPROM.h>
- // Redis
- #include <Redis.h>
- // Ethernet settings
- EEPROMVariable<byte[6], 0, FixedSizeByteStringEepromConverter<6>> ethernet_connection_mac_address{ EEPROM };
- EEPROMVariable<IPAddress, 6 + decltype(ethernet_connection_mac_address)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_ip{ EEPROM };
- EEPROMVariable<IPAddress, 4 + decltype(ethernet_connection_ip)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_subnet{ EEPROM };
- EEPROMVariable<IPAddress, 4 + decltype(ethernet_connection_subnet)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_gateway_ip{ EEPROM };
- EEPROMVariable<IPAddress, 4 + decltype(ethernet_connection_gateway_ip)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_dns_ip{ EEPROM };
- // Web server settings
- EEPROMVariable<uint16_t, 400, DefaultEepromConverter<uint16_t>> web_server_port{ EEPROM };
- // Redis connection settings
- EEPROMVariable<IPAddress, 1024, DefaultEepromConverter<IPAddress>> redis_connection_ip{ EEPROM };
- EEPROMVariable<uint16_t, 4 + decltype(redis_connection_ip)::eeprom_address, DefaultEepromConverter<uint16_t>> redis_connection_port{ EEPROM };
- EEPROMVariable<char[200], 2 + decltype(redis_connection_port)::eeprom_address, FixedSizeCharStringEepromConverter<200>> redis_connection_client{ EEPROM };
- EEPROMVariable<char[200], 200 + decltype(redis_connection_client)::eeprom_address, FixedSizeCharStringEepromConverter<200>> redis_connection_password{ EEPROM };
- // Connections
- EthernetWebServer web_server{ 8090 };
- EthernetClient redis_tcp_connection{};
- Redis redis_connection{ redis_tcp_connection };
- // Temporary buffers
- int32_t sensor_value{ std::numeric_limits<int32_t>::min() };
- void setup() {
- pinMode(GPIO_NUM_4, INPUT);
- EEPROM.begin(4096);
- Serial.begin(9600);
- while(not Serial);
- Serial.println();
- Serial.println("Begin Ethernet");
- // You can use Ethernet.init(pin) to configure the CS pin
- //Ethernet.init(10); // Most Arduino shields
- Ethernet.init(5); // MKR ETH Shield
- //Ethernet.init(0); // Teensy 2.0
- //Ethernet.init(20); // Teensy++ 2.0
- //Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
- //Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
- if (Ethernet.hardwareStatus() == EthernetNoHardware) {
- Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
- while (true) {
- delay(1); // do nothing, no point running without Ethernet hardware
- }
- }
- if (Ethernet.linkStatus() == LinkOFF) {
- Serial.println("Ethernet cable is not connected.");
- }
- const uint8_t mac_address[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- Ethernet.begin(mac_address,
- { 192, 168, 1, 10 },
- { 192, 168, 1, 1 },
- { 192, 168, 1, 1 },
- { 255, 255, 255, 0 }
- );
- delay(500);
- Serial.print("Local IP: ");
- Serial.println(Ethernet.localIP());
- Serial.print("Subnet Mask: ");
- Serial.println(Ethernet.subnetMask());
- Serial.print("Gateway IP: ");
- Serial.println(Ethernet.gatewayIP());
- Serial.print("DNS Server: ");
- Serial.println(Ethernet.dnsServerIP());
- Serial.print("Link status: ");
- Serial.println(Ethernet.linkStatus());
- Serial.print("Hardware status: ");
- Serial.println(Ethernet.hardwareStatus());
- Serial.println("Initilize redis:");
- Serial.print("\tTCP port:");
- Serial.print(redis_tcp_connection.connect({ 192, 168, 1, 2 }, 6379));
- Serial.print("; Connected: ");
- Serial.println(redis_tcp_connection.connected());
- Serial.print("\tRedis authentication: ");
- Serial.println(redis_connection.authenticate(""));
- {
- web_server.on("/", []() {
- Serial.println("on [/]");
- web_server.send_P(200, "text/plain", "Ok");
- });
- web_server.on("/sensor/value", HTTP_GET, []() {
- Serial.println("on [/sensor/value][GET]");
- web_server.send(200, "text/plain", String{ sensor_value }.c_str());
- });
- web_server.onNotFound([]() {
- Serial.print("on [NotFound]: ");
- Serial.println(web_server.uri());
- web_server.send(200, "text/plain", "Not found...");
- });
- web_server.begin();
- }
- /*
- web_server.on("/values/1", HTTP_POST, []() {
- Serial.println("on [/values/1][POST]");
- if(not web_server.hasArg("value")) {
- web_server.send(400, "text/plain", "Invalid request");
- } else {
- value_1 = web_server.arg("value").toInt();
- web_server.send(200, "text/plain", "Ok");
- }
- });
- */
- }
- void loop() {
- Ethernet.maintain();
- web_server.handleClient();
- static unsigned long last_time = 0;
- if(const auto current_time = millis(); last_time < current_time - 5000) {
- last_time = current_time;
- sensor_value = analogRead(GPIO_NUM_4);
- Serial.print("Alive: ");
- Serial.print(sensor_value);
- Serial.print("; Redis: ");
- Serial.println(redis_connection.set("sensor:test", String{ sensor_value }.c_str()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement