So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Good Science For You
#78683 I have a successfully running program I can control from my serial monitor on the MEGA using a switch to turn things on and off on a machine.

Now I am trying to use the ESP8266 Wifi to send data that will do the same thing. I have the Wifi working on a browser to click on and off the devices and return to the screen that they are on or off. Then I tried to send the data to the Mega. It shows up on the ESP Serial monitor using Arduino IDE 1.8.7 The '1', '2', '3' all show on the serial monitor of the ESP but they do not go to the Mega's switch. So I know the data is there but I don't know how to transfer it to a data that works the same switch.

I have the pin TX 14 and RX 15 on the mega hooked through a HiLetgo 10pcs 4 Channels IIC I2C Logic Level Converter Bi-Directional 3.3V-5V Shifter Module for Arduino, and the TX RX pins on the NodeMcu 12E Tx to RX and RX to TX

I tried Serial.print('1'); (etc) to send data but it does not work to send to the Mega. Unplugged the USB to my computer on the ESP hoping that was the problem. I must not have the data in the correct format for the Mega to use as a 'char'. This works perfectly with the normal serial monitor input on the Mega alone. Changing from just Serial.available as shown below works.
How do I format the "send" data from the ESP to the Mega to work the switch?

Set up fro the serial is: (along with the rest of the programs pinModes that all work find)

Serial.begin(9600);
delay(20);
Serial.println(F("Started..."));

sensors.begin();
Serial3.begin(9600);
delay(20);
pinMode(14, OUTPUT);//serial 3 on Mega
pinMode(15, INPUT);


void (loop)

if(Serial3.available()>0)
//if(Serial.available()>0)
{
Serial.print("We are here");

char ch=Serial3.read();
//char ch=Serial.read();
Serial.print(ch);

switch(ch)
{

case '1': All_Off_Set_Pilot();break;
case 'J': All_Off_Set_Pilot();break;
case '2': LeftValveOpenRightClosed(); break;
case '3': RightValveOpenLeftClosed();break;
case '4': RightValveOpenLeftOPEN();break;
case '5': Pump1LOW_ON(); break;
case '6': Pump1HIGH_ON(); break;
case '7': Pump1_OFF(); break;
case '8': TURBO_ON(); break;
case '9': LIGHT_ON(); break;
case '0': LIGHT_OFF(); break;
case 'a': Pump3LOW_ON(); break;
case 'b': Pump3HIGH_ON(); break;
case 'c': Pump3_OFF(); break;
case 'd': BLOWER_ON(); break;
case 'e': BLOWER_OFF(); break;
case 'f': ONE_DRMV_Slow();break;
case 'g': ONE_DRMV_Medium(); break;
case 'h': ONE_DRMV_FAST(); break;
case 'i': ChainStop();break;
case 'j': checkTempAdjust(); break;
case 'k': TA_ONE_ON(); break;
case 'l': TA_ONE_OFF(); break;
case 'm': TA_TWO_ON(); break;
case 'n': TA_TWO_OFF(); break;
case 'o': TA_THREE_ON(); break;
case 'p': TA_THREE_OFF(); break;
case 'q': TA_FOUR_ON(); break;
case 'r': TA_FOUR_OFF(); break;
case 's': VA_ONE_ON(); break;
case 't': VA_ONE_OFF(); break;
case 'u': VA_TWO_ON(); break;
case 'v': VA_TWO_OFF(); break;
case 'w': VA_THREE_ON(); break;
case 'x': VA_THREE_OFF(); break;
case 'y': VA_FOUR_ON(); break;
case 'z': VA_FOUR_OFF(); break;
case 'A': HOT_WATER_ON(); break;
case 'B': HOT_WATER_OFF(); break;
case 'C': DRAIN_ON(); break;
case 'D': DRAIN_OFF(); break;
case 'E': HEATER_ON(); break;
case 'F': HEATER_OFF(); break;
case 'G': CIRC_PUMP_ON(); break;
case 'H': CIRC_PUMP_OFF(); break;
case 'I': TURBO_OFF(); break;
}
}
}
Last edited by Good Science For You on Thu Oct 18, 2018 1:25 pm, edited 1 time in total.
User avatar
By QuickFix
#78697 What's a "MEGA"? :?
Sorry, but I can't make any sense of your question.

Also: use the [ CODE ]-tags when putting code on the forum.
We do love pictures and schematics as well, so if it could clarify the problem, don't hesitate to use them. :idea:
User avatar
By Good Science For You
#78716 The switch code is on an Arduino Mega which is getting a signal from the ESP8266. But I don't know how to send the data to the Arduino in a format that simulates the Serial Monitor's keystrokes. I want the switch to work as it did when I used the serial input from the Arduino IDE's serial monitor. How do I get the ESP8266 to send data that the switch can read and respond to.

Right now all I get is "254" and 255" but I know I am getting serial communication from the ESP8266 to the Arduino Mega.
The statement that is sending is this Serial.println('1'); or Serial.println('J'); and so on.. So I know that does not work. The other side is reading 254 constantly when the ESP is sending. Everytime I press the button on the web browser to the ESP, it responds and sends "254" over and over and sometimes "255".

If I set the Arduino Mega's code to read from the serial monitor and I press 1 and enter then the function in the switch runs fine. If I send: Serial.println('1'); it does not respond at all. The Serial.println(); from the ESP is not correct to send a character for the switch to read.
You do not have the required permissions to view the files attached to this post.
User avatar
By QuickFix
#78733 You actually only need to connect the TX of the ESP to the RX of the Arduino; there's no communication from Arduino to ESP, so you can leave that out.
Since the ESP outputs at 3.3V you can cut corners by directly connecting the ESP's TX to the RX of the Arduino (this can't be done directly the other way 'round though).

You're talking about a USB connection on your ESP, which means you're using a development board (NodeMCU, Wemos, ...) and not a plain ESP.
The USB to serial convertor on those boards is always connected (and powered) to the ESP, even when there's no USB cable connected to the board; this means that the convertor can (and possibly will) always influence your serial communication when using TX and RX directly from the I/O pins.

Fortunately, GPIO2 on the ESP doubles (as a special function) as a second serial TX connection and can be used for debug purposes.
Of course we can also use it for other things like, in this case, just a regular free serial output to control your Arduino.

In the ESP-Arduino core you can open this port as Serial1; don't forget to set the Baud rate your Arduino listens to.
After that you can write the commands to Serial1 that are needed to control the Arduino/switch.