experiments with Pylontech/GroWatt PV tech
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.3 KiB

  1. // MQTT server + client for ESP32
  2. #include"sMQTTBroker.h"
  3. #include <WiFi.h>
  4. #include <PubSubClient.h>
  5. #include <WiFiClientSecure.h>
  6. #include <MQTT.h>
  7. const char* ssid = "NOS-3B26"; // Setubal
  8. const char* password = "RMKSX2GL"; // Setubal
  9. //const char* ssid = "MEO-AA9030"; // Andre
  10. //const char* password = "81070ce635"; // Andre
  11. // Set your Static IP address
  12. IPAddress local_IP(192, 168, 1, 123);
  13. // Set your Gateway IP address
  14. IPAddress gateway(192, 168, 1, 1);
  15. IPAddress subnet(255, 255, 0, 0);
  16. IPAddress primaryDNS(8, 8, 8, 8); // optional
  17. IPAddress secondaryDNS(8, 8, 4, 4); // optional
  18. sMQTTBroker broker;
  19. WiFiClient net;
  20. //WiFiClientSecure net;
  21. MQTTClient mqtt_client(1024);
  22. //WiFiClient espClient;
  23. //PubSubClient client(espClient);
  24. //long lastMsg = 0;
  25. //char msg[50];
  26. //int value = 0;
  27. unsigned long lastMillis = 0;
  28. //////////////////////////////////
  29. void WiFiconnect(){
  30. int nn = 0;
  31. while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
  32. delay(500);
  33. if(nn<9){
  34. Serial.print(".");
  35. nn++;
  36. }else{
  37. nn=0;
  38. Serial.println(".");
  39. }
  40. }
  41. if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  42. Serial.println("STA Failed to configure");
  43. }
  44. Serial.println("Connection established!");
  45. Serial.print("IP address:\t");
  46. Serial.println(WiFi.localIP());
  47. // Serial.print("\nconnecting...");
  48. //// while (!mqtt_client.connect("bsidebotham", "", "")) {
  49. // while (!mqtt_client.connect("tezmqlientx2323", "", "")) {
  50. // Serial.print(".");
  51. // delay(1000);
  52. // }
  53. //
  54. // Serial.println("\nconnected!");
  55. //
  56. // mqtt_client.subscribe("/hello");
  57. //
  58. // // MQTT brokers usually use port 8883 for secure connections.
  59. //// client.begin("broker.shiftr.io", 8883, net);
  60. // mqtt_client.begin("192.168.1.123", 1883, net);
  61. // mqtt_client.onMessage(messageReceived);
  62. }
  63. //////////////////////////////
  64. void messageReceived(String &topic, String &payload) {
  65. Serial.println("incoming: " + topic + " - " + payload);
  66. // Note: Do not use the client in the callback to publish, subscribe or
  67. // unsubscribe as it may cause deadlocks when other things arrive while
  68. // sending and receiving acknowledgments. Instead, change a global variable,
  69. // or push to a queue and handle it in the loop after calling `client.loop()`.
  70. }
  71. //////////////////////////////
  72. void setup()
  73. {
  74. Serial.begin(115200);
  75. // const char* ssid = "MEO-AA9030"; // The SSID (name) of the Wi-Fi network you want to connect to
  76. // const char* password = "81070ce635"; // The password of the Wi-Fi network
  77. Serial.print("Connecting to ");
  78. Serial.println(ssid);
  79. WiFi.begin(ssid, password);
  80. const unsigned short mqttPort=1883;
  81. broker.init(mqttPort);
  82. // all done
  83. WiFiconnect();
  84. }
  85. //////////////////////////////
  86. void loop()
  87. {
  88. broker.update();
  89. // mqtt_client.loop();
  90. delay(10); // <- fixes some issues with WiFi stability
  91. // publish a message roughly every second.
  92. // if (millis() - lastMillis > 1000) {
  93. // if (!mqtt_client.connected()) {
  94. // Serial.print("lastError: ");
  95. // Serial.println(mqtt_client.lastError());
  96. // WiFiconnect();
  97. // }
  98. // lastMillis = millis();
  99. // int rnum = random(1,23);
  100. // mqtt_client.publish("/hello", "ciccio" + String(rnum));
  101. // }
  102. //
  103. }