ESP8266: UDP broadcast receiver

 

ESP8266 UDP broadcast receiver

 

 

/*
Wireless Serial using UDP ESP8266
Hardware: NodeMCU
Circuits4you.com
2018
UDP Broadcast multi esp to esp communication
*/

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_7segment matrix = Adafruit_7segment();

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

const char *ssid = "KMTRONIC";
const char *pass = "KMTRONIC";

unsigned int localPort = 12345; // local port to listen for UDP packets

IPAddress SendIP(192,168,1,255); //UDP Broadcast IP data sent to all devicess on same network

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;

char packetBuffer[9]; //Where we get the UDP data
float temp;
//======================================================================
// Setup
//=======================================================================
void setup()
{
matrix.begin(0x70);

Serial.begin(115200);
Serial.println();

WiFi.begin(ssid, pass); //Connect to access point

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

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{
int cb = udp.parsePacket();
if (!cb) 
{
//If serial data is recived send it to UDP
if(Serial.available()>0)
{
udp.beginPacket(SendIP, 2000); //Send Data to Master unit
//Send UDP requests are to port 2000

char a[1];
a[0]=char(Serial.read()); //Serial Byte Read
udp.write(a,1); //Send one byte to ESP8266 
udp.endPacket();
}
}
else {
// We've received a UDP packet, send it to serial
memset(packetBuffer, 0, sizeof(packetBuffer));
udp.read(packetBuffer, 10); // read the packet into the buffer, we are reading only one byte
Serial.println(packetBuffer);
delay(20);
temp = atof (packetBuffer);
matrix.print(temp);
matrix.writeDisplay();
}
}
//=======================================================================

 

 

PHP broadcast

<?php

while(1) {
// Loading the XML file
$xml = simplexml_load_file("http://192.168.1.199/status.xml");

foreach($xml->children() as $sensor)
{
if ($sensor->name == "Office 1" )
{
$msg = $sensor->temp;
//echo $msg . "\r\n";
}
}


$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 

$len = strlen($msg); 

echo $msg . "\r\n";

socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1);

socket_sendto($sock, $msg, $len, 0 ,'192.168.1.255', 12345);

socket_close($sock);
usleep(500000);

}

?>

 

 

 

ESP8266: Adafruit VEML6070 I2C Test

ESP8266: Adafruit VEML6070 I2C Test

 

ESP8266: Adafruit VEML6070 I2C Test
ESP8266: Adafruit VEML6070 I2C Test


Connection:

– NodeMCU GPIO0 -> D1 -> SCL (VEML6070)
– NodeMCU GPIO4 -> D2 -> SDA (VEML6070)
– NodeMCU +3.3 -> 3V3 -> VCC (VEML6070)
– NodeMCU GND -> GND -> GND (VEML6070)

Arduino code:

#include <Wire.h>
#include "Adafruit_VEML6070.h"

Adafruit_VEML6070 uv = Adafruit_VEML6070();

void setup() {
Serial.begin(9600);
Serial.println("VEML6070 Test");
uv.begin(VEML6070_1_T); // pass in the integration time constant
}


void loop() {
Serial.print("UV light level: "); Serial.println(uv.readUV());

delay(1000);
}

 

ESP8266: Adafruit VEML6070 I2C Test
ESP8266: Adafruit VEML6070 I2C Test

 

ESP8266: Adafruit VEML6070 I2C Test
ESP8266: Adafruit VEML6070 I2C Test

 

 

 

ESP8266: HT-16k33 7 Segment I2C Test

 

ESP8266: HT-16k33 7 Segment I2C Test

ESP8266: HT-16k33 7 Segment I2C Test
ESP8266: HT-16k33 7 Segment I2C Test

 

 

Connection:

– NodeMCU GPIO0 -> D1 -> SCL (HT-16k33)
– NodeMCU GPIO4 -> D2 -> SDA (HT-16k33)
– NodeMCU +3.3 -> 3V3 -> VCC (HT-16k33)
– NodeMCU GND -> GND -> GND (HT-16k33)

 

Library:

