Acces to an URL with Arduino Ehernet shield - arduino

I am trying to access a PHP file in my server with an Arduino and the Ethernet shield. This file captures the URL parameters "Sensor" and "Value" and stores the read data into a database.
This is my code:
#include <SPI.h>
#include <Ethernet.h>
EthernetClient client;
byte MACaddress[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
byte IPaddress[] = {10,0,0,178};
byte DNSserverIPaddress[] = {4,4,4,4};
byte gatewayIPaddress[] = { 10, 0, 0, 100 };
byte subnetMask[] = { 255, 255, 255, 0 };
char serverName[] = "log.server.com";
void setup() {
Serial.begin(9600);
Ethernet.begin(MACaddress, IPaddress, DNSserverIPaddress, gatewayIPaddress, subnetMask);
}
void loop()
{
delay (5000);
Serial.println("connecting to server...");
client.connect(serverName, 80);
Serial.println("making HTTP request...");
client.println("GET /logger.php?sensor=temp&value=19 HTTP/1.1");
client.println("HOST: log.server.com");
client.println();
}
After uploading this code to my Arduino Mega + Ethernet shield, nothing changes in my database...
What is wrong?

Well, first you should add a condition check to know if the Arduino think it worked or not:
if (client.connect(...)) {
/* Stuff you do on success */
}
else {
Serial.println("failure! :-(")
}
If it does print failure! you got a network configuration problem on the Arduino side. If it does work, the problem is after the Arduino.
Then try opening a server with nc -kl 42000 and change the port you connect to on 42000 in the Arduino sketch, to be sure whether the network connection works.
If it does work, then you've got a problem on your host side (the webserver), if it does not, you may have a problem on the network between the Arduino and the host.
You should then try to connect to a server's IP address instead of a fully qualified domain name (FQDN). If that works, it may be the DNS server that is unreachable, and you should try to use 8.8.8.8 instead (or your local network's DNS server).
Also check that the IP address you're using is indeed free of use (and not assigned by a DHCP or used by another computer), as well as the MAC address is really unused... (addresses like {0xDE,0xAD,0xBE,0xEF,0xFE,0xED} tend to be used a lot in hacks...).
My opinion, is that your bug is the DNS server that is unreachable, as there's no DNS resolver opened on 4.4.4.4.

Related

Failed to connect ESP8266: Invalid head to Packet (0xf0) D:

