ESP8266 - Arduino firmware : IP address ok, but router don't route anything - arduino

I have a problem, i execute this simple code - send an http request :
include <ESP8266WiFi.h>
//Router 1 - Livebox
const char* ssid = "Livebox-9e38";
const char* password = "xxxxxxxxxxx";
//Router 2 - Android phone
//const char* ssid = "DARKSIDE";
//const char* password = "xxxxxxxxx";
// server address:
char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);
WiFiClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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 httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
When I am on the router 2 (a simple hotspot router on a android phone) the code works.
When I am on the router 1 (a livebox supply by my internet provider), i have an IP address, i can see the ESP in the router (it's a web interface) but i can't get any result form the request (and i can't ping the module). The router don't route anything.
Of course the router have access to internet and from a computer connect to the router in wifi with the Curl command, the request work.
Have you some idea for me ?

try with these changes:
client.println("GET /latest.txt")
client.println( HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
client.println("{\"on\":true}");

Related

Communication between Postman and Arduino

I plan to send from a server a POST request to my Arduino Uno WiFi Rev2. More correctly, when the server sends this request a servomotor controlled by the Arduino should start moving. Right now, I'm using Postman to try and connect with the Arduino but I can't get it to work. So first I connect the Arduino to WiFi using my smartphone as a hotspot. This should be the unit's IP address, right?
I then try to send a POST request to this IP but it doesn't work. I'm also unsure which port number I should use, so I have just been trying with the standard ones (80, 4430, etc).
What am I doing wrong and how should I proceed?
EDIT: Here is my code.
#include <SPI.h>
#include <Servo.h>
#include <WiFiNINA.h>
char ssid[] = "MyNetwork"; // The network SSID
char pass[] = "testtest"; // The network password
int status = WL_IDLE_STATUS; // The Wifi radio's connection status
Servo servo_9; // Initializes the servomotor
WiFiServer server(80); // Server socket
//WiFiClient client;
WiFiClient client = server.available();
void setup() {
// Connects the servomotor to pin 9
servo_9.attach(9, 500, 2500);
// Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
enable_WiFi();
connect_WiFi();
server.begin();
printCurrentNet();
printWifiData();
}
void loop() {
// Check the network connection once every 10 seconds:
delay(10000);
client = server.available();
if(client){
printWEB();
}
}
void enable_WiFi(){
// Check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// Don't continue
while (true);
}
// Check if the latest Firmware version is installed
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
}
void connect_WiFi(){
// 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);
// Wait 10 seconds for connection:
delay(10000);
}
// Now the arduino is connected, so print out the data:
Serial.print("You're connected to the network: ");
Serial.println();
}
void printCurrentNet() {
// Print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// Print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// Print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI): ");
Serial.println(rssi);
// Print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type: ");
Serial.println(encryption, HEX);
Serial.println();
}
void printWifiData() {
Serial.println("Your board's IP and MAC address: ");
// Print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// Print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
Serial.println();
}
// Find the MAC adress for your Arduino board
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}
void printWEB() {
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
void servomotorGate(){
int position = 0;
for (position = 0; position <= 90; position += 1) {
servo_9.write(position);
Serial.println("Opening the gate");
}
delay(5000); // Wait for 5000 millisecond(s)
for (position = 90; position >= 0; position -= 1) {
servo_9.write(position);
Serial.println("Closing the gate");
}
}
I added the server client to listen for connections. However, the main reason why I couldn't connect to my IP address with Postman was that I was trying to get WiFi from my phone as a hotspot. I guess there is a problem with the port forwarding when using a hotspot. In the end, I solved the issue by connecting to a normal router WiFi network.

Sending GET Request with NodeMCU ESP8266

I'm trying to send a standard http GET request to my website using the NodeMCU example client code however for some reason it is not working.
If I type into my browser: snackrefill.com/GetData.php?username=aaaa&pin=bbbb&cost=cccc then it will successfully connect with the php script and save a data base entry.
The problem is that when I try to do the same using the NodeMCU module it does not work. it seems to be connecting to WiFi and the server just fine but when it sends the request nothing seems to happen.
Am I structuring my request wrong?
#include <ESP8266WiFi.h>
const char* ssid = "BELL473";
const char* password = "XXXXXXX";
const char* host = "ns8451.hostgator.com";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //works!
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) { //works!
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/GetData.php";
url += "?username=";
url += "aaaa";
url += "&pin=";
url += "bbbb";
url += "&cost=";
url += "cccc";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
Serial.println("closing connection");
}
The reason it was not sending was because the host was wrong, i was pointing to my website nameserver when i should have pointed it to the regular website name. I changed const char* host = "ns8451.hostgator.com"; to const char* host = "snackrefill.com"; and now it works!

Unable to use DS18B20 temperature sensor with a GSM shield on an Arduino UNO