https://github.com/adafruit/Adafruit_LED_Backpack 

 

Arduino code:

/*************************************************** 
This is a library for our I2C LED Backpacks

Designed specifically to work with the Adafruit LED 7-Segment backpacks 
----> http://www.adafruit.com/products/881
----> http://www.adafruit.com/products/880
----> http://www.adafruit.com/products/879
----> http://www.adafruit.com/products/878

These displays use I2C to communicate, 2 pins are required to 
interface. There are multiple selectable I2C addresses. For backpacks
with 2 Address Select pins: 0x70, 0x71, 0x72 or 0x73. For backpacks
with 3 Address Select pins: 0x70 thru 0x77

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries. 
BSD license, all text above must be included in any redistribution
****************************************************/

// Enable one of these two #includes and comment out the other.
// Conditional #include doesn't work due to Arduino IDE shenanigans.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.

#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

Adafruit_7segment matrix = Adafruit_7segment();

void setup() {
#ifndef __AVR_ATtiny85__
Serial.begin(9600);
Serial.println("7 Segment Backpack Test");
#endif
matrix.begin(0x70);
}

void loop() {


// print a hex number
matrix.print(0xBEEF, HEX);
matrix.writeDisplay();
delay(500);

// print a floating point 
matrix.print(12.34);
matrix.writeDisplay();
delay(500);

// print with print/println
for (uint16_t counter = 0; counter < 9999; counter++) {
matrix.println(counter);
matrix.writeDisplay();
delay(10);
}

// method #2 - draw each digit
uint16_t blinkcounter = 0;
boolean drawDots = false;
for (uint16_t counter = 0; counter < 9999; counter ++) {
matrix.writeDigitNum(0, (counter / 1000), drawDots);
matrix.writeDigitNum(1, (counter / 100) % 10, drawDots);
matrix.drawColon(drawDots);
matrix.writeDigitNum(3, (counter / 10) % 10, drawDots);
matrix.writeDigitNum(4, counter % 10, drawDots);

blinkcounter+=50;
if (blinkcounter < 500) {
drawDots = false;
} else if (blinkcounter < 1000) {
drawDots = true;
} else {
blinkcounter = 0;
}
matrix.writeDisplay();
delay(10);
}
}

 

 

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

 

Arduino: Microchip Keeloq Retransmit Garage Door Code

Arduino: Microchip Keeloq Retransmit Garage Door Code

 

// Taken from http://arduino.ru/forum/apparatnye-voprosy/biblioteka-chteniya-id-brelkov-signalizatsiii-hcs301-keeloq?page=1

#include "math.h"

#define txPin 8
#define rxPin 7
#define signalPin 6

int lastRxValue = 0;
int tempRxValue = 0;
unsigned long lastRxTime = 0;
unsigned long tempTime = 0;
unsigned long difTime = 0;
boolean bValidPacket = false;
int decodeMethod = 1; //0 real - as in manual, 1 inver as Oleg do


//Global vars end
void send_meander(int time){
 digitalWrite(txPin, HIGH);
 delayMicroseconds(time);
 digitalWrite(txPin, LOW);
 delayMicroseconds(time);
}
//keelog start
int keelog_state = 0;
int keelogCounter = 0;
byte keelog_code[9];

void keelog_vardump(){
 if(decodeMethod == 0){
 Serial.println(" - keelog origin - ");
 }
 else{
 Serial.println(" - keelog invert - ");
 }
 Serial.print(keelog_code[0], HEX);
 Serial.print(keelog_code[1], HEX);
 Serial.print(keelog_code[2], HEX);
 Serial.print(keelog_code[3], HEX);
 Serial.println("-hop");
 Serial.print(keelog_code[4], HEX);
 Serial.print(keelog_code[5], HEX);
 Serial.print(keelog_code[6], HEX);
 Serial.println("-fix");
 Serial.print(keelog_code[7], HEX);
 Serial.println("-btn");
 Serial.print(keelog_code[8], HEX);
 Serial.println("-dop");
 
 /*for(int i = 0; i<9; i++){
 Serial.print(keelog_code[i], HEX);
 Serial.print(" - ");
 }*/
 keelog_send(keelog_code);
 digitalWrite(signalPin, HIGH);
 delay(100);
 digitalWrite(signalPin, LOW);
 keelog_state = 0;
 for(int i = 0; i<9; i++){
 keelog_code[i]=0;
 }
}
void keelog_send(byte* keelog_code){
 Serial.println("- sending keelog -");
 for(int i = 0; i<9; i++){
 Serial.print(keelog_code[i], HEX);
 Serial.print(" - ");
 }
 for(int i = 0; i<11; i++){//посылаем преамблу
 send_meander(400);
 }
 digitalWrite(txPin, HIGH);
 delayMicroseconds(400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(4000);//посылаем хедер
 
 for( int i = 0; i<9; i++){
 if(decodeMethod==1){
 for(int i2 = 7;i2>=0;i2--){
 if(bitRead(keelog_code[i], i2)){
 digitalWrite(txPin, HIGH);
 delayMicroseconds(400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(2*400);
 }
 else{
 digitalWrite(txPin, HIGH);
 delayMicroseconds(2*400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(400);
 }
 }
 }
 else{
 for(int i2 = 0;i2<8;i2++){
 if(!bitRead(keelog_code[i], i2)){
 digitalWrite(txPin, HIGH);
 delayMicroseconds(400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(2*400);
 }
 else{
 digitalWrite(txPin, HIGH);
 delayMicroseconds(2*400);
 digitalWrite(txPin, LOW);
 delayMicroseconds(400);
 }
 } 
 }
 
 }
}
void keelog_get(){
 bValidPacket = false;
 if(keelog_state==0){//ждем преамбулу и хедер
 if(difTime > 280 && difTime < 620 && lastRxValue != tempRxValue){
 keelogCounter ++;
 }
 else{
 if(keelogCounter==23){
 if(difTime>2800 && difTime<6200 && lastRxValue == 0){
 keelog_state=1;
 }
 }
 keelogCounter = 0;
 }
 }
 else if(keelog_state==1){// получаем биты
 if(difTime > 560 && difTime < 1240 && lastRxValue == 1){// получили 1
 if(decodeMethod==0){
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]>>1)|B10000000;
 }
 else{
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]<<1)|B00000000;
 }
 bValidPacket = true;
 }
 else if(difTime > 280 && difTime < 620 && lastRxValue == 1){
 if(decodeMethod==0){
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]>>1)|B00000000;
 }
 else{
 keelog_code[round(keelogCounter/8)] = (keelog_code[round(keelogCounter/8)]<<1)|B00000001;
 }
 bValidPacket = true;
 }
 else if(lastRxValue == 0){
 }
 else{
 keelog_state=1;
 keelogCounter = 0;
 }
 
 if(bValidPacket){
 keelogCounter++;
 if(keelogCounter==66){
 keelog_vardump();
 keelogCounter = 0;
 keelog_state = 0;
 }
 }
 }
}
//keelog end


void setup() { 
 pinMode(txPin, OUTPUT);
 pinMode(rxPin, INPUT);
 pinMode(signalPin, OUTPUT);
 //
 Serial.begin(9600);
 Serial.println("start");
 lastRxValue = digitalRead(rxPin);
 lastRxTime = micros();
 
 //attachInterrupt(0, grab, CHANGE);
}

void loop() {
 tempRxValue = digitalRead(rxPin);
 
 if(tempRxValue != lastRxValue){
 tempTime = micros();
 difTime = tempTime - lastRxTime;
 keelog_get(); 
 lastRxTime = tempTime;
 lastRxValue = tempRxValue;
 }
}

 

LOG:

 - keelog invert - 
F9A4C3E-hop
89FA0-fix
84-btn
2-dop
- sending keelog -
F - 9A - 4C - 3E - 89 - FA - 0 - 84 - 2 -

 

Arduino: Microchip Keeloq Retransmit Garage Door Code
Arduino: Microchip Keeloq Retransmit Garage Door Code