Hello I would like to try to retrieve data from a source that sends data via UDP protocol with an address and a port. So, I have taken a program in C to retrieve data via port 3000 but I am not retrieving any data as the program is stuck on the recvfrom() function.
So, I checked if the source is sending data via the resource monitor. So, it does send data (send 400B/s). I also have no error when connecting to the corresponding port.
Here is the c code below:
#include <winsock2.h> // pour les fonctions socket
#include <cstdio> // Pour les Sprintf
#pragma comment(lib,"ws2_32.lib")
WSADATA initialisation_win32;
int erreur;
int tempo;
int nombre_de_caractere;
char buffer[65535];
SOCKET id_de_la_socket;
SOCKADDR_IN information_sur_la_source;
int main (int argc, char* argv[])
{
printf("\nEcoute du port 3000\n");
erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
if (erreur!=0)
printf("\nDesole, je ne peux pas initialiser Winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nWSAStartup : OK");
id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
if (id_de_la_socket==INVALID_SOCKET)
printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d",WSAGetLastError());
else
printf("\nsocket : OK");
information_sur_la_source.sin_family=AF_INET;
information_sur_la_source.sin_addr.s_addr=INADDR_ANY;
information_sur_la_source.sin_port=htons(3000); // Ecoute sur le port 3000
erreur=bind(id_de_la_socket,(struct sockaddr*)&information_sur_la_source,sizeof(information_sur_la_source));
if (erreur!=0)
printf("\nDesole, je ne peux pas ecouter ce port, code d'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nbind : OK");
tempo=sizeof(information_sur_la_source);
printf("\nbefore recvfrom");
nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1024,0,(struct sockaddr*)&information_sur_la_source,&tempo);
printf("\nafter recvfrom");
buffer[nombre_de_caractere]=0;
printf("\nVoici les donnees : %s",buffer);
erreur=closesocket(id_de_la_socket);
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nclosesocket : OK");
erreur=WSACleanup();
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer winsock du a l'erreur : %d %d",erreur,WSAGetLastError());
else
printf("\nWSACleanup : OK");
}
Regards,
Related
I want to send data from DHT11 to URL using an ESP8266 NodeMCU. I use the board "NodeMCU 1.0 (ESP-12E Module)".
My code is as follows:
#include <dht.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SPI.h>
#include <MFRC522.h>
dht DHT;
#define DHTPIN 2
float humidityData;
float temperatureData;
const char* ssid = "My_SSID";
const char* password = "Wifi_Password";
//WiFiClient client;
char server[] = "192.168.1.1";
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
// server.begin();
Serial.println("Server started");
Serial.print(WiFi.localIP());
delay(1000);
Serial.println("connecting...");
}
void loop()
{
int chk = DHT.read11(DHTPIN);
humidityData = DHT.temperature;
temperatureData = DHT.humidity;
Sending_To_phpmyadmindatabase();
delay(30000); // interval
}
void Sending_To_phpmyadmindatabase() //CONNECTING WITH MYSQL
{
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
Serial.print("GET localhost/project_folder/dht.php?humidity=");
client.print("GET localhost/project_folder/dht.php?humidity=");
Serial.println(humidityData);
client.print(humidityData);
client.print("&temperature=");
Serial.println("&temperature=");
client.print(temperatureData);
Serial.println(temperatureData);
client.print(" "); //SPACE BEFORE HTTP/1.1
client.print("HTTP/1.1");
client.println();
client.println("Host: Your Local IP");
client.println("Connection: close");
client.println();
} else {
// if connection to the server failed:
Serial.println("connection to the server failed");
}
}
When it works correctly (1/3 of the running time), i get this serial message:
Connecting to Omni_777318
...........
WiFi connected
Server started
192.168.39.178connecting...
connected
GET localhost/michael/dht11.php?humidity=26.00
&temperature=
40.00
BUT! 2/3 of the time, i get a weird error that i dont understand:
tail 4
chksum 0xc9
csum 0xc9
v00044840
~ld
Connecting to Omni_777318
.....
ets Jan 8 2013,rst cause:4, boot mode:(3,7)
wdt reset
load 0x4010f000, len 3460, room 16
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4
tail 4
chksum 0xc9
csum 0xc9
v00044840
~ld
Anyone here who can help me solve this issue?
Cause 4 is a hardware watchdog reset, and in that case I suspect a power problem.
During the connection phases we observe current peak, and if the power supply is too weak ESP Reboot, as voltage falls under limit. In this case you must either change the power source, or put a capacitor of a few hundred microfarads on the supply terminals, as close as possible to ESP.
I AM WORKING WITH SIM808 for tcp connection, below is what my code looks like it is pretty much the basic tcp connection and i keep get fetch over and I can tell that is a problem with my content lenght but i feel if i have a better understanding that will be fine.
#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
//#define PIN_TX 10
//#define PIN_RX 11
//SoftwareSerial mySerial(PIN_TX,PIN_RX);
//DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,
DFRobot_SIM808 sim808(&Serial);
char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n";
char buffer[512];
void setup(){
//mySerial.begin(9600);
Serial.begin(9600);
//******** Initialize sim808 module *************
while(!sim808.init()) {
delay(1000);
Serial.print("Sim808 init error\r\n");
}
delay(3000);
//*********** Attempt DHCP *******************
while(!sim808.join(F("cmnet"))) {
Serial.println("Sim808 join network error");
delay(2000);
}
//************ Successful DHCP ****************
Serial.print("IP Address is ");
Serial.println(sim808.getIPAddress());
//*********** Establish a TCP connection ************
if(!sim808.connect(TCP,"mbed.org", 80)) {
Serial.println("Connect error");
}else{
Serial.println("Connect mbed.org success");
}
//*********** Send a GET request *****************
Serial.println("waiting to fetch...");
sim808.send(http_cmd, sizeof(http_cmd)-1);
while (true) {
int ret = sim808.recv(buffer, sizeof(buffer)-1);
if (ret <= 0){
Serial.println("fetch over...");
break;
}
buffer[ret] = '\0';
Serial.print("Recv: ");
Serial.print(ret);
Serial.print(" bytes: ");
Serial.println(buffer);
break;
}
//************* Close TCP or UDP connections **********
sim808.close();
//*** Disconnect wireless connection, Close Moving Scene *******
sim808.disconnect();
}
void loop(){
}
but i dont seem to understand what goes on at a point of the code, what does the part below, mean in the code
while (true) {
int ret = sim808.recv(buffer, sizeof(buffer)-1);
if (ret <= 0){
Serial.println("fetch over...");
break;
}
buffer[ret] = '\0';
Serial.print("Recv: ");
Serial.print(ret);
Serial.print(" bytes: ");
Serial.println(buffer);
break;
}
with emphasis on int ret = sim808.recv(buffer, sizeof(buffer)-1); how is the value of ret gotten?
If you just open up the .h file in the library you will find that it is pretty well documented. Have a look at the declaration for the function you are asking about:
/** read data from socket
* #param socket socket
* #param buf buffer that will store the data read from socket
* #param len string length need to read from socket
* #returns bytes that actually read
*/
int recv(char* buf, int len);
It details exactly what each parameter is and what the return value is. Looks like it returns the number of bytes read.
I want to change parameters with udp sentences. I'm getting udp's but my if clause is not working. How can compere 2 variables?
I have changed variable types and/or if clause type (ex if ( strcmp ( UdpMsg , "relay01_off" ) == 0) ).
Setup IF clause variable
String UdpMsg = "12345";...
Getting UDP Message
String UdpMsg ((char *)buffer);...
IF clause
if ( UdpMsg == "relay01_off" ) {
digitalWrite(relay_pin, HIGH);
Serial.print ("You!\n");
}
I expected to see "you!" when i send UDP but it's not finding equals.
Full Code for info:
#include <WiFi.h>
#include <WiFiUdp.h>
#include <cstring> //For the string functions
const char* wifi_name = "SSID"; // Your Wifi network name here
const char* wifi_pass = "password"; // Your Wifi network password here
WiFiServer server(80); // Server will be at port 80
// IP address to send UDP data to.
const char * udpAddress = "192.168.1.6";
int udpPort = 4444;
//create UDP instance
WiFiUDP udp;
// relay PINi
int relay_pin = 15;
// UDP Mesajı gelen
String UdpMsg = "12345";
void setup()
{
Serial.begin (115200);
pinMode (relay_pin, OUTPUT);
Serial.print ("Connecting to ");
Serial.print (wifi_name);
WiFi.begin (wifi_name, wifi_pass); // Connecting to the wifi network
while (WiFi.status() != WL_CONNECTED) // Waiting for the response of wifi network
{
delay (500);
Serial.print (".");
}
Serial.println("");
Serial.println("Connection Successful");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // Getting the IP address
Serial.println("Type the above IP address into browser search bar");
server.begin(); // Starting the server
digitalWrite(relay_pin, LOW); // switch i AC
//This initializes udp and transfer buffer
udp.begin(udpPort);
}
void loop()
{
uint8_t buffer[50] = "";
//processing incoming packet, must be called before reading the buffer
udp.parsePacket();
//receive response from server, it will be HELLO WORLD
if(udp.read(buffer, 50) > 0){
Serial.print("Recevied UDP: ");
Serial.println((char *)buffer);
String UdpMsg ((char *)buffer);
}
//////// UDP IF leri ///////////
if ( UdpMsg == "relay01_off" ) { // Equal strings
digitalWrite(relay_pin, HIGH);
Serial.print ("You!\n");
}
}
It seems that you construct a second UdpMsg variable in:
if(udp.read(buffer, 50) > 0){
Serial.print("Recevied UDP: ");
Serial.println((char *)buffer);
String UdpMsg ((char *)buffer); // <--- 2nd UdpMsg, released after 'if' clause
}
While UdpMsg at this line:
if ( UdpMsg == "relay01_off" )
refers to the first one, the global, that keeps its initial value "12345".
I want to send/receive data from a device via UDP. The device acts as a server and if I send it a value it sends me some values back. First I write a little python code which works without any problems:
import socket
import time
UDP_IP = "192.168.42.33"
UDP_PORT = 5004
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('',UDP_PORT))
while True:
MESSAGE = input('Write new message: ')
sock.sendto(bytes.fromhex(MESSAGE), (UDP_IP, UDP_PORT))
time.sleep(0.1)
data, addr = sock.recvfrom(1076)
a = len(data)
print ("Byte: ", data.hex()[46], data.hex()[47])
I can write a value to a register with this script and get an array with all updated register values from the device back. I write an equal program in c++:
#pragma once
#pragma comment(lib, "Ws2_32.lib")
#include <sdkddkver.h>
#include <WinSock2.h>
#include <Windows.h>
#include <iostream>
#include <string>
#define SCK_VERSION2 0x0202
#define BUFLEN 2048
using namespace std;
int main()
{
int inputI;
long SUCCESSFUL;
WSAData WinSockData;
WORD DLLVersion;
SOCKET sock;
char msg_input[] = { 0x60, 0x01, 0x00, 0x00}; //Write REG mode, 1 byte
char* SU_IP = "192.168.42.33"; //IP Address of Scanner Unit
u_short SU_PORT = 5004; //Port of Scanner Unit
SOCKADDR_IN ADDRESS;
char buf[BUFLEN];
int slen = sizeof(ADDRESS);
DLLVersion = MAKEWORD(2, 1);
SUCCESSFUL = WSAStartup(DLLVersion, &WinSockData);
sock = socket(AF_INET, SOCK_DGRAM, 0);
ADDRESS.sin_addr.s_addr = inet_addr(SU_IP);
ADDRESS.sin_family = AF_INET;
ADDRESS.sin_port = htons(SU_PORT);
connect(sock, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS));
while (1)
{
cout << "Adresse des Registers (hex): ";
cin >> hex >> inputI;
msg_input[2] = inputI;
cout << "Wert des Registers (hex): ";
cin >> hex >> inputI;
msg_input[3] = inputI;
send(sock, msg_input, sizeof(msg_input), NULL);
//recv(sock, buf, BUFLEN, 0);
}
}
I am able to sent values and the device sends its register values back so the communication works (I checked this with wireshark). But I can't receive the data in my program. I oncomment the recv function because the program gets stuck at this point if I want to receive. The recvfrom() function doesn't work at this point too. I tried the bind() function instead of connect because in the python script it works with sock.bind. But than I cant send or receive. I was reading several posts about UDP receive function but can't find my mistake. Can someone help me?
The Python code binds to all existing local interfaces at port 5004, whereas the C code will implicitly bind to a local free port during the call to connect(), thus if the remote peer is hard-coded to respond to port 5004, the socket will not receive it.
You should add a call to bind() right after creating your socket:
bind(sock, (sockaddr *)&ADDRESS, sizeof(ADDRESS));
And see what happens :)
I am trying to set up a client utilizing libmosquitto. Connecting to the broker works like a charm, with and without TLS. But whenever I try to send data over the TLS connection, I get the error:
Blockquote
1486712210: OpenSSL Error: error:14094438:SSL routines:ssl3_read_bytes:tlsv1 alert internal error
1486712210: OpenSSL Error: error:140940E5:SSL routines:ssl3_read_bytes:ssl handshake failure
1486712210: Socket error on client , disconnecting.
Blockquote
I've tried the test client from the git repository of mosquitto, mosquitto/test/lib/c/08-ssl-connect-cert-auth-enc.c
, same result.
My current server configuration:
listener 1883
listener 8883
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/werkstatt.logicway.net.crt
keyfile /etc/mosquitto/certs/werkstatt.logicway.net.key
require_certificate true
tls_version tlsv1
The source code:
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <QtCore/QCoreApplication>
#include <QtDBus/QtDBus>
#include <mosquitto.h>
#include "logicgateway_data.h"
#include "logicgateway_client.h"
using namespace std;
const char *data_out;
struct mosquitto *mosq = NULL;
// BEschreibung kommt
int LGW_Client::receive_data(QString in_basket)
{
data_out = in_basket.QString::toLatin1();
cout << "Wert erhalten: " << data_out << endl;
mosquitto_publish(mosq,0,LGW_TOPIC,strlen(data_out),data_out,0,true);
return 0;
}
int main (int argc, char **argv)
{
// Verbindung mit der Qt-Dbus Session erstellen
QCoreApplication app(argc,argv);
if(!QDBusConnection::sessionBus().isConnected()){
fprintf(stderr,"Kann nicht mit D-Bus Session verbinden.\n"
"Um sie zu starten, geben Sie bitte ein:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
// Mosquitto initialisieren
mosquitto_lib_init();
mosq = mosquitto_new(CID,false,NULL);
if(!mosq)
{
printf("Nicht erstellt\n");
return 1;
}
else
{
printf("Mosquitto erfolgreich verbunden!\n");
}
// TLS Anbindung
if (LGW_PORT == 8883)
{
mosquitto_tls_opts_set(mosq,1,"tlsv1",NULL);
mosquitto_tls_set(mosq,"ca.crt",NULL,"client.crt","client.key", NULL);
}
// Mosquitto Verbindung zum Broker erstellen
if(mosquitto_connect(mosq,BROKER_ADRESS,LGW_PORT,60))
{
fprintf(stderr, "Fehler!\n");
return 1;
}
else
{
printf("Laeuft!\n");
}
// DBus Service registrieren um Daten zu empfangen
if(!QDBusConnection::sessionBus().registerService(SERVICE_NAME_CLIENT)) {
fprintf(stderr, "%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message()));
exit(1);
}
printf ("LGW-Client: Gestartet...\n");
LGW_Client lgwclient;
QDBusConnection::sessionBus().registerObject("/",&lgwclient, QDBusConnection::ExportAllSlots);
app.exec();
// Fehlerbehandlung und aufräumen
//fprintf(stderr, "%s\n", qPrintable(QDBusConnection::sessionBus().lastError().message()));
mosquitto_loop_forever(mosq, -1, 1); //Bin mir nicht sicher, ob ich das hier wegen der While-Schleife brauche
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
return 1;
}
(Please be gentle regarding the code in general. I am a bloody beginner and fully aware, that there is much to improve. But at the moment i just want to get the TLS connection working)
Things that work:
Running the code with port 1883 (without TLS)
Running mosquitto_sub and mosquitto_pub with the ca.crt, client.crt and client.key I generated with generate-CA.sh and the information from the mosquitto documentation
I am pretty positive, it is just a tiny error and I am just too inexperienced to see it. Hope you can help.
Many thanks in advance!
Mathias
You cannot connect to the MQTT broker via your application because you are probably using an IP Address to connect to it, as the BROKER_ADRESS variable would suggest. TLS expects the connection to be directed to the same CN (Common Name) as the one used in the generation step of the client.csr request file. If you use the broker hostname (werkstatt.logicway.net) instead of a plain address you should be able to connect to the device.
The CN (Common Name) should be the same as the server/broker hostname if no SAN extension is used.
I had the same issue and input from here https://openest.io/en/2020/01/03/mqtts-how-to-use-mqtt-with-tls/ help me out.