I would like to communicate between two microcontrollers (Attiny202/402 and a ESP32/ESP32S2/ESP32C3). The easiest way would be using a serial communication or something like i2C or similar. But they consume too mutch flashmemory so I am not able to flash them.
So I just thought why not do the communication via GPIO pins.
I decided to use 3 pins for that on each microcontroller (Clock-,Sender- and Receiverpin). To get a stable signal do I need to use Pullup/Pulldown resistors for that or how can I remove the floating state ( which you got on buttons, when you don“t use and 10k resistor)
If I understand right I need to connect the grounds of the controllers together so they have the same reference?(If I am wrong please correct me)
Sender code:
#include <Arduino.h>
#ifdef ESP32
#pragma message "using ESP32"
#define CLOCKPIN 1
#define SENDDATAPIN 42
#define RECEIVEDATAPIN 2
#else
#define CLOCKPIN 0
#define SENDDATAPIN 1
#define RECEIVEDATAPIN 2
#endif
#define SETTIME B001
#define SETALERT B010
#define DELETEALERT B011
#define GETALERT B100
//TODO build timeot when there is no answer
/**
* @brief Send a message which is compressed in a
*
* @param message A message needs to contain following informations, so it can get processed:
* (Data) (Data) (Data) (Data) (Type of Data) (More Data Bit)
* XXXXXXXX XXXXXXXX XXXXXXXX XXXX TTT D
*/
bool sendMessage(uint32_t message)
{
#ifdef DEBUGGING
Serial.println("Sendmessage");
#endif
// Send transmission bytes
digitalWrite(CLOCKPIN, 1);
digitalWrite(SENDDATAPIN, 1);
#ifdef DEBUGGING
Serial.println(digitalRead(CLOCKPIN));
Serial.println(digitalRead(SENDDATAPIN));
Serial.println("Waiting for receiver");
#endif
// Wait for answer
while (digitalRead(RECEIVEDATAPIN) == 0)
{
delay(10);
#ifdef DEBUGGING
Serial.print(".");
#endif
}
// Sending answer
digitalWrite(CLOCKPIN, 0);
digitalWrite(SENDDATAPIN, 0);
// // Wait alittle bit so the other side can get the answer
delay(100);
#ifdef DEBUGGING
Serial.println("Transmission:");
#endif
uint32_t mask = 0x80000000;
for (uint8_t i = 0; i < 32; i++)
{
digitalWrite(SENDDATAPIN, (message&mask)==0?0:1);
#ifdef DEBUGGING
Serial.print((message&mask)==0?"0":"1");
Serial.println(digitalRead(SENDDATAPIN));
#endif
message <<= 1;
digitalWrite(CLOCKPIN, 1);
delay(100);
digitalWrite(CLOCKPIN, 0);
delay(100);
}
#ifdef DEBUGGING
Serial.println();
#endif
}
// Different messages
uint32_t messages[4]
{
0xFFFFFFFF,
0xAAAAAAAA,
0x00000001,
0x10000000
};
/**
* @brief Test the message send and receive methodes
* Wire the pins accoring to the defined pins
* There are differnet pins needed:
* ClockIN ClockOut (Connect them together)
* SendDataPin ReceiveDataPin (Connect them together)
*
* @return * Wire
*/
bool testSendMessage()
{
sendMessage(messages[1]);
// for(uint8_t index =0;index<sizeof(messages)/sizeof(uint32_t);index++)
// {
// sendMessage(messages[index]);
// }
return true;
}
#include "Wire.h"
void setup()
{
#ifdef DEBUGGING
Serial.begin(115200);
#endif
#ifdef DEBUGGING
Serial.print("Pin:");
Serial.println(RECEIVEDATAPIN);
#endif
delay(2000);
pinMode(CLOCKPIN, OUTPUT);
pinMode(SENDDATAPIN, OUTPUT);
pinMode(RECEIVEDATAPIN, INPUT);
digitalWrite(CLOCKPIN, LOW);
digitalWrite(SENDDATAPIN, LOW);
delay(2000);
}
void loop()
{
testSendMessage();
delay(5000);
}
Receivercode:
#include <Arduino.h>
#ifdef ESP32
#pragma message "using ESP32"
#define CLOCKPIN 1
#define SENDDATAPIN 2
#define RECEIVEDATAPIN 42
#define OUTPUTPIN 10
#else
#define CLOCKPIN 0
#define SENDDATAPIN 1
#define RECEIVEDATAPIN 2
#define OUTPUTPIN 3
#endif
#define SETTIME B001
#define SETALERT B010
#define DELETEALERT B011
#define GETALERT B100
// Different messages
uint32_t messages[4]{
0xFFFFFFFF,
0xAAAAAAAA,
0x00000001,
0x10000000};
uint32_t receiveMessage()
{
uint32_t buffer;
byte index = 0;
// Check if transmission started
if (digitalRead(CLOCKPIN) == 1 && digitalRead(RECEIVEDATAPIN) == 1)
{
#ifdef DEBUGGING
Serial.println("Incomming message");
// Send answer
Serial.println("Sending answer");
#endif
digitalWrite(SENDDATAPIN, 1);
Serial.println("Waiting for data ");
// Wait For answer
while (digitalRead(CLOCKPIN) != 0 && digitalRead(RECEIVEDATAPIN) != 0)
{
#ifdef DEBUGGING
Serial.print(".");
#endif
delay(10);
}
#ifdef DEBUGGING
Serial.println();
Serial.print("Data: ");
#endif
bool newData = true;
while (index < 32)
{
if (digitalRead(CLOCKPIN) == 1 && newData)
{
buffer <<= 1;
buffer += digitalRead(RECEIVEDATAPIN);
#ifdef DEBUGGING
Serial.print(digitalRead(RECEIVEDATAPIN));
#endif
newData = false;
index++;
}
else if (digitalRead(CLOCKPIN) == 0)
{
newData = true;
}
}
#ifdef DEBUGGING
Serial.println();
#endif
return buffer;
}
else
{
return 0;
}
}
bool testReceiveMessage()
{
uint8_t index = 1;
// for (uint8_t index = 0; index < sizeof(messages) / sizeof(uint32_t); index++)
// {
uint32_t message = receiveMessage();
while (message == 0)
{
message = receiveMessage();
}
if (messages[index] == message)
{
#ifdef DEBUGGING
Serial.print("Received message ");
Serial.print(index);
Serial.println("successful");
#endif
digitalWrite(OUTPUTPIN, 1);
}
else
{
#ifdef DEBUGGING
Serial.print("Received message ");
Serial.print(index);
Serial.println(" failed.");
Serial.print("Expected: ");
Serial.println(messages[index], BIN);
Serial.print("Received: ");
Serial.print(message, BIN);
#endif
}
// }
return true;
}
void setup()
{
#ifdef DEBUGGING
Serial.begin(115200);
#endif
#ifdef DEBUGGING
Serial.print("Pin:");
Serial.println(RECEIVEDATAPIN);
#endif
delay(2000);
pinMode(CLOCKPIN, INPUT);
pinMode(SENDDATAPIN, OUTPUT);
pinMode(RECEIVEDATAPIN, INPUT);
pinMode(OUTPUTPIN, OUTPUT);
digitalWrite(SENDDATAPIN, LOW);
digitalWrite(OUTPUTPIN, LOW);
delay(2000);
}
void loop()
{
testReceiveMessage();
}
Currently it does not work (not stable, receiver only get "1" but not "0". Maybe someone could help me or explain, how I can solve that?