WiFi Status 1 on ESP8266 - arduino

I'm trying to connect my Arduino Uno R3 + ESP8266 to a WiFi connection, and it returned a status of 1 when I printed out WiFi.status(), does anyone now what does it really mean and what's the solution? Here's my ESP8266 code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// WiFi CREDENTIALS
const char *ssid = "xxxx";
const char *password = "xxxx";
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
Serial.println("Connect to: ");
Serial.println(ssid);
}
void loop(){
delay(5000);
WiFi.mode(WIFI_STA);
Serial.println();
Serial.println("Connect to: ");
Serial.println(ssid);
Serial.println(WiFi.status());
Serial.println(WL_CONNECTED);
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
delay(15000);
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("From ESP Connected!");
}
else {
Serial.println("From ESP Not Connected!");
}
}
=== UPDATE ===
I've tried using my smartphone's hotspot and it worked on the first try.

I found this in Arduino Forum. I hope it is useful for you. And status 1 means no ssid according to enum below.
typedef enum {
WL_NO_SHIELD = 255, // for compatibility with WiFi Shield library
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL = 1,
WL_SCAN_COMPLETED = 2,
WL_CONNECTED = 3,
WL_CONNECT_FAILED = 4,
WL_CONNECTION_LOST = 5,
WL_DISCONNECTED = 6
} wl_status_t;

I'm not sure myself, but when I start over using the cleaner code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// WiFi CREDENTIALS
const char *ssid = "xxxx";
const char *password = "xxxx";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
Serial.println("From ESP Connected!");
}
delay(5000);
}
My gut is telling me that maybe WiFi.mode(WIFI_STA) is causing the error?
By the way, it worked already, thanks Juraj and cbalakus for the help!

Related

NodeMCU ESP8266 Client-Server communication

I am trying to connect two ESP8266 boards to communicate with each other. The client board first connects to the server and sends a message, the character 'd', after the server receives the message it replies with "Hello from server".
Server
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
char *ssid = "SSID";
char *pass = "PASS";
int stat;
WiFiServer server(8080);
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,pass);
Serial.println("Connecting to wifi ");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}
Serial.print("SSID :");
Serial.print(ssid);
Serial.println("ip: ");
Serial.print(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if(client){
if(client.connected()){
Serial.println("Client connected");
if(client.available() > 0){
delay(500);
char data = client.read();
Serial.println(data);
delay(500);
}
char d[] = "Hello from server";
client.println(d);
}
client.stop();
}
}
Client
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
char *ssid = "SSID";
char *pass = "PASS";
char *host = "ip";
WiFiClient client;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,pass);
Serial.println("Connecting to wifi");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
delay(500);
}
//Serial.println("Connecting to wifi network");
Serial.print("SSID: ");
Serial.print(ssid);
Serial.println("Connected");
}
void loop() {
// put your main code here, to run repeatedly:
if(client.connect(host, 8080)) {
Serial.println("Connected to server");
client.println('d');
if(client.available() > 0){
delay(500);
Serial.println(client.read());
delay(500);
}
else{
Serial.println("No data received");
}
}
else{
Serial.println("Cannot connect to server");
}
client.stop();
}
The problem is that the message from client is received by the server. But the reply is not sent. When I try to see the reply from server using serial monitor in the client it gets connected to the server and the message is sent but the reply is not received.
I'm new to this domain and would like to know whether I'm doing it correctly or not. Any suggestions and solutions will help a lot.
Thank you.

ESP8266 stopped receiving data from firebase

I am trying to receive data from firebase to my ESP8266 so that I can send a mail using received data on button press. But the ESP stops receiving data after a couple of minutes. Can anyone tell the reason behind it?
Here's the code.
#include <ESP8266WiFi.h>
#include "Gsender.h"
#include <FirebaseArduino.h>
#define FIREBASE_HOST "iotapp11.firebaseio.com"
int button = 0;
const char* ssid = "Redmi 3s";
const char* password = "alohomora";
void setup()
{
pinMode(button,INPUT);
Serial.begin(115200);
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");}
Serial.println(" connected");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST);
}
void loop(){
Firebase.get(FIREBASE_HOST); //Tried putting it in void setup and even removed it but nothing worked.
int but_val = digitalRead(button);
Gsender *gsender = Gsender::Instance();
String emailid =Firebase.getString("Email");
String subject =Firebase.getString("Subject");
String content =Firebase.getString("Content");
Serial.println(emailid);
Serial.println(subject);
Serial.println(content);
if ( but_val == LOW){
Serial.println("Button pressed");
delay(1000);
gsender->Subject(subject)->Send(emailid, content) ;
Serial.println("Message sent.");
delay(1000);
} else {
Serial.println("Button Not pressed");
delay(1000);
}
}

Go to link using if condition using Arduino IDE

I'm just trying to get myself here "http://192.168.1.103:30000/?k=23&v=capture" when an if condition meet its requirement.
#include <ESP8266WiFi.h>
// I purposely don't include the ssid and ssid1 here
WiFiServer server(80);
void setup() {
pinMode(1, INPUT);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
String link = "http://192.168.1.103:30000/?k=23&v=capture";
WiFiClient client = server.available();
if (!client) {
return;
}
int var = digitalRead(1);
if (var == HIGH) {
client.print(link);
}
Let's say:
I already run Chrome.
How can that link above be called without even typing it on Chrome? I want to connect to it automatically.
Any method you could teach? I got the feeling this code itself is wrong.
Thanks.
-- EDIT --
NEW CODE FOR UNO
//language c++
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x3F // Scanning address
LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
Servo Servo1;
int servopin = 9;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
// initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight(); // finish with backlight on
lcd.setCursor(3, 0); //Start at character 4 on line 0
lcd.print("WAITING...");
pinMode(12, OUTPUT); // pin LaserLight
pinMode(11, INPUT); // pin LaserDetector
pinMode(10, INPUT); // pin PIR
pinMode(9, OUTPUT); // pin Servo
pinMode(8, OUTPUT); // MCU PIN GPIO2
Servo1.attach(servopin);
}
void loop() {
digitalWrite(12, HIGH);
boolean inputlaser = digitalRead(11);
boolean inputpir = digitalRead(10);
Serial.println(inputlaser);
Serial.println(inputpir);
if (inputlaser < 1) {
digitalWrite(8, HIGH);
lcd.setCursor(0, 0);
lcd.print("camera on");
lcd.setCursor(0, 1);
lcd.print("robber!");
delay(5000);
Servo1.write(180);
} else if (inputpir > 0) {
Servo1.write(180);
lcd.setCursor(0, 0);
lcd.print("robber inside!");
lcd.setCursor(0, 1);
lcd.print("HELP ROBBER!");
delay(500);
} else {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("standby...");
delay(500);
}
}
NEW CODE FOR MCU
#include <ESP8266WiFi.h>
char server[] = "192.168.1.103";
WiFiClient client;
void setup() {
pinMode(4, INPUT);
digitalWrite(4, LOW);
Serial.begin(115200);
delay(10);
Serial.println();
WiFi.softAP(ssid1, password1);
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
boolean var = digitalRead(4);
if (var == HIGH) {
client.connect(server, 30000);
Serial.println("connected");
// Make your API request:
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
Serial.println(digitalRead(4));
}
First understand a fact that you doesn't need google chrome for requesting a website.
client.println("GET /?k=23&v=capture");
client.println("Host: 192.168.1.103");
What you do in the above line is that you request for /?k=23&v=capture addressed content in the ip address 192.168.1.103, Actully this is what you do when you use a google chrome. On PC you require a google chrome (or any other browser) for web site request because its difficult to request using commands each time (Think of requesting for a single page using a hell of http commands instead of using chrome, Ohh that's mess). So understand chrome isn't needed to access a site.

Multi-file arduino esp8266 sketch sharing a reference to server

As my sketches get bigger, the code looks awful as one file and the number of global variables is too high.
For the webconfig part of my sketch the device attempts to get online and if it fails it scans for available wifi networks, goes into Access Point Mode, starts a server, sends the list of ssid's and listens for a response of an ssid/passwd pair. The server is only for the configuration.
Ideally I'd like the cleaned up main.ino file to look like
#include "config.h"
void setup(){
Serial.begin(115200);
Serial.println();
Serial.println("--------------------------");
Serial.println("ESP8266 multifile");
Serial.println("--------------------------");
getOnline();
}
void loop(){
if(IN_CONFIG_MODE){
server.handleClient();
}
}
with a config.h something like..
#ifndef config_h
#define config_h
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
ESP8266WebServer server;
void getOnline();
#endif
and then a config.cpp where it does all stuff described above yet gives the main.ino program access to the server instance so it can listen in the main loop.
#include "config.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
char *espssid = "espAPsb";
char *ssid = "street_no_vale2";
char *pwd = "jjjjjjjjx";
char ssids[300];
extern server(80);
void handleRoot(){
server.send(200, "text/html", "<h1>root of espAPsb AP server</h1>");
//send json of available ssids
}
void scan(){
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
delay(10);
}
}
Serial.println("");
}
void setupAP(){
WiFi.softAP(espssid);
server.on("/", handleRoot);
server.begin();
Serial.println();
Serial.print("connected as AP ");
Serial.println(espssid);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
}
void getOnline(){
WiFi.begin(ssid, pwd);
int tries =0;
int success=1;
while (WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
tries++;
if (tries==15){
success=0;
Serial.println("WiFi not connected");
scan();
setupAP();
break;
}
}
if (success){
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
}
You can make an own library from your networking code, which you can easily reuse.
https://www.arduino.cc/en/Hacking/LibraryTutorial
Alternatively, you can use multiple "tabs" in Arduino IDE which works like a standard multi-source project. http://arduino.land/FAQ/content/7/43/en/breaking-a-sketch-into-multiple-files.html
(Btw, StackExchange hosts a dedicated Ardunio microsite here: https://arduino.stackexchange.com/ Have fun there :)

Can't commute a relay with Arduino wifi shield + Xively

I’m trying to turn on and off an LED light bulb connected to a Tinterkit Relay.
I’m using an Arduino UNO r3 connected to internet thanks to an official Arduino Wifi shield.
I’ve done a simple website with two buttons to send a 1 (on) or a 0 (off) to my Xively account.
I’ve written a code to detect the last posted channel value. The code is working fine and I’m able to detect a 1 or a 0 every 3 seconds approximately. The problem is that the relay doesn’t commute.
Please please, I would appreciate a lot your help to solve this problem.
Here is the code:
#include <SPI.h>
#include <WiFi.h>
#include <HttpClient.h>
#include <Xively.h>
#include <TinkerKit.h>
char ssid[] = "XXXXXX";
char pass[] = "XXXXXX";
int state;
int status = WL_IDLE_STATUS;
char myIntStream[]="LED";
char xivelyKey[] = "XXXXXX";
#define FEED_ID XXXXXX
TKMosFet relay(O0);
XivelyDatastream datastreams[] = {
XivelyDatastream(myIntStream, strlen(myIntStream), DATASTREAM_INT),
};
XivelyFeed feed(FEED_ID,datastreams,1);
WiFiClient client;
XivelyClient xivelyclient(client);
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void setup() {
Serial.begin(9600);
Serial.println("LED light bulb");
Serial.println();
while ( status != WL_CONNECTED) {
Serial.println("Connecting to internet ...");
status = WiFi.begin(ssid, pass);
Serial.println("");
delay(5000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
Serial.println();
Serial.print("Datastream is:");
Serial.println(datastreams[0]);
Serial.print("LED value = ");
state = datastreams[0].getInt();
Serial.print(state);
if(state == 0){
relay.off();
Serial.println(" => Light is on.");
}
else if(state == 1){
relay.on();
Serial.println(" => Light is off.");
}
xivelyclient.get(feed, xivelyKey);
}

Resources