Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ArduinoJson.h>
  2. #include <SoftwareSerial.h>
  3. SoftwareSerial mySerial(10, 11);
  4.  
  5. String ID = "a";
  6. uint8_t MaxHops = 10;
  7.  
  8. void setup() {
  9.   Serial.begin(115200);
  10.  
  11.   while (!Serial) { ; }
  12.   Serial.println("Serial started!");
  13.   Serial.print("Your ID: ");
  14.   Serial.println(ID);
  15.   Serial.println("You can now send your messages with \"ID:MESSAGE\"");
  16.  
  17.   mySerial.begin(19200);
  18. }
  19.  
  20. void sendData(String toId, String msg, int hops) {
  21.   DynamicJsonBuffer jsonBuffer;
  22.   JsonObject& json = jsonBuffer.createObject();
  23.  
  24.   json["fromId"] = ID;
  25.   json["toId"] = toId;
  26.   json["msg"] = msg;
  27.   json["hops"] = hops;
  28.  
  29.   json.printTo(mySerial);
  30. }
  31.  
  32. JsonObject& readData(String json) {
  33.   DynamicJsonBuffer jsonBuffer;
  34.   JsonObject& json_obj = jsonBuffer.parseObject(json);
  35.  
  36.   return json_obj;
  37. }
  38.  
  39. void loop() {
  40.   static String toId = "";
  41.   static String msg = "";
  42.   static boolean split = false;
  43.   while (Serial.available() >= 1) {
  44.     char c = Serial.read();
  45.  
  46.     if (!split) {
  47.       if (c == 58) {
  48.         split = true;
  49.       } else {
  50.         toId += c;
  51.       }
  52.     } else {
  53.       if (c == 10) {
  54.         // Send Data to Next Arduino
  55.         Serial.print("Sending \"");
  56.         Serial.print(msg);
  57.         Serial.print("\" to ID ");
  58.         Serial.println(toId);
  59.         sendData(toId, msg, 1);
  60.         toId = "";
  61.         msg = "";
  62.         split = false;
  63.       } else {
  64.         msg += c;
  65.       }
  66.     }
  67.   }
  68.  
  69.   while (mySerial.available() >= 1) {
  70.     String json = mySerial.readString();
  71.  
  72.     JsonObject& json_obj = readData(json);
  73.     int hops = json_obj["hops"];
  74.     String fromId = json_obj["fromId"];
  75.     String msg = json_obj["msg"];
  76.     String toId = json_obj["toId"];
  77.  
  78.     if (toId.equals(ID)) {
  79.       Serial.print(fromId);
  80.       Serial.print(" (HOPS: ");
  81.       Serial.print(hops);
  82.       Serial.print(")");
  83.       Serial.print(" -> ");
  84.       Serial.println(msg);
  85.     } else {
  86.       if (hops < MaxHops) {
  87.         json_obj["hops"] = hops + 1;
  88.         json_obj.printTo(mySerial);
  89.       } else {
  90.         Serial.print("MSG from ");
  91.         Serial.print(fromId);
  92.         Serial.print(" to ");
  93.         Serial.print(toId);
  94.         Serial.print(" Reached ");
  95.         Serial.print(MaxHops);
  96.         Serial.println(" HOPS");
  97.       }
  98.     }
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement