Portin sketch from ethernet to wifi

Hi all.

First, thanks for all the help I’ve gotten through the forum so far, and now to my problem:

I have a sketch working with an Arduino Uno and an Ethernet Shield - and it’s working fine.
Now I’ve gotten my hands on some Arduino Uno WiFi, and I want to port the sketch from ethernet to wifi - but I’ve run into a wall now >_<
Most of the guide/FAQ/help I can find, is for a WiFi Shield, and not a WiFi 'duino, so I’m kinda stuck here.

Below are my (original ethernet) code, I can post my kinda-modified Wifi code, but I can’t even compile it without errors.

//  Hartmann fugtighedsmåler v 0.1
//  Lavet af Jan Andreasen
//  Skriver til DB på FDKTO517
    
    #include <Ethernet.h>
    #include <SPI.h>
    #include <DHT.h>
    #define DHTPIN 2 // Siger sig selv
    #define DHTTYPE DHT11 // Typen af sensor. 
 
    float h = 0;
    float t = 0;
    byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; // Macadresse på kortet
    IPAddress server(10,16,9,229); // Server adressen på SQL'en
 
    EthernetClient client;
    DHT dht(DHTPIN, DHTTYPE);
 
    void setup() {
      Serial.begin(9600);
      Serial.println("Starting...");
      Ethernet.begin(mac);
      dht.begin();
    }
 
    void loop() {
      readTempHum();
      delay(300000); // Loop timer i millis - 5 minutter
    }
 
    void get_request(float t, float h)
      {
        Serial.println("Connecting to Client...");      
        if (client.connect(server, 10080)) {
          Serial.println("--> connection ok\n");
          client.print("GET /test.php?"); // Placering af PHP script til upload til DB
          client.print("t="); // Temp
          client.print(t); 
          client.print("&h="); // Fugtighed
          client.print(h);
          client.println(" HTTP/1.1"); // Må åbenbart ikke undlades
          client.print( "Host: " );
          client.println(server);
          client.println("Connection: close"); 
          client.println(); 
          client.println(); 
          client.stop();    
          Serial.println("--> finished transmission\n");  
        }
        else {
          Serial.println("--> connection failed\n");
        }
      }
 
    void readTempHum() {
      h = dht.readHumidity();
      t = dht.readTemperature();
        {
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print("%\t");
        Serial.print("Temperature:");
        Serial.print(t);
        Serial.println("*C");
        get_request(t,h);
        }
      }

I really hope that there are some out here who can help me, or give me a little advice.
(I’ve also posted this on StackExchange. I’m sorry if you see my double-post, and I’ll post the solution to my problem here as well)

Thanks for noting that you also posted on SE but you should also add a link to the other post. That will make it easy for people to check what has happened on your SE question and take any additional information into consideration for their replies.

For example, someone might ask you to post the errors but they could also check if you've already done so over on SE.

I'd hope the API would be quite similar, as is the case with the Ethernet and WiFi (for the Arduino WiFi Shield) libraries, which would make porting quite simple. However, I'm not familiar with the Uno WiFi's UnoWiFiDevEd (or whatever it's called) library and that board and library haven't seemed to get much love yet from arduino.cc.

Below are my (original ethernet) code

Why on earth does readTempHum() do more than read the temperature and humidity?

I can post my kinda-modified Wifi code, but I can't even compile it without errors.

We can't help you with code we can't see.

First of all, link to SE: Link

PaulS: Why on earth does readTempHum() do more than read the temperature and humidity?

What do you mean with "do more"?

PaulS: We can't help you with code we can't see.

I'd love to post that code, but I just started from scratch - that what I've already done was kinda hopeless, so I just reverted to the "original" ethernet-sketch.

//  Hartmann fugtighedsmåler v 0.2.1
//  Lavet af Jan Andreasen
//  Skriver til DB på FDKTO517
//  WiFi udgave, testversion


    #include <Wire.h>
    #include <UnoWiFiDevEd.h>
    #include <DHT.h>
    #define DHTPIN 2 // Siger sig selv
    #define DHTTYPE DHT22 // Typen af sensor. DHT11 og DHT22 kan bruges med dette DHT lib
 
    float h = 0;
    float t = 0;
//    IPAddress server(10,16,9,229); // Server adressen på SQL'en

