ENC28j60 Arduino does not show decimals - arduino

I am using ENC28j60 with temp sensor in Arduino uno to see the temperature on the internet, but the problem is that it does not show decimals And shows only the integer.
Image of Issue
enter code here#include <OneWire.h>
#include <DallasTemperature.h>
#define DS18B20 2
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);
float TempDS;
#include "etherShield.h"
#include "ETHER_28J60.h"
static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
static uint8_t ip[4] = {192, 168, 0, 15};
static uint16_t port = 80;
ETHER_28J60 ethernet;
void setup(){
ethernet.setup(mac, ip, port);
Serial.begin(9600);
delay(1000);
sensors.begin();
}
void loop(){ sensors.requestTemperatures(); TempDS = sensors.getTempCByIndex(0);
Serial.print(TempDS);
Serial.println(" C");
if (ethernet.serviceRequest()){
ethernet.print("<H1>Hello World</H1>");
ethernet.print( TempDS );
ethernet.respond();}
}

ETHER_28J60's print either takes a char pointer or an integer. You're stuffing a float into it so you're losing the decimals.
Convert your float to a string

Related

Trying to get a photoresistor to take an analog reading through a wire command to an EV3

Here is the part of the code we are getting the error on
//turns on the i2c commands
#include <Wire.h>
//creates a variable called SLAVE_ADDRESS and permanently sets the value to 0x04 (register #4)
#define SLAVE_ADDRESS 0x04
#define IR1 A0
#define IR2 A1
#define prnt
#define measure
#define measurea
void setup()
{
Serial.begin(9600);
//sets the address of the Arduino
Wire.begin(SLAVE_ADDRESS);
//the .onRequest command will run a function when it gets a request from the EV3.
//In this case, it will run the requestEvent function, which is defined later in the sketch.
Wire.onRequest(requestEvent);
}
int measureMap = 0;
void loop() {
int measure = analogRead(A0);
int measurea = analogRead(A1);
measureMap = map(measure, 0, 1023, 0, 63);
Serial.print(mapMeasure);
Serial.print (" ");
Serail.println(measure);
Serail.println(measure1);
}
Where we have the variable measureawe get an error saying
measurea
exit status 1
expected unqualified-id before '=' token
How to fix the error?
The problem is that you already declared measurea and measure as a define macro.
You can't make a variable with the same name.
try this:
//turns on the i2c commands
#include <Wire.h>
//creates a variable called SLAVE_ADDRESS and permanently sets the value to 0x04 (register #4)
#define SLAVE_ADDRESS 0x04
#define IR1 A0
#define IR2 A1
void setup()
{
Serial.begin(9600);
//sets the address of the Arduino
Wire.begin(SLAVE_ADDRESS);
//the .onRequest command will run a function when it gets a request from the EV3.
//In this case, it will run the requestEvent function, which is defined later in the sketch.
Wire.onRequest(requestEvent);
}
int measureMap = 0;
void loop() {
int measure = analogRead(A0);
int measurea = analogRead(A1);
measureMap = map(measure, 0, 1023, 0, 63);
Serial.print(mapMeasure);
Serial.print (" ");
Serail.println(measure);
Serail.println(measure1);
}

Need to know how to send HEX values via SoftwareSerial on Arduino Uno

