First of all, sorry for my bad English, I'm French. :)
OK, so my goal is to send toward a web server a data of temperature at regular time. I use a LAMP server on a Raspberry Pi computer and the temperature is measured from an Arduino board linked to an Ethernet shield. For this purpose I set a POST request on the Arduino side to send the value of the variable "temp" to the server.
This part seems to work properly because the result to the client.read() function is good and match to the result of the page test.php that I host on my Raspberry Pi.
Here you can see my Arduino script:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xF6, 0xFF };
byte ip[] = { 192, 168, 0, 9};
byte gateway[] = { x,x,x,x };
EthernetClient client;
String temp= "data=5";
void setup()
{
Ethernet.begin(mac, ip, gateway);
Serial.begin(9600);
Serial.println(Ethernet.localIP());
delay(1000);
delay(1000);
Serial.println("connecting...");
if (client.connect("192.168.0.55",80))
{
Serial.println("Sending to Server: ");
client.println("POST /test.php HTTP/1.1");
Serial.print("POST /test.php HTTP/1.1");
client.println("Host: 192.168.0.55");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.println("User-Agent: Arduino/1.0");
client.print("Content-Length: ");
client.println(temp.length());
client.println();
client.print(temp);
client.println();
}
else
{
Serial.println("Cannot connect to Server");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
And this is my file test.php :
<?php
echo 'Temperature = ' . htmlspecialchars($_POST["data"]) . '!';
?>
The result of client.read() on the Arduino serial terminal is 5, that's proved that the POST request and PHP part are working.
However, if I go on my browser at the url : 192.168.0.55/test.php, only "Temperature = " is displayed. The value (5) is missing.
So if somebody know how can I display the value directly on my browser, it helps me a lot.
Regards
Guillaume
Well, you're not doing it right, but it's the whole http request concept you're not getting right so it would be quite long to explain in detail. To be short :
your arduino is doing a post http request to your server. As you're not specifying a page, it's targeting the default page, most probably index.php. What you should do there is capture the POST data and store it somewhere, most probably a DB but it could aswell be a file.
when you're displaying the test.php page, there was no posted data during that request (which is not the one initiated by your arduino controller), so there's nothing to show. What you should do there is querying your database to display the data stored in previous step
EDIT :
Here's a few step about what you should do :
Create database table to store your data. It would be good to
store the temperature, but also the date when it was stored, and
eventually some "remark" field for the future
Create a PhP script
that handles POSTED data and store it in the database
Make a request to that script from a very basic HTML form : 1 texbox with
the name of your post variable on your arduino , 1 button for
submit. This form will allow you to test your PhP script
once you're ok with it, make the same request from your arduino
from there, if everything goes well, you'll be able to store data sent
from your arduino into the database. Using something like PhPMyadmin
you should be able to see the data
Write a PhP script to read and
display data stored in your database (also quite a basic operation,
you'll find lots of tutorials to do it)
Related
I am not new here but this is my first question.
I have searched a lot and quite frankly can't understand how this is supposed to work.
I get data periodically (temperature) to my ESP32 and while having it set as a WiFi client, connect to my router and somehow store this data on my Laptop(or somewhere else, like a local/web site, don't know if that's possible/better).
How is the connection supposed to work? I have installed XAMPP and run the Apache and MySQL servers and I tried to connect to my Laptop with some sketches from Arduino using the ESP32 libraries
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
const char* host = "192.168.1.109"; //The local IP of my Laptop
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
but it doesn't connect.
Can someone please explain to me how this connection is supposed to take form or is this question too vague? I really just wanna know the "how-things-should-work-together" in this situation.
Thank you in advance.
OK, so after a lot of research and trying, I managed to work it out. I can now send an HTTP request (like GET or POST) from my ESP32 to a local server that is running on my laptop using XAMP and get a response. I can also connect to my local IP from my mobile phone (which is also in the same WiFi network).
Just for anyone else who wants to connect to a location in a server hosted on a PC in a local network, the steps are:
Create a local server on your PC, laptop whatever using an application like XAMPP (I have Windows 10 so WAMP would also work), download, install, open and start Apache.
Make sure that the Firewall lets your requests pass through (for me it was open by default, but I had seen elsewhere Firewall being an issue)
Go to your network settings, select the network that your devices(ESP32, phone, etc.)are connected and change its profile to Private, meaning that you trust this network, making your PC discoverable and able to accept requests. (That is really simple but took me hours to find)
Now, in order to connect from your phone to your PC, open a browser and enter the local IP (that is the IP that is given to your PC from the router as a local network name) of your PC to a browser and that's it, you are connected.
If you installed and ran XAMP, when connecting to your local IP(from same PC or other local device), it will forward you to 192.168.x.x/dashboard. If you want to create new workspaces and files, browse the XAMP folder in the installed location and inside the '/htdocs' subfolder do your testing.
For the ESP32 communication in Arduino(basic steps, not full code):
#include <WiFi.h>
#include <HTTPClient.h>
String host = "http://192.168.x.x/testfolder/";
String file_to_access = "test_post.php";
String URL = host + file_to_access;
void setup(){
WiFi.begin(ssid, password); //Connect to WiFi
HTTPClient http;
bool http_begin = http.begin(URL);
String message_name = "message_sent";
String message_value = "This is the value of a message sent by the ESP32 to local server
via HTTP POST request";
String payload_request = message_name + "=" + message_value; //Combine the name and value
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpResponseCode = http.sendRequest("POST", payload_request);
String payload_response = http.getString();
}
In the test_post.php (located in "C:\xampp\htdocs\testfolder\") file I used a simple script to echo a message received using a POST request, so it's only 'readable' from POST requests. Connecting to it from your browser will give you the "Sorry, accepting..." message.
<?php
$message_received = "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$message_received = $_POST["message_sent"];
echo "Welcome ESP32, the message you sent me is: " . $message_received;
}
else {
echo "Sorry, accepting only POST requests...";
}
?>
Finally, using Serial prints, the output is:
Response Code: 200
Payload: Welcome ESP32, the message you sent me is: This is the value of a message sent by the ESP32 to local server via HTTP POST request
There it is, hope that this helps someone.
I am beginner in C# and I am trying to create a tool to read snmp OID for some of my devices.
In general the system is working fine at the exception of when I cannot reach the IP address or when the IP Address is not using the same OID.
What I would like to achieve is : In case the device is not reachable : skip to the next one . In case the device has not the right OID : skip to the next one.
Currently when it happens I have an error like this one :
Error
SnmpSharpNet.SnmpNetworkException: 'Network error: connection reset by peer.'
Caused by
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
Sample of my code
//Start
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add(".1.3.6.1.4.1.1552.21.3.1.1.5.1.0");
pdu.VbList.Add(".1.3.6.1.4.1.1552.21.3.1.1.5.2.0");
pdu.VbList.Add(".1.3.6.1.4.1.1552.21.3.1.1.5.7.0");
pdu.VbList.Add(".1.3.6.1.4.1.1552.21.3.1.1.5.8.0");
// Make SNMP request
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
MessageBox.Show("Error");
}
Thank you for your help
I have faced the same issue and the solution was to install SNMP Service on your windows and it should start working fine
this link should help you https://support.microsoft.com/en-us/help/324263/how-to-configure-the-simple-network-management-protocol-snmp-service-i
I have a c# application that downloads multiple tiny files from websites (torrents). Some sites restrict the number of downloads per IP per day.
I do a HttpWebRequest and if the stream is a valid torrent, I save it to disk.
Is there a way for my c# application to spoof my IP when performing the HttpWebRequest, so that the download will not fail ?
I spaced out the download time to one per 10 minutes, but no luck. I still get blocked eventually.
I have heard that "TOR" can use diffrent IPs, but I don't want the people using my desktop app to have to install TOR browser separately.
HttpWebResponse resp = null;
try
{
var req = (HttpWebRequest)WebRequest.Create("http://www.exampe.com/test.torrent);
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
req.Timeout = 30000;
req.KeepAlive = true;
resp = (HttpWebResponse)(req.GetResponse());
}
Any solutions ?
To do so, you need to manipulate tcp/ip packets. This means that you need to capture the outgoing packet created by HttpWebRequest and change its source IP to the spoofed one.
I found this forum post that seemingly has to do with what you want to do, check it out : http://pcapdotnet.codeplex.com/discussions/349978
As far as I know you can do it through PCap.net or SharpPcap libraries.
I am using NodeMCU (with ESP8266-E) with an upgraded firmware. All basic commands work perfectly but there is one problem.
I wanted to create an independent access point, which could have a behaviour like a UDP server. That means without direct connection to any other access points. A simple UDP server like soft AP.
I followed these steps:
I have uploaded a new firmware to NodeMCU.
I have downloaded ESPlorer for better work with NodeMCU.
I have uploaded the source code below.
I have connected to the NodeMCU access point on my desktop.
I have sent some strings to the NodeMCU using a Java UDP client program.
I have looked at the messages on ESPlorer.
NodeMCU has not received any such strings.
--
print("ESP8266 Server")
wifi.setmode(wifi.STATIONAP);
wifi.ap.config({ssid="test",pwd="12345678"});
print("Server IP Address:",wifi.ap.getip())
-- 30s timeout for an inactive client
srv = net.createServer(net.UDP, 30)
-- server listens on 5000, if data received, print data to console
srv:listen(5000, function(sk)
sk:on("receive", function(sck, data)
print("received: " .. data)
end)
sk:on("connection", function(s)
print("connection established")
end)
end)
When I tried to send a message using a Java application, there was no change in ESPlorer. Not even when I tried to send a message using the Hercules program (great program for TCP, UDP communication).
I guess that maybe it will be the wrong IP address. I am using the IP address of the AP and not the IP address of the station.
In other words I am using this address: wifi.ap.getip() and not this address wifi.sta.getip() for connections to the UDP server. But sta.getip() returns a nil object. Really I don't know.
I will be glad for any advice.
Thank you very much.
Ok, let's restart this since you updated the question. I should have switched on my brain before I gave you the first hints, sorry about this.
UDP is connectionless and, therefore, there's of course no s:on("connection"). As a consequence you can't register your callbacks on a socket but on the server itself. It is in the documentation but it's easy to miss.
This should get you going:
wifi.setmode(wifi.STATIONAP)
wifi.ap.config({ ssid = "test", pwd = "12345678" })
print("Server IP Address:", wifi.ap.getip())
srv = net.createServer(net.UDP)
srv:listen(5000)
srv:on("receive", function(s, data)
print("received: " .. data)
s:send("echo: " .. data)
end)
I ran this against a firmware from the dev branch and tested from the command line like so
$ echo "foo" | nc -w1 -u 192.168.4.1 5000
echo: foo
ESPlorer then also correctly printed "received: foo".
This line is invalid Lua code. connected is in the wrong place here. you can't just put a single word after a function call.
print(wifi.ap.getip()) connected
I guess you intended to do something like
print(wifi.ap.getip() .. " connected")
Although I think you should add som error handling here in case wifi.ap.getip() does not return an IP.
Here you do not finish the function definition. Neither did you complete the srv:on call
srv:on("receive", function(srv, pl)
print("Strings received")
srv:listen(port)
I assume you just did not copy/paste the complete code.
I made a project with the Arduino and the ethernet-shield. The Arduino is hosting a website which I can open via the browser on my laptop. The Arduino is connected to the router via ethernet. All of this works just fine.
Now I have to present this project at school. To prevent unpleasant surprises I wanted to connect the Arduino directly with the laptop via ethernet. My problem is that I am really not well informed about this topic. Please, if possible, tell me what I should do.
If you take the router out of the loop you will need to:
Assign a manual IP address to the laptop's Ethernet connection say 192.168.0.1
Subnet mask 255.255.255.0
Assign a manual IP address to the Arduino's Ethernet say 192.168.0.2
Subnet mask 255.255.255.0
default Gateway empty
Use a cross-over cable to link the two (a standard patch lead will NOT work)
You should then be able to get your Arduino site up on http://192.168.0.2 from the laptop.
To look smart :) edit your hosts table on the laptop (C:\windows\system32\drivers\etc\hosts for windows) (/etc/hosts for linux)
and make an entry:
192.168.0.2 my.arduino
Then you can access it with http://my.arduino
Good luck
You must assign a manual IP address to the laptop and Arduino.
Then include Ethernet.h in your sketch and try to make Ethernet connection. Finally you can see your webpage in your laptop by enter Arduino's IP in your browser. Example:
#include <SPI.h>
#include <Ethernet.h>
/******************** ETHERNET SETTINGS ********************/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 }; //physical mac address
byte ip[] = { 192, 168, 1, 172 }; // ip in lan
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte gateway[] = { 192, 168, 1, 254 }; // default gateway
EthernetServer server(80); //server port
void setup()
{
Ethernet.begin(mac,ip,gateway,subnet); // initialize Ethernet device
server.begin(); // start to listen for clients
pinMode(8, INPUT); // input pin for switch
}
void loop()
{
EthernetClient client = server.available(); // look for the client
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
/*
This portion is the webpage which will be
sent to client web browser one can use html , javascript
and another web markup language to make particular layout
*/
client.println("<!DOCTYPE html>"); //web page is made using html
client.println("<html>");
client.println("<head>");
client.println("<title>Ethernet Tutorial</title>");
client.println("<meta http-equiv=\"refresh\" content=\"1\">");
/*
The above line is used to refresh the page in every 1 second
This will be sent to the browser as the following HTML code:
<meta http-equiv="refresh" content="1">
content = 1 sec i.e assign time for refresh
*/
client.println("</head>");
client.println("<body>");
client.println("<h1>A Webserver Tutorial </h1>");
client.println("<h2>Observing State Of Switch</h2>");
client.print("<h2>Switch is: </2>");
if (digitalRead(8))
{
client.println("<h3>ON</h3>");
}
else
{
client.println("<h3>OFF</h3>");
}
client.println("</body>");
client.println("</html>");
delay(1); // giving time to receive the data
/*
The following line is important because it will stop the client
and look for the new connection in the next iteration i.e
EthernetClient client = server.available();
*/
client.stop();
}