ESP8266: Yet Another Arduino Wiegand Library

 

Yet Another Arduino Wiegand Library

 

 

#define PIN_D0 D1
#define PIN_D1 D2

// The object that handles the wiegand protocol
Wiegand wiegand;

// Initialize Wiegand reader
void setup() {
Serial.begin(9600);
delay(1);
Serial.println("Start...");

//Install listeners and initialize Wiegand reader
wiegand.onReceive(receivedData, "Card readed: ");
wiegand.onStateChange(stateChanged, "State changed: ");
wiegand.begin(WIEGAND_LENGTH_AUTO);

//initialize pins as INPUT
pinMode(PIN_D0, INPUT);
pinMode(PIN_D1, INPUT);
}

// Continuously checks for pending messages and polls updates from the wiegand inputs
void loop() {
// Checks for pending messages 
//Serial.println("Start2 ..");
wiegand.flush();

// Check for changes on the the wiegand input pins
wiegand.setPin0State(digitalRead(PIN_D0));
wiegand.setPin1State(digitalRead(PIN_D1));
}

// Notifies when a reader has been connected or disconnected.
// Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onStateChange()`
void stateChanged(bool plugged, const char* message) {
Serial.print(message);
Serial.println(plugged ? "CONNECTED" : "DISCONNECTED");
}

// Notifies when a card was read.
// Instead of a message, the seconds parameter can be anything you want -- Whatever you specify on `wiegand.onReceive()`
void receivedData(uint8_t* data, uint8_t bits, const char* message) {
Serial.print(message);

//Print value in HEX
uint8_t bytes = (bits+7)/8;
for (int i=0; i<bytes; i++) {
Serial.print(data[i] >> 4, 16);
Serial.print(data[i] & 0xF, 16);
}
Serial.println();
}

 

ESP8266: Reading Voltage from SDM120 Modbus Power Energy Meter

 

http://en.trialcommand.com/blog/esp8266-slave-modbus-tcpip-no-libraries/

 

 

/*
 * More Tutorials: 
 * Website http://trialcommand.com
 * In English: http://en.trialcommand.com
 * En Espa├▒ol: http://en.trialcommand.com 
 */
#include <ESP8266WiFi.h>

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

// RS485 Control pin
const int RS485_control = D0;

// Receive buffer
long Buffer[9];

int VoltageReadCommand[] ={0x01, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, 0xCB};

int ModbusTCP_port = 502;

//////// Required for Modbus TCP / IP /// Requerido para Modbus TCP/IP /////////
#define maxInputRegister 20
#define maxHoldingRegister 20

#define MB_FC_NONE 0
#define MB_FC_READ_REGISTERS 3 //implemented
#define MB_FC_WRITE_REGISTER 6 //implemented
#define MB_FC_WRITE_MULTIPLE_REGISTERS 16 //implemented
//
// MODBUS Error Codes
//
#define MB_EC_NONE 0
#define MB_EC_ILLEGAL_FUNCTION 1
#define MB_EC_ILLEGAL_DATA_ADDRESS 2
#define MB_EC_ILLEGAL_DATA_VALUE 3
#define MB_EC_SLAVE_DEVICE_FAILURE 4
//
// MODBUS MBAP offsets
//
#define MB_TCP_TID 0
#define MB_TCP_PID 2
#define MB_TCP_LEN 4
#define MB_TCP_UID 6
#define MB_TCP_FUNC 7
#define MB_TCP_REGISTER_START 8
#define MB_TCP_REGISTER_NUMBER 10

byte ByteArray[260];
unsigned int MBHoldingRegister[maxHoldingRegister];

//////////////////////////////////////////////////////////////////////////

WiFiServer MBServer(ModbusTCP_port);

void setup() {

pinMode(14, OUTPUT);

// Set RS485 Control pin to OUTPUT
 pinMode(RS485_control, OUTPUT); 
 
 // Set pin to send data HIGH
 digitalWrite(RS485_control, HIGH);
 
 Serial.begin(2400);
 delay(100) ;
 WiFi.begin(ssid, password);
 delay(100) ;
 Serial.println(".");
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 MBServer.begin();
 Serial.println("Connected ");
 Serial.print("ESP8266 Slave Modbus TCP/IP ");
 Serial.print(WiFi.localIP());
 Serial.print(":");
 Serial.println(String(ModbusTCP_port));
 Serial.println("Modbus TCP/IP Online");
 
}


