Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <PubSubClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266mDNS.h>
  4. #include <SimpleTimer.h>
  5. #include <Roomba.h>
  6.  
  7.  
  8. //USER CONFIGURED SECTION START//
  9. const char* ssid = "House-White 2.4Ghz";
  10. const char* password = "ikbencool123";
  11. const char* mqtt_server = "192.168.10.11";
  12. const int mqtt_port = 1883;
  13. const char *mqtt_user = "mqttadmin";
  14. const char *mqtt_pass = "bBhKY30wX64eaR9q";
  15. const char *mqtt_client_name = "Roomba650"; // Client connections can't have the same connection name
  16. //USER CONFIGURED SECTION END//
  17.  
  18.  
  19. WiFiClient espClient;
  20. PubSubClient client(espClient);
  21. SimpleTimer timer;
  22. Roomba roomba(&Serial, Roomba::Baud115200);
  23.  
  24.  
  25. // Variables
  26. bool boot = true;
  27. long battery_Current_mAh = 0;
  28. long battery_Voltage = 0;
  29. long battery_Total_mAh = 0;
  30. long battery_percent = 0;
  31. char battery_percent_send[50];
  32. char battery_Current_mAh_send[50];
  33. uint8_t tempBuf[10];
  34.  
  35. //Functions
  36.  
  37. void setup_wifi()
  38. {
  39.   WiFi.begin(ssid, password);
  40.   while (WiFi.status() != WL_CONNECTED)
  41.   {
  42.     delay(500);
  43.   }
  44. }
  45.  
  46. void reconnect()
  47. {
  48.   // Loop until we're reconnected
  49.   int retries = 0;
  50.   while (!client.connected())
  51.   {
  52.     if(retries < 50)
  53.     {
  54.       // Attempt to connect
  55.       if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, "roomba/status", 0, 0, "Dead Somewhere"))
  56.       {
  57.         // Once connected, publish an announcement...
  58.         if(boot == false)
  59.         {
  60.           client.publish("checkIn/roomba", "Reconnected");
  61.         }
  62.         if(boot == true)
  63.         {
  64.           client.publish("checkIn/roomba", "Rebooted");
  65.           boot = false;
  66.         }
  67.         // ... and resubscribe
  68.         client.subscribe("roomba/commands");
  69.       }
  70.       else
  71.       {
  72.         retries++;
  73.         // Wait 5 seconds before retrying
  74.         delay(5000);
  75.       }
  76.     }
  77.     if(retries >= 50)
  78.     {
  79.     ESP.restart();
  80.     }
  81.   }
  82. }
  83.  
  84. void callback(char* topic, byte* payload, unsigned int length)
  85. {
  86.   String newTopic = topic;
  87.   payload[length] = '\0';
  88.   String newPayload = String((char *)payload);
  89.   if (newTopic == "roomba/commands")
  90.   {
  91.     if (newPayload == "start")
  92.     {
  93.       startCleaning();
  94.     }
  95.     if (newPayload == "stop")
  96.     {
  97.       stopCleaning();
  98.     }
  99.   }
  100. }
  101.  
  102.  
  103. void startCleaning()
  104. {
  105.   Serial.write(128);
  106.   delay(50);
  107.   Serial.write(131);
  108.   delay(50);
  109.   Serial.write(135);
  110.   client.publish("roomba/status", "Cleaning");
  111. }
  112.  
  113. void stopCleaning()
  114. {
  115.   Serial.write(128);
  116.   delay(50);
  117.   Serial.write(131);
  118.   delay(50);
  119.   Serial.write(143);
  120.   client.publish("roomba/status", "Returning");
  121. }
  122.  
  123. void sendInfoRoomba()
  124. {
  125.   roomba.start();
  126.   roomba.getSensors(21, tempBuf, 1);
  127.   battery_Voltage = tempBuf[0];
  128.   delay(50);
  129.   roomba.getSensors(25, tempBuf, 2);
  130.   battery_Current_mAh = tempBuf[1]+256*tempBuf[0];
  131.   delay(50);
  132.   roomba.getSensors(26, tempBuf, 2);
  133.   battery_Total_mAh = tempBuf[1]+256*tempBuf[0];
  134.   if(battery_Total_mAh != 0)
  135.   {
  136.     int nBatPcent = 100*battery_Current_mAh/battery_Total_mAh;
  137.     String temp_str2 = String(nBatPcent);
  138.     temp_str2.toCharArray(battery_percent_send, temp_str2.length() + 1); //packaging up the data to publish to mqtt
  139.     client.publish("roomba/battery", battery_percent_send);
  140.   }
  141.   if(battery_Total_mAh == 0)
  142.   {  
  143.     client.publish("roomba/battery", "NO DATA");
  144.   }
  145.   String temp_str = String(battery_Voltage);
  146.   temp_str.toCharArray(battery_Current_mAh_send, temp_str.length() + 1); //packaging up the data to publish to mqtt
  147.   client.publish("roomba/charging", battery_Current_mAh_send);
  148. }
  149.  
  150.  
  151.  
  152. void setup()
  153. {
  154.   Serial.begin(115200);
  155.   Serial.write(129);
  156.   delay(50);
  157.   Serial.write(11);
  158.   delay(50);
  159.   setup_wifi();
  160.   client.setServer(mqtt_server, mqtt_port);
  161.   client.setCallback(callback);
  162.   timer.setInterval(5000, sendInfoRoomba);
  163. }
  164.  
  165. void loop()
  166. {
  167.   if (!client.connected())
  168.   {
  169.     reconnect();
  170.   }
  171.   client.loop();
  172.   timer.run();
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement