Any examples/pointers/advice gratefully received.
Any examples/pointers/advice gratefully received.
Explore... Chat... Share...
// Some LEDs and a relay. Lamp control end.
// parse packet
// UDP recieve packet. If "pwm\n" then send back current pwm value
// else use value received as integer for pwm update. range 0-1023
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
const char* ssid = "esp-net"; // your network SSID (name)
const char* pass = "123456789"; // your network password
//IPAddress relayIP = (192,168,100,118);
IPAddress relayIP = (192,168,100,101);
unsigned int localPort = 5000;
int status = WL_IDLE_STATUS;
char packetBuffer[255];
char ReplyBuffer[] = "OK\n";
int pwm = 0;
bool switch0State = 0;
const int switch0 = 0;
const int led2 = 2;
const int led12 = 12;
const int led13 = 13;
const int led14 = 14;
const int led15 = 15;
const int relay = 4;
WiFiUDP Udp;
//------------------------------------------------------------------
void rxUDP() {
int packetSize = Udp.parsePacket(); //Checks for a UDP packet, and reports the size.
//--------------------------------------
if (packetSize) {
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(packetBuffer, 255); // read packet into packetBuffer
if (len > 0) {
packetBuffer[len] = 0; // reset length
}
//---
if (strcmp(packetBuffer, "pwm\n") == 0) { // if packet "pwm\n"
char p[5];
String str = String(pwm);
str.toCharArray(p, 5);
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(p); // current value for pwm
Udp.endPacket();
}
else { // make number into pwm for output 0-1023
pwm = atoi(packetBuffer);
Serial.print("pwm= ");
Serial.println(pwm);
analogWrite(led15, pwm);
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write(ReplyBuffer); // send 'OK\n'
// Udp.endPacket();
}
//--------------------- serial print details ----------
if (packetSize > 0)
{
Serial.println("----------------------"); //
Serial.print("packetSize=");
Serial.println(packetSize);
Serial.print("Data was -> ");
Serial.print(packetBuffer);
Serial.print(" <- From IP : ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
}
//----------------------------------------------------
// Use a switch statement to examine the first character of the packet buffer
switch (packetBuffer[0]) {
case '1': //
digitalWrite(relay, HIGH);
break;
case '2': //
digitalWrite(relay, LOW);
break;
case '3': //
digitalWrite(led2, HIGH);
break;
case '4': //
digitalWrite(led2, LOW);
break;
case '5': //
digitalWrite(led13, HIGH);
break;
case '6': //
digitalWrite(led13, LOW);
break;
case '7': //
digitalWrite(led14, HIGH);
break;
case '8': //
digitalWrite(led14, LOW);
break;
}
}
}
//==================================================================
void setup() {
pinMode(led2, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(led14, OUTPUT);
pinMode(led15, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(switch0, INPUT_PULLUP);
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP: ");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.println(localPort);
Udp.begin(localPort);
}
//==================================================================
void loop() {
rxUDP(); //check for input and process if available
delay(10);
/*
if ((digitalRead(switch0)) != switch0State)
{
switch0State = !switch0State;
Serial.print("switch0State= ");
Serial.println(switch0State);
Udp.beginPacket(Udp.relayIP, Udp.remotePort());
Udp.write(p); // current value for pwm
Udp.endPacket();
}
*/
}
// parse packet
// UDP recieve packet. If "pwm\n" then send back current pwm value
// else use value received as integer for pwm update. range 0-1023
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
const char* ssid = "esp-net"; // your network SSID (name)
const char* pass = "123456789"; // your network password
//IPAddress relayIP = (192, 168, 100, 114);
IPAddress DrelayIP = (192, 168, 100, 101);
IPAddress relayIP (192, 168, 100, 103);
//unsigned int remotePort = 5000;
unsigned int remotePort = 9999;
unsigned int localPort = 5000;
int status = WL_IDLE_STATUS;
char packetBuffer[255];
char ReplyBuffer[] = "OK\n";
int pwm = 0;
bool switch0State = 0;
const int switch5 = 5;
const int switch0 = 0;
const int led2 = 2;
const int led12 = 12;
const int led13 = 13;
const int led14 = 14;
const int led15 = 15;
const int relay = 4;
WiFiUDP Udp;
//------------------------------------------------------------------
void rxUDP() {
int packetSize = Udp.parsePacket(); //Checks for a UDP packet, and reports the size.
//--------------------------------------
if (packetSize) {
IPAddress remoteIp = Udp.remoteIP();
int len = Udp.read(packetBuffer, 255); // read packet into packetBuffer
if (len > 0) {
packetBuffer[len] = 0; // reset length
}
//---
if (strcmp(packetBuffer, "pwm\n") == 0) {
char p[5];
String str = String(pwm);
str.toCharArray(p, 5);
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write(p); // current value for pwm
// Udp.endPacket();
}
else { // make number into pwm for output 0-1023
pwm = atoi(packetBuffer);
Serial.print("pwm= ");
Serial.println(pwm);
analogWrite(led15, pwm);
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write(ReplyBuffer); // send 'OK\n'
// Udp.endPacket();
}
//--------------------- serial print details ----------
if (packetSize > 0)
{
Serial.println("----------------------"); //
Serial.print("packetSize=");
Serial.println(packetSize);
Serial.print("Data was -> ");
Serial.print(packetBuffer);
Serial.print(" <- From IP : ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.println(Udp.remotePort());
}
//----------------------------------------------------
// Use a switch statement to examine the first character of the packet buffer
switch (packetBuffer[0]) {
case '1': //
digitalWrite(relay, HIGH);
break;
case '2': //
digitalWrite(relay, LOW);
break;
case '3': //
digitalWrite(led2, HIGH);
break;
case '4': //
digitalWrite(led2, LOW);
break;
case '5': //
digitalWrite(led13, HIGH);
break;
case '6': //
digitalWrite(led13, LOW);
break;
case '7': //
digitalWrite(led14, HIGH);
break;
case '8': //
digitalWrite(led14, LOW);
break;
}
}
}
//==================================================================
void setup() {
pinMode(led2, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(led14, OUTPUT);
pinMode(led15, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(switch0, INPUT_PULLUP);
pinMode(switch5, INPUT_PULLUP);
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP: ");
Serial.print(WiFi.localIP());
Serial.print(":");
Serial.println(localPort);
Udp.begin(localPort);
}
//==================================================================
// Udp.beginPacket("192.168.100.103", 5000);
void loop() {
rxUDP(); //check for input and process if available
delay(10);
if ((digitalRead(switch0)) == 0)
{
Serial.println("turn on");
Udp.beginPacket(relayIP, 5000);
Udp.write("1"); // relay on
Udp.endPacket();
delay(500);
Udp.beginPacket(DrelayIP, 5000); // Desk relay
Udp.write("1"); // relay on
Udp.endPacket();
delay(500);
}
if ((digitalRead(switch5)) == 0)
{
Serial.println("turn off");
Udp.beginPacket(relayIP, 5000);
Udp.write("2"); // relay off
Udp.endPacket();
delay(500);
Udp.beginPacket(DrelayIP, 5000); // Desk relay
Udp.write("2"); // relay off
Udp.endPacket();
delay(500);
}
}
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]