Hello all,
I've been trying for a while to manage two other ESPs with a Nunchuk controller with an ESP8266.
I've collected and pasted a code for the Nunchuk ESP. I manage to drive an ESP32. that works.
Only now I am trying to control 2nd ESP8266 where 2 leds are controlled.
The intention is that I use the Nunchuck joystick to control an existing device that has an ESP32 and an IP 192.168.2.150 port 8000.
And control the 2nd ESP, LEDs with the buttons (Z&C) of the Nunchuk
-The Nunchuk has an ESP8266 with the IP 192.168.2.145 port 8000.
Which has a code with I2C and the "NintendoExtensionCtrl.h" library
-the first receiver is an existing device with an ESP32 with ip 192.186.2.150 port 8000, (which works with what I'm trying).
-the second receiver is a device with an ESP8266 with ip 192.186.2.140 port 8000.
where two LEDs can be turned on and off with the Nunchuk's "Z and C" buttons.
My problem is that the 2nd ESP8266 with the leds is not working.
In the serial monitor nothing happens until I press a button.
Then I get to see everything that the Nunchuk broadcasts, so also what needs to be sent to the other ESP32.
Because all data is sent to the 2nd ESP8266, I am unable to distill the piece of data (A29999 and V09999) for turning the LEDs on or off.
With my brief knowledge of code I have tried everything and googled a lot. The answer may well have passed me by, but unfortunately I don't see it.
I've been stuck with this problem for weeks now. who wants to help me further on my way.
Later when the LEDs work on the 2nd receiver I want to build more code around it. But that's for later concern.
Transmitter code Nunchuk ESP8266.
// Wii Nunchuck I2C address 0x53
//---------OSR----------
//V0 is vled pin 18
//A2 is lled pin 19
//-------deze IP--------
//192.168.2.150 for ESP32 Receiver1
//192.168.2.145 for ESP8266 Nunchuck
//192.168.2.140 for ESP8266 Receiver2
#include <SPI.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <NintendoExtensionCtrl.h>
//#define SDA 2 // SDA IS GROEN ESP8266 Def PIN 2
//#define SCL 1 // SCL IS GEEL ESP8266 Def PIN 1
//#define SDA 0 // SDA ESP-01s PIN 0
//#define SCL 2 // SCL ESP-01s PIN 2
#define SDA 2 // SDA ESP8266 TTGO Oled 2
#define SCL 14 // SCL ESP8266 TTGO Oled 14
Nunchuk nchuk;
int Linear = 10;
const char* LLEDOn = " V09999";
const char* LLEDOff = " V00";
const char* VLEDOn = " A29999";
const char* VLEDOff = " A20";
const char* IPadress1 = "192.168.2.10"; //PC Test
//const char* IPadress1 = "192.168.2.150"; //ESP32 Receiver1
const char* IPadress2 = "192.168.2.140"; //ESP8266 Receiver2
const char* ssid = "SIDD";
const char* password = "Code";
IPAddress staticIP(192,168,2,145); // ESP8266 Nunchuck
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
IPAddress dns1(8,8,8,8);
IPAddress dns2(8,8,4,4);
unsigned int localPort = 8000;
WiFiUDP Udp;
void setup()
{
Serial.begin(115200);
Wire.pins(SDA,SCL);
nchuk.begin();
while (!nchuk.connect())
{
Serial.println("Nunchuk not detected!");
delay(100);
}
WiFi.config(staticIP, gateway, subnet, dns1, dns2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Serial.print("IP address: ");
Serial.print(WiFi.localIP());
Serial.print(", UDP port: ");
Serial.println(localPort);
delay(1000);
Udp.begin(localPort);
}
void loop(){
boolean success = nchuk.update();
boolean zButton = nchuk.buttonZ();
boolean cButton = nchuk.buttonC();
int joyY = map(nchuk.joyY(), 32, 228, 1, 10); // Up, Down
int joyX = map(nchuk.joyX(), 33, 226, 1, 9999); // org nintendo L, R
//int joyX = map(nchuk.joyX(), 0, 255, 1, 9999); // imitation L, R
//Udp.beginPacket(IPadress2, localPort);
Udp.beginPacket(IPadress1, localPort);
if (joyY <= 1){
int (Linear = 0);
//------------PC------------
Serial.print("L0");
Serial.print(Linear);
Serial.print(" R0");
Serial.print(joyX);
if (cButton){
Serial.print(LLEDOn);
} else {
Serial.print(LLEDOff);
}
if (zButton){
Serial.print(VLEDOn);
} else {
Serial.print(VLEDOff);
}
Serial.println();
//----------IPadress1-----------
Udp.print("L0");
Udp.print(Linear);
Udp.print(" R0");
Udp.print(joyX);
if (cButton){
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOn);
} else {
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOff);
}
if (zButton){
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOn);
} else {
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOff);
}
Udp.println();
Udp.endPacket();
} else {
for (Linear = 10; Linear <= 99; Linear += joyY){
//------------PC------------
Serial.print("L0");
Serial.print(Linear);
Serial.print(" R0");
Serial.print(joyX);
if (cButton){
Serial.print(LLEDOn);
} else {
Serial.print(LLEDOff);
}
if (zButton){
Serial.print(VLEDOn);
} else {
Serial.print(VLEDOff);
}
Serial.println();
//----------IPadress1-----------
Udp.print("L0");
Udp.print(Linear);
Udp.print(" R0");
Udp.print(joyX);
if (cButton){
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOn);
} else {
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOff);
}
if (zButton){
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOn);
} else {
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOff);
}
Udp.println();
Udp.endPacket();
delay(25);
}
}
Udp.beginPacket(IPadress1, localPort);
if (joyY <= 1){
int (Linear = 0);
//------------PC------------
Serial.print("L0");
Serial.print(Linear);
Serial.print(" R0");
Serial.print(joyX);
if (cButton){
Serial.print(LLEDOn);
} else {
Serial.print(LLEDOff);
}
if (zButton){
Serial.print(VLEDOn);
} else {
Serial.print(VLEDOff);
}
Serial.println();
//----------IPadress1-----------
Udp.print("L0");
Udp.print(Linear);
Udp.print(" R0");
Udp.print(joyX);
if (cButton){
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOn);
} else {
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOff);
}
if (zButton){
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOn);
} else {
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOff);
}
Udp.println();
Udp.endPacket();
} else {
for (Linear = 99; Linear >= 10; Linear -= joyY){
//------------PC------------
Serial.print("L0");
Serial.print(Linear);
Serial.print(" R0");
Serial.print(joyX);
if (cButton){
Serial.print(LLEDOn);
} else {
Serial.print(LLEDOff);
}
if (zButton){
Serial.print(VLEDOn);
} else {
Serial.print(VLEDOff);
}
Serial.println();
//----------IPadress1-----------
Udp.print("L0");
Udp.print(Linear);
Udp.print(" R0");
Udp.print(joyX);
if (cButton){
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOn);
} else {
Udp.beginPacket(IPadress2, localPort);
Udp.print(LLEDOff);
}
if (zButton){
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOn);
} else {
Udp.beginPacket(IPadress1, localPort);
Udp.print(VLEDOff);
}
Udp.println();
Udp.endPacket();
delay(25);
}
}
}
Receive code 2 LEDs ESP8266
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
int VLED = 16;
int LLED = 2;
const char* ssid = "SIDD";
const char* password = "CODE";
IPAddress staticIP(192,168,2,140);
IPAddress gateway(192,168,2,1);
IPAddress subnet(255,255,255,0);
IPAddress dns1(8,8,8,8);
IPAddress dns2(8,8,4,4);
const char* LLEDOn = " V09999";
//const char* LLEDOff = " V00";
const char* VLEDOn = " A29999";
//const char* VLEDOff = " A20";
unsigned int localPort = 8000;
char incomingPacket[255];
WiFiUDP Udp;
void setup(){
Serial.begin(115200);
pinMode(VLED, OUTPUT);
pinMode(LLED, OUTPUT);
//digitalWrite(VLED, HIGH);
//digitalWrite(LLED, HIGH);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.config(staticIP, gateway, subnet, dns1, dns2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Serial.println(" connected");
Serial.print("IP address: ");
Serial.print(WiFi.localIP());
Serial.print(", UDP port: ");
Serial.println(localPort);
delay(1000);
Udp.begin(localPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize){
int len = Udp.read(incomingPacket, 255);
if (len > 0){
incomingPacket[len] = 0;
}
//Serial.print(incomingPacket);
Serial.print (Udp.parsePacket());
//if ((incomingPacket, 255) == VLEDOn){ // NIET, NIET UP
//if (incomingPacket == VLEDOn) { // NIET, WEL UP
//if ((incomingPacket) == VLEDOn) { // NIET, WEL UP
if ((incomingPacket) == "V09999") {
//if (incomingPacket, 255 == "V09999") {
//if ((incomingPacket, 255) == "V09999") {
//if (Udp.parsePacket() == VLEDOn){
//if (Udp.parsePacket() == "V09999") {
//if (Udp.parsePacket() <= 0) { // WERKT GAAT NIE UIT
//if (Udp.parsePacket() == 0) { // WERKT GAAT NIE UIT
Serial.print("VledOn");
digitalWrite(VLED, HIGH);
} else {
Serial.print("VledOff");
digitalWrite(VLED, LOW);
}
//if ((incomingPacket, 255) = LLEDOn){
//Serial.print("LledOn");
//digitalWrite(LLED, HIGH);
//} else {
//Serial.print("LledOff");
//digitalWrite(LLED, LOW);
//}
}
}
Thanks everyone for reading my post