Hello all,
I’ve got a project on my mind that requires an Arduino Uno, wifi connection and Blynk ofcourse.
I’ve managed to program an ESP as standalone and has been working great so far but i havent had the same luck when it comes to connecting it with my Arduino.
What i am trying to do is program the ESP via the Arduino IDE so it can connect to my WIFI using the WPS method, and then act as a wifi shield for my arduino Uno which runs Blynk.
What i’ve written so far:
ESP CODE:
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
int AlarmState = LOW;
String command;
bool startWPSPBC() {
bool wpsSuccess = WiFi.beginWPSConfig();
if (wpsSuccess) {
//in case of a timeout returns an empty ssid
String newSSID = WiFi.SSID();
if (newSSID.length() > 0) {
// WPSConfig has already connected in STA mode successfully
} else {
wpsSuccess = false;
}
}
return wpsSuccess;
}
void Communicate() {
if (Serial.available()) {
String command = "";
while (Serial.available()>0)
{
command += (char)Serial.read();
delay(100);
}
}
}
void Translate() {
if (command == "Movement") {
AlarmState = HIGH;
}
}
void setup() {
Serial.begin(9600);
delay(100);
WiFi.mode(WIFI_STA);
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM,
while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
delay(500);
}
wl_status_t status = WiFi.status();
if (status == WL_CONNECTED) {
digitalWrite(4, HIGH); // GPIO4, LED ON, show WPS & connect OK
Serial.write("C");
} else {
digitalWrite(4, LOW);
pinMode(2, INPUT_PULLUP); // Push Button for GPIO2 active LOW
while (digitalRead(2) == HIGH) // wait for WPS Button active
yield();
if (!startWPSPBC()) {
} else {
WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM,
while (WiFi.status() == WL_DISCONNECTED) { // last saved credentials
delay(500);
}
digitalWrite(4, HIGH); // GPIO4, LED ON, show WPS & connect OK
Serial.write("C")
}
}
char auth[] = "My auth code";
Blynk.config(auth);
Blynk.connect(3333);
}
void loop() {
Blynk.run();
Communicate();
Translate();
if (AlarmState == HIGH) {
Blynk.notify("Someone Pushed the Button");
}
}
ARDUINO CODE:
#include <SoftwareSerial.h>
SoftwareSerial esp(2,3);
const int WifiPin= 12;
const int AlarmPin=8;
int buttonState;
int WifiState;
int AlarmState= LOW;
int LastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
String command;
void ButtonReading(){
int reading = digitalRead(AlarmPin);
if (reading != LastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
AlarmState = HIGH;
}else AlarmState = LOW;
}
}
LastButtonState = reading;
}
void Communicate() {
if (esp.available()) {
String command = "";
while (esp.available()>0)
{
command += (char)esp.read();
delay(100);
}
}
}
void Translate() {
if (command == "C") {
WifiState = HIGH;
}else{
WifiState = LOW;
}
delay(100);
}
void setup() {
esp.begin(9600);
pinMode(AlarmPin,INPUT);
pinMode(WifiPin,OUTPUT);
}
void loop() {
ButtonReading();
if (AlarmState == HIGH){
esp.write("Movement");
}
Communicate();
Translate();
if (WifiState == HIGH){
digitalWrite(WifiPin,HIGH);
}else{
digitalWrite(WifiPin,LOW);
}
}
You can see in both codes that i am using a LED to check if my wifi connection is up.
On ESP it lights when i connect but not on Arduino.
Also when i press the button connected to my Arduino, soon after the ESP hangs.
My bet is that i am not using the serial commands properly, but i would love to get some guidance since its almost 2 weeks that i am stuck here, and it gets very frasturating.
Any other tips are also welcome.
My goal is to have Arduino control some motors and gather some data and then send them to my Blynk app, where i can access them from anywhere and act accordingly to my needs.