void loop() {

// Check if a client has connected // Modbus TCP/IP
 WiFiClient client = MBServer.available();
 if (!client) {
 return;
 }


 boolean flagClientConnected = 0;
 byte byteFN = MB_FC_NONE;
 int Start;
 int WordDataLength;
 int ByteDataLength;
 int MessageLength;
 
 // Modbus TCP/IP
 while (client.connected()) {
 
 if(client.available())
 {
 flagClientConnected = 1;
 int i = 0;
 while(client.available())
 {
 ByteArray[i] = client.read();
 i++;
 }

client.flush();

///// code here --- codigo aqui

///////// Holding Register [0] A [9] = 10 Holding Registers Escritura
 ///////// Holding Register [0] A [9] = 10 Holding Registers Writing
 
 // Set pin to HIGH > Transmit mode
 digitalWrite(RS485_control, HIGH); 
 
 // Send command to ModBus Meter
 for (int i=0; i < 8; i++) Serial.write(VoltageReadCommand[ i ]);
 delay(50);

// Set pin to LOW > Receive mode
 digitalWrite(RS485_control, LOW);
 
 // Waiting for receive reply
 while ( Serial.available()<9); 
 
 // Read reply in Buffer[];
 for (int i=0; i < 9; i++) Buffer[i] = Serial.read();

// Print voltage data received
 Serial.println();
 Serial.print(Buffer[3], HEX); Serial.print(" ");
 Serial.print(Buffer[4], HEX); Serial.print(" ");
 Serial.print(Buffer[5], HEX); Serial.print(" ");
 Serial.print(Buffer[6], HEX); Serial.print(" "); 
 Serial.println();

// Convert to Float
 float x;
 ((byte*)&x)[3]= Buffer[3];
 ((byte*)&x)[2]= Buffer[4];
 ((byte*)&x)[1]= Buffer[5];
 ((byte*)&x)[0]= Buffer[6];
 
 // Convert to Float
 Serial.print(x,2);
 Serial.println(" Volts");

MBHoldingRegister[0] = word(Buffer[3],Buffer[4]);
 MBHoldingRegister[1] = word(Buffer[5],Buffer[6]);
 MBHoldingRegister[4] = random(0,12);
 MBHoldingRegister[5] = random(0,12);
 MBHoldingRegister[6] = random(0,12);
 MBHoldingRegister[7] = random(0,12);
 MBHoldingRegister[8] = random(0,12);
 MBHoldingRegister[9] = random(0,12);


 ///////// Holding Register [10] A [19] = 10 Holding Registers Lectura
 ///// Holding Register [10] A [19] = 10 Holding Registers Reading
 
 int Temporal[10];
 
 Temporal[0] = MBHoldingRegister[10];
 Temporal[1] = MBHoldingRegister[11];
 Temporal[2] = MBHoldingRegister[12];
 Temporal[3] = MBHoldingRegister[13];
 Temporal[4] = MBHoldingRegister[14];
 Temporal[5] = MBHoldingRegister[15];
 Temporal[6] = MBHoldingRegister[16];
 Temporal[7] = MBHoldingRegister[17];
 Temporal[8] = MBHoldingRegister[18];
 Temporal[9] = MBHoldingRegister[19];

/// Enable Output 14
 digitalWrite(14, MBHoldingRegister[14] );


 //// debug

for (int i = 0; i < 10; i++) {

Serial.print("[");
 Serial.print(i);
 Serial.print("] ");
 Serial.print(Temporal[i]);
 
 }
 Serial.println("");


//// end code - fin 


//// rutine Modbus TCP
 byteFN = ByteArray[MB_TCP_FUNC];
 Start = word(ByteArray[MB_TCP_REGISTER_START],ByteArray[MB_TCP_REGISTER_START+1]);
 WordDataLength = word(ByteArray[MB_TCP_REGISTER_NUMBER],ByteArray[MB_TCP_REGISTER_NUMBER+1]);
 }
 
 // Handle request

switch(byteFN) {
 case MB_FC_NONE:
 break;
 
 case MB_FC_READ_REGISTERS: // 03 Read Holding Registers
 ByteDataLength = WordDataLength * 2;
 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
 ByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).
 for(int i = 0; i < WordDataLength; i++)
 {
 ByteArray[ 9 + i * 2] = highByte(MBHoldingRegister[Start + i]);
 ByteArray[10 + i * 2] = lowByte(MBHoldingRegister[Start + i]);
 }
 MessageLength = ByteDataLength + 9;
 client.write((const uint8_t *)ByteArray,MessageLength);
 
 byteFN = MB_FC_NONE;
 
 break;
 
 
 case MB_FC_WRITE_REGISTER: // 06 Write Holding Register
 MBHoldingRegister[Start] = word(ByteArray[MB_TCP_REGISTER_NUMBER],ByteArray[MB_TCP_REGISTER_NUMBER+1]);
 ByteArray[5] = 6; //Number of bytes after this one.
 MessageLength = 12;
 client.write((const uint8_t *)ByteArray,MessageLength);
 byteFN = MB_FC_NONE;
 break;
 
 case MB_FC_WRITE_MULTIPLE_REGISTERS: //16 Write Holding Registers
 ByteDataLength = WordDataLength * 2;
 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
 for(int i = 0; i < WordDataLength; i++)
 {
 MBHoldingRegister[Start + i] = word(ByteArray[ 13 + i * 2],ByteArray[14 + i * 2]);
 }
 MessageLength = 12;
 client.write((const uint8_t *)ByteArray,MessageLength); 
 byteFN = MB_FC_NONE;
 
 break;
 }
 }

}

 

 

 

 

 

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);
}