I'm trying every solution but no way....
I have the official Antenova GSM shield and the sensor DS18B20.
If I connect only the GSM shield without the sensor, I get -127 from the sensor and the shield is able to do the HTTP post to my server successfully. If the sensor is connected, it returns the right temperature, but the client.connect(server, port) never returns. I set the sensor pin to 12 instead of 2 to avoid problems, but seem like they are in conflict. I'm already using the external power supply.
// libraries
#include <GSM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// PIN Number
#define PINNUMBER "1218"
// APN data
#define GPRS_APN "web.omnitel.it" // replace your GPRS APN
#define GPRS_LOGIN "" // replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 12
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;
// URL, path & port (for example: arduino.cc)
char server[] = "mancioboxblog.altervista.org";
char path[] = "/add.php";
int port = 80; // port 80 is the default for HTTP
// check the connection status
int con = 0;
// string to save the temp
String data = "";
// temp random
long temp;
void setup() {
//file version
Serial.println("Version: 1.6");
// initialize serial communications and wait for port to open:
Serial.begin(9600);
/* only for old USB usually not required
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}*/
Serial.println("Starting Arduino web client.");
// connection state
boolean notConnected = true;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (notConnected) {
Serial.println("I'm trying to connect");
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
notConnected = false;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("I'm connected");
// this operation is faster than connect to sim card
// Start up the temperature sensor library
Serial.println("initialize sensors");
sensors.begin();
// wait the the gsm shield is initialized
delay(1000);
}
void loop() {
/* GET THE TEMPERATURE */
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
Serial.print("Temperature for Device 1 is: ");
temp = sensors.getTempCByIndex(0);
Serial.print(temp); // Why "byIndex"?
// You can have more than one IC on the same bus.
// 0 refers to the first IC on the wire
//only for test
//temp = random(33);
data = "temp=" + (String)temp;
Serial.println("temp stored = " + data);
delay(2000);
con = client.connect(server, port);
// wait connection engaged
delay(2000);
// if you get a connection, report back via serial:
if (con == 1) {
Serial.println("connected");
// Make a HTTP request:
client.print("POST ");
client.print(path);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(data.length());
client.println("");
client.println(data);
client.println("");
//what I'm sending
Serial.print("POST ");
Serial.print(path);
Serial.println(" HTTP/1.1");
Serial.print("Host: ");
Serial.println(server);
Serial.println("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
Serial.println("Connection: close");
Serial.print("Content-Length: ");
Serial.println(data.length());
Serial.println("");
Serial.println(data);
Serial.println("");
// if you didn't get a connection to the server:
} else if(con == -1){
Serial.println("connection failed: TIMED_OUT -1");
} else if(con == -2){
Serial.println("connection failed: INVALID_SERVER -2");
} else if(con == -3){
Serial.println("connection failed: TRUNCATED -3");
} else if(con == -4){
Serial.println("connection failed: INVALID_RESPONSE -4");
}
if(client.connected()){
client.stop(); // DISCONNECT FROM THE SERVER
Serial.println("enter in the if and stop the client");
}else{
Serial.println("the client is not connected");
}
//keep over 2 second otherwise is not able to close and reopen connection
delay(2000); // WAIT MINUTES BEFORE SENDING AGAIN
Serial.println("code repeat");
}
Is there a problem with my code? Maybe I shouldn't read the sensor before the client connects?

400 Bad Request arduino esp8266

Hi i am trying to test a value form nodemcu board i am using arduino ide to code and have modified the WIFIClient example to send a value to my localhost using GET. it gives me bad request error. any help or suggestion is appreciated
Modified WIFIClinet
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "pwd";
const char* host = "http://10.0.0.39/edu/arduino.php";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url ="?v=we";
//
//
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
//
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
Output from serial monitor
connecting to http://10.0.0.39/edu/arduino.php
Requesting URL: ?v=we
HTTP/1.0 400 Bad Request
Server: httpd
Date: Sat, 01 Jan 2011 22:02:19 GMT
Content-Type: text/html
Connection: close
<HTML><HEAD><TITLE>400 Bad Request</TITLE></HEAD>
<BODY BGCOLOR="#cc9999"><H4>400 Bad Request</H4>
Bad filename.
</BODY></HTML>
I hope this is helpful for people running this on local host i changed
const char* host = "http://10.0.0.39/edu/arduino.php";
to
const char* host = "10.0.0.39";
and
String url ="?v=we";
to
String url ="/edu/arduino.php?v=we";

simple web server nort working with arduino wifi shield

I have recently bought an arduino wifi shield(Atmal chip 32UC3A1512-U), which I connected with
my Arduino Mega ADK R3 board)...It is getting connected to my wifi network, But when I run the
SimpleWebServer Example provided in the library to on/off the LED is not working. The code is given below...
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "belkin.E33"; // your network SSID (name)
char pass[] = "abc123cde456"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(9, OUTPUT); // set the LED pin mode
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("new client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click here turn the LED on pin 9 on<br>");
client.print("Click here turn the LED on pin 9 off<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(9, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(9, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
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");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
The result that I am getting in the serial monitor is
Attempting to connect to Network named: belkin.E33
SSID: belkin.E33
IP Address: 192.168.2.5
strength (RSSI):-56 dBm
To see this page in action, open a browser to http://192.168.2.5
But When I am opening the browser with the specified IP address, It is showing
Could not Connect to 192.168.2.5
I have tried this in mozilla and chrome from my ubuntu machine...also tried from some other machines in the same network but with the same result. But when I am pinging to 192.168.2.5 it is pinging...What went wrong??? . My friend adviced to change the firmware...Is it an issue,bcas as told earlier simple examples for establishing the connection are working...Please guide me
I've got the same problem after upgrading Arduino IDE to lastest version (v2) from v1.0.8 which is doing fine with the wifi shield tests (client and server).
Going to try the nightly build now and see if it's fixed.
Edit: Yeap, Nightly build solves this issue.

Resources