arduinoarduino-unoarduino-uno-wifi

Porting Arduino Sketch from Ethernet to Wifi


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 Arduino, so I'm stuck here.

Below is my (original Ethernet) code. I can post my somewhat 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");
    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've also posted this on the Arduino Forum. I'm sorry if you see my double-post, and I'll post the solution to my problem here as well.

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 :/


Solution

  • If it is the Arduino.org Arduino UNO WiFi Developer Edition, then use WiFi Link with UNO WiFi Serial1 library