{"action":"reopened","issue":{"url":"https://api.github.com/repos/jivjen/Nero/issues/1","repository_url":"https://api.github.com/repos/jivjen/Nero","labels_url":"https://api.github.com/repos/jivjen/Nero/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/jivjen/Nero/issues/1/comments","events_url":"https://api.github.com/repos/jivjen/Nero/issues/1/events","html_url":"https://github.com/jivjen/Nero/issues/1","id":362625479,"node_id":"MDU6SXNzdWUzNjI2MjU0Nzk=","number":1,"title":"NodeMCU Code","user":{"login":"jivjen","id":25795490,"node_id":"MDQ6VXNlcjI1Nzk1NDkw","avatar_url":"https://avatars0.githubusercontent.com/u/25795490?v=4","gravatar_id":"","url":"https://api.github.com/users/jivjen","html_url":"https://github.com/jivjen","followers_url":"https://api.github.com/users/jivjen/followers","following_url":"https://api.github.com/users/jivjen/following{/other_user}","gists_url":"https://api.github.com/users/jivjen/gists{/gist_id}","starred_url":"https://api.github.com/users/jivjen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jivjen/subscriptions","organizations_url":"https://api.github.com/users/jivjen/orgs","repos_url":"https://api.github.com/users/jivjen/repos","events_url":"https://api.github.com/users/jivjen/events{/privacy}","received_events_url":"https://api.github.com/users/jivjen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-09-21T13:52:09Z","updated_at":"2018-09-21T13:52:30Z","closed_at":null,"author_association":"OWNER","body":"/*\r\n This code basically sets up an ESP8266 board as a MQTT client and helps in subscribing to desired topics that are published.\r\n\r\n It is required that the board in the 'Tools' section must be selected to NodeMCU and the ESP8266 drivers are installed.\r\n The baud rate must be set at 115200\r\n Here we are operating three servo motors using the nodeMCU based on the values received via the MQTT topic\r\n \r\n*/\r\n\r\n#include <ESP8266WiFi.h>\r\n#include <PubSubClient.h>\r\n#include <Servo.h>\r\n// Creating objects for the 3 servo motors\r\nServo servo;\r\nServo servo1;\r\nServo servo2;\r\n// The pins we have used for the servo motors are D1,D2 and D3\r\n// Update these with values suitable for your network.\r\n\r\nconst char* ssid = \"CDI\";\r\nconst char* password = \"Cdi*1717\";\r\nconst char* mqtt_server = \"172.16.73.4\";\r\n\r\nWiFiClient espClient;\r\nPubSubClient client(espClient);\r\nlong lastMsg = 0;\r\nchar msg[50];\r\nint value = 0;\r\nchar old ;\r\n\r\nvoid setup() {\r\n  //Initial Setup Code\r\n  Serial.begin(115200);\r\n  setup_wifi();\r\n  client.setServer(mqtt_server, 1883);\r\n  client.setCallback(callback);\r\n  delay(1000);\r\n}\r\n//Code for connecting to the wifi\r\nvoid setup_wifi() {\r\n\r\n  delay(10);\r\n  // We start by connecting to a WiFi network\r\n  Serial.println();\r\n  Serial.print(\"Connecting to \");\r\n  Serial.println(ssid);\r\n\r\n  WiFi.begin(ssid, password);\r\n\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    delay(500);\r\n    Serial.print(\".\");\r\n  }\r\n\r\n  Serial.println(\"\");\r\n  Serial.println(\"WiFi connected\");\r\n  Serial.println(\"IP address: \");\r\n  Serial.println(WiFi.localIP());\r\n}\r\n\r\n//This function is responsible for subscribing to a topic and receiving the values from it \r\nvoid callback(char* topic, byte* payload, unsigned int length) {\r\n  Serial.print(\"Message arrived [\");\r\n  Serial.print(topic);\r\n  Serial.print(\"] \");\r\n \r\n  old = (char)payload[0];\r\n  //detach each time to avoid jitter of the servo motors\r\n  servo2.detach();\r\n  servo.detach();\r\n  servo1.detach();\r\n for (int i = 0; i < length; i++) {\r\n  Serial.println(\"Received message is :\");\r\n  Serial.println((char)payload[i]);\r\n  servo.detach();\r\n  servo1.detach();\r\n  servo2.detach();\r\n  if((char)payload[i] == '1')\r\n  {\r\n\r\n    servo.detach();\r\n    servo2.detach();\r\n    servo1.detach();\r\n    servo.attach(0);\r\n    servo.write(89);\r\n    delay(1000);\r\n    servo.detach();\r\n    servo1.attach(4);\r\n    servo1.write(89);\r\n    delay(1000);\r\n    servo1.detach();\r\n    servo2.detach();\r\n    old='1';\r\n    break;\r\n  }\r\nif((char)payload[i] == '2')\r\n  {\r\n    servo.detach();\r\n    servo2.detach();\r\n    servo1.detach();\r\n    servo1.attach(4);\r\n    servo1.write(89);\r\n    delay(1000);\r\n    servo1.detach();\r\n    servo2.detach();\r\n    old='2';\r\n    break;\r\n  }\r\n if((char)payload[i] == '3')\r\n  {\r\n    servo.detach();\r\n    servo2.detach();\r\n    servo1.detach();\r\n    servo.attach(0);\r\n    servo.write(89);\r\n    delay(1000);\r\n    servo.detach();\r\n    servo2.detach();\r\n    old='3';\r\n    break;\r\n  }\r\n if((char)payload[i] == '4')\r\n  {\r\n    servo.detach();\r\n    servo1.detach();\r\n    servo2.detach();\r\n    servo.attach(0);\r\n    servo.write(180);\r\n    delay(1000);\r\n    servo.detach();\r\n    servo1.attach(4);\r\n    servo1.write(180);\r\n    delay(1000);\r\n    servo1.detach();\r\n    servo2.detach();\r\n    old='4';\r\n    break;\r\n  }\r\n  if((char)payload[i]=='5')\r\n  {\r\n    servo2.detach();\r\n    servo2.attach(5);\r\n    servo2.write(179);\r\n    delay(1000);\r\n    servo2.detach();\r\n  }\r\n  if((char)payload[i]=='6')\r\n  { servo2.detach();\r\n    servo2.attach(5);\r\n    servo2.write(89);\r\n    delay(1000);\r\n    servo2.detach();\r\n   }\r\n  if((char)payload[i]=='7')\r\n  { servo2.detach();\r\n    servo2.attach(5);\r\n    servo2.write(2);\r\n    delay(1000);\r\n    servo2.detach();\r\n  }\r\n    servo2.detach();\r\n  }\r\n\r\n    servo.detach();\r\n    servo1.detach();\r\n    servo2.detach();\r\n}\r\n//This function is to reconnect once the connection is lost\r\nvoid reconnect() {\r\n  // Loop until we're reconnected\r\n  while (!client.connected()) {\r\n    Serial.print(\"Attempting MQTT connection...\");\r\n    // Attempt to connect\r\n    if (client.connect(\"ESP8266Client\")) {\r\n      Serial.println(\"connected\");\r\n      // Once connected, publish an announcement...\r\n      \r\n      // ... and resubscribe\r\n      client.subscribe(\"Ultrasonic\");\r\n    } else {\r\n      Serial.print(\"failed, rc=\");\r\n      Serial.print(client.state());\r\n      Serial.println(\" try again in 5 seconds\");\r\n      // Wait 5 seconds before retrying\r\n      delay(5000);\r\n    }\r\n  }\r\n}\r\nvoid loop() {\r\n\r\n  if (!client.connected()) {\r\n    reconnect();\r\n  }\r\n  client.loop();\r\n\r\n \r\n}"}}
{"actor":{"display_login":"jivjen"}}