I’m trying to write Android Uno code to send HEX values via SoftwareSerial but when watching the Serial Monitor the output isn’t what I expected or wanted, and I am not experienced enough yet to know what to do next.
I don’t know where the FFFFs come from to be able to eliminate them.
My desired output is:
<br>
AA130120<br>
AA0400AE
What I get is:
<br>
FFFFFFFFAA1311E<br>
FFFFFFAA20
My code:
#include <SoftwareSerial.h>
// Define the RX and TX pins to establish UART communication with the MP3 Player Module.
#define Pin_RX 0 // to RX
#define Pin_TX 1 // to TX
#define d_sleep 10000
// Define the required MP3 Player Commands:
static byte ccPlay[] = {0xAA, 0x02, 0x00, 0xAC}; // Play:
static byte ccSetVol[] = {0xAA, 0x13, 0x01, 0x20}; // Set Volume (00-30):
// Define the Serial MP3 Player Module.
SoftwareSerial MP3(Pin_RX, Pin_TX);
void setup() {
// Initiate the serial monitor.
Serial.begin(9600);
}
void loop() { // Play a song.
ccSetVol[3] = {0x1E};
send_command_to_MP3_player(ccSetVol, 4);
delay(d_sleep);
send_command_to_MP3_player(ccPlay, 4);
delay(d_sleep);
}
void send_command_to_MP3_player(int8_t command[], int len){
//Serial.print("\nMP3 Command => ");
for (int i=0; i<len; i++) {
MP3.write(command[i]);
Serial.print(command[i], HEX);
}
delay(100);
}
Could you set the baud rate in the serial monitor to 9600Bd, run this code, and see if it spits out the bytes you expect in the serial monitor?
#define d_sleep 10000
static byte ccPlay[] = {0xAA, 0x02, 0x00, 0xAC};
static byte ccSetVol[] = {0xAA, 0x13, 0x01, 0x20};
void send_command_to_MP3_player(byte command[], int len) {
for (int i=0; i<len; i++) {
//Serial.println(command[i], HEX);
Serial.print(command[i], HEX);
}
//Serial.println();
delay(100);
}
void setup() {
Serial.begin(9600);
delay(1500);
}
void loop() {
ccSetVol[3] = 0x1E;
send_command_to_MP3_player(ccSetVol, 4);
delay(d_sleep);
send_command_to_MP3_player(ccPlay, 4);
delay(d_sleep);
}

Sending GPS values via XBee and Arduino (Float problem!!!)

I have XBEE S2C's. I have 2 Arduino MEGA. One part of them will be receiver and other is transmitter. I communicated and configured XBee's.I tested it That's okey. But the problem is I can't send float value like longtitude= 38,45682469 latitude= 85,24698534. When I try to do this I receive only latitude = 85
longtitude = 38. How can I change my code to receive healthy data.
TRANSMITTER
#include "SoftwareSerial.h"
#include <TinyGPS.h>
float lat,lon;
TinyGPS gps;
SoftwareSerial XBee(10, 11);
void setup(){
Serial.begin(9600);
XBee.begin(9600);
Serial.println("The GPS Received Signal:");
Serial3.begin(9600);
}
void loop(){
while(Serial3.available()){
if(gps.encode(Serial3.read()))
{
gps.f_get_position(&lat,&lon);
Serial.print("Position: ");
Serial.print("Latitude: ");
Serial.print(lat,8);
Serial.print(",");
Serial.print("Longitude: ");
Serial.println(lon,8);
}
}
XBee.write("l");
XBee.write(lat);
delay(100);
XBee.write("k");
XBee.write(lon);
delay(100);
}
Most robust is to convert float to string, send it over serial, and let the receiver convert back to float.
Sender:
XBee.write(String(lat,5)); // keep 5 decimals
Receiver:
String inString = "";
while (Serial.available()) {
char c = Serial.read();
inString += c;
}
float myFloat = inString.toFloat();
Further reading:
https://www.arduino.cc/en/Tutorial/StringToFloatExample
https://www.arduino.cc/en/Reference.StringConstructor

How to fix "aes128_enc_single' was not declared in this scope"

I am connecting a sensor to android device through arduino. I need to encrypt my sensor data and then send it to android device. But when I include aes128_enc_single(key, temp), It gives an error and said "aes128_enc_single' was not declared in this scope "
What should I do for this?
I have included AESLib.h library
My sample code is
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <AESLib.h>
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
SoftwareSerial blue(2,3);
int i = 0;
float temp = 0;
void setup() {
Serial.begin(9600);
blue.begin(9600);
}
void loop() {
i = analogRead(A0);
temp = (i/1024.0)*500;
Serial.println(temp);
aes128_enc_single(key, temp);
Serial.print("encrypted:");
Serial.println(temp);
blue.print("Encrypted Temperature: ");
blue.println(temp);
delay(1000);
}

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 :/
If it is the Arduino.org Arduino UNO WiFi Developer Edition, then use WiFi Link with UNO WiFi Serial1 library
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.

Resources