UDP packet send from ESP8266 to KMtronic UDP LAN Relay board

 

KMtronic UDP LAN Ethernet IP 2 channels Relay Controller
KMtronic UDP LAN Ethernet IP 2 channels Relay Controller

 

NodeMcu
NodeMcu

 

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char *ssid = "KMTRONIC";
const char *password = "12345678";

WiFiUDP Udp;
unsigned int localUdpPort = 12345; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back


void setup()
{
 Serial.begin(115200);
 Serial.println();

Serial.printf("Connecting to %s ", ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED)
 {
 delay(500);
 Serial.print(".");
 }
 Serial.println(" connected");

Udp.begin(localUdpPort);
 Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
 
 // Turn relay 1 ON KMtronic UDP Relay board
 // connected to IP: 192.168.1.199 PORT:12345
 Udp.beginPacket("192.168.1.199", 12345);
 Serial.println("Send UDP...");
 Udp.write("FF0101");
 Serial.println(Udp.endPacket());
 // --------------------------------------
}


void loop()
{
 int packetSize = Udp.parsePacket();
 if (packetSize)
 {
 // receive incoming UDP packets
 Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
 int len = Udp.read(incomingPacket, 255);
 if (len > 0)
 {
 incomingPacket[len] = 0;
 }
 Serial.printf("UDP packet contents: %s\n", incomingPacket);

// send back a reply, to the IP address and port we got the packet from
 Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
 Udp.write(replyPacket);
 Udp.endPacket();
 }
}

 

 

 

 

 

Randomnerdtutorials: ESP8266 Controlled with Android App (MIT App Inventor)

 

ESP8266 Controlled with Android App (MIT App Inventor)

 

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char *ssid = "KMTRONIC";
const char *password = "12345678";

ESP8266WebServer server(80);

const int led = D0;

void handleRoot() {
 digitalWrite(led, 1);
 server.send(200, "text/plain", "hello from esp8266!");
 digitalWrite(led, 0);
}

void handleNotFound(){
 digitalWrite(led, 1);
 String message = "File Not Found\n\n";
 message += "URI: ";
 message += server.uri();
 message += "\nMethod: ";
 message += (server.method() == HTTP_GET)?"GET":"POST";
 message += "\nArguments: ";
 message += server.args();
 message += "\n";
 for (uint8_t i=0; i<server.args(); i++){
 message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
 }
 server.send(404, "text/plain", message);
 digitalWrite(led, 0);
}

void setup(void){
 pinMode(led, OUTPUT);
 digitalWrite(led, 0);
 Serial.begin(115200);
 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, password);
 Serial.println("");

// Wait for connection
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.print("Connected to ");
 Serial.println(ssid);
 Serial.print("IP address: ");
 Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) {
 Serial.println("MDNS responder started");
 }

server.on("/", handleRoot);

server.on("/ON1", [](){
 server.send(200, "text/plain", "this works as well");
 digitalWrite(led, 0);
 });

server.on("/OFF1", [](){
 server.send(200, "text/plain", "this works as well");
 digitalWrite(led, 1);
 });

server.onNotFound(handleNotFound);

server.begin();
 Serial.println("HTTP server started");
}

void loop(void){
 server.handleClient();
}

 

 

Randomnerdtutorials: ESP8266 Controlled with Android App (MIT App Inventor)
Randomnerdtutorials: ESP8266 Controlled with Android App (MIT App Inventor)

 

 

ESP8266 NodeMCU: KMtronic UART 8 Channel Relay Controller control

KMtronic UART 8 Channel Relay Controller control

 

KMtronic UART Relay Controller Eight Channel
KMtronic UART Relay Controller Eight Channel

 

ESP8266 NodeMCU: KMtronic UART 8 Channel Relay Controller control
ESP8266 NodeMCU: KMtronic UART 8 Channel Relay Controller control

 

 

void setup() {
 //Initialize serial and wait for port to open:
 Serial.begin(9600);

}


