0

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

3
  • Are we are supposed to play a guessing game on what the compiler error says? And also waste our time, repeating whatever was already said on Arduino Forum? If you want answers, you need to make it easy for us. Commented Nov 22, 2017 at 14:18
  • Thank you for owning up to the cross-posting. It is generally not received well, because it can lead to a situation where someone does not know about other copies of the same question, and they expend a great effort on an answer, which essentially duplicates a pre-existing reply on another copy. At the very least, please add links to all of your cross-posts to all of the others, so that people can check on the other posts what answers have been made already. Would you make that edit? Commented Nov 22, 2017 at 15:00
  • @halfer Well, I'm not getting anywhere by not being honest. I know that some of you may be using both forums, but if X searches at Y and doesn't find the answer, well - nomatter what, I'll post the answer on the forum that didn't come up with it (with credit ofc) - does it make sense? Oh well - I edited the question with compiler error and the link to Arduino.cc forum. Although, I think that I'll start from scratch using Juraj's answer. Commented Nov 23, 2017 at 7:20

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I'll look into this. Is it straight forward to flash to new firmware, or is there a huge brick-risk involved?
very simple. source code build and upload from IDE. there is an option for the AT firmware but I recommend you WiFi Link
Actually, when I had a fresh cup of coffee this morning, i realised something. At first, I couldn't get the RestClient demo-sketch to working, but then i realised that arduino.org is down. Then I tried my own server, but I couldn't find anywhere to change the port number from 80 to something else. Then, i tried to change my port back to 80 in XAMPP, but I couldn't - after browsing Stack, i found an answer - SQL Reporting Services are hogging port 80 for some reason?! Anyway, now the RestClient is working, and I don't think that a firmware change is required. I'll update this thread as i build
the new sketch. I'll accept your answer anyway though, as it seems that it would be the way to go. But being a project for my work (with 15 Unos) I'd rather not try and fiddle with the firmware - I'll do that on my test platform instead.
so you are lucky. what you needed could be done with the limited abilities of the factory firmware. congrats. at least my Uno WiFi Wiki was at help. (stackoverflow doesn't alow me to comment the question or other answers. I did not collect enough points in this 'game' :-), but now I have 25 more )
0

Okay - so I made it work. I had to start from scratch, and with the help from Juraj (I'll accept your answer as well) 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.