I am working with an arduino, rather learning to use it, I bought the esp8266 module to transmit data over wifi, but every time I upload a script it throws me the following error:
Failed to connect ESP8266: Invalid head to Packet (0xf0)
The selected port does not exist or the board is not connected
I already installed the drivers and libraries, select the COM4 port and checked in the device manager that it is correct.
As I was asked, here is the code I am trying to use.
#include <SoftwareSerial.h>
void setup()
{ Serial.begin(115200);
BT1.begin(115200);
}
void loop()
{ String B= "." ;
if (BT1.available())
{ char c = BT1.read() ;
Serial.print(c);
}
if (Serial.available())
{ char c = Serial.read();
BT1.print(c);
}
}
I connected the arduino via usb, the esp8266 using the breadboard, it is an ESP8266 ESP-01.
I hope you can help me.
(It's my first time on this site, sorry for the lack of info the first time)

Using Modbus TCP on Arduino

I have a scd30 sensor wired to an arduino uno. The scd30 works on theh I2c protocol. I am able to read data live on the serial monitor on the arduino IDE. I have an ethernet shield on my arduino. I would like the arduino to communicate with a field agent which will upload the data to the internet.
I have tried numerous modbus tcp libraries and dont seem to be getting anywhere. I can connect my arduino to the field agent but whenever it sends data I get a 0x02 exception code - Illegal data address. This is the Library im using https://github.com/andresarmento/modbus-arduino/tree/master/libraries/ModbusIP/examples
I believe the right way to go about it is through holding registers but im not sure how to do this when using i2c. The connection is fine, the problem is the format. any help is appreciated thanks.
/*
Reading CO2, humidity and temperature from the SCD30
This example prints the current CO2 level, relative humidity, and temperature in C.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>
#include <Wire.h>
#include <Streaming.h>
#include "SparkFun_SCD30_Arduino_Library.h"
SCD30 airSensor;
//Modbus Registers Offsets (0-9999)
const int SENSOR_ISTS = 100;
//ModbusIP object
ModbusIP mb;
long ts;
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("SCD30 Example");
// The media access control (ethernet hardware) address for the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// The IP address for the shield
byte ip[] = { 000 , 00,0, 00 };
byte gateway[] = { 0, 0, 0, 0 };
byte subnet[] = { 255, 255, 255, 0 };
//Config Modbus IP
mb.config(mac, ip,gateway,subnet);
// Add SWITCH_ISTS register - Use addIsts() for digital inputs
mb.addHreg(SENSOR_ISTS);
airSensor.begin(); //This will cause readings to occur every two seconds
}
void loop()
{
mb.task();
mb.Hreg(SENSOR_ISTS, digitalRead(airSensor.getTemperature()));
}
I have read your problem.
In my view you first have to create a local server, somewhere like thingspace (https://thingspace.verizon.com/) or other online local servers, from there you can easily handle the data coming from the sensors.
You are using a code from library so it must be correct in any way. So, from my view you should check the data transactions.
wish my ans helps you
thanks!
The ModbusIP library expects from you that you supply the value of the register. The AirSensor library gives you that value.
Set the register value to Hreg:
mb.Hreg(SENSOR_ISTS, airSensor.getTemperature());
I tested your sketch without the sensor library and it is working. Client was my java test client I use to test access to Modbus TCP registers of my photovoltaic system.
Make sure that the client calls "0x03 - Read Holding Registers" and test address 100 and 101 because some modbus clients offsets are 1-based.

Booting programs from an Intel Edison board

I just bought an Intel Edison Breakout Board Kit with the Grove Starter Kit Plus I want to write a program which gives the board an IP address, checks for a temperature in a room and if that temperature is exceeded it turns a LED on. I can check what's going on with a RGB display. When I connect the USB OTG port to the my laptop the wifi connection is perfectly detected, the IP address is perfectly received and the temperature control works fine. The program works as I ask for. Now the questions. I would like to execute the same program unplugging the USB connection, so I would like to make this board independent from PC, like if it is a standing alone device
1) if I upload this working program in the board, unplugging the USB and of course giving just the power supply through the jack on the breakout board it does not work anymore and stops on the message "preparing network connection...". But Intel Edison has an integrated wifi module and I really can't understand why it is not able to establish an internet connection anymore without the help of the USB connection with my laptop. What's wrong with it? how to boot and execute this program just with the power supply? This is the setup function() of my program, we don't reach the loop() one so I will not post it
void setup()
{
// ------ LCD IN ACTION ------ //
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// setting the color for the connection
lcd.setRGB(colorR, colorG, colorB);
lcd.print("preparing network connection...");
// scroll 40 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 40; positionCounter++)
{
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(350);
}
// ------ SERIAL CONNECTION ------ //
// opening the serial connection
// Serial.begin(9600); // initialize serial communication
// ------ SETTING I/O PINS ------ //
pinMode(potentiometer, INPUT); // angle sensor's pin for input.
pinMode(12, OUTPUT); // set the blue LED pin mode
pinMode(13, OUTPUT); // set the red LED pin mode
pinMode(pinLed, OUTPUT); // set the green LED pin mode
// ------ WIFI CONNECTION CONTROLS ------ //
// check for the presence of the wifi shield:
if (WiFi.status() == WL_NO_SHIELD)
{
// Serial.println("WiFi shield not present");
while(true); // don't continue
}
// check firmware version
String fv = WiFi.firmwareVersion();
// if( fv != "1.1.0" )
// Serial.println("Please upgrade the firmware");
// ------ WIFI CONNECTION ------ //
// 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 3 seconds for connection:
delay(3000);
}
server.begin(); // start the web server on port 70
printWifiStatus();
where
void printWifiStatus() {
// print the SSID of the network you're attached to:
// Serial.print("SSID: ");
// Serial.println(WiFi.SSID());
colorR = 255;
colorG = 180;
colorB = 0;
lcd.clear();
lcd.setRGB(colorR, colorG, colorB);
lcd.print(WiFi.SSID());
delay(3000);
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
// Serial.print("IP Address: ");
// Serial.println(ip);
colorR = 0;
colorG = 255;
colorB = 0;
lcd.setCursor(0, 1);
lcd.setRGB(colorR, colorG, colorB);
lcd.print(ip);
delay(3000);
/*
delay(5000);
colorR = 180;
colorG = 255;
colorB = 255;
lcd.setRGB(colorR, colorG, colorB);
lcd.noDisplay();
*/
// 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);
}
2) always about the execution of pre-loaded programs: if I load the default blink program of ArduinoIDE, unplug the USB and just give the power supply it works perfectly [this one is correctly self-booted, yes] but if I upload the same program with a Serial.begin() and a Serial.print("Arduino blinking") it doesn't work anymore without the USB connection to my laptop [this is why I commented the Serial stuff before]. Is it because the board is so smart to detect that in the first case the serial connection is not really happening?
3) last question about program storage: when I compile a program with the arduino IDE it gives me the percentage of memory used related to the available one for programs
Sketch is using 103.266 byte (1%) of program memory. The limit is 10.000.000 byte
but Intel Edison has a 4GB eMMC so why there are only 10MB available to programs? in which memory are the programs uploaded?
Thanks in advance to those who will try to help
Do you know how can I do to check where is it stored?
You can check your files on serial communication terminal. If you have possible directories that your file could be in, you can use linux commands like:
cd directory and ls
or even you can 'search' the file if you know the name. You may want to check this site.
or you can use SCP, FTP client programs like WinSCP to see the files on your Edison.
if I upload this working program in the board
Are you sure that your program is flashed into persistent memory ? I do not know this board. In most cases this kind of problems comes when your program is only transferred to RAM memory, but not stored in Flash.
Sketch is using 103.266 byte (1%) of program memory. The limit is
10.000.000 byte
10MB RAM, or Flash memory ?
As far as I understand you use Arduino + Edison ?
Which board 10MB limit corresponds to ? Sketch is using 103.266 byte: is it RAM or Flash ?
Try typing these commands in through putty
ifconfig usb0 down
ifconfig wlan0 down
ifconfig usb0 up
ifconfig wlan0 up
that might do it

