Advertisement
Ilya_Bykonya

Untitled

Apr 15th, 2024
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.28 KB | Source Code | 0 0
  1. // Arduino
  2. #include <Arduino.h>
  3. #include <Wire.h>
  4. #include <SPI.h>
  5. #include <FS.h>
  6. // Ethernet
  7. #include <EthernetENC.h>
  8. #define USE_CUSTOM_ETHERNET true
  9. #include <EthernetWebServer.h>
  10. // Settings
  11. #include "settings/FixedSizeStringEepromConverter.h"
  12. #include "settings/DefaultEepromConverter.h"
  13. #include "settings/EEPROMVariable.h"
  14. #include <EEPROM.h>
  15. // Redis
  16. #include <Redis.h>
  17.  
  18. // Ethernet settings
  19. EEPROMVariable<byte[6], 0, FixedSizeByteStringEepromConverter<6>> ethernet_connection_mac_address{ EEPROM };
  20. EEPROMVariable<IPAddress, 6 + decltype(ethernet_connection_mac_address)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_ip{ EEPROM };
  21. EEPROMVariable<IPAddress, 4 + decltype(ethernet_connection_ip)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_subnet{ EEPROM };
  22. EEPROMVariable<IPAddress, 4 + decltype(ethernet_connection_subnet)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_gateway_ip{ EEPROM };
  23. EEPROMVariable<IPAddress, 4 + decltype(ethernet_connection_gateway_ip)::eeprom_address, DefaultEepromConverter<IPAddress>> ethernet_connection_dns_ip{ EEPROM };
  24. // Web server settings
  25. EEPROMVariable<uint16_t, 400, DefaultEepromConverter<uint16_t>> web_server_port{ EEPROM };
  26. // Redis connection settings
  27. EEPROMVariable<IPAddress, 1024, DefaultEepromConverter<IPAddress>> redis_connection_ip{ EEPROM };
  28. EEPROMVariable<uint16_t, 4 + decltype(redis_connection_ip)::eeprom_address, DefaultEepromConverter<uint16_t>> redis_connection_port{ EEPROM };
  29. EEPROMVariable<char[200], 2 + decltype(redis_connection_port)::eeprom_address, FixedSizeCharStringEepromConverter<200>> redis_connection_client{ EEPROM };
  30. EEPROMVariable<char[200], 200 + decltype(redis_connection_client)::eeprom_address, FixedSizeCharStringEepromConverter<200>> redis_connection_password{ EEPROM };
  31.  
  32. // Connections
  33. EthernetWebServer web_server{ 8090 };
  34. EthernetClient redis_tcp_connection{};
  35. Redis redis_connection{ redis_tcp_connection };
  36.  
  37. // Temporary buffers
  38.  
  39. int32_t sensor_value{ std::numeric_limits<int32_t>::min() };
  40.  
  41. void setup() {
  42.   pinMode(GPIO_NUM_4, INPUT);
  43.   EEPROM.begin(4096);
  44.  
  45.   Serial.begin(9600);
  46.   while(not Serial);
  47.   Serial.println();
  48.  
  49.   Serial.println("Begin Ethernet");
  50.   // You can use Ethernet.init(pin) to configure the CS pin
  51.   //Ethernet.init(10);  // Most Arduino shields
  52.   Ethernet.init(5);   // MKR ETH Shield
  53.   //Ethernet.init(0);   // Teensy 2.0
  54.   //Ethernet.init(20);  // Teensy++ 2.0
  55.   //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  56.   //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet
  57.  
  58.   if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  59.     Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
  60.     while (true) {
  61.       delay(1); // do nothing, no point running without Ethernet hardware
  62.     }
  63.   }
  64.   if (Ethernet.linkStatus() == LinkOFF) {
  65.     Serial.println("Ethernet cable is not connected.");
  66.   }
  67.  
  68.   const uint8_t mac_address[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
  69.   Ethernet.begin(mac_address,
  70.     { 192, 168, 1, 10 },
  71.     { 192, 168, 1, 1 },
  72.     { 192, 168, 1, 1 },
  73.     { 255, 255, 255, 0 }
  74.   );
  75.   delay(500);
  76.  
  77.   Serial.print("Local IP: ");
  78.   Serial.println(Ethernet.localIP());
  79.   Serial.print("Subnet Mask: ");
  80.   Serial.println(Ethernet.subnetMask());
  81.   Serial.print("Gateway IP: ");
  82.   Serial.println(Ethernet.gatewayIP());
  83.   Serial.print("DNS Server: ");
  84.   Serial.println(Ethernet.dnsServerIP());
  85.   Serial.print("Link status: ");
  86.   Serial.println(Ethernet.linkStatus());
  87.   Serial.print("Hardware status: ");
  88.   Serial.println(Ethernet.hardwareStatus());
  89.  
  90.   Serial.println("Initilize redis:");
  91.   Serial.print("\tTCP port:");
  92.   Serial.print(redis_tcp_connection.connect({ 192, 168, 1, 2 }, 6379));
  93.   Serial.print("; Connected: ");
  94.   Serial.println(redis_tcp_connection.connected());
  95.   Serial.print("\tRedis authentication: ");
  96.   Serial.println(redis_connection.authenticate(""));
  97.  
  98.  
  99.   {  
  100.     web_server.on("/", []() {
  101.       Serial.println("on [/]");
  102.       web_server.send_P(200, "text/plain", "Ok");
  103.     });
  104.     web_server.on("/sensor/value", HTTP_GET, []() {
  105.       Serial.println("on [/sensor/value][GET]");
  106.       web_server.send(200, "text/plain", String{ sensor_value }.c_str());
  107.     });
  108.     web_server.onNotFound([]() {
  109.       Serial.print("on [NotFound]: ");
  110.       Serial.println(web_server.uri());
  111.       web_server.send(200, "text/plain", "Not found...");
  112.     });
  113.     web_server.begin();
  114.   }
  115.   /*
  116.     web_server.on("/values/1", HTTP_POST, []() {
  117.       Serial.println("on [/values/1][POST]");
  118.       if(not web_server.hasArg("value")) {
  119.         web_server.send(400, "text/plain", "Invalid request");
  120.       } else {
  121.         value_1 = web_server.arg("value").toInt();
  122.         web_server.send(200, "text/plain", "Ok");
  123.       }
  124.     });
  125.   */
  126. }
  127.  
  128. void loop() {
  129.   Ethernet.maintain();
  130.   web_server.handleClient();
  131.  
  132.   static unsigned long last_time = 0;
  133.   if(const auto current_time = millis(); last_time < current_time - 5000) {
  134.     last_time = current_time;
  135.  
  136.     sensor_value = analogRead(GPIO_NUM_4);
  137.     Serial.print("Alive: ");
  138.     Serial.print(sensor_value);
  139.     Serial.print("; Redis: ");
  140.     Serial.println(redis_connection.set("sensor:test", String{ sensor_value }.c_str()));
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement