daily pastebin goal
90%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Connected Things 2019
  2. //
  3. // UDP transmit and receive example.
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiUDP.h>
  7.  
  8. // WiFi variables
  9. const char* ssid = "BTUgoo";        // ssid
  10. const char* password = "blowItUp"; // password
  11. boolean wifiConnected = false;
  12.  
  13. // My IP address:
  14. IPAddress ip(10, 0, 0, 100);
  15. IPAddress gateway(10, 0, 0, 1);
  16. IPAddress subnet(255, 255, 255, 0);
  17.  
  18. #define nListeners 2
  19. #define ledPin 16     // built in LED pin on nodeMCU
  20. #define buttonPin 5   // GPIO 5 = nodeMCU D1
  21. #define buttonOtherPin 4   // GPIO 4 = nodeMCU D1
  22. #define slider A0   //analog input
  23.  
  24. String potReading ;
  25. // keep track of button state to check if it has changed
  26. int buttonState;
  27. int lastButtonState;
  28.  
  29. int buttonState1;
  30. int lastButtonState1;
  31.  
  32. // this number will increment and be sent to the remote IP address list
  33. int beat = 0;
  34.  
  35. int analogIn;    // stores input values
  36. int digitalIn;
  37.  
  38. // Define output strings
  39.  
  40. String str_analogIn = "0000";
  41. String str_digitalIn = "0";
  42. String str_out;
  43.  
  44. // list of IP addresses to send to:
  45. IPAddress remoteList[nListeners] =
  46. {
  47.   IPAddress(10, 0, 0, 101),
  48.   IPAddress(10, 0, 0, 102)
  49. };
  50.  
  51. // UDP variables
  52. WiFiUDP UDP;
  53. unsigned int localPort = 8000;
  54. boolean udpConnected = false;
  55. char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
  56.  
  57. // char array (string) to send
  58. char  msgBuffer[] = "pressing a button";   // this could be up to 8192 bytes
  59.  
  60.  
  61. // *************************
  62. // Initialization
  63. // *************************
  64. void setup() {
  65.   // Initialise Serial connection
  66.   Serial.begin(115200);
  67.  
  68.   // Initialise wifi connection
  69.   wifiConnected = connectWifi();
  70.  
  71.   // only proceed if wifi connection successful
  72.   if (wifiConnected) {
  73.     udpConnected = connectUDP();
  74.     if (udpConnected) {
  75.       // initialize pins
  76.       pinMode(ledPin, OUTPUT);
  77.       pinMode(buttonPin, INPUT_PULLUP);
  78.       pinMode(buttonOtherPin, INPUT_PULLUP) ;
  79.     }
  80.   }
  81. }
  82.  
  83. // *************************
  84. // main loop
  85. // *************************
  86. void loop() {
  87.  
  88.   // check if the WiFi and UDP connections were successful
  89.   if (wifiConnected) {
  90.     if (udpConnected) {
  91.  
  92.       String potString ;
  93.       int potValue = analogRead(A0);
  94.       int analogValue = map(potValue, 0, 1023, 0, 255);
  95.       analogIn = analogValue ;
  96.      
  97.       //potString = String(map(analogRead(A0),0,1023,0,255));
  98.       Serial.println(analogIn);
  99.      
  100.       // Convert analog read integer to string
  101.       if (analogIn < 100 && analogIn > 9) {
  102.         str_analogIn = "0" + String(analogIn);
  103.       }
  104.       else if (analogIn < 10) {
  105.         str_analogIn = "00" + String(analogIn);
  106.       }
  107.       else {
  108.         str_analogIn = String(analogIn);
  109.       }
  110.    
  111.      
  112.       // Combine analog and digital sensors into one string (payload)
  113.       str_out = "msg" + str_analogIn ;
  114.    
  115.       // Compose output character
  116.       static char *msg = str_out.c_str();
  117.       Serial.print("sending message: ");
  118.       Serial.println(msg);
  119.       Serial.print("message length: ");
  120.       Serial.println(strlen(msg));
  121.    
  122.       //rf_driver.send((uint8_t *)msg, strlen(msg));
  123.       //rf_driver.waitPacketSent();
  124.      
  125.         for (int i = 0; i < nListeners; i++) {
  126.         UDP.beginPacket(remoteList[i], localPort);
  127.         UDP.write(msg);
  128.         int success = UDP.endPacket();
  129.       }
  130.  
  131.  
  132.       //Serial.print("send number:");
  133.       //Serial.println(beat);
  134.  
  135.       // UDP message 1:
  136.  
  137.       // Only sending a 2 byte message here
  138.       // to represent up to a 16 bit integer
  139.       byte message[2];
  140.       message[0] = 0;
  141.       message[1] = beat++;
  142.       beat %= 16;           // 2 bytes can hold integer value up to  2^16 = 65,535
  143.  
  144.       // broadcast this value to all the IP Addresses in the list:
  145.       for (int i = 0; i < nListeners; i++) {
  146.         UDP.beginPacket(remoteList[i], localPort);
  147.         UDP.write(message, sizeof(message));
  148.         int success = UDP.endPacket();
  149.       }
  150.       buttonState = digitalRead(buttonPin);
  151.       buttonState1 = digitalRead(buttonOtherPin) ;
  152.  
  153.       // UDP message 2:
  154.  
  155.       // if the button has changed from HIGH to LOW
  156.       if (buttonState == LOW && buttonState != lastButtonState) {
  157.         // when button is pressed, send a message to the first IP address onlye:
  158.         Serial.println("ow");
  159.         UDP.beginPacket(remoteList[0], localPort);
  160.         //UDP.write(msgBuffer);
  161.         UDP.write(1);
  162.         int success = UDP.endPacket();
  163.       }
  164.  
  165.             // if the button has changed from HIGH to LOW
  166.       if (buttonState1 == HIGH && buttonState1 != lastButtonState1) {
  167.         // when button is pressed, send a message to the first IP address onlye:
  168.         Serial.println("yo yo");
  169.         UDP.beginPacket(remoteList[0], localPort);
  170.         //UDP.write(msgBuffer);
  171.         UDP.write(2);
  172.         int success = UDP.endPacket();
  173.       }
  174.  
  175.       // save button state to compare next time
  176.       lastButtonState = buttonState;
  177.       delay(100); // pause a moment
  178.  
  179.  
  180.       // save button state to compare next time
  181.       lastButtonState1 = buttonState1;
  182.       delay(100); // pause a moment
  183.      
  184.     }
  185.   }
  186. }
  187.  
  188.  
  189. // *************************
  190. // connect to UDP – returns true if successful or false if not
  191. // *************************
  192. boolean connectUDP() {
  193.   boolean state = false;
  194.  
  195.   Serial.println("");
  196.   Serial.println("Connecting to UDP");
  197.  
  198.   if (UDP.begin(localPort) == 1) {
  199.     Serial.println("Connection successful");
  200.     state = true;
  201.   }
  202.   else {
  203.     Serial.println("Connection failed");
  204.   }
  205.  
  206.   return state;
  207. }
  208.  
  209.  
  210. // *************************
  211. // connect to wifi – returns true if successful or false if not
  212. // *************************
  213. boolean connectWifi() {
  214.   boolean state = true;
  215.   int i = 0;
  216.   WiFi.begin(ssid, password);
  217.   WiFi.config(ip, gateway, subnet);
  218.   Serial.println("");
  219.   Serial.println("Connecting to WiFi");
  220.  
  221.   // Wait for connection
  222.   Serial.print("Connecting");
  223.   while (WiFi.status() != WL_CONNECTED) {
  224.     delay(500);
  225.     Serial.print(".");
  226.     if (i > 10) {
  227.       state = false;
  228.       break;
  229.     }
  230.     i++;
  231.     Serial.print("Try ");
  232.     Serial.println(i);
  233.   }
  234.   if (state) {
  235.     Serial.println("");
  236.     Serial.print("Connected to ");
  237.     Serial.println(ssid);
  238.     Serial.print("IP address: ");
  239.     Serial.println(WiFi.localIP());
  240.   }
  241.   else {
  242.     Serial.println("");
  243.     Serial.println("Connection failed.");
  244.   }
  245.   return state;
  246. }
  247.  
  248. void udpReset() {
  249.   // asm volatile ("  jmp 0");
  250.   if (wifiConnected) {
  251.     udpConnected = connectUDP();
  252.     if (udpConnected) {
  253.       Serial.print(" ...success");
  254.     }
  255.     else {
  256.       Serial.print(" on nose!");
  257.  
  258.     }
  259.   }
  260. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top