What is the benefit of Arduino Ethernet's library IPAddress()?

The Arduino default Ethernet library class contains an IPAddress variable type. What is this IPAddress for? Why should I use it and why is it not used for the gateway and subnet IPs in the official example?
It just, like you said, a type of variable (such as int (integer)) that can store a IP address. Using integers, you cannot add the .s needed in the IP address. Also, the library only accepts integers, because with strings, things "can get messy." For example, if you have 1 in a string, you cannot add that with another number. However, if you have the integer variable type with the value of 1, it would add easily.
How can I use this?:
On Arduino's EthernetIpAdress page, there is this code:
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// the router's gateway address:
byte gateway[] = { 10, 0, 0, 1 };
// the subnet:
byte subnet[] = { 255, 255, 0, 0 };
EthernetServer server = EthernetServer(23);
//the IP address is dependent on your network
IPAddress ip(192,168,1,1);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
//print out the IP address
Serial.println(myIPaddress);
}
On the line IPAddress ip(192,168,1,1);, it creates a variable that holds the IP address. In the line Ethernet.begin(mac, ip, gateway, subnet); the variable is looked up and given to the Ethernet library. I don't know what the advantage would be, besides trying to prevent people from using the integer type and making it look cleaner. It could look up the automatically issued IP address and then store it for later so if it goes into an "idle mode," it can ask for the same IP address so it is almost like a dynamic IP that wouldn't interfere with other devices and would reset when the reset button is pushed. I'm sure that there is some use for it, but I cannot think of one. I just wanted to tell you what it is and how to use it. I would think though that it would be easier just to use #define IPadress 192.168.1.1 or a similar thing if you wanted it to easily be changed or to be more user readable.

Arduino WiFly - ad hoc network setup

