How can I send data from a raspberry pi to my laptop? - sqlite

I need a simple way of sending a string from my raspberry pi to my laptop (on the same WiFi network) and then store in a Sqlite database.

The most basic way could be through socket programming :
Code on raspberrypi :
import socket
s = socket.socket()
port = 12345
s.connect(('<your_ip_address>', port))
s.send('Hello this is your rpi')
s.close()
Code on your laptop :
import socket
s = socket.socket()
print "Socket successfully created"
port = 12345
s.bind(('<your_ip_address>', port))
print "socket binded to %s" %(port)
s.listen(5)
print "socket is listening"
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
Another way could be through mqtt where you will have to set up an mqtt server on the pi and a client on the laptop, the raspberrypi would then send messages over a specific topic which the client (laptop) can subscribe to and keep listening to it till a message is received.
You can refer to python mqtt script on raspberry pi to send and receive messages for the same

Related

Modbus TCP/IP on RaspberryPi wit PyModBus

I need to communicate with several modules and devices using the ModBus Protocol and the Raspberry Pi.
I am using PyModBus on Raspberry Pi to read/write Modbus function codes. (https://pymodbus.readthedocs.io/en/latest/readme.html)
I was able to communicate Modbus RTU over RS485 with the device but now that I am trying to communicate ModbusTCP over a Ethernet cable and keep running into the following error:
import pymodbus
from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('127.0.0.1')
connection = client.connect()
Output:
ERROR.pymodbus.client.sync: Conection to (127.0.0.1, 502) failed: [Errno 111] connection refused
Any tips or explanation for the error?
127.0.0.1 is a loopback address; this means that ModbusTcpClient('127.0.0.1') will attempt to establish a connection to the Pi iteslf. Unless there is a Modbus server running on the Pi the error you received is to be expected.
"I am trying to communicate ModbusTCP over a Ethernet cable" indicates you are communicating with another device which should have it's own IP address. You need to work out what that address is and use that when attempting to connect (as well as ensuring your network setup is valid). The method used to set/determine a devices address varies from device to device so you would need to check the documentation (you did not specify what the device is).

Unable to read holding register PLC usign Pymodbus serial

I am trying to read a holding register on my PLC suning pymodbus
I connected to my PLC using serial RS232 through com port 5
client = ModbusClient(method='rtu', port='com5', timeout=1,baudrate=9600)
client.connect()
Out[8]: True
client.read_holding_registers(00x00,1,unit=1)
pymodbus.exceptions.ModbusIOException('No Response received from the remote unit/Unable to decode response', 3)
PLC MODBUS ADDRESS SCREENSHOT
Getting following error
What am i doing wrong?
Please help

TCP connection to ESP8266 via WiFiClient.h from Android phone

I looking for clarification on how the WiFiClient and WiFiServer objects in this example ESP8266 sketch starts a TCP connection and allows an Android app to read and write to buffers that's setup in the sketch.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
WiFiServer server(port);
WiFiClient client;
uint8_t buf1[1024];
server.begin(); // start TCP server
client = server.available(); // wait for it to connect
buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app)
client.write((char*)buf2, i2);
This sketch talks to a closed-source Android app call Roboremo. It uses WiFiServer to create a TCP server, and WiFiClient to read/write to buffers. What is this TCP server, and what mechanism is being used to read/write to the ESP8266? So, if I were having a conversation with an app developer, how do I tell them how to write to this TCP server (other than IP and port number)? Is this a "TCP socket", and does that translate into something that mobile app people would know how to proceed?
Another way to ask this question: I'd like to be able to test read/write to the ESP8266 without the Android app. So if I have a Raspberry Pi on the same network as this ESP8266, what utility (mechanism again) can I use to read/write to those buffers from the Pi?
Checking the Roboremo sketch shows that you will be working with esp8266 on STA mode by providing your ssid and pass into the sketch. That means you need to find the ip address of esp8266 before making a connection. A ridiculous comment from sketch is :
Then somehow find the IP that the ESP got from router
So, if you will continue with STA mode, you may go to your router settings and find the ESP8266 from DHCP clients.
For a raspberry TCP client connection, you need nothing else from a Linux socket client. Here a basic python client to test on raspberry :
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
sys.exit();
print 'Socket Created'
host = 'YOUR_ESP8266_IP '
port = 9876
s.connect((remote_ip , port))
print 'Socket Connected to ' + host
#Send some data to remote server
message = "test esp8266 server"
try :
s.sendall(message)
except socket.error:
print 'Send failed'
sys.exit()
print 'Message send successfully'

Arduino and Processing (PDE) via Ethernet

Arduino: Is it possible to communicate with Arduino through Ethernet, using a Processing (PDE) script?
I've already created a desktop application using Processing, but in this case I communicate with Arduino through the USB.
Yes you can
You could for example create an arduino chat server : http://arduino.cc/en/Tutorial/ChatServer
And create a processing client like this : http://processing.org/reference/libraries/net/Client_write_.html
ofcourse you would need to change the
myClient = new Client(this, "127.0.0.1", 10002);
to
myClient = new Client(this, "IP of Arduino", 23);
If you would then connect with a telnet session to the arduin o you would see output from the prosessing script comming back from the arduino

Porting a TCP Twisted app to the serial port

I've been using Twisted for a while to connect a computer with some devices over the net. I wrote a custom Protocol and Factory.
factory = TModBusFactory()
reactor.listenTCP(9007, factory)
Now I'd like to connect the devices to the serial port of the server machine. As long as I know, no change in the protocol is needed but I need to switch from sending bytes over TCP to send them over the serial port.
How hard to do is this change? How can I change the code snippet in order to make it work?
Thanks!
Serial port setup looks like this:
from twisted.internet.serialport import SerialPort
from twisted.internet import reactor
factory = TModBusFactory()
protocol = factory.buildProtocol(None)
deviceName = "ttyS0"
port = SerialPort(protocol, deviceName, reactor)

Resources