I'm using NodeMCU board to communicate a laptop through WiFi. Basic communication is ok with a code like this:
void loop (){
WiFiClient client = server.available();
if (client) {
while (client.connected()){
client.println(Data_mem[0]);
delay(2000);
}
client.stop(); // tarminates the connection with the client
}
}
But when I want to send data a Timer Tick, it seems the client couldn't connect to me.
void setup(){
....
//Initialize Ticker every 40ms
Data_Rec.attach_ms(40, 40ms_Data );
}
void 40ms_Data (){
WiFiClient client = server.available();
Serial.println("40ms_Data A");
if (client) {
Serial.println("40ms_Data B");
if (client .connected()){
Serial.println("40ms_Data C");
client.println(40ms_Data [0]);
}
else{
client.stop();
}
}
}
I arduino serial monitor, i see only this:
40ms_Data A \r\n
40ms_Data A \r\n
....
So, could anyone help me? maybe it a problem of WiFiClient deceleration in a non-forever loop (like example 1). But I have no idea to fix it.
According to M.R.'s idea, this is my complete new code:
#include <ESP8266WiFi.h>
#include <Ticker.h>
/* Put your SSID & Password */
const char* ssid = "NodeMCU"; // Enter SSID here
const char* password = "12345678"; //Enter Password here
/* Put IP Address details */
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WiFiServer server(80);
const int CLIENT_TIMEOUT = 2000;
Ticker Data_Rec;
bool Sending_40ms_Start_Flag = false;
void setup() {
Serial.begin(115200);
pinMode(D0, OUTPUT);
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.begin();
Serial.println("HTTP server started");
Data_Rec.attach_ms(500, flag_enable);//500ms is for test
}
void flag_enable(){
//Read FPGA Data from SPI
//...
Sending_40ms_Start_Flag = true;
}
void loop(){
WiFiClient client = server.available();
if (client) {
while(client.connected()){
Serial.println("40ms_Data B");
if (Sending_40ms_Start_Flag){
client.println("Server listening.\r");
Sending_40ms_Start_Flag = false;
}
delay(1);//without this delay, ESP would be reset(because it cannot handle background processes)
}
// else{
// client.stop();
// }
}
}
This code worked. But the server RST the TCP connection after sending one,two or tree "Server listening." as Wireshark shows: Wireshark Capture
What is probable cause of TCP Reset from ESP?
Besides that some syntax errors in your code:
A good solution is to create a static pointer object from client and pass it as an argument to ms40_Data function:
void ms40_Data(WiFiClient *client)
{
Serial.println("40ms_Data A");
if (*client)
{
Serial.println("40ms_Data B");
if (client->connected())
{
Serial.println("40ms_Data C");
//client.println(40ms_Data [0]);
}
else
{
client->stop();
}
}
}
void setup()
{
...
// create a static object from WiFiClient
static WiFiClient client = server.available();
//Initialize Ticker every 40ms
Data_Rec.attach_ms(40, ms40_Data, &client);
}
void loop()
{
}
Another solution is, you can set a flag inside your ticker and then call your ms40_Data function:
bool start_40ms_Data = false;
void ms40_Data()
{
static WiFiClient client = server.available();
Serial.println("40ms_Data A");
if (client)
{
Serial.println("40ms_Data B");
if (client.connected())
{
Serial.println("40ms_Data C");
//client.println(40ms_Data [0]);
}
else
{
client.stop();
}
}
}
void flag_enable()
{
start_40ms_Data = true;
}
void setup()
{
...
//Initialize Ticker every 40ms
Data_Rec.attach_ms(40, flag_enable);
}
void loop()
{
if (start_40ms_Data)
{
ms40_Data();
start_40ms_Data = false;
}
}
The point is to adding this to setup function:
WiFi.mode(WIFI_AP);
Full explanation: Here
Thank you,
Related
Derived from the following code.
I uploaded that code to my ESP8266 and it's all good and okay when I communicate with it with my laptop while my laptop is connected to my network with a LAN cable.
The problem is: when I try to communicate with the ESP with my laptop or phone over Wi-Fi I get ERR_CONNECTION_REFUSED though they rarely work and communicate. I tried another phone another router, and did a factory reset to my router, and all the same.
I know that there is an option in the router that is called AP Isolation and it's been checked and it's disabled.
My question is: What could possibly be the reason for this error ERR_CONNECTION_REFUSED when I communicate with ESP8266 with that code?
If someone could help me I would be pleased as I am stuck in this situation.
The ESP code (same as the link):
#include <ESP8266WiFi.h>
const char* ssid = "*****";
const char* password = "*******";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected");
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
// prepare a web page to be send to a client (web browser)
// the connection will be closed after completion of the response
// the page will be refreshed automatically every 5 sec
String prepareHtmlPage() {
String htmlPage = String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" +
"Refresh: 5\r\n" + "\r\n" +
"<!DOCTYPE HTML>" + "<html>" +
"Analog input: " + String(analogRead(A0)) +
"</html>" + "\r\n";
return htmlPage;
}
void loop() {
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client) {
Serial.println("\n[Client connected]");
while (client.connected()) {
// read line by line what the client (web browser) is requesting
if (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n') {
client.println(prepareHtmlPage());
break;
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disconnected]");
}
}
I hope its not too late and it helps someone in need.
You need to do 2 things
Before WiFi.begin(), you need to add
WiFi.mode(WIFI_STA);
Second, you need to
#include <ESP8266mDNS.h>
in setup()
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
and in loop()
MDNS.update();
Lastly, do not forget to add the following in every server response. Else, you will hit CORS error.
server.sendHeader("Access-Control-Allow-Origin", "*");
Please dont forget to add supporting libraries. Let me know if it works. Demo code would look like the below
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "<your SSID>";
const char* password = "<your WIFI Password>>";
int serverPort = 80;
int boardBaud = 115200;
ESP8266WebServer server(serverPort);
void handleRoot() {
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", "<h1>Hello World</h1>");
}
void handleNotFound() {
if (server.method() == HTTP_OPTIONS)
{
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "*");
server.send(204);
}
else
{
server.send(404, "text/plain", "Error");
}
}
void setup(void) {
Serial.begin(boardBaud);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
I would like to use https://github.com/tzapu/WiFiManager in conjunction with onSoftAPModeProbeRequestReceived. The end goal is to config the wifi with WifiMaager then "switch over" and send probe request information via the wifi.
I get this to work without wifi manager using the following
#include <ESP8266httpUpdate.h>
#include <ESP8266WiFi.h>
#include <esp8266httpclient.h>
#include <stdio.h>
const char* ssid = "someap"; // The SSID (name) of the Wi-Fi
network you want to connect to
const char* password = ""; // The password of the Wi-Fi network
int status = WL_IDLE_STATUS;
String macAddr = "";
WiFiEventHandler probeRequestPrintHandler;
WiFiEventHandler probeRequestBlinkHandler;
bool blinkFlag;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("Starting");
WiFi.persistent(false);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
probeRequestPrintHandler =
WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
probeRequestBlinkHandler =
WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestBlink);
while ( status != 3)
{
Serial.print("Attempting to connect to network, SSID: ");
Serial.println(ssid);
status = WiFi.status();
WiFi.begin(ssid, password);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println(WiFi.localIP());
}
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt)
{
if (macAddr != macToString(evt.mac))
{
macAddr = macToString(evt.mac);
Serial.print("Probe request from: ");
Serial.print(macToString(evt.mac));
Serial.print(" RSSI: ");
Serial.println(evt.rssi);
}
}
void onProbeRequestBlink(const WiFiEventSoftAPModeProbeRequestReceived&) {
blinkFlag = true;
}
void loop() {
if (blinkFlag) {
HTTPClient http; //Declare an object of class HTTPClient
http.begin("http://requestbin.fullcontact.com/110f1ss6a1?test=true");
//Specify request destination
int httpCode = http.GET();
Serial.println(httpCode);
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(payload); //Print the response payload
}
http.end(); //Close connection
macAddr="";
blinkFlag = false;
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
status = 0;
}
delay(1000);
}
String macToString(const unsigned char* mac) {
char buf[20];
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buf);
}
When the connection is established we rely on WiFiEventHandler probeRequestPrintHandler; and WiFiEventHandler probeRequestBlinkHandler; This does work and does collect the mac address, how ever it can not connect to the AP. Do I need to close the current wifi mode open the connection then close it?
Seems all I needed to do is add WiFi.begin(); before void loop()
Made some code for a NodeMCU in the Arduino IDE to push a button using MQTT. The code works perfectly fine for some time, but after a couple of hours it will not respond anymore.
The code is very frankenstein, since I am a mega rookie, and is as follows:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>
const char* ssid = "ap_name"; //change
const char* password = "ap_pw"; //change
const char* mqttServer = "server_ip"; //change
const int mqttPort = 1883;
const char* mqttUser = "server_name"; //change
const char* mqttPassword = "server_pw"; //change
WiFiClient espClient;
PubSubClient client(espClient);
Servo servo;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
servo.attach(D4);
servo.write(70);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.publish("esp/test", "Hello from ESP8266");
client.subscribe("esp/test");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
if(*payload == 49){
rotServo();
Serial.println();
Serial.print("Roterar servo");
delay(3000);
client.publish("esp/test", "0");
}
Serial.println();
Serial.println("-----------------------");
}
void rotServo(){
servo.attach(D4);
servo.write(70);
delay(1000);
servo.write(175);
delay(2000);
servo.write(70);
delay(3000);
servo.detach();
}
void loop() {
client.loop();
}
Anyone that would know what might be causing it to stop working?
It could be because, you never reconnect if your client ever gets disconnected.
See the example here for more details, but here is the reconnect function from the example and how it is called in the loop. You would need to tailor it to suite your application.
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
...
}
On another note, while I don't think it would cause your specific problem, in your callback, you just compare the contents of the payload to the number 49.
if(*payload == 49){
...
}
You should check that the topic is the actual topic you are interested in and that the length is also what you expect, before you start looking at the payload.
I am trying to send data from an Arduino Uno using ESP8266 to Firebase via PHP. The connection between the Firebase DB and PHP seems to be fine since I can manually send data to it through my browser. However, I am not able to send data from the Arduino to PHP.
Here is the Arduino code:
#include "SoftwareSerial.h"
String ssid ="xxxx";
String password="xxxx";
String server = "firstfirebase.000webhostapp.com"; // www.example.com
String uri = "/firebaseTest.php";// our example is /esppost.php
SoftwareSerial esp(2, 3);// RX, TX
void setup() {
esp.begin(9600);
Serial.begin(9600);
reset();
connectWifi();
}
void reset() {
esp.println("AT+RST");
delay(1000);
if(esp.find("OK"))
Serial.println("Module Reset");
}
void connectWifi() {
String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
esp.println(cmd);
delay(4000);
if(esp.find("OK")) {
Serial.println("Connected!");
} else {
connectWifi();
Serial.println("Cannot connect to wifi");
}
}
void loop() {
httppost();
delay(1000);
}
void httppost () {
esp.println("AT+CIPSTART=\"TCP\",\"firstfirebase.000webhostapp.com\",80");//start a TCP connection.
if( esp.find("OK")) {
Serial.println("TCP connection ready");
}
delay(1000);
String getRequest = "GET firebaseTest.php?arduino_data=56.00 HTTP/1.1\r\nHost: firstfirebase.000webhostapp.com\r\n\r\n";
String sendCmd = "AT+CIPSEND";//determine the number of caracters to be sent.
esp.print(sendCmd);
esp.println(getRequest.length() );
delay(500);
if(esp.find(">")) {
Serial.println("Sending..");
esp.print(getRequest+"\r\n\r\n");
delay(500);
esp.println("AT+CIPSTATUS");
if( esp.find("SEND OK")) {
Serial.println("Packet sent");
while (esp.available()) {
String tmpResp = esp.readString();
Serial.println(tmpResp);
}
// close the connection
esp.println("AT+CIPCLOSE");
}
}
}
Here is a screenshot of the serial monitor:
You are missing a / in front of your path.
It should be:
String getRequest = "GET /firebaseTest.php?arduino_data=56.00 HTTP/1.1\r\nHost: firstfirebase.000webhostapp.com\r\n\r\n";
The user Agent it is very important:
AT+CIPSTART="TCP","somedomain.it",80
AT+CIPSEND=EXACT NUMBER OF GET REQ
GET /Arduino/ciao.php HTTP/1.1\r\n
Host: 192.168.0.111\r\n
User-Agent: wget/1.12\r\n\r\n
In this code I wrote that the server send again what it received from client:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
WiFiServer server(8080);
WiFiClient client;
void setup()
{
initHardware();
setupWiFi();
server.begin();
}
void loop()
{
if (!client.connected()) {
// try to connect to a new client
client = server.available();
} else {
// read data from the connected client
if (client.available() > 0) {
char inChar= client.read();
String in=(String) inChar;
Serial.print(in);
server.write(inChar);
}
}
}
void setupWiFi()
{
WiFi.mode(WIFI_AP_STA);
WiFi.softAP("esp", "123456789");
// WiFi.softAP("RControl", WiFiAPPSK);
}
void initHardware()
{
Serial.begin(115200);
}
Now it receive what I send from my PC, but it doesn't send anything to me.
Why? What is wrong here?