Now, you have a lots of options.
- Change to a 3.3V Arduino
- Use a resistor divider for lower BAUD rates
- Use a level-shifter for higher BAUD rates
Overview of level shifters: https://learn.sparkfun.com/tutorials/logic-levels
Explore... Chat... Share...
martinayotte wrote:So you mean, the library does not exist yet!
SPI library already exist, but your own communication protocol isn't.voltage divider approach does not work well for higher speed
Right, at high speed, the square wave of the signal becomes distorded. MOSFET level shifter is better :
#include "Arduino.h"
#include <avr/interrupt.h> // Use timer interrupt library
void setup() {
/****Set timer1 for 8-bit fast PWM output ****/
pinMode(9, OUTPUT); // Make timer’s PWM pin an output
TCCR1B = (1 << CS10); // Set prescaler to full 16MHz,
TCCR1A |= (1 << COM1A1); // Pin low when TCNT1=OCR1A
TCCR1A |= (1 << WGM10); // Use 8-bit fast PWM mode, so it will count from 0-255, and the duty cycle would be set by OCR1AL(OCR1A lower byte)
TCCR1B |= (1 << WGM12);
//freq calculation: at 16mhz, time taken to count till 255 is (1,000,000/16,000,000)*256usec=16usec
//freq is 16,000,000/256=62500hz=62.5khz,
/******** Set up timer2 to call ISR ********/
TCCR2A = 0; // No options in control register A
TCCR2B = (1 << CS21); // Set prescaler to divide by 8,
//i.e., 16,000,000/8=2,000,000 clock tick/sec,
//so every tick at 500ns (.5us), so to spend 32 usec it needs to count upto 64, 64*.5=32
TIMSK2 = (1 << OCIE2A); // Call ISR when TCNT2 = OCRA2
OCR2A = 64; // Set frequency of generated wave=2,000,000/64=31250hz
sei(); // Enable interrupts to generate waveform!
}
void loop() { // Nothing to do!
}
/******** Called every time TCNT2 = OCR2A ********/
ISR(TIMER2_COMPA_vect) { // Called when TCNT2 == OCR2A
TCNT2 = 0; // Timing to compensate for ISR run time
if(Serial.available()){
uint8_t byte=Serial.read();
OCR1AL=byte;
}else{
OCR1AL=0;// duty cycle is zero, no output
}
}
#include "ESP8266WiFi.h"
const char* ssid = "debojit-dlink";
const char* password = "India@123";
uint8_t outBuffer[2920];
uint8_t inBuffer[3000];
const char* host="192.168.0.101";
WiFiClient client;
void setup(){
Serial.begin(500000);
setup_connect_as_station();
}
void setup_connect_as_station(){
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP("ESPAP");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected ip is");
Serial.println(WiFi.localIP());
Serial.println("#################");
WiFi.printDiag(Serial);
WiFi.hostname("esp8266");
WiFi.printDiag(Serial);
}
void loop(){
const int httpPort = 8086;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
delay(2000);
return;
}
Serial.println("Connected, now receiving...");
int cnt=0;
//uint32_t sTime=0;
while(true){
int readBytes=client.read(inBuffer,2920);
if(readBytes>0){
int writeBytestoSerial=Serial.write(inBuffer,readBytes);
}
}
}
package com.test.socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class DataTransceiverSocket extends Thread {
private ServerSocket serverSocket;
private SocketAddress remoteSocketAddress;
public DataTransceiverSocket(ServerSocket serverSocket, String name) throws IOException {
this.setName(name);
this.serverSocket = serverSocket;
// serverSocket.setSoTimeout(10000);
}
public void run() {
while(true){
System.out.println(getName() + "->Waiting for client on port " + serverSocket.getLocalPort() + "...");
Socket server = null;
try {
server = serverSocket.accept();
remoteSocketAddress = server.getRemoteSocketAddress();
System.out.println(getName() + "->Just connected to " + remoteSocketAddress);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
Sender sender=new Sender(server);
sender.start();
}
}
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8086);
Thread t = new DataTransceiverSocket(serverSocket, "T1");
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Receiver extends Thread{
Socket socket;
byte buffer[] = new byte[2920];
public Receiver(Socket socket) {
// TODO Auto-generated constructor stub
this.socket=socket;
}
@Override
public void run() {
try{
DataInputStream in = new DataInputStream(socket.getInputStream());
AudioFormat af = new AudioFormat(38500, 8, 1, false, false);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(af, 2920);
line.start();
while (true) {
try {
if (in.available() > -1) {
int readBytesInterim = in.read(buffer, 0, buffer.length);
System.out.print(".");
line.write(buffer, 0, readBytesInterim);
}
} catch (SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch (IOException e) {
e.printStackTrace();
break;
}
}
line.drain();
line.stop();
line.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
class Sender extends Thread{
Socket socket;
byte buffer[] = new byte[2920];
public Sender(Socket socket) {
// TODO Auto-generated constructor stub
this.socket=socket;
}
@Override
public void run() {
while(true){
try{
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
FileInputStream fis=new FileInputStream("C:\\Users\\debojitk\\Desktop\\test1.bin");
int readBytes=0;
while((readBytes=fis.read(buffer, 0, buffer.length))>-1){
out.write(buffer,0,readBytes);
}
fis.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]