Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Connected Things 2019
- //
- // UDP transmit and receive example.
- #include <ESP8266WiFi.h>
- #include <WiFiUDP.h>
- // WiFi variables
- const char* ssid = "BTUgoo"; // ssid
- const char* password = "blowItUp"; // password
- boolean wifiConnected = false;
- // My IP address:
- IPAddress ip(10, 0, 0, 100);
- IPAddress gateway(10, 0, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- #define nListeners 2
- #define ledPin 16 // built in LED pin on nodeMCU
- #define buttonPin 5 // GPIO 5 = nodeMCU D1
- #define buttonOtherPin 4 // GPIO 4 = nodeMCU D1
- #define slider A0 //analog input
- String potReading ;
- // keep track of button state to check if it has changed
- int buttonState;
- int lastButtonState;
- int buttonState1;
- int lastButtonState1;
- // this number will increment and be sent to the remote IP address list
- int beat = 0;
- int analogIn; // stores input values
- int digitalIn;
- // Define output strings
- String str_analogIn = "0000";
- String str_digitalIn = "0";
- String str_out;
- // list of IP addresses to send to:
- IPAddress remoteList[nListeners] =
- {
- IPAddress(10, 0, 0, 101),
- IPAddress(10, 0, 0, 102)
- };
- // UDP variables
- WiFiUDP UDP;
- unsigned int localPort = 8000;
- boolean udpConnected = false;
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
- // char array (string) to send
- char msgBuffer[] = "pressing a button"; // this could be up to 8192 bytes
- // *************************
- // Initialization
- // *************************
- void setup() {
- // Initialise Serial connection
- Serial.begin(115200);
- // Initialise wifi connection
- wifiConnected = connectWifi();
- // only proceed if wifi connection successful
- if (wifiConnected) {
- udpConnected = connectUDP();
- if (udpConnected) {
- // initialize pins
- pinMode(ledPin, OUTPUT);
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(buttonOtherPin, INPUT_PULLUP) ;
- }
- }
- }
- // *************************
- // main loop
- // *************************
- void loop() {
- // check if the WiFi and UDP connections were successful
- if (wifiConnected) {
- if (udpConnected) {
- String potString ;
- int potValue = analogRead(A0);
- int analogValue = map(potValue, 0, 1023, 0, 255);
- analogIn = analogValue ;
- //potString = String(map(analogRead(A0),0,1023,0,255));
- Serial.println(analogIn);
- // Convert analog read integer to string
- if (analogIn < 100 && analogIn > 9) {
- str_analogIn = "0" + String(analogIn);
- }
- else if (analogIn < 10) {
- str_analogIn = "00" + String(analogIn);
- }
- else {
- str_analogIn = String(analogIn);
- }
- // Combine analog and digital sensors into one string (payload)
- str_out = "msg" + str_analogIn ;
- // Compose output character
- static char *msg = str_out.c_str();
- Serial.print("sending message: ");
- Serial.println(msg);
- Serial.print("message length: ");
- Serial.println(strlen(msg));
- //rf_driver.send((uint8_t *)msg, strlen(msg));
- //rf_driver.waitPacketSent();
- for (int i = 0; i < nListeners; i++) {
- UDP.beginPacket(remoteList[i], localPort);
- UDP.write(msg);
- int success = UDP.endPacket();
- }
- //Serial.print("send number:");
- //Serial.println(beat);
- // UDP message 1:
- // Only sending a 2 byte message here
- // to represent up to a 16 bit integer
- byte message[2];
- message[0] = 0;
- message[1] = beat++;
- beat %= 16; // 2 bytes can hold integer value up to 2^16 = 65,535
- // broadcast this value to all the IP Addresses in the list:
- for (int i = 0; i < nListeners; i++) {
- UDP.beginPacket(remoteList[i], localPort);
- UDP.write(message, sizeof(message));
- int success = UDP.endPacket();
- }
- buttonState = digitalRead(buttonPin);
- buttonState1 = digitalRead(buttonOtherPin) ;
- // UDP message 2:
- // if the button has changed from HIGH to LOW
- if (buttonState == LOW && buttonState != lastButtonState) {
- // when button is pressed, send a message to the first IP address onlye:
- Serial.println("ow");
- UDP.beginPacket(remoteList[0], localPort);
- //UDP.write(msgBuffer);
- UDP.write(1);
- int success = UDP.endPacket();
- }
- // if the button has changed from HIGH to LOW
- if (buttonState1 == HIGH && buttonState1 != lastButtonState1) {
- // when button is pressed, send a message to the first IP address onlye:
- Serial.println("yo yo");
- UDP.beginPacket(remoteList[0], localPort);
- //UDP.write(msgBuffer);
- UDP.write(2);
- int success = UDP.endPacket();
- }
- // save button state to compare next time
- lastButtonState = buttonState;
- delay(100); // pause a moment
- // save button state to compare next time
- lastButtonState1 = buttonState1;
- delay(100); // pause a moment
- }
- }
- }
- // *************************
- // connect to UDP – returns true if successful or false if not
- // *************************
- boolean connectUDP() {
- boolean state = false;
- Serial.println("");
- Serial.println("Connecting to UDP");
- if (UDP.begin(localPort) == 1) {
- Serial.println("Connection successful");
- state = true;
- }
- else {
- Serial.println("Connection failed");
- }
- return state;
- }
- // *************************
- // connect to wifi – returns true if successful or false if not
- // *************************
- boolean connectWifi() {
- boolean state = true;
- int i = 0;
- WiFi.begin(ssid, password);
- WiFi.config(ip, gateway, subnet);
- Serial.println("");
- Serial.println("Connecting to WiFi");
- // Wait for connection
- Serial.print("Connecting");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- if (i > 10) {
- state = false;
- break;
- }
- i++;
- Serial.print("Try ");
- Serial.println(i);
- }
- if (state) {
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- else {
- Serial.println("");
- Serial.println("Connection failed.");
- }
- return state;
- }
- void udpReset() {
- // asm volatile (" jmp 0");
- if (wifiConnected) {
- udpConnected = connectUDP();
- if (udpConnected) {
- Serial.print(" ...success");
- }
- else {
- Serial.print(" on nose!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement