`I am new to ESP8266.
I have created a website hosted on 000webhost, however whenever I connect to the server using the ESP8266's AT command set the connection closes instantly. I am trying to send some data from my Arduino board to the website via the ESP8266
Therefore I can not send any message to the website.
Any ideas on how to handle this issue? Thanks!
Output on the serial monitor
Arduino Code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //RX,TX
int LEDPIN = 13;
void setup()
{
pinMode(LEDPIN, OUTPUT);
Serial.begin(9600); // communication with the host computer
//while (!Serial) { ; }
// Start the software serial for communication with the ESP8266
mySerial.begin(9600);
Serial.println("");
Serial.println("Remember to to set Both NL & CR in the serial monitor.");
Serial.println("Ready");
Serial.println("");
}
void loop()
{
digitalWrite(13,1);
// listen for communication from the ESP8266 and then write it to the serial
monitor
if ( mySerial.available() ) { Serial.write( mySerial.read() ); }
// listen for user input and send it to the ESP8266
if ( Serial.available() )
// { mySerial.write( mySerial.print("AT+CWMODE=1") ); }
{ mySerial.write( Serial.read() ); }
}
PHP Code:
<?php
if(isset($_GET['stat'])){
$status=$_GET['stat'];
$a2="<!DOCTYPE html><br><html><br>" . $status ."<br><html>";
$fp = fopen("a2.php", "w");
fwrite($fp, $a2);
fclose($fp);
echo $status;
}
else {echo "house";}
?>
Related
I started to test the ESP8266 module, i wrote in the serial port this commands: AT, AT+RST, AT+CWMODE=1(station mode), AT+CWLAP, AT+CWJAP="My network name","password", AT+CIPMUX=1, AT+CIPSERVER=1,80, AT+CIPSEND=0,"numer of char of my string", "MyMessage", AT+CIPCLOSE=0 and it work, its sends the message to my phone.
Now I try to do some function that sends automatically those commands, but when i get to the sending message part it gives me an error.
I post my code below:
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(9, 10); // RX | TX
void setup()
{
Serial.begin(9600); // communication with the host computer
// Start the software serial for communication with the ESP8266
ESPserial.begin(9600);
delay(1000);
Serial.println("");
Serial.println("Remember to to set Both NL & CR in the serial monitor.");
Serial.println("Ready");
Serial.println("");
trimiteDate("AT+IPR=9600",1000);
trimiteDate("AT",1000);
trimiteDate("AT+RST",1000);
trimiteDate("AT+CWMODE=1",1000);
trimiteDate("AT+CWLAP",1000);
trimiteDate("AT+CWJAP=\"DIGI_d61430\",\"ddcc29eb\"",1000);
trimiteDate("AT+CIFSR",1000);
trimiteDate("AT+CIPMUX=1",1000);
trimiteDate("AT+CIPSERVER=1,80",1000);
scrie("Sir de caractere",1000);
}
void loop()
{
// listen for communication from the ESP8266 and then write it to the serial monitor
if ( ESPserial.available() ) { Serial.write( ESPserial.read() ); }
// listen for user input and send it to the ESP8266
if ( Serial.available() ) { ESPserial.write( Serial.read() ); }
}
void trimiteDate(String comanda, const int timp ){
ESPserial.println(comanda);
while ( ESPserial.available() ) { Serial.write( ESPserial.read() ); }
while ( Serial.available() ) { ESPserial.write( Serial.read() );}
delay(timp);
}
void scrie(String sir, const int timp){
int lungimeSir = sir.length();
String startTransmisie = "AT+CIPSEND=1,";
String stopTransmisie = "AT+CIPCLOSE=0";
String sirDate = startTransmisie + lungimeSir;
trimiteDate(sirDate,1000);
ESPserial.println(sir);
trimiteDate(stopTransmisie,1000);
delay(timp);
}
I am trying to access a simple web page running in my Rpi-server using ESP8266 and Arduino.
I have refereed this similar SO question , but it's not the solution for my problem.
Here is my current Arduino Code :
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(2,3); // RX, TX
#endif
char ssid[] = "RPi"; // your network SSID (name)
char pass[] = "raspberry"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "192.168.50.1";
// Initialize the Ethernet client object
WiFiEspClient client;
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
printWifiStatus();
Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (client.connect(server, 80)) {
Serial.println("Connected to server");
// Make a HTTP request
client.println("GET /simple.html HTTP/1.1");
client.println("Host: 192.168.50.1");
client.println("Connection: close");
client.println();
}
}
void loop()
{
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
// do nothing forevermore
while (true);
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Output:
Starting connection to server...
[WiFiEsp] Connecting to 192.168.50.1
Connected to server
[WiFiEsp] Data packet send error (2)
[WiFiEsp] Failed to write to socket 3
[WiFiEsp] Disconnecting 3
My simple.html looks like this.
<html>
<body>
<p>1</p>
</body>
</html>
}
I accessed to this page from web browser and it shows the content properly.
What is missing here?
Thanks in advance.
Try this line in your code
client.print("GET /simple.html HTTP/1.0\r\n\r\n");
This is ESP8266 code to turn ON/OFF an LED. I'm using the Arduino IDE built-in example code. The LED is working properly but I want to send HTTP requests to my locally-hosted web site (which send emails) but it's not working.
Connected with my local Wifi
Assigned a static IP
When I hit 192.168.1.101/gpio/1(Led ON)
When I hit 192.168.1.101/gpio/0(Led OFF) It's working but unable to hit my web site.
When I hit 192.168.1.101/gpio/1 then it should hit my locally-hosted URL (192.168.1.100/home/ardchk)
Kindly help me to sort out this issue.
#include <ESP8266WiFi.h>
const char* ssid = "SMART";
const char* password = "123456789";
const char* host = "192.168.1.100"; // Your domain
IPAddress ip(192, 168, 1, 101); // where xx is the desired IP Address
IPAddress gateway(192, 168, 1, 1); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO3
pinMode(3, OUTPUT);
digitalWrite(3, 0);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip,gateway,subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
// Match the request
int val;
if (req.indexOf("/gpio/0") != -1) {
val = 0;
if (client.connect(host, 80)) {
//starts client connection, checks for connection
Serial.println("connected to website/server");
client.println("GET /home/ardchk HTTP/1.1"); //Send data
client.println("Host: 192.168.1.100");
Serial.println("Email Sended");
client.println("Connection: close");
//close 1.1 persistent connection
client.println(); //end of get request
}
} else if (req.indexOf("/gpio/1") != -1) {
val = 1;
} else {
Serial.println("invalid request");
client.stop();
return;
}
// Set GPIO2 according to the request
digitalWrite(3, val);
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
s += (val)?"high":"low";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is destroyed
}
Based on your question/comment, I am assuming that client.connect(host, 80) is returning false.
I believe you are unable to connect to your host because your are trying to connect twice with the same client.
Your code looks like this:
// returns an already connected client, if available
WiFiClient client = server.available()
//...
if (client.connect(host, 80)) {/*...*/}
You see, you are using the already connected client to attempt to connect to your host. Instead, try creating a separate WiFiClient for that job:
WiFiClient requesting_client = server.available();
//...
if (req.indexOf("/gpio/0") != -1) {
val = 0;
// create a fresh, new client
WiFiClient emailing_client;
if (emailing_client.connect(host, 80)) {
// ...
// don't forget that you need to close this client as well!
emailing_client.stop();
}
Hope this helps!
I send my Lua code by Arduino IDE.
My sketch in Arduino:
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // RX | TX
void setup()
{
Serial.begin(9600); // communication with the host computer
//while (!Serial) { ; }
// Start the software serial for communication with the ESP8266
ESPserial.begin(9600);
Serial.println("");
Serial.println("Remember to to set Both NL & CR in the serial monitor.");
Serial.println("Ready");
Serial.println("");
delay(1000);
}
void loop()
{
// listen for communication from the ESP8266 and then write it to the serial monitor
if ( ESPserial.available() ) { Serial.write( ESPserial.read() ); }
// listen for user input and send it to the ESP8266
if ( Serial.available() ) { ESPserial.write( Serial.read() ); }
}
I open serial port and send my code:
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
conn:connect(8080, "192.168.0.100")
conn:send("GET /json.htm?type=command¶m=udevice&idx=3&nvalue=0&svalue=69 HTTP/1.1\r\nHost: 192.168.0.100\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
But my ESP return ">>" after last command.. there is screen
When I replace conn:send on this:
conn:send("GET / HTTP/1.1\r\nHost: 192.168.0.100\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
All is correct, I take respond from server...
Thanks for help!
I have a problem making my Arduino Ethernet shield to communicate with the server,
the result on the serial monitor is always:
my arduino code is
#include <Ethernet.h> //library for ethernet functions
#include <SPI.h>
#include <Dns.h>
#include <Client.h> //library for client functions
#include <DallasTemperature.h> //library for temperature sensors
// Ethernet settings
byte mac[] = {0x09,0xA2,0xDA,0x00,0x01,0x26}; //Replace with your Ethernet shield MAC
byte ip[] = { 192,168,0,54}; //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(192,168,0,53); // IP-adress of server arduino sends data to
EthernetClient client;
bool connected = false;
void setup(void) {
Serial.begin(9600);
Serial.println("Initializing Ethernet.");
delay(1000);
Ethernet.begin(mac, ip , gateway , subnet);
}
void loop(void) {
if(!connected) {
Serial.println("Not connected");
if (client.connect(server, 80)) {
connected = true;
int temp =analogRead(A1);
Serial.print("Temp is ");
Serial.println(temp);
Serial.println();
Serial.println("Sending to Server: ");
client.print("GET /formSubmit.php?t0=");
Serial.print("GET /formSubmit.php?t0=");
client.print(temp);
Serial.print(temp);
client.println(" HTTP/1.1");
Serial.println(" HTTP/1.1");
client.println("Host: http://localhost/PhpProject1/");
Serial.println("Host: http://localhost/PhpProject1/");
client.println("User-Agent: Arduino");
Serial.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("Accept: text/html");
//client.println("Connection: close");
//Serial.println("Connection: close");
client.println();
Serial.println();
delay(10000);
}
else{
Serial.println("Cannot connect to Server");
}
}
else {
delay(1000);
while (client.connected() && client.available()) {
char c = client.read();
Serial.print(c);
}
Serial.println();
client.stop();
connected = false;
}
}
the server is an Apache server running on a pc, the server ip address in the code is the pc ip address. For testing purposes I work at my homes network, there's no proxy or firewall, and I also turned of the antivirus and firewall on my pc.
the result in the serial monitor is always:
Not connected
Cannot connect to Server
Any thoughts??
It worked, the problem was in Ethernet.begin(mac, ip , gateway , subnet), I removed this line and configure the Ethernet Shield using the DHCP, Ethernet.begin(mac)
Have you verified the MAC address is correct?
If it still doesn't work, try using
Client client(server, 80);
in stead of
EthernetClient client
And change
if (client.connect(server, 80)) {
to
if (client.connect()) {
Hope this helps