void loop() {

for(uint8_t i=1; i < 9; i++ ){
 KMtronic_Relay_ON(i);
 delay(50);
 }

for(uint8_t i=1; i < 9; i++ ){
 KMtronic_Relay_OFF(i);
 delay(50);
 }


 KMtronic_Relay_ON(1);
 delay(500);

KMtronic_Relay_OFF(1);
 delay(500);

KMtronic_Relay_ON(8);
 delay(500);

KMtronic_Relay_OFF(8);
 delay(500);
 
}

void KMtronic_Relay_ON(uint8_t number)
{
 Serial.write((uint8_t)0xFF);
 Serial.write((uint8_t)number);
 Serial.write((uint8_t)0x01);
}

void KMtronic_Relay_OFF(uint8_t number)
{
 Serial.write((uint8_t)0xFF);
 Serial.write((uint8_t)number);
 Serial.write((uint8_t)0x00);
}

 

 

 

MFRC522 RFID Reader interfaced with NODEMCU

 

MFRC522 RFID Reader interfaced with NODEMCU
MFRC522 RFID Reader interfaced with NODEMCU

 

#include <SPI.h>
#include <MFRC522.h>
#include <ESP8266WiFi.h>

#define RST_PIN D2 // Configurable, see typical pin layout above
#define SS_PIN D3 // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

// Thing Speak API server 
const char* myserver = "xxxxxxxxxx.com";
const char* resource = "/MFRC522-RFID-Reader/get.php?id=";

const char *ssid = "KMTRONIC";
const char *password = "12345678";

char CardID;

unsigned long UID_unsigned;
long UID_LONG=(long)UID_unsigned;
String UID_string = (String)UID_unsigned;

void Post() {
 WiFiClient client;
 int retries = 5;
 while(!!!client.connect(myserver, 80) && (retries-- > 0)) {
 Serial.print("/");
 delay(10);
 }
 client.print(String("GET ") + resource + UID_string +
 " HTTP/1.1\r\n" +
 "Host: " + myserver + "\r\n" + 
 "Connection: close\r\n\r\n");
 Serial.println("\n\n. . . POST . . .\n");
 delay(50);
 client.stop();
}

void setup() {
 Serial.begin(9600); // Initialize serial communications with the PC
 while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
 SPI.begin(); // Init SPI bus
 mfrc522.PCD_Init(); // Init MFRC522
 mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
 Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

WiFi.begin(ssid,password); //trying to connect the modem

while(WiFi.status() != WL_CONNECTED) {
 delay(100);
 }

Serial.println("\n\n. . . BOOTING ESP8266 . . .\n");
 Serial.print("\n");
 Serial.print("CONNECTED TO ACCESS POINT : ");
 Serial.println(ssid);
 Serial.flush();
 Serial.println(WiFi.localIP());
 //it is sending the IP to Arduino's port. So I can see the IP on Serial Monitor
 Serial.flush();
 
 
}

void loop() {
 // Look for new cards
 if ( ! mfrc522.PICC_IsNewCardPresent()) {
 
 return;
 }

// Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial()) {
 return;
 }

// Dump debug info about the card; PICC_HaltA() is automatically called
 //dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
 //Serial.println(mfrc522.uid.uidByte[0], HEX);

Serial.print("Printing HEX UID : ");
 for (byte i = 0; i < mfrc522.uid.size; i++) {
 Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
 Serial.print(mfrc522.uid.uidByte[i], HEX);
 } 
 Serial.println("");


 UID_unsigned = mfrc522.uid.uidByte[0] << 24;
 UID_unsigned += mfrc522.uid.uidByte[1] << 16;
 UID_unsigned += mfrc522.uid.uidByte[2] << 8;
 UID_unsigned += mfrc522.uid.uidByte[3];

UID_LONG=(long)UID_unsigned;
 
 UID_string = (String)UID_unsigned;

Serial.println();
 Serial.println("UID Unsigned int"); 
 Serial.println(UID_unsigned);

Serial.println("UID Long :");
 Serial.println(UID_LONG);

Serial.println("UID String :");
 Serial.println(UID_string);

Post();

}