//  Der skal højst sansynligt lige laves noget fikundik når den skal skiftes fra ethernet til WiFi
//  men det må man lige tage når det kommer dertil.

 
//    WifiClient client;
    DHT dht(DHTPIN, DHTTYPE);
 
    void setup() {
      const char* server ="10.16.9.229";
//      const char* method = "GET";
//      const char* resource = "/test.php";
      Serial.begin(9600);
      Serial.println("Starting...");
      Wifi.begin();
      Serial.print("IP = ");
//      Serial.println(Wifi.localIP());
      dht.begin();
    }
 
    void loop() {
      readTempHum();
      delay(300000); // Loop timer i millis - 5 minutter
    }
 
    void get_request(float t, float h)
      {
        Serial.println("Connecting to Client...");      
        if (client.connect(server, 10080)) {
          Serial.println("--> connection ok\n");
          client.print("GET /test.php?"); // Placering af PHP script til upload til DB
          // OBS! Det PHP script skal være forskellig fra lokation til lokation!!!!
          client.print("t="); // Temp
          client.print(t); 
          client.print("&h="); // Fugtighed
          client.print(h);
          client.println(" HTTP/1.1"); // Må åbenbart ikke undlades
          client.print( "Host: " );
          client.println(server);
          client.println("Connection: close"); 
          client.println(); 
          client.println(); 
          client.stop();    
          Serial.println("--> finished transmission\n");  
        }
        else {
          Serial.println("--> connection failed\n");
        }
      }
 
    void readTempHum() {
      h = dht.readHumidity();
      t = dht.readTemperature();
        {
        Serial.print("Humidity: ");
        Serial.print(h);
        Serial.print("%\t");
        Serial.print("Temperature:");
        Serial.print(t);
        Serial.println("*C");
        get_request(t,h);
        }
      }

The uncommented network related lines, are where the IDE fails to compile. I need to fix the client var somehow. 9 out of 10 links points to arduino.org, so thats a dead end as well.

What do you mean with "do more"?

"Here, read this book" does not mean "read this book, do a book report, and write a sequel".

The function to read the temperature and humidity should do that and nothing more.

The function to read the temperature and humidity should NOT try to make a GET request to send the data to a server.

The uncommented network related lines, are where the IDE fails to compile.

That is NOT what the compiler told you. Of that I am 100% certain. Tell US what the compiler told you.

Post a link to the UnoWiFiDevEd library so that we can see what classes it actually defines.

PaulS: That is NOT what the compiler told you. Of that I am 100% certain. Tell US what the compiler told you.

Post a link to the UnoWiFiDevEd library so that we can see what classes it actually defines.

Easy now - try and keep the friendly tone here.

Error1: 'IPAddress' does not name a type

Error2: 'WifiClient' does not name a type

Error3: 'class ArduinoWifiClass' has no member named 'localIP'

Error4: 'client' was not declared in this scope

The 4th error is kinda obvious, looking at the 2nd error.

Link to lib: https://github.com/arduino-libraries/UnoWiFi-Developer-Edition-Lib/blob/master/src/UnoWiFiDevEd.h

Error1: 'IPAddress' does not name a type

No, that is NOT all the compiler told you. Copy and paste the COMPLETE, EXACT error messages.

The examples that come with that library seem to suggest that it is not meant to develop a client. Rather, it appears that it is only meant to develop servers.

Arduino: 1.8.4 (Windows 8.1), Board: "Arduino Uno WiFi"

HumidSQL3_Wifi_master:15: error: 'IPAddress' does not name a type

     IPAddress server(10,16,9,229); // Server adressen pÃ¥ SQL'en

     ^

HumidSQL3_Wifi_master:21: error: 'WifiClient' does not name a type

     WifiClient client;

     ^

H:\Documents and settings\Arduino\HumidSQL3_Wifi_master\HumidSQL3_Wifi_master.ino: In function 'void setup()':

HumidSQL3_Wifi_master:32: error: 'class ArduinoWifiClass' has no member named 'localIP'

       Serial.println(Wifi.localIP());

                           ^

H:\Documents and settings\Arduino\HumidSQL3_Wifi_master\HumidSQL3_Wifi_master.ino: In function 'void get_request(float, float)':

