Advertisement
Guest User

Untitled

a guest
May 4th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.05 KB | None | 0 0
  1.  
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include <ESP8266WiFi.h>
  5. #include <Adafruit_GFX.h>
  6. #include <Adafruit_SSD1306.h>
  7. #include <MQTT.h>
  8.  
  9. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  10. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  11.  
  12. // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
  13. #define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  14. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  15.  
  16. // WiFi credentials
  17. const char * ssid = "XXXXXXXXXX";      
  18. const char * password = "XXXXXXXXXX";  
  19. // MQTT Broker
  20. const char *mqtt_broker = "core-mosquitto";
  21. const char *topic = "esp32/robot_esp";
  22. const char *mqtt_username = "XXXXXXXXXX"; // for homeassistant
  23. const char *mqtt_password = "XXXXXXXXXX"; // for homeassistant
  24. const int mqtt_port = 1883;
  25. // MQTT Network
  26. const char* mqtt_server = "192.168.0.62"; // this would be the internal server i would like to connect
  27.  
  28.  
  29. WiFiClient espClient;
  30. MQTTClient mqttClient;
  31.  
  32. void setup_wifi() {
  33.   delay(10);
  34.   // We start by connecting to a WiFi network
  35.   Serial.println();
  36.   Serial.print("Connecting to ");
  37.   Serial.println(ssid);
  38.   WiFi.begin(ssid, password);
  39.  
  40.   while (WiFi.status() != WL_CONNECTED) {
  41.     delay(500);
  42.     Serial.print(".");
  43.   }
  44.   Serial.println();
  45.   Serial.println("WiFi connected");
  46.   Serial.print("IP address: ");
  47.   Serial.println(WiFi.localIP());
  48. }
  49. void setup_mqtt() {
  50.   // Set up the MQTT client
  51.   Serial.print("\nconnecting...");
  52.   unsigned long startAttemptTime = millis();
  53.   while (!mqttClient.connect("wYh0FE4G3wM-kM6Zh", "public", "public")) {
  54.     Serial.print(".");
  55.     delay(1000);
  56.     if (millis() - startAttemptTime > 10000) { // Timeout after 10 seconds
  57.       Serial.println("\nFailed to connect to MQTT broker. Restarting...");
  58.       ESP.restart(); // Restart the ESP8266
  59.     }
  60.   }
  61.  
  62.   Serial.println("\nconnected!");
  63.  
  64. }
  65. void messageReceived(String &topic, String &payload) {
  66.   Serial.println("incoming: " + topic + " - " + payload);
  67.  
  68.   // Note: Do not use the client in the callback to publish, subscribe or
  69.   // unsubscribe as it may cause deadlocks when other things arrive while
  70.   // sending and receiving acknowledgments. Instead, change a global variable,
  71.   // or push to a queue and handle it in the loop after calling `client.loop()`.
  72. }
  73. void setup() {
  74.   Serial.begin(9600);
  75.   delay(1000);
  76.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  77.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  78.     Serial.println(F("SSD1306 allocation failed"));
  79.     for(;;); // Don't proceed, loop forever
  80.   }
  81.   setup_wifi(); // Connect to WiFi
  82.   Serial.println("Connected to WIFI.");
  83.   mqttClient.begin("public.cloud.shiftr.io", espClient);
  84.   mqttClient.onMessage(messageReceived);
  85.   //setup_mqtt();
  86.   // Commented out because crash
  87. }
  88.  
  89. // Function to draw scewed rectangle
  90. void draw_scewed_rect(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int color) {
  91.   display.fillTriangle(x1, y1, x2, y2, x3, y3, color);
  92.   display.fillTriangle(x1, y1, x3, y3, x4, y4, color);
  93. }
  94.  
  95. void draw_eye(int s = 0, int expression = 0, int w = 24, int h = 35) {
  96.   int x = 32 * (s == 0 ? 1 : 3); // CENTERED x position
  97.   x -= w / 2; // Adjust x position to top left corner
  98.   x += (s == 0 ? 10 : -10); // move towards center
  99.   int y = 32 - (h / 2); // Adjust y position to top left corner
  100.  
  101.   display.fillRoundRect(x, y, w, h, 5, SSD1306_WHITE);
  102.   switch(expression) {
  103.     case 0: // Normal
  104.       break;
  105.     case 1: // suspicious
  106.       draw_scewed_rect(x - 2, y - 2, x + w + 2, y - 2, x + w + 2, y + 3, x - 2, y + 9, SSD1306_BLACK);
  107.     default:
  108.       break;
  109.   }
  110. }
  111.  
  112. void loop() {
  113.   // Draw the robot face with a different expression
  114.   display.clearDisplay();
  115.   draw_eye(0, 0); // Left eye
  116.   draw_eye(1, 1); // Right eye with suspicious expression
  117.   display.display();
  118.   delay(2000); // Wait for 2 seconds
  119.   display.clearDisplay();
  120.   draw_eye(0, 0); // Left eye
  121.   draw_eye(1, 0); // Right eye
  122.   display.display();
  123.   delay(2000); // Wait for 2 seconds
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement