I've wired up Rx/Tx from the UNO (bent away the shield pins) to Rx/Tx on debug port of my shield. I've uploaded this code to my ESP8266 shield and I was sending mqtt messages and receiving them on the shield and I could see the logs for raising and lowering as expected when the mqtt messages were successfully parsed by my code. I just realized that this code was in my ESP8266, not my shield, which is why the voltage on in1 and in2 pins in code was still 0. I just tried V between GPIO3 & GND on ESP chip:
and i got the 3.3V correctly.
QUESTION:
Now I need 5V, so is there any way to get the ESP8266 to send HIGH to the Arduino UNO pins?
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
//#include <Adafruit_INA219.h>
#define in1 3 //We are not using PWM for this demo.
#define in2 4
// Connect to the WiFi
const char* ssid = "myssid";
const char* password = "mypwd";
IPAddress mqtt_server(192, 168, 1, 113);
WiFiClient espClient;
PubSubClient client(espClient);
const byte ledPin = 0; // Pin with LED on Adafruit Huzzah UNNECESSARY
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
char receivedChar = (char)payload[i];
Serial.print(receivedChar);
if (receivedChar == '0')
// ESP8266 Huzzah outputs are "reversed"
raiseCurtain();
digitalWrite(ledPin, HIGH);
if (receivedChar == '1')
lowerCurtain();
digitalWrite(ledPin, LOW);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266 Client")) {
Serial.println("connected");
// ... and subscribe to topic
client.subscribe("curtainStatus");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup(){
Serial.begin(9600);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin, OUTPUT);//UNNECESSARY
setupMotor();
}
void setupMotor(){
//set the pins for output
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
//set the pins low - this will keep the motor from moving.
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
void raiseCurtain() {
Serial.println("raising curtains");
//set the pins low - this will keep the motor from moving.
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
//Spin Motor in one direction UP
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(5000); //let it spin for about 10 seconds
}
void lowerCurtain() {
Serial.println("lowering curtains");
//Spin Motor in the other direction DOWN
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(5000); //let it spin for about 10 seconds
//Stop the motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
void loop(){
if (!client.connected()) {
reconnect();
}
client.loop();
}