I'm currently working with an Arduino trying to build an ad hoc network to which a device can connect to and send web requests to. The problem I am currently having is that I can only set up one connection and then when that connection is terminated (with client.stop()), all subsequent connections are not picked up by the server, even a cURL command just sits there spinning. The first connection I start when I reset the server works fine, and I am able to talk to the server; but after that, the Arduino can no longer find new clients (even though it's trying with the library given).
I`m using the SparkFun library for the WiFly shield cloned from GitHub, along with an Arduino Uno.
My current code is based off their default example 'WiFly_AdHoc_Example', but I had to remove a few things to get the network to start up which might be the cause of this problem.
Here is the .ino file that I am running.
#include <SPI.h>
#include <WiFly.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial( 5, 4); //Part from example not used (see below)
WiFlyServer server(80); //Use telnet port instead, if debugging with telnet
void setup()
{
Serial.begin(9600);
//The code below is from the example, but when I run it the WiFly will hang
// on Wifly.begin(). Without it, the WiFly starts up fine.
//mySerial.begin(9600);
//WiFly.setUart(&mySerial); // Tell the WiFly library that we are not
// using the SPIUart
Serial.println("**************Starting WiFly**************");
// Enable Adhoc mod
WiFly.begin(true);
Serial.println("WiFly started, creating network.");
if (!WiFly.createAdHocNetwork("wifly"))
{
Serial.print("Failed to create ad hoc network.");
while (1)
{
// Hang on failure.
}
}
Serial.println("Network created");
Serial.print("IP: ");
Serial.println(WiFly.ip());
Serial.println("Starting Server...");
server.begin();
Serial.print("Server started, waiting for client.");
}
void loop()
{
delay(200);
WiFlyClient client = server.available();
if (client)
{
Serial.println("Client Found.");
// A string to store received commands
String current_command = "";
while (client.connected())
{
if (client.available())
{
//Gets a character from the sent request.
char c = client.read();
if (c=='#' || c=='\n') //End of extraneous output
{
current_command = "";
}
else if(c!= '\n')
{
current_command+=c;
}
if (current_command== "get")
{
// output the value of each analog input pin
for (int i = 0; i < 6; i++)
{
client.print("analog input ");
client.print(i);
client.print(" is ");
client.print(analogRead(i));
client.println("<br />");
}
}
else if(current_command== "hello")
{
client.println("Hello there, I'm still here.");
}
else if (current_command== "quit")
{
client.println("Goodbye...");
client.stop();
current_command == "";
break;
}
else if (current_command == "*OPEN*")
{
current_command == "";
}
}
}
// Give the web browser time to receive the data
delay(200);
// close the connection
client.stop();
}
}
This script is just a mini protocol I set up to test. Once connected with the wifly module you can send text such as "get" "hello" or "quit" and the wifly module should respond back.
Using Telnet I can successfully connect (the first time) and send commands to the Arduino including "quit" to terminate the connection (calls the client.stop() method). But when I try to reconnect though Telnet, it says the connection was successful, but on the Arduino it's still looping thinking the client is still false. What??
I know right, I'm getting mixed messages from Telnet vs Arduino. None of the commands work obviously since the Ardunio is still looping waiting for a client that evaluates to true. I'm going to take a look at WiFlyServer from the library I imported and see if I can dig up the problem, because somehow that server.available() method isn't finding new clients.
I am noticing a lot of TODO's in the library code....
So I found the reason for the problem. It was in the WiFlyServer.cpp file from the SparkFun library. The code that was causing the reconnect issue was in fact the server.availible() method. Right at the top of the method, there is a check:
// TODO: Ensure no active non-server client connection.
if (!WiFly.serverConnectionActive) {
activeClient._port = 0;
}
For some reason when I comment this out, I can connect and reconnect perfectly fine and everything works as it should. I will now dive into the library and see if I can fix this, I'm not exactly sure what this is doing, but it gets called when the server connection is not active and is somehow blocking subsequent connections. The problem with this solution is that the Arduino always thinks it has found a client since client and client.connected() evaluate to true even if one doesn't exist. Even client.available() evaluates to true right when the connection is terminated and the ghost "client" is found, but after that first run through the if-statement the ghost "client" is no longer available(). Even with this flaw it still picks up a new client when it comes along which is why it works.
How might I get to the root of this problem without using this commenting hack?
Are their any risks or future problems I might run into doing it this way?
What is the purpose of the block that I commented out in the first place?
Well, when you're calling client.stop(); how does the Arduino know whether the client has to start again?
Remember setup() executes only once.
Have you tried to include the following code in your loop to tell the Arduino to create the WiFly AdHoc network again? This may or may not work. I don't have one myself and haven't played with the Wifly shield but it's worth a try.
Remember to only ever execute the code once every time you need to connect again since it's sitting inside a loop that's always going to be running.
WiFly.begin(true);
Serial.println("WiFly started, creating network.");
if (!WiFly.createAdHocNetwork("wifly"))
{
Serial.print("Failed to create ad hoc network.");
while (1)
{
// Hang on failure.
}
}

Resources