HumidSQL3_Wifi_master:44: error: 'client' was not declared in this scope

         if (client.connect(server, 10080)) {

             ^

HumidSQL3_Wifi_master:44: error: 'server' was not declared in this scope

         if (client.connect(server, 10080)) {

                            ^

exit status 1
'IPAddress' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Okay, i found some demo sketches - RestClient and RestServer
At first, I thought that it didn’t work either, getting “error” from the console, but then i realised, that Arduino.org is taken down >_<

So, the error i got was from the following line
const char* server = “download.arduino.org

I’m sorry for all the fuzz, and I think that I can port my original sketch now that I know how the Ciao works.

I’ll update this thread as I get the finished sketch working.

I’m glad you found the source of the error. Please report the arduino.org issue to the Arduino developers here:
https://github.com/arduino-libraries/UnoWiFi-Developer-Edition-Lib/issues/new

The arduino.cc equivalent URL is https://www.arduino.cc/latest.txt but I don’t know if that will work since it’s https and the http URL redirects to https. I don’t own one of these boards so I can’t test.

New sketch:

    #include <Wire.h>
    #include <UnoWiFiDevEd.h>
    #include <DHT.h>
    #define DHTPIN 4
    #define DHTTYPE DHT11

    float h = 0;
    float t = 0;

    DHT dht(DHTPIN, DHTTYPE);

    void setup() {

      const char* connector = "rest";
      const char* server = "10.16.9.229";
      const char* method = "GET";
      const char* resource = "/test.php?t=";

      Serial.begin(9600);
      Ciao.begin();
      dht.begin();

      pinMode(2, INPUT);

      delay(10000);
    }

    void loop() {

      readTempHum();
    //  doRequest(connector, server, resource, method);
      delay(300000);
    }

    void doRequest(const char* conn, const char* server, const char* command, const char* method, float t, float h){
      CiaoData data = Ciao.write(conn, server, command, method);
    }

    void readTempHum() {
      h = dht.readHumidity();
      t = dht.readTemperature();
      const char* connector = "rest";
      const char* server = "10.16.9.229";
      const char* method = "GET";
      const char* resource = "/test.php?t=";
  
        {
        doRequest(connector, server, resource, method,t,h);
        }
      }

Now, I’ve ran into a new problem. The value from the sensor (t and h) are supposed to be output in the HTTP/GET command like this:

   test.php?t=1&h=2

But I can’t seem to make that work. If I try to define the resource as this

   const char* resource = "/test.php?t="+t+"&h="+h;

I get an error (obviously), but if I try to declare it as a string, I the same error again.

Error:

HumidSQL3_Wifi_master:24: error: invalid operands of types 'const char [13]' and 'float' to binary 'operator+'

const char* resource = "/test.php?t="+t+"&h="+h;

Now, I hope that some of you could help me out a bit here :confused:

Found out that I had to rethink the URL - Been working to much in Pascal recently.
Instead of “thing” + A + “thing” + B it had to be defined as you see below.

Anyhow, I made it work. I had to start from scratch, and with the help from Juraj it works now.

Below are the final sketch (“final”, as the DHT11 sensor only were for testing purpose, as a proof-of-concept)

    //  Hartmann fugtighedsmåler v 0.2.2
    //  Lavet af Jan Andreasen
    //  Skriver til DB på FDKTO517
    //  WiFi udgave, testversion


    #include <Wire.h>
    #include <UnoWiFiDevEd.h>
    #include <DHT.h>
    #define DHTPIN 4
    #define DHTTYPE DHT11
    #define CONNECTOR "rest"
    #define SERVER_ADDR "10.16.9.229"


    float h = 0;
    float t = 0;

    DHT dht(DHTPIN, DHTTYPE);

    void setup() {

      Serial.begin(9600);
      Ciao.begin();
      dht.begin();
    
      pinMode(2, INPUT); // I'm not sure if this is required, just saw it now

      delay(10000); // A 10 second delay from start to initialization
    }

    void loop() {

      readTempHum();
      delay(300000); // A 5 minute delay between measurements
    }

    void readTempHum() {
      h = dht.readHumidity(); // Reads the humidity from sensor
      t = dht.readTemperature(); // Reads the temperature from sensor
      String uri = "/test.php?t="; // URL to the PHP-file
      uri += t;  // Insert the T-value
      uri +="&h=";
      uri += h; // Insert the H-value

      CiaoData data = Ciao.write(CONNECTOR, SERVER_ADDR, uri); // Make a basic HTTP request to the specified server using REST and the URL specified above
      }

Maybe not the prettiest code you’ve seen, however, it works now. Thanks for all your inputs :slight_smile:

to that String concatenation: the concatenate operator + is overloaded in String class. to String concatenation to work there must by an object of type String. That is why String s = "a" + "b", doesn't work and String s = String("